createaci.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/errdefs"
  21. )
  22. // AciCreateOpts options for creating ACI context
  23. type AciCreateOpts struct {
  24. Description string
  25. Location string
  26. SubscriptionID string
  27. ResourceGroup string
  28. }
  29. func createAciCommand() *cobra.Command {
  30. var opts AciCreateOpts
  31. cmd := &cobra.Command{
  32. Use: "aci CONTEXT [flags]",
  33. Short: "Create a context for Azure Container Instances",
  34. Args: cobra.ExactArgs(1),
  35. RunE: func(cmd *cobra.Command, args []string) error {
  36. return runCreateAci(cmd.Context(), args[0], opts)
  37. },
  38. }
  39. addDescriptionFlag(cmd, &opts.Description)
  40. cmd.Flags().StringVar(&opts.Location, "location", "eastus", "Location")
  41. cmd.Flags().StringVar(&opts.SubscriptionID, "subscription-id", "", "Location")
  42. cmd.Flags().StringVar(&opts.ResourceGroup, "resource-group", "", "Resource group")
  43. return cmd
  44. }
  45. func runCreateAci(ctx context.Context, contextName string, opts AciCreateOpts) error {
  46. if contextExists(ctx, contextName) {
  47. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName)
  48. }
  49. contextData, description, err := getAciContextData(ctx, opts)
  50. if err != nil {
  51. return err
  52. }
  53. return createDockerContext(ctx, contextName, store.AciContextType, description, contextData)
  54. }
  55. func getAciContextData(ctx context.Context, opts AciCreateOpts) (interface{}, string, error) {
  56. cs, err := client.GetCloudService(ctx, store.AciContextType)
  57. if err != nil {
  58. return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
  59. }
  60. return cs.CreateContextData(ctx, opts)
  61. }