gar.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  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(fmt.Sprintf(`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 := ioutil.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(fmt.Sprintf(`Please enter the artifact registry region. For example, us-central-1.
  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(fmt.Sprintf(`Give this registry a name: `))
  53. if err != nil {
  54. return 0, err
  55. }
  56. reg, err := client.CreateRegistry(
  57. context.Background(),
  58. projectID,
  59. &types.CreateRegistryRequest{
  60. Name: regName,
  61. GCPIntegrationID: integration.ID,
  62. URL: region + "-docker.pkg.dev/" + integration.GCPProjectID,
  63. },
  64. )
  65. if err != nil {
  66. return 0, err
  67. }
  68. color.New(color.FgGreen).Printf("created registry with id %d and name %s\n", reg.ID, reg.Name)
  69. return reg.ID, nil
  70. }
  71. return 0, fmt.Errorf("could not read service account key file")
  72. }