gcr.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // create the registry
  45. // query for registry name
  46. regName, err := utils.PromptPlaintext(fmt.Sprintf(`Give this registry a name: `))
  47. if err != nil {
  48. return 0, err
  49. }
  50. reg, err := client.CreateGCR(
  51. context.Background(),
  52. projectID,
  53. &api.CreateGCRRequest{
  54. Name: regName,
  55. GCPIntegrationID: integration.ID,
  56. },
  57. )
  58. if err != nil {
  59. return 0, err
  60. }
  61. color.New(color.FgGreen).Printf("created registry with id %d and name %s\n", reg.ID, reg.Name)
  62. return reg.ID, nil
  63. }
  64. return 0, fmt.Errorf("could not read service account key file")
  65. }