|
18 | 18 | package cmdinstanceconfig
|
19 | 19 |
|
20 | 20 | import (
|
21 |
| - "os" |
22 |
| - "path/filepath" |
23 |
| - |
24 |
| - "github.com/elastic/cloud-sdk-go/pkg/api/platformapi/instanceconfigapi" |
25 |
| - "github.com/elastic/cloud-sdk-go/pkg/input" |
26 |
| - sdkcmdutil "github.com/elastic/cloud-sdk-go/pkg/util/cmdutil" |
27 | 21 | "github.com/spf13/cobra"
|
28 | 22 |
|
29 | 23 | cmdutil "github.com/elastic/ecctl/cmd/util"
|
30 |
| - "github.com/elastic/ecctl/pkg/ecctl" |
31 | 24 | )
|
32 | 25 |
|
33 | 26 | // Command is the top instance-config subcommand.
|
34 | 27 | var Command = &cobra.Command{
|
35 | 28 | Use: "instance-configuration",
|
36 | 29 | Short: cmdutil.AdminReqDescription("Manages instance configurations"),
|
37 |
| - PreRunE: cobra.MaximumNArgs(0), |
| 30 | + PreRunE: cobra.NoArgs, |
38 | 31 | Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
|
39 | 32 | }
|
40 |
| - |
41 |
| -var platformInstanceConfigurationListCmd = &cobra.Command{ |
42 |
| - Use: "list", |
43 |
| - Short: "Lists the instance configurations", |
44 |
| - PreRunE: cobra.MaximumNArgs(0), |
45 |
| - RunE: func(cmd *cobra.Command, args []string) error { |
46 |
| - res, err := instanceconfigapi.List(instanceconfigapi.ListParams{ |
47 |
| - API: ecctl.Get().API, |
48 |
| - }) |
49 |
| - |
50 |
| - if err != nil { |
51 |
| - return err |
52 |
| - } |
53 |
| - |
54 |
| - return ecctl.Get().Formatter.Format(filepath.Join("instance-configuration", "list"), res) |
55 |
| - }, |
56 |
| -} |
57 |
| - |
58 |
| -var platformInstanceConfigurationCreateCmd = &cobra.Command{ |
59 |
| - Use: "create -f <config.json>", |
60 |
| - Short: "Creates a new instance configuration", |
61 |
| - PreRunE: cobra.MaximumNArgs(0), |
62 |
| - |
63 |
| - RunE: func(cmd *cobra.Command, args []string) error { |
64 |
| - if err := sdkcmdutil.FileOrStdin(cmd, "file"); err != nil { |
65 |
| - return err |
66 |
| - } |
67 |
| - |
68 |
| - file, err := input.NewFileOrReader(os.Stdin, cmd.Flag("file").Value.String()) |
69 |
| - if err != nil { |
70 |
| - return err |
71 |
| - } |
72 |
| - defer file.Close() |
73 |
| - |
74 |
| - config, err := instanceconfigapi.NewConfig(file) |
75 |
| - if err != nil { |
76 |
| - return err |
77 |
| - } |
78 |
| - |
79 |
| - if id := cmd.Flag("id").Value.String(); id != "" { |
80 |
| - config.ID = id |
81 |
| - } |
82 |
| - |
83 |
| - res, err := instanceconfigapi.Create(instanceconfigapi.CreateParams{ |
84 |
| - API: ecctl.Get().API, |
85 |
| - Config: config, |
86 |
| - }) |
87 |
| - |
88 |
| - if err != nil { |
89 |
| - return err |
90 |
| - } |
91 |
| - |
92 |
| - return ecctl.Get().Formatter.Format(filepath.Join("instance-configuration", "create"), res) |
93 |
| - }, |
94 |
| -} |
95 |
| - |
96 |
| -var platformInstanceConfigurationShowCmd = &cobra.Command{ |
97 |
| - Use: "show <config id>", |
98 |
| - Short: "Shows an instance configuration", |
99 |
| - PreRunE: cobra.MinimumNArgs(1), |
100 |
| - RunE: func(cmd *cobra.Command, args []string) error { |
101 |
| - res, err := instanceconfigapi.Get(instanceconfigapi.GetParams{ |
102 |
| - API: ecctl.Get().API, |
103 |
| - ID: args[0], |
104 |
| - }) |
105 |
| - |
106 |
| - if err != nil { |
107 |
| - return err |
108 |
| - } |
109 |
| - |
110 |
| - return ecctl.Get().Formatter.Format(filepath.Join("instance-configuration", "show"), res) |
111 |
| - }, |
112 |
| -} |
113 |
| - |
114 |
| -var platformInstanceConfigurationDeleteCmd = &cobra.Command{ |
115 |
| - Use: "delete <config id>", |
116 |
| - Short: "Deletes an instance configuration", |
117 |
| - PreRunE: cobra.MinimumNArgs(1), |
118 |
| - RunE: func(cmd *cobra.Command, args []string) error { |
119 |
| - return instanceconfigapi.Delete(instanceconfigapi.DeleteParams{ |
120 |
| - API: ecctl.Get().API, |
121 |
| - ID: args[0], |
122 |
| - }) |
123 |
| - }, |
124 |
| -} |
125 |
| - |
126 |
| -var platformInstanceConfigurationUpdateCmd = &cobra.Command{ |
127 |
| - Use: "update <config id> -f <config.json>", |
128 |
| - Short: "Overwrites an instance configuration", |
129 |
| - PreRunE: cobra.MinimumNArgs(1), |
130 |
| - RunE: func(cmd *cobra.Command, args []string) error { |
131 |
| - if err := sdkcmdutil.FileOrStdin(cmd, "file"); err != nil { |
132 |
| - return err |
133 |
| - } |
134 |
| - |
135 |
| - file, err := input.NewFileOrReader(os.Stdin, cmd.Flag("file").Value.String()) |
136 |
| - if err != nil { |
137 |
| - return err |
138 |
| - } |
139 |
| - defer file.Close() |
140 |
| - |
141 |
| - config, err := instanceconfigapi.NewConfig(file) |
142 |
| - if err != nil { |
143 |
| - return err |
144 |
| - } |
145 |
| - |
146 |
| - return instanceconfigapi.Update(instanceconfigapi.UpdateParams{ |
147 |
| - API: ecctl.Get().API, |
148 |
| - ID: args[0], |
149 |
| - Config: config, |
150 |
| - }) |
151 |
| - }, |
152 |
| -} |
153 |
| - |
154 |
| -var platformInstanceConfigurationPullCmd = &cobra.Command{ |
155 |
| - Use: "pull --path <path>", |
156 |
| - Short: "Downloads instance configuration into a local folder", |
157 |
| - PreRunE: cobra.MaximumNArgs(0), |
158 |
| - |
159 |
| - RunE: func(cmd *cobra.Command, args []string) error { |
160 |
| - |
161 |
| - return instanceconfigapi.PullToDirectory(instanceconfigapi.PullToDirectoryParams{ |
162 |
| - API: ecctl.Get().API, |
163 |
| - Directory: cmd.Flag("path").Value.String(), |
164 |
| - }) |
165 |
| - }, |
166 |
| -} |
167 |
| - |
168 |
| -func init() { |
169 |
| - Command.AddCommand( |
170 |
| - platformInstanceConfigurationListCmd, |
171 |
| - platformInstanceConfigurationCreateCmd, |
172 |
| - platformInstanceConfigurationShowCmd, |
173 |
| - platformInstanceConfigurationDeleteCmd, |
174 |
| - platformInstanceConfigurationUpdateCmd, |
175 |
| - platformInstanceConfigurationPullCmd, |
176 |
| - ) |
177 |
| - |
178 |
| - platformInstanceConfigurationCreateCmd.Flags().StringP("file", "f", "", "Instance configuration JSON file definition") |
179 |
| - platformInstanceConfigurationCreateCmd.Flags().String("id", "", "Optional ID to set for the instance configuration (Overrides id if present)") |
180 |
| - platformInstanceConfigurationUpdateCmd.Flags().StringP("file", "f", "", "Instance configuration JSON file definition") |
181 |
| - |
182 |
| - platformInstanceConfigurationPullCmd.Flags().StringP("path", "p", "", "Local path with instance configuration.") |
183 |
| - platformInstanceConfigurationPullCmd.MarkFlagRequired("path") |
184 |
| -} |
0 commit comments