Skip to content

Commit fa7fc09

Browse files
authored
instance-configuration: Add region to commands (#331)
Splits `command.go` into individual command files, populating the Region property on each of the commands. Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent feb03d0 commit fa7fc09

File tree

7 files changed

+328
-153
lines changed

7 files changed

+328
-153
lines changed

cmd/platform/instance-configuration/command.go

Lines changed: 1 addition & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -18,167 +18,15 @@
1818
package cmdinstanceconfig
1919

2020
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"
2721
"github.com/spf13/cobra"
2822

2923
cmdutil "github.com/elastic/ecctl/cmd/util"
30-
"github.com/elastic/ecctl/pkg/ecctl"
3124
)
3225

3326
// Command is the top instance-config subcommand.
3427
var Command = &cobra.Command{
3528
Use: "instance-configuration",
3629
Short: cmdutil.AdminReqDescription("Manages instance configurations"),
37-
PreRunE: cobra.MaximumNArgs(0),
30+
PreRunE: cobra.NoArgs,
3831
Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
3932
}
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-
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cmdinstanceconfig
19+
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+
"github.com/spf13/cobra"
28+
29+
"github.com/elastic/ecctl/pkg/ecctl"
30+
)
31+
32+
var createCmd = &cobra.Command{
33+
Use: "create -f <config.json>",
34+
Short: "Creates a new instance configuration",
35+
PreRunE: cobra.NoArgs,
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
if err := sdkcmdutil.FileOrStdin(cmd, "file"); err != nil {
38+
return err
39+
}
40+
41+
file, err := input.NewFileOrReader(os.Stdin, cmd.Flag("file").Value.String())
42+
if err != nil {
43+
return err
44+
}
45+
defer file.Close()
46+
47+
config, err := instanceconfigapi.NewConfig(file)
48+
if err != nil {
49+
return err
50+
}
51+
52+
if id := cmd.Flag("id").Value.String(); id != "" {
53+
config.ID = id
54+
}
55+
56+
res, err := instanceconfigapi.Create(instanceconfigapi.CreateParams{
57+
API: ecctl.Get().API,
58+
Region: ecctl.Get().Config.Region,
59+
Config: config,
60+
})
61+
62+
if err != nil {
63+
return err
64+
}
65+
66+
return ecctl.Get().Formatter.Format(filepath.Join("instance-configuration", "create"), res)
67+
},
68+
}
69+
70+
func init() {
71+
Command.AddCommand(createCmd)
72+
73+
createCmd.Flags().StringP("file", "f", "", "Instance configuration JSON file definition")
74+
createCmd.Flags().String("id", "", "Optional ID to set for the instance configuration (Overrides id if present)")
75+
createCmd.MarkFlagRequired("file")
76+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cmdinstanceconfig
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api/platformapi/instanceconfigapi"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var deleteCmd = &cobra.Command{
28+
Use: "delete <config id>",
29+
Short: "Deletes an instance configuration",
30+
PreRunE: cobra.ExactArgs(1),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return instanceconfigapi.Delete(instanceconfigapi.DeleteParams{
33+
API: ecctl.Get().API,
34+
Region: ecctl.Get().Config.Region,
35+
ID: args[0],
36+
})
37+
},
38+
}
39+
40+
func init() {
41+
Command.AddCommand(deleteCmd)
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cmdinstanceconfig
19+
20+
import (
21+
"path/filepath"
22+
23+
"github.com/elastic/cloud-sdk-go/pkg/api/platformapi/instanceconfigapi"
24+
"github.com/spf13/cobra"
25+
26+
"github.com/elastic/ecctl/pkg/ecctl"
27+
)
28+
29+
var listCmd = &cobra.Command{
30+
Use: "list",
31+
Short: "Lists the instance configurations",
32+
PreRunE: cobra.MaximumNArgs(0),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
res, err := instanceconfigapi.List(instanceconfigapi.ListParams{
35+
API: ecctl.Get().API,
36+
Region: ecctl.Get().Config.Region,
37+
})
38+
39+
if err != nil {
40+
return err
41+
}
42+
43+
return ecctl.Get().Formatter.Format(filepath.Join("instance-configuration", "list"), res)
44+
},
45+
}
46+
47+
func init() {
48+
Command.AddCommand(listCmd)
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cmdinstanceconfig
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api/platformapi/instanceconfigapi"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var pullCmd = &cobra.Command{
28+
Use: "pull --path <path>",
29+
Short: "Downloads instance configuration into a local folder",
30+
PreRunE: cobra.NoArgs,
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return instanceconfigapi.PullToDirectory(instanceconfigapi.PullToDirectoryParams{
33+
API: ecctl.Get().API,
34+
Region: ecctl.Get().Config.Region,
35+
Directory: cmd.Flag("path").Value.String(),
36+
})
37+
},
38+
}
39+
40+
func init() {
41+
Command.AddCommand(pullCmd)
42+
43+
pullCmd.Flags().StringP("path", "p", "", "Local path with instance configuration.")
44+
pullCmd.MarkFlagRequired("path")
45+
}

0 commit comments

Comments
 (0)