createaci.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. )
  21. type aciCreateOpts struct {
  22. description string
  23. location string
  24. subscriptionID string
  25. resourceGroup string
  26. }
  27. func createAciCommand() *cobra.Command {
  28. var opts aciCreateOpts
  29. cmd := &cobra.Command{
  30. Use: "aci CONTEXT [flags]",
  31. Short: "Create a context for Azure Container Instances",
  32. Args: cobra.ExactArgs(1),
  33. RunE: func(cmd *cobra.Command, args []string) error {
  34. contextData, description, err := getAciContextData(cmd.Context(), opts)
  35. if err != nil {
  36. return nil
  37. }
  38. return createDockerContext(cmd.Context(), args[0], store.AciContextType, description, contextData)
  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 getAciContextData(ctx context.Context, opts aciCreateOpts) (interface{}, string, error) {
  48. cs, err := client.GetCloudService(ctx, store.AciContextType)
  49. if err != nil {
  50. return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
  51. }
  52. return cs.CreateContextData(ctx, convertAciOpts(opts))
  53. }
  54. func convertAciOpts(opts aciCreateOpts) map[string]string {
  55. return map[string]string{
  56. "aciSubscriptionId": opts.subscriptionID,
  57. "aciResourceGroup": opts.resourceGroup,
  58. "aciLocation": opts.location,
  59. "description": opts.description,
  60. }
  61. }