gcr.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // GCR creates a GCR integration
  13. func GCR(
  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(fmt.Sprintf(`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 := ioutil.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. 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.
  47. Registry URL: `))
  48. if err != nil {
  49. return 0, err
  50. }
  51. // create the registry
  52. // query for registry name
  53. regName, err := utils.PromptPlaintext(fmt.Sprintf(`Give this registry a name: `))
  54. if err != nil {
  55. return 0, err
  56. }
  57. reg, err := client.CreateRegistry(
  58. ctx,
  59. projectID,
  60. &types.CreateRegistryRequest{
  61. Name: regName,
  62. GCPIntegrationID: integration.ID,
  63. URL: regURL,
  64. },
  65. )
  66. if err != nil {
  67. return 0, err
  68. }
  69. color.New(color.FgGreen).Printf("created registry with id %d and name %s\n", reg.ID, reg.Name)
  70. return reg.ID, nil
  71. }
  72. return 0, fmt.Errorf("could not read service account key file")
  73. }