Skip to content

Commit 21176cd

Browse files
authored
deployment: Add update command (#55)
Adds the `deployment update` command which mainly relies on a JSON file defnition as the payload to which the specified cluster ID is going to be updated. By default it sets prune_orphans=false in the API payload, which causes the JSON formatted deployment update payload to be treated as a partial update, meaning, any resources not explicitly specified, will be ignored and not shut down. To override this behavior toggle `--prune-orphans` flag. Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent 143ffe5 commit 21176cd

File tree

10 files changed

+898
-195
lines changed

10 files changed

+898
-195
lines changed

cmd/deployment/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ var createCmd = &cobra.Command{
152152
API: ecctl.Get().API,
153153
RequestID: reqID,
154154
Request: &r,
155-
Overrides: &deployment.CreateOverrides{
155+
Overrides: &deployment.PayloadOverrides{
156156
Name: name,
157157
Region: region,
158158
Version: version,

cmd/deployment/update.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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 cmddeployment
19+
20+
import (
21+
"os"
22+
23+
"github.com/elastic/cloud-sdk-go/pkg/models"
24+
"github.com/spf13/cobra"
25+
26+
cmdutil "github.com/elastic/ecctl/cmd/util"
27+
"github.com/elastic/ecctl/pkg/deployment"
28+
"github.com/elastic/ecctl/pkg/ecctl"
29+
)
30+
31+
const updateLong = `updates a deployment from a file definition, defaulting prune_orphans=false, making the default
32+
update action safe for partial updates, to override this behavior toggle --prune-orphans.
33+
34+
Read more about the deployment definition in https://www.elastic.co/guide/en/cloud-enterprise/current/Deployment_-_CRUD.html`
35+
36+
var updateExample = `
37+
#### Same base deployment as the create example, changing cluster_topology[0].zone_count to 3.
38+
$ cat deployment_example_update.json
39+
{
40+
"resources": {
41+
"elasticsearch": [
42+
{
43+
"display_name": "my elasticsearch cluster",
44+
"ref_id": "my-es-cluster",
45+
"plan": {
46+
"deployment_template": {
47+
"id": "default"
48+
},
49+
"elasticsearch": {
50+
"version": "6.8.4"
51+
},
52+
"cluster_topology": [
53+
{
54+
"instance_configuration_id": "data.default",
55+
"memory_per_node": 1024,
56+
"node_count_per_zone": 1,
57+
"node_type": {
58+
"data": true,
59+
"ingest": true,
60+
"master": true,
61+
"ml": false
62+
},
63+
"zone_count": 3
64+
}
65+
]
66+
}
67+
}
68+
]
69+
}
70+
}
71+
$ ecctl deployment update f44c06c3af6f85dac05023cf243f4ab1 -f deployment_example_update.json
72+
{
73+
"id": "f44c06c3af6f85dac05023cf243f4ab1",
74+
"name": "my example cluster",
75+
"resources": [
76+
{
77+
"id": "205745432f6345a4999dd2d77ceb1812",
78+
"kind": "elasticsearch",
79+
"ref_id": "my-es-cluster",
80+
"region": "ece-region"
81+
},
82+
{
83+
"elasticsearch_cluster_ref_id": "my-es-cluster",
84+
"id": "3617594f01074b76a5ca4f903f9d33ec",
85+
"kind": "kibana",
86+
"ref_id": "my-kibana-instance",
87+
"region": "ece-region"
88+
},
89+
{
90+
"elasticsearch_cluster_ref_id": "my-es-cluster",
91+
"id": "90c3c9566f454861b0dc935c5c7420d8",
92+
"kind": "apm",
93+
"ref_id": "my-apm-instance",
94+
"region": "ece-region"
95+
}
96+
]
97+
}
98+
#### Setting --prune-orphans, will cause any non-specified resources to be shut down.
99+
$ ecctl deployment update f44c06c3af6f85dac05023cf243f4ab1 -f deployment_example_update.json --prune-orphans
100+
setting --prune-orphans to "true" will cause any resources not specified in the update request to be removed from the deployment, do you want to continue? [y/n]: y
101+
{
102+
"id": "f44c06c3af6f85dac05023cf243f4ab1",
103+
"name": "my example cluster",
104+
"resources": [
105+
{
106+
"id": "205745432f6345a4999dd2d77ceb1812",
107+
"kind": "elasticsearch",
108+
"ref_id": "my-es-cluster",
109+
"region": "ece-region"
110+
}
111+
],
112+
"shutdown_resources": {
113+
"apm": [
114+
"90c3c9566f454861b0dc935c5c7420d8"
115+
],
116+
"appsearch": [],
117+
"elasticsearch": [],
118+
"kibana": [
119+
"3617594f01074b76a5ca4f903f9d33ec"
120+
]
121+
}
122+
}`[1:]
123+
124+
var updateCmd = &cobra.Command{
125+
Use: `update -f <file definition.json>`,
126+
Short: "Updates a deployment from a file definition, allowing certain flag overrides",
127+
Long: updateLong,
128+
Example: updateExample,
129+
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
130+
RunE: func(cmd *cobra.Command, args []string) error {
131+
filename, _ := cmd.Flags().GetString("file")
132+
var r models.DeploymentUpdateRequest
133+
if err := cmdutil.DecodeFile(filename, &r); err != nil {
134+
return err
135+
}
136+
137+
pruneOrphans, _ := cmd.Flags().GetBool("prune-orphans")
138+
r.PruneOrphans = &pruneOrphans
139+
140+
force, _ := cmd.Flags().GetBool("force")
141+
var msg = `setting --prune-orphans to "true" will cause any resources not specified in the update request to be removed from the deployment, do you want to continue? [y/n]: `
142+
if pruneOrphans && !force && !cmdutil.ConfirmAction(msg, os.Stderr, os.Stdout) {
143+
return nil
144+
}
145+
146+
skipSnapshot, _ := cmd.Flags().GetBool("skip-snapshot")
147+
hidePrunedOrphans, _ := cmd.Flags().GetBool("hide-pruned-orphans")
148+
149+
var region string
150+
if ecctl.Get().Config.Region == "" {
151+
region = cmdutil.DefaultECERegion
152+
}
153+
154+
res, err := deployment.Update(deployment.UpdateParams{
155+
DeploymentID: args[0],
156+
API: ecctl.Get().API,
157+
Region: region,
158+
Request: &r,
159+
SkipSnapshot: skipSnapshot,
160+
HidePrunedOrphans: hidePrunedOrphans,
161+
})
162+
163+
if err != nil {
164+
return err
165+
}
166+
167+
return ecctl.Get().Formatter.Format("", res)
168+
},
169+
}
170+
171+
func init() {
172+
Command.AddCommand(updateCmd)
173+
updateCmd.Flags().Bool("prune-orphans", false, "When set to true, it will remove any resources not specified in the update request, treating the json file contents as the authoritative deployment definition")
174+
updateCmd.Flags().Bool("skip-snapshot", false, "Skips taking an Elasticsearch snapshot prior to shutting down the deployment")
175+
updateCmd.Flags().Bool("hide-pruned-orphans", false, "Hides orphaned resources that were shut down (only relevant if --prune-orphans=true)")
176+
updateCmd.Flags().StringP("file", "f", "", "Partial (default) or full JSON file deployment update payload")
177+
updateCmd.MarkFlagRequired("file")
178+
updateCmd.MarkFlagFilename("file", "*.json")
179+
}

docs/ecctl_deployment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ ecctl deployment [flags]
5050
* [ecctl deployment search](ecctl_deployment_search.md) - Performs advanced deployment search using the Elasticsearch Query DSL
5151
* [ecctl deployment show](ecctl_deployment_show.md) - Shows the specified deployment resources
5252
* [ecctl deployment shutdown](ecctl_deployment_shutdown.md) - Shuts down a deployment and all of its associated sub-resources
53+
* [ecctl deployment update](ecctl_deployment_update.md) - Updates a deployment from a file definition, allowing certain flag overrides
5354

docs/ecctl_deployment_update.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
## ecctl deployment update
2+
3+
Updates a deployment from a file definition, allowing certain flag overrides
4+
5+
### Synopsis
6+
7+
updates a deployment from a file definition, defaulting prune_orphans=false, making the default
8+
update action safe for partial updates, to override this behavior toggle --prune-orphans.
9+
10+
Read more about the deployment definition in https://www.elastic.co/guide/en/cloud-enterprise/current/Deployment_-_CRUD.html
11+
12+
```
13+
ecctl deployment update -f <file definition.json> [flags]
14+
```
15+
16+
### Examples
17+
18+
```
19+
#### Same base deployment as the create example, changing cluster_topology[0].zone_count to 3.
20+
$ cat deployment_example_update.json
21+
{
22+
"resources": {
23+
"elasticsearch": [
24+
{
25+
"display_name": "my elasticsearch cluster",
26+
"ref_id": "my-es-cluster",
27+
"plan": {
28+
"deployment_template": {
29+
"id": "default"
30+
},
31+
"elasticsearch": {
32+
"version": "6.8.4"
33+
},
34+
"cluster_topology": [
35+
{
36+
"instance_configuration_id": "data.default",
37+
"memory_per_node": 1024,
38+
"node_count_per_zone": 1,
39+
"node_type": {
40+
"data": true,
41+
"ingest": true,
42+
"master": true,
43+
"ml": false
44+
},
45+
"zone_count": 3
46+
}
47+
]
48+
}
49+
}
50+
]
51+
}
52+
}
53+
$ ecctl deployment update f44c06c3af6f85dac05023cf243f4ab1 -f deployment_example_update.json
54+
{
55+
"id": "f44c06c3af6f85dac05023cf243f4ab1",
56+
"name": "my example cluster",
57+
"resources": [
58+
{
59+
"id": "205745432f6345a4999dd2d77ceb1812",
60+
"kind": "elasticsearch",
61+
"ref_id": "my-es-cluster",
62+
"region": "ece-region"
63+
},
64+
{
65+
"elasticsearch_cluster_ref_id": "my-es-cluster",
66+
"id": "3617594f01074b76a5ca4f903f9d33ec",
67+
"kind": "kibana",
68+
"ref_id": "my-kibana-instance",
69+
"region": "ece-region"
70+
},
71+
{
72+
"elasticsearch_cluster_ref_id": "my-es-cluster",
73+
"id": "90c3c9566f454861b0dc935c5c7420d8",
74+
"kind": "apm",
75+
"ref_id": "my-apm-instance",
76+
"region": "ece-region"
77+
}
78+
]
79+
}
80+
#### Setting --prune-orphans, will cause any non-specified resources to be shut down.
81+
$ ecctl deployment update f44c06c3af6f85dac05023cf243f4ab1 -f deployment_example_update.json --prune-orphans
82+
setting --prune-orphans to "true" will cause any resources not specified in the update request to be removed from the deployment, do you want to continue? [y/n]: y
83+
{
84+
"id": "f44c06c3af6f85dac05023cf243f4ab1",
85+
"name": "my example cluster",
86+
"resources": [
87+
{
88+
"id": "205745432f6345a4999dd2d77ceb1812",
89+
"kind": "elasticsearch",
90+
"ref_id": "my-es-cluster",
91+
"region": "ece-region"
92+
}
93+
],
94+
"shutdown_resources": {
95+
"apm": [
96+
"90c3c9566f454861b0dc935c5c7420d8"
97+
],
98+
"appsearch": [],
99+
"elasticsearch": [],
100+
"kibana": [
101+
"3617594f01074b76a5ca4f903f9d33ec"
102+
]
103+
}
104+
}
105+
```
106+
107+
### Options
108+
109+
```
110+
-f, --file string Partial (default) or full JSON file deployment update payload
111+
-h, --help help for update
112+
--hide-pruned-orphans Hides orphaned resources that were shut down (only relevant if --prune-orphans=true)
113+
--prune-orphans When set to true, it will remove any resources not specified in the update request, treating the json file contents as the authoritative deployment definition
114+
--skip-snapshot Skips taking an Elasticsearch snapshot prior to shutting down the deployment
115+
```
116+
117+
### Options inherited from parent commands
118+
119+
```
120+
--apikey string API key to use to authenticate (If empty will look for EC_APIKEY environment variable)
121+
--config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config")
122+
--force Do not ask for confirmation
123+
--format string Formats the output using a Go template
124+
--host string Base URL to use
125+
--insecure Skips all TLS validation
126+
--message string A message to set on cluster operation
127+
--output string Output format [text|json] (default "text")
128+
--pass string Password to use to authenticate (If empty will look for EC_PASS environment variable)
129+
--pprof Enables pprofing and saves the profile to pprof-20060102150405
130+
-q, --quiet Suppresses the configuration file used for the run, if any
131+
--timeout duration Timeout to use on all HTTP calls (default 30s)
132+
--trace Enables tracing saves the trace to trace-20060102150405
133+
--user string Username to use to authenticate (If empty will look for EC_USER environment variable)
134+
--verbose Enable verbose mode
135+
```
136+
137+
### SEE ALSO
138+
139+
* [ecctl deployment](ecctl_deployment.md) - Manages deployments
140+

0 commit comments

Comments
 (0)