create_aci.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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/aci"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/api/context/store"
  21. "github.com/docker/compose-cli/api/errdefs"
  22. )
  23. func init() {
  24. extraCommands = append(extraCommands, createAciCommand)
  25. extraHelp = append(extraHelp, `
  26. Create Azure Container Instances context:
  27. $ docker context create aci CONTEXT [flags]
  28. (see docker context create aci --help)
  29. `)
  30. }
  31. func createAciCommand() *cobra.Command {
  32. var opts aci.ContextParams
  33. cmd := &cobra.Command{
  34. Use: "aci CONTEXT [flags]",
  35. Short: "Create a context for Azure Container Instances",
  36. Args: cobra.ExactArgs(1),
  37. RunE: func(cmd *cobra.Command, args []string) error {
  38. return runCreateAci(cmd.Context(), args[0], opts)
  39. },
  40. }
  41. addDescriptionFlag(cmd, &opts.Description)
  42. cmd.Flags().StringVar(&opts.Location, "location", "eastus", "Location")
  43. cmd.Flags().StringVar(&opts.SubscriptionID, "subscription-id", "", "Location")
  44. cmd.Flags().StringVar(&opts.ResourceGroup, "resource-group", "", "Resource group")
  45. return cmd
  46. }
  47. func runCreateAci(ctx context.Context, contextName string, opts aci.ContextParams) error {
  48. if contextExists(ctx, contextName) {
  49. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName)
  50. }
  51. contextData, description, err := getAciContextData(ctx, opts)
  52. if err != nil {
  53. if aci.IsSubscriptionNotFoundError(err) {
  54. return errors.New("could not find the requested subscription from your Azure login. You might need to specify a tenant ID with docker login azure --tenant-id xxx")
  55. }
  56. return err
  57. }
  58. return createDockerContext(ctx, contextName, store.AciContextType, description, contextData)
  59. }
  60. func getAciContextData(ctx context.Context, opts aci.ContextParams) (interface{}, string, error) {
  61. cs, err := client.GetCloudService(ctx, store.AciContextType)
  62. if err != nil {
  63. return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
  64. }
  65. return cs.CreateContextData(ctx, opts)
  66. }