docr.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. "github.com/porter-dev/porter/cli/cmd/utils"
  9. ints "github.com/porter-dev/porter/internal/models/integrations"
  10. )
  11. // DOCR creates a DOCR integration
  12. func DOCR(
  13. client *api.Client,
  14. projectID uint,
  15. ) (uint, error) {
  16. // if project ID is 0, ask the user to set the project ID or create a project
  17. if projectID == 0 {
  18. return 0, fmt.Errorf("no project set, please run porter project set [id]")
  19. }
  20. // list oauth integrations and make sure DO exists
  21. oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
  22. if err != nil {
  23. return 0, err
  24. }
  25. linkedDO := false
  26. var doAuth ints.OAuthIntegrationExternal
  27. // iterate through oauth integrations to find do
  28. for _, oauthInt := range oauthInts {
  29. if oauthInt.Client == ints.OAuthDigitalOcean {
  30. linkedDO = true
  31. doAuth = oauthInt
  32. break
  33. }
  34. }
  35. if !linkedDO {
  36. doAuth, err = triggerDigitalOceanOAuth(client, projectID)
  37. if err != nil {
  38. return 0, err
  39. }
  40. }
  41. // use the digital ocean oauth to create a registry
  42. regURL, err := utils.PromptPlaintext(fmt.Sprintf(`Please provide the registry URL, in the form registry.digitalocean.com/[REGISTRY_NAME]. For example, registry.digitalocean.com/porter-test.
  43. Registry URL: `))
  44. if err != nil {
  45. return 0, err
  46. }
  47. urlArr := strings.Split(regURL, "/")
  48. regName := urlArr[len(urlArr)-1]
  49. if err != nil {
  50. return 0, err
  51. }
  52. reg, err := client.CreateDOCR(
  53. context.Background(),
  54. projectID,
  55. &api.CreateDOCRRequest{
  56. Name: regName,
  57. DOIntegrationID: doAuth.ID,
  58. URL: regURL,
  59. },
  60. )
  61. return reg.ID, nil
  62. }
  63. func triggerDigitalOceanOAuth(client *api.Client, projectID uint) (ints.OAuthIntegrationExternal, error) {
  64. var doAuth ints.OAuthIntegrationExternal
  65. oauthURL := fmt.Sprintf("%s/oauth/projects/%d/digitalocean", client.BaseURL, projectID)
  66. fmt.Printf("Please visit %s in your browser to connect to Digital Ocean (it should open automatically).", oauthURL)
  67. utils.OpenBrowser(oauthURL)
  68. for {
  69. oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
  70. if err != nil {
  71. return doAuth, err
  72. }
  73. linkedDO := false
  74. // iterate through oauth integrations to find do
  75. for _, oauthInt := range oauthInts {
  76. if oauthInt.Client == ints.OAuthDigitalOcean {
  77. linkedDO = true
  78. doAuth = oauthInt
  79. break
  80. }
  81. }
  82. if linkedDO {
  83. break
  84. }
  85. time.Sleep(2 * time.Second)
  86. }
  87. return doAuth, nil
  88. }