1
0

create_ecs.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2020 Docker, Inc.
  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/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. if localSimulation {
  40. return runCreateLocalSimulation(cmd.Context(), args[0], opts)
  41. }
  42. return runCreateEcs(cmd.Context(), args[0], opts)
  43. },
  44. }
  45. addDescriptionFlag(cmd, &opts.Description)
  46. cmd.Flags().BoolVar(&localSimulation, "local-simulation", false, "Create context for ECS local simulation endpoints")
  47. cmd.Flags().StringVar(&opts.Profile, "profile", "", "Profile")
  48. cmd.Flags().StringVar(&opts.Region, "region", "", "Region")
  49. cmd.Flags().StringVar(&opts.AwsID, "key-id", "", "AWS Access Key ID")
  50. cmd.Flags().StringVar(&opts.AwsSecret, "secret-key", "", "AWS Secret Access Key")
  51. return cmd
  52. }
  53. func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  54. if contextExists(ctx, contextName) {
  55. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  56. }
  57. cs, err := client.GetCloudService(ctx, store.EcsLocalSimulationContextType)
  58. if err != nil {
  59. return errors.Wrap(err, "cannot connect to ECS backend")
  60. }
  61. data, description, err := cs.CreateContextData(ctx, opts)
  62. if err != nil {
  63. return err
  64. }
  65. return createDockerContext(ctx, contextName, store.EcsLocalSimulationContextType, description, data)
  66. }
  67. func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  68. if contextExists(ctx, contextName) {
  69. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  70. }
  71. contextData, description, err := getEcsContextData(ctx, opts)
  72. if err != nil {
  73. return err
  74. }
  75. return createDockerContext(ctx, contextName, store.EcsContextType, description, contextData)
  76. }
  77. func getEcsContextData(ctx context.Context, opts ecs.ContextParams) (interface{}, string, error) {
  78. cs, err := client.GetCloudService(ctx, store.EcsContextType)
  79. if err != nil {
  80. return nil, "", errors.Wrap(err, "cannot connect to ECS backend")
  81. }
  82. return cs.CreateContextData(ctx, opts)
  83. }