1
0

create_ecs.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/api/client"
  19. "github.com/docker/api/context/store"
  20. "github.com/docker/api/ecs"
  21. "github.com/docker/api/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 opts ecs.ContextParams
  33. cmd := &cobra.Command{
  34. Use: "ecs CONTEXT [flags]",
  35. Short: "Create a context for Amazon ECS",
  36. Args: cobra.ExactArgs(1),
  37. RunE: func(cmd *cobra.Command, args []string) error {
  38. return runCreateEcs(cmd.Context(), args[0], opts)
  39. },
  40. }
  41. addDescriptionFlag(cmd, &opts.Description)
  42. cmd.Flags().StringVar(&opts.Profile, "profile", "", "Profile")
  43. cmd.Flags().StringVar(&opts.Region, "region", "", "Region")
  44. cmd.Flags().StringVar(&opts.AwsID, "key-id", "", "AWS Access Key ID")
  45. cmd.Flags().StringVar(&opts.AwsSecret, "secret-key", "", "AWS Secret Access Key")
  46. return cmd
  47. }
  48. func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  49. if contextExists(ctx, contextName) {
  50. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  51. }
  52. contextData, description, err := getEcsContextData(ctx, opts)
  53. if err != nil {
  54. return err
  55. }
  56. return createDockerContext(ctx, contextName, store.EcsContextType, description, contextData)
  57. }
  58. func getEcsContextData(ctx context.Context, opts ecs.ContextParams) (interface{}, string, error) {
  59. cs, err := client.GetCloudService(ctx, store.EcsContextType)
  60. if err != nil {
  61. return nil, "", errors.Wrap(err, "cannot connect to AWS backend")
  62. }
  63. return cs.CreateContextData(ctx, opts)
  64. }