create.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (c) 2020 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package context
  25. import (
  26. "context"
  27. "fmt"
  28. "github.com/pkg/errors"
  29. "github.com/spf13/cobra"
  30. "github.com/docker/api/client"
  31. "github.com/docker/api/context/store"
  32. )
  33. // AciCreateOpts Options for ACI context create
  34. type AciCreateOpts struct {
  35. description string
  36. aciLocation string
  37. aciSubscriptionID string
  38. aciResourceGroup string
  39. }
  40. func createCommand() *cobra.Command {
  41. var opts AciCreateOpts
  42. cmd := &cobra.Command{
  43. Use: "create CONTEXT BACKEND [OPTIONS]",
  44. Short: "Create a context",
  45. Args: cobra.ExactArgs(2),
  46. RunE: func(cmd *cobra.Command, args []string) error {
  47. return runCreate(cmd.Context(), opts, args[0], args[1])
  48. },
  49. }
  50. cmd.Flags().StringVar(&opts.description, "description", "", "Description of the context")
  51. cmd.Flags().StringVar(&opts.aciLocation, "aci-location", "eastus", "Location")
  52. cmd.Flags().StringVar(&opts.aciSubscriptionID, "aci-subscription-id", "", "Location")
  53. cmd.Flags().StringVar(&opts.aciResourceGroup, "aci-resource-group", "", "Resource group")
  54. return cmd
  55. }
  56. func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType string) error {
  57. contextData, description, err := getContextData(ctx, contextType, opts)
  58. if err != nil {
  59. return nil
  60. }
  61. s := store.ContextStore(ctx)
  62. return s.Create(
  63. name,
  64. contextType,
  65. description,
  66. contextData,
  67. )
  68. }
  69. func getContextData(ctx context.Context, contextType string, opts AciCreateOpts) (interface{}, string, error) {
  70. switch contextType {
  71. case "aci":
  72. cs, err := client.GetCloudService(ctx, "aci")
  73. if err != nil {
  74. return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
  75. }
  76. params := map[string]string{
  77. "aciSubscriptionId": opts.aciSubscriptionID,
  78. "aciResourceGroup": opts.aciResourceGroup,
  79. "aciLocation": opts.aciLocation,
  80. "description": opts.description,
  81. }
  82. return cs.CreateContextData(ctx, params)
  83. case "moby":
  84. return store.MobyContext{}, opts.description, nil
  85. case "example":
  86. return store.ExampleContext{}, opts.description, nil
  87. default:
  88. return nil, "", errors.New(fmt.Sprintf("incorrect context type %s, must be one of (aci | moby | docker)", contextType))
  89. }
  90. }