create.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "github.com/pkg/errors"
  28. "github.com/docker/api/client"
  29. "github.com/spf13/cobra"
  30. "github.com/docker/api/context/store"
  31. )
  32. // AciCreateOpts Options for ACI context create
  33. type AciCreateOpts struct {
  34. description string
  35. aciLocation string
  36. aciSubscriptionID string
  37. aciResourceGroup string
  38. }
  39. func createCommand() *cobra.Command {
  40. var opts AciCreateOpts
  41. cmd := &cobra.Command{
  42. Use: "create CONTEXT BACKEND [OPTIONS]",
  43. Short: "Create a context",
  44. Args: cobra.ExactArgs(2),
  45. RunE: func(cmd *cobra.Command, args []string) error {
  46. return runCreate(cmd.Context(), opts, args[0], args[1])
  47. },
  48. }
  49. cmd.Flags().StringVar(&opts.description, "description", "", "Description of the context")
  50. cmd.Flags().StringVar(&opts.aciLocation, "aci-location", "eastus", "Location")
  51. cmd.Flags().StringVar(&opts.aciSubscriptionID, "aci-subscription-id", "", "Location")
  52. cmd.Flags().StringVar(&opts.aciResourceGroup, "aci-resource-group", "", "Resource group")
  53. return cmd
  54. }
  55. func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType string) error {
  56. var description string
  57. var contextData interface{}
  58. switch contextType {
  59. case "aci":
  60. cs, err := client.GetCloudService(ctx, "aci")
  61. if err != nil {
  62. return errors.Wrap(err, "cannot connect to backend")
  63. }
  64. params := map[string]string{
  65. "aciSubscriptionId": opts.aciSubscriptionID,
  66. "aciResourceGroup": opts.aciResourceGroup,
  67. "aciLocation": opts.aciLocation,
  68. "description": opts.description,
  69. }
  70. contextData, description, err = cs.CreateContextData(ctx, params)
  71. if err != nil {
  72. return errors.Wrap(err, "cannot create context")
  73. }
  74. default: // TODO: we need to implement different contexts for known backends
  75. description = opts.description
  76. contextData = store.ExampleContext{}
  77. }
  78. s := store.ContextStore(ctx)
  79. return s.Create(
  80. name,
  81. contextType,
  82. description,
  83. contextData,
  84. )
  85. }