gar.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/fatih/color"
  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. // GAR creates a GAR integration
  12. func GAR(
  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 config set-project")
  19. }
  20. keyFileLocation, err := utils.PromptPlaintext(`Please provide the full path to a service account key file.
  21. Key file location: `)
  22. if err != nil {
  23. return 0, err
  24. }
  25. // attempt to read the key file location
  26. if info, err := os.Stat(keyFileLocation); !os.IsNotExist(err) && !info.IsDir() {
  27. // read the file
  28. bytes, err := os.ReadFile(keyFileLocation)
  29. if err != nil {
  30. return 0, err
  31. }
  32. // create the gcp integration
  33. integration, err := client.CreateGCPIntegration(
  34. context.Background(),
  35. projectID,
  36. &types.CreateGCPRequest{
  37. GCPKeyData: string(bytes),
  38. },
  39. )
  40. if err != nil {
  41. return 0, err
  42. }
  43. color.New(color.FgGreen).Printf("created gcp integration with id %d\n", integration.ID)
  44. region, err := utils.PromptPlaintext(`Please enter the artifact registry region. For example, us-central1.
  45. Artifact registry region: `)
  46. if err != nil {
  47. return 0, err
  48. }
  49. // create the registry
  50. // query for registry name
  51. regName, err := utils.PromptPlaintext("Give this registry a name: ")
  52. if err != nil {
  53. return 0, err
  54. }
  55. reg, err := client.CreateRegistry(
  56. context.Background(),
  57. projectID,
  58. &types.CreateRegistryRequest{
  59. Name: regName,
  60. GCPIntegrationID: integration.ID,
  61. URL: region + "-docker.pkg.dev/" + integration.GCPProjectID,
  62. },
  63. )
  64. if err != nil {
  65. return 0, err
  66. }
  67. color.New(color.FgGreen).Printf("created registry with id %d and name %s\n", reg.ID, reg.Name)
  68. return reg.ID, nil
  69. }
  70. return 0, fmt.Errorf("could not read service account key file")
  71. }