gcr.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "github.com/fatih/color"
  8. "github.com/porter-dev/porter/cli/cmd/api"
  9. "github.com/porter-dev/porter/cli/cmd/utils"
  10. )
  11. // GCR creates a GCR integration
  12. func GCR(
  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 project set [id]")
  19. }
  20. keyFileLocation, err := utils.PromptPlaintext(fmt.Sprintf(`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 := ioutil.ReadFile(keyFileLocation)
  29. if err != nil {
  30. return 0, err
  31. }
  32. // create the aws integration
  33. integration, err := client.CreateGCPIntegration(
  34. context.Background(),
  35. projectID,
  36. &api.CreateGCPIntegrationRequest{
  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. regURL, err := utils.PromptPlaintext(fmt.Sprintf(`Please provide the registry URL, in the form [GCP_DOMAIN]/[GCP_PROJECT_ID]. For example, gcr.io/my-project-123456.
  45. Registry URL: `))
  46. if err != nil {
  47. return 0, err
  48. }
  49. // create the registry
  50. // query for registry name
  51. regName, err := utils.PromptPlaintext(fmt.Sprintf(`Give this registry a name: `))
  52. if err != nil {
  53. return 0, err
  54. }
  55. reg, err := client.CreateGCR(
  56. context.Background(),
  57. projectID,
  58. &api.CreateGCRRequest{
  59. Name: regName,
  60. GCPIntegrationID: integration.ID,
  61. URL: regURL,
  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. }