2
0

docr.go 2.4 KB

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