createaci.go 2.1 KB

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