2
0

gar.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "github.com/fatih/color"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/cli/cmd/utils"
  11. )
  12. // GAR creates a GAR integration
  13. func GAR(
  14. ctx context.Context,
  15. client api.Client,
  16. projectID uint,
  17. ) (uint, error) {
  18. // if project ID is 0, ask the user to set the project ID or create a project
  19. if projectID == 0 {
  20. return 0, fmt.Errorf("no project set, please run porter config set-project")
  21. }
  22. keyFileLocation, err := utils.PromptPlaintext(`Please provide the full path to a service account key file.
  23. Key file location: `)
  24. if err != nil {
  25. return 0, err
  26. }
  27. // attempt to read the key file location
  28. if info, err := os.Stat(keyFileLocation); !os.IsNotExist(err) && !info.IsDir() {
  29. // read the file
  30. bytes, err := os.ReadFile(keyFileLocation)
  31. if err != nil {
  32. return 0, err
  33. }
  34. // create the gcp integration
  35. integration, err := client.CreateGCPIntegration(
  36. ctx,
  37. projectID,
  38. &types.CreateGCPRequest{
  39. GCPKeyData: string(bytes),
  40. },
  41. )
  42. if err != nil {
  43. return 0, err
  44. }
  45. color.New(color.FgGreen).Printf("created gcp integration with id %d\n", integration.ID)
  46. region, err := utils.PromptPlaintext(`Please enter the artifact registry region. For example, us-central1.
  47. Artifact registry region: `)
  48. if err != nil {
  49. return 0, err
  50. }
  51. // create the registry
  52. // query for registry name
  53. regName, err := utils.PromptPlaintext("Give this registry a name: ")
  54. if err != nil {
  55. return 0, err
  56. }
  57. // GCP project IDs can have the ':' character like example.com:my-project
  58. // if this is the case then we need to case on this
  59. //
  60. // see: https://cloud.google.com/artifact-registry/docs/docker/names#domain
  61. var registryURL string
  62. if domain, projectID, found := strings.Cut(integration.GCPProjectID, ":"); found {
  63. if domain == "" || projectID == "" {
  64. return 0, fmt.Errorf("invalid project ID: %s", integration.GCPProjectID)
  65. }
  66. registryURL = fmt.Sprintf("%s-docker.pkg.dev/%s/%s", region, domain, projectID)
  67. } else {
  68. registryURL = fmt.Sprintf("%s-docker.pkg.dev/%s", region, integration.GCPProjectID)
  69. }
  70. reg, err := client.CreateRegistry(
  71. ctx,
  72. projectID,
  73. &types.CreateRegistryRequest{
  74. Name: regName,
  75. GCPIntegrationID: integration.ID,
  76. URL: registryURL,
  77. },
  78. )
  79. if err != nil {
  80. return 0, err
  81. }
  82. color.New(color.FgGreen).Printf("created registry with id %d and name %s\n", reg.ID, reg.Name)
  83. return reg.ID, nil
  84. }
  85. return 0, fmt.Errorf("could not read service account key file")
  86. }