1
0

createaws.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/amazon"
  19. "github.com/docker/api/client"
  20. "github.com/docker/api/context/store"
  21. "github.com/docker/api/errdefs"
  22. )
  23. func createAwsCommand() *cobra.Command {
  24. var opts amazon.ContextParams
  25. cmd := &cobra.Command{
  26. Use: "aws CONTEXT [flags]",
  27. Short: "Create a context for Amazon ECS",
  28. Args: cobra.ExactArgs(1),
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. return runCreateAws(cmd.Context(), args[0], opts)
  31. },
  32. }
  33. addDescriptionFlag(cmd, &opts.Description)
  34. cmd.Flags().StringVar(&opts.Profile, "profile", "", "AWS Profile")
  35. cmd.Flags().StringVar(&opts.Cluster, "cluster", "", "ECS cluster")
  36. cmd.Flags().StringVar(&opts.Region, "region", "", "AWS region")
  37. cmd.Flags().StringVar(&opts.AwsID, "key-id", "", "AWS Access Key ID")
  38. cmd.Flags().StringVar(&opts.AwsSecret, "secret-key", "", "AWS Secret Access Key")
  39. return cmd
  40. }
  41. func runCreateAws(ctx context.Context, contextName string, opts amazon.ContextParams) error {
  42. if contextExists(ctx, contextName) {
  43. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName)
  44. }
  45. contextData, description, err := getAwsContextData(ctx, opts)
  46. if err != nil {
  47. return err
  48. }
  49. return createDockerContext(ctx, contextName, store.AwsContextType, description, contextData)
  50. }
  51. func getAwsContextData(ctx context.Context, opts amazon.ContextParams) (interface{}, string, error) {
  52. cs, err := client.GetCloudService(ctx, store.AwsContextType)
  53. if err != nil {
  54. return nil, "", errors.Wrap(err, "cannot connect to AWS backend")
  55. }
  56. return cs.CreateContextData(ctx, opts)
  57. }