gar.go 2.6 KB

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