create_ecs.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package context
  14. import (
  15. "context"
  16. "github.com/pkg/errors"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose-cli/api/client"
  19. "github.com/docker/compose-cli/context/store"
  20. "github.com/docker/compose-cli/ecs"
  21. "github.com/docker/compose-cli/errdefs"
  22. )
  23. func init() {
  24. extraCommands = append(extraCommands, createEcsCommand)
  25. extraHelp = append(extraHelp, `
  26. Create Amazon ECS context:
  27. $ docker context create ecs CONTEXT [flags]
  28. (see docker context create ecs --help)
  29. `)
  30. }
  31. func createEcsCommand() *cobra.Command {
  32. var localSimulation bool
  33. var opts ecs.ContextParams
  34. cmd := &cobra.Command{
  35. Use: "ecs CONTEXT [flags]",
  36. Short: "Create a context for Amazon ECS",
  37. Args: cobra.ExactArgs(1),
  38. RunE: func(cmd *cobra.Command, args []string) error {
  39. opts.Name = args[0]
  40. if localSimulation {
  41. return runCreateLocalSimulation(cmd.Context(), args[0], opts)
  42. }
  43. return runCreateEcs(cmd.Context(), args[0], opts)
  44. },
  45. }
  46. addDescriptionFlag(cmd, &opts.Description)
  47. cmd.Flags().BoolVar(&localSimulation, "local-simulation", false, "Create context for ECS local simulation endpoints")
  48. cmd.Flags().StringVar(&opts.Profile, "profile", "", "Profile")
  49. cmd.Flags().StringVar(&opts.Region, "region", "", "Region")
  50. return cmd
  51. }
  52. func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  53. if contextExists(ctx, contextName) {
  54. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  55. }
  56. cs, err := client.GetCloudService(ctx, store.EcsLocalSimulationContextType)
  57. if err != nil {
  58. return errors.Wrap(err, "cannot connect to ECS backend")
  59. }
  60. data, description, err := cs.CreateContextData(ctx, opts)
  61. if err != nil {
  62. return err
  63. }
  64. return createDockerContext(ctx, contextName, store.EcsLocalSimulationContextType, description, data)
  65. }
  66. func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  67. if contextExists(ctx, contextName) {
  68. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  69. }
  70. contextData, description, err := getEcsContextData(ctx, opts)
  71. if err != nil {
  72. return err
  73. }
  74. return createDockerContext(ctx, contextName, store.EcsContextType, description, contextData)
  75. }
  76. func getEcsContextData(ctx context.Context, opts ecs.ContextParams) (interface{}, string, error) {
  77. cs, err := client.GetCloudService(ctx, store.EcsContextType)
  78. if err != nil {
  79. return nil, "", errors.Wrap(err, "cannot connect to ECS backend")
  80. }
  81. return cs.CreateContextData(ctx, opts)
  82. }