ecr.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/fatih/color"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. awsLocal "github.com/porter-dev/porter/cli/cmd/providers/aws/local"
  9. "github.com/porter-dev/porter/cli/cmd/utils"
  10. )
  11. // ECR creates an ECR integration
  12. func ECR(
  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. // query for the region
  21. region, err := utils.PromptPlaintext(fmt.Sprintf(`Please provide the AWS region where the ECR instance is located.
  22. AWS Region: `))
  23. if err != nil {
  24. return 0, err
  25. }
  26. userResp, err := utils.PromptPlaintext(
  27. fmt.Sprintf(`Porter can set up an IAM user in your AWS account to connect to this ECR instance automatically.
  28. Would you like to proceed? %s `,
  29. color.New(color.FgCyan).Sprintf("[y/n]"),
  30. ),
  31. )
  32. if err != nil {
  33. return 0, err
  34. }
  35. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  36. agent := awsLocal.NewDefaultAgent()
  37. creds, err := agent.CreateIAMECRUser(region)
  38. if err != nil {
  39. color.New(color.FgRed).Printf("Automatic creation failed, manual input required. Error was: %v\n", err)
  40. return ecrManual(client, projectID, region)
  41. }
  42. integration, err := client.CreateAWSIntegration(
  43. context.Background(),
  44. projectID,
  45. &api.CreateAWSIntegrationRequest{
  46. AWSAccessKeyID: creds.AWSAccessKeyID,
  47. AWSSecretAccessKey: creds.AWSSecretAccessKey,
  48. AWSRegion: region,
  49. },
  50. )
  51. if err != nil {
  52. return 0, err
  53. }
  54. color.New(color.FgGreen).Printf("created aws integration with id %d\n", integration.ID)
  55. return linkRegistry(client, projectID, integration.ID)
  56. }
  57. return ecrManual(client, projectID, region)
  58. }
  59. func ecrManual(
  60. client *api.Client,
  61. projectID uint,
  62. region string,
  63. ) (uint, error) {
  64. // if project ID is 0, ask the user to set the project ID or create a project
  65. if projectID == 0 {
  66. return 0, fmt.Errorf("no project set, please run porter project set [id]")
  67. }
  68. // query for the access key id
  69. accessKeyID, err := utils.PromptPlaintext(fmt.Sprintf(`AWS Access Key ID: `))
  70. if err != nil {
  71. return 0, err
  72. }
  73. // query for the secret access key
  74. secretKey, err := utils.PromptPlaintext(fmt.Sprintf(`AWS Secret Access Key: `))
  75. if err != nil {
  76. return 0, err
  77. }
  78. // create the aws integration
  79. integration, err := client.CreateAWSIntegration(
  80. context.Background(),
  81. projectID,
  82. &api.CreateAWSIntegrationRequest{
  83. AWSAccessKeyID: accessKeyID,
  84. AWSSecretAccessKey: secretKey,
  85. AWSRegion: region,
  86. },
  87. )
  88. if err != nil {
  89. return 0, err
  90. }
  91. color.New(color.FgGreen).Printf("created aws integration with id %d\n", integration.ID)
  92. return linkRegistry(client, projectID, integration.ID)
  93. }
  94. func linkRegistry(client *api.Client, projectID uint, intID uint) (uint, error) {
  95. // create the registry
  96. // query for registry name
  97. regName, err := utils.PromptPlaintext(fmt.Sprintf(`Give this registry a name: `))
  98. if err != nil {
  99. return 0, err
  100. }
  101. reg, err := client.CreateECR(
  102. context.Background(),
  103. projectID,
  104. &api.CreateECRRequest{
  105. Name: regName,
  106. AWSIntegrationID: intID,
  107. },
  108. )
  109. if err != nil {
  110. return 0, err
  111. }
  112. color.New(color.FgGreen).Printf("created registry with id %d and name %s\n", reg.ID, reg.Name)
  113. return reg.ID, nil
  114. }