docr.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/cli/cmd/utils"
  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. resp, err := client.ListOAuthIntegrations(context.TODO(), projectID)
  22. if err != nil {
  23. return 0, err
  24. }
  25. oauthInts := *resp
  26. linkedDO := false
  27. var doAuth *types.OAuthIntegration
  28. // iterate through oauth integrations to find do
  29. for _, oauthInt := range oauthInts {
  30. if oauthInt.Client == types.OAuthDigitalOcean {
  31. linkedDO = true
  32. doAuth = oauthInt
  33. break
  34. }
  35. }
  36. if !linkedDO {
  37. doAuth, err = triggerDigitalOceanOAuth(client, projectID)
  38. if err != nil {
  39. return 0, err
  40. }
  41. }
  42. // use the digital ocean oauth to create a registry
  43. 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.
  44. Registry URL: `))
  45. if err != nil {
  46. return 0, err
  47. }
  48. urlArr := strings.Split(regURL, "/")
  49. regName := urlArr[len(urlArr)-1]
  50. if err != nil {
  51. return 0, err
  52. }
  53. reg, err := client.CreateRegistry(
  54. context.Background(),
  55. projectID,
  56. &types.CreateRegistryRequest{
  57. Name: regName,
  58. DOIntegrationID: doAuth.ID,
  59. URL: regURL,
  60. },
  61. )
  62. return reg.ID, nil
  63. }
  64. func triggerDigitalOceanOAuth(client *api.Client, projectID uint) (*types.OAuthIntegration, error) {
  65. var doAuth *types.OAuthIntegration
  66. oauthURL := fmt.Sprintf("%s/projects/%d/oauth/digitalocean", client.BaseURL, projectID)
  67. fmt.Printf("Please visit %s in your browser to connect to Digital Ocean (it should open automatically).", oauthURL)
  68. utils.OpenBrowser(oauthURL)
  69. for {
  70. resp, err := client.ListOAuthIntegrations(context.TODO(), projectID)
  71. if err != nil {
  72. return doAuth, err
  73. }
  74. linkedDO := false
  75. oauthInts := *resp
  76. // iterate through oauth integrations to find do
  77. for _, oauthInt := range oauthInts {
  78. if oauthInt.Client == types.OAuthDigitalOcean {
  79. linkedDO = true
  80. doAuth = oauthInt
  81. break
  82. }
  83. }
  84. if linkedDO {
  85. break
  86. }
  87. time.Sleep(2 * time.Second)
  88. }
  89. return doAuth, nil
  90. }