Skip to content

Commit b32c889

Browse files
authored
deployment: Add create command (#36)
Adds the deployment create command which enables the creation of multi-resource deployments from a file definition in one API call. The main features are two flag overrides (name and version) which allow the file definition to be used more like a template. Additionally, a `--request-id` flag is provided which enables setting an idempotency token for the API calls. By default an auto-generated value is sent to the API and printed to the Standard Error device so that users can use the token in case the deployment creation fails. ``` $ dev-cli deployment create -f deployment_example.json --name marcdeploy --version=7.4.1 The deployment creation returned with an error, please use the displayed idempotency token to recreate the deployment resources Idempotency token: GMZPMRrcMYqHdmxjIQkHbdjnhPIeBElcwrHwzVlhGUSMXrEIzVXoBykSVRsKncNb unknown error (status 500) ``` Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent f1c5258 commit b32c889

File tree

10 files changed

+897
-4
lines changed

10 files changed

+897
-4
lines changed

build/errcheck-exclusions.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(*github.com/spf13/cobra.Command).MarkFlagRequired
2+
(*github.com/spf13/cobra.Command).MarkFlagFilename
23
github.com/spf13/cobra.MarkFlagRequired
34
github.com/spf13/cobra.MarkFlagFilename
5+
github.com/spf13/cobra.MarkFlagFilename
46
(*github.com/spf13/cobra.Command).Help
57
(*github.com/spf13/viper.Viper).BindPFlags

cmd/deployment/create.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
"fmt"
22+
"os"
23+
24+
"github.com/elastic/cloud-sdk-go/pkg/models"
25+
"github.com/spf13/cobra"
26+
27+
cmdutil "github.com/elastic/ecctl/cmd/util"
28+
"github.com/elastic/ecctl/pkg/deployment"
29+
"github.com/elastic/ecctl/pkg/ecctl"
30+
"github.com/elastic/ecctl/pkg/util"
31+
)
32+
33+
const createLong = `Creates a deployment from a file definition with an automatically generated idempotency token.
34+
On creation failure, please use the displayed idempotency token to retry the cluster creation with --request-id=<token>.
35+
36+
Read more about the deployment definition in https://www.elastic.co/guide/en/cloud-enterprise/current/Deployment_-_CRUD.html`
37+
38+
var createExample = `
39+
$ cat deployment_example.json
40+
{
41+
"name": "my example cluster",
42+
"resources": {
43+
"apm": [
44+
{
45+
"display_name": "my apm instance",
46+
"ref_id": "my-apm-instance",
47+
"elasticsearch_cluster_ref_id": "my-es-cluster",
48+
"plan": {
49+
"apm": {
50+
"version": "6.8.4"
51+
},
52+
"cluster_topology": [{
53+
"instance_configuration_id": "apm",
54+
"size": {
55+
"resource": "memory",
56+
"value": 512
57+
},
58+
"zone_count": 1
59+
}]
60+
}
61+
}
62+
],
63+
"elasticsearch": [
64+
{
65+
"display_name": "my elasticsearch cluster",
66+
"ref_id": "my-es-cluster",
67+
"plan": {
68+
"deployment_template": {
69+
"id": "default"
70+
},
71+
"elasticsearch": {
72+
"version": "6.8.4"
73+
},
74+
"cluster_topology": [
75+
{
76+
"instance_configuration_id": "data.default",
77+
"memory_per_node": 1024,
78+
"node_count_per_zone": 1,
79+
"node_type": {
80+
"data": true,
81+
"ingest": true,
82+
"master": true,
83+
"ml": false
84+
},
85+
"zone_count": 1
86+
}
87+
]
88+
}
89+
}
90+
],
91+
"kibana": [
92+
{
93+
"display_name": "my kibana instance",
94+
"ref_id": "my-kibana-instance",
95+
"elasticsearch_cluster_ref_id": "my-es-cluster",
96+
"plan": {
97+
"zone_count": 1,
98+
"kibana": {
99+
"version": "6.8.4"
100+
},
101+
"cluster_topology": [
102+
{
103+
"instance_configuration_id": "kibana",
104+
"memory_per_node": 1024,
105+
"node_count_per_zone": 1
106+
}
107+
]
108+
}
109+
}
110+
]
111+
}
112+
}
113+
$ ecctl deployment create -f deployment_example.json --version=7.4.1
114+
[...]
115+
116+
## If th previous deployment creation failed
117+
$ ecctl deployment create -f deployment_example.json --name adeploy --version=7.4.1
118+
The deployment creation returned with an error, please use the displayed idempotency token
119+
to recreate the deployment resources
120+
Idempotency token: GMZPMRrcMYqHdmxjIQkHbdjnhPIeBElcwrHwzVlhGUSMXrEIzVXoBykSVRsKncNb
121+
unknown error (status 500)
122+
$ ecctl deployment create -f deployment_example.json --name adeploy --version=7.4.1 --request-id=GMZPMRrcMYqHdmxjIQkHbdjnhPIeBElcwrHwzVlhGUSMXrEIzVXoBykSVRsKncNb
123+
[...]`[1:]
124+
125+
var createCmd = &cobra.Command{
126+
Use: `create -f <file definition.json>`,
127+
Short: "Creates a deployment from a file definition, allowing certain flag overrides",
128+
Long: createLong,
129+
Example: createExample,
130+
PreRunE: cobra.NoArgs,
131+
RunE: func(cmd *cobra.Command, args []string) error {
132+
filename, _ := cmd.Flags().GetString("file")
133+
var r models.DeploymentCreateRequest
134+
if err := cmdutil.DecodeFile(filename, &r); err != nil {
135+
return err
136+
}
137+
138+
var region string
139+
if ecctl.Get().Config.Region == "" {
140+
region = cmdutil.DefaultECERegion
141+
}
142+
143+
name, _ := cmd.Flags().GetString("name")
144+
version, _ := cmd.Flags().GetString("version")
145+
146+
reqID, _ := cmd.Flags().GetString("request-id")
147+
if reqID == "" {
148+
reqID = util.RandomString(64)
149+
}
150+
151+
res, err := deployment.Create(deployment.CreateParams{
152+
API: ecctl.Get().API,
153+
RequestID: reqID,
154+
Request: &r,
155+
Overrides: &deployment.CreateOverrides{
156+
Name: name,
157+
Region: region,
158+
Version: version,
159+
},
160+
})
161+
162+
if err != nil {
163+
fmt.Fprintln(os.Stderr,
164+
"The deployment creation returned with an error, please use the displayed idempotency token to recreate the deployment resources",
165+
)
166+
fmt.Fprintln(os.Stderr, "Idempotency token:", reqID)
167+
return err
168+
}
169+
170+
return ecctl.Get().Formatter.Format("", res)
171+
},
172+
}
173+
174+
func init() {
175+
Command.AddCommand(createCmd)
176+
createCmd.Flags().String("name", "", "Overrides the deployment name")
177+
createCmd.Flags().String("version", "", "Overrides all thee deployment's resources to the specified version")
178+
createCmd.Flags().String("request-id", "", "Optional idempotency token - Can be found in the Stderr device when a previous deployment creation failed, for more information see the examples in the help command page")
179+
createCmd.Flags().StringP("file", "f", "", "JSON file that contains JSON-style domain-specific deployment definition")
180+
createCmd.MarkFlagRequired("file")
181+
createCmd.MarkFlagFilename("file", "*.json")
182+
}

cmd/deployment/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
var deleteCmd = &cobra.Command{
2929
Use: "delete <deployment-id>",
30-
Short: "deletes a previously stopped deployment from the platform",
30+
Short: "Deletes a previously stopped deployment from the platform",
3131
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
3232
RunE: func(cmd *cobra.Command, args []string) error {
3333
res, err := deployment.Delete(deployment.DeleteParams{

cmd/util/region.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 cmdutil
19+
20+
// DefaultECERegion is the region for ECE
21+
const DefaultECERegion = "ece-region"

docs/ecctl_deployment.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ ecctl deployment [flags]
4141

4242
* [ecctl](ecctl.md) - Elastic Cloud Control
4343
* [ecctl deployment apm](ecctl_deployment_apm.md) - Manages APM deployments
44-
* [ecctl deployment delete](ecctl_deployment_delete.md) - deletes a previously stopped deployment from the platform
44+
* [ecctl deployment create](ecctl_deployment_create.md) - Creates a deployment from a file definition, allowing certain flag overrides
45+
* [ecctl deployment delete](ecctl_deployment_delete.md) - Deletes a previously stopped deployment from the platform
4546
* [ecctl deployment elasticsearch](ecctl_deployment_elasticsearch.md) - Manages Elasticsearch clusters
4647
* [ecctl deployment kibana](ecctl_deployment_kibana.md) - Manages Kibana clusters
4748
* [ecctl deployment list](ecctl_deployment_list.md) - Lists the platform's deployments

docs/ecctl_deployment_create.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
## ecctl deployment create
2+
3+
Creates a deployment from a file definition, allowing certain flag overrides
4+
5+
### Synopsis
6+
7+
Creates a deployment from a file definition with an automatically generated idempotency token.
8+
On creation failure, please use the displayed idempotency token to retry the cluster creation with --request-id=<token>.
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 create -f <file definition.json> [flags]
14+
```
15+
16+
### Examples
17+
18+
```
19+
$ cat deployment_example.json
20+
{
21+
"name": "my example cluster",
22+
"resources": {
23+
"apm": [
24+
{
25+
"display_name": "my apm instance",
26+
"ref_id": "my-apm-instance",
27+
"elasticsearch_cluster_ref_id": "my-es-cluster",
28+
"plan": {
29+
"apm": {
30+
"version": "6.8.4"
31+
},
32+
"cluster_topology": [{
33+
"instance_configuration_id": "apm",
34+
"size": {
35+
"resource": "memory",
36+
"value": 512
37+
},
38+
"zone_count": 1
39+
}]
40+
}
41+
}
42+
],
43+
"elasticsearch": [
44+
{
45+
"display_name": "my elasticsearch cluster",
46+
"ref_id": "my-es-cluster",
47+
"plan": {
48+
"deployment_template": {
49+
"id": "default"
50+
},
51+
"elasticsearch": {
52+
"version": "6.8.4"
53+
},
54+
"cluster_topology": [
55+
{
56+
"instance_configuration_id": "data.default",
57+
"memory_per_node": 1024,
58+
"node_count_per_zone": 1,
59+
"node_type": {
60+
"data": true,
61+
"ingest": true,
62+
"master": true,
63+
"ml": false
64+
},
65+
"zone_count": 1
66+
}
67+
]
68+
}
69+
}
70+
],
71+
"kibana": [
72+
{
73+
"display_name": "my kibana instance",
74+
"ref_id": "my-kibana-instance",
75+
"elasticsearch_cluster_ref_id": "my-es-cluster",
76+
"plan": {
77+
"zone_count": 1,
78+
"kibana": {
79+
"version": "6.8.4"
80+
},
81+
"cluster_topology": [
82+
{
83+
"instance_configuration_id": "kibana",
84+
"memory_per_node": 1024,
85+
"node_count_per_zone": 1
86+
}
87+
]
88+
}
89+
}
90+
]
91+
}
92+
}
93+
$ ecctl deployment create -f deployment_example.json --version=7.4.1
94+
[...]
95+
96+
## If th previous deployment creation failed
97+
$ ecctl deployment create -f deployment_example.json --name adeploy --version=7.4.1
98+
The deployment creation returned with an error, please use the displayed idempotency token
99+
to recreate the deployment resources
100+
Idempotency token: GMZPMRrcMYqHdmxjIQkHbdjnhPIeBElcwrHwzVlhGUSMXrEIzVXoBykSVRsKncNb
101+
unknown error (status 500)
102+
$ ecctl deployment create -f deployment_example.json --name adeploy --version=7.4.1 --request-id=GMZPMRrcMYqHdmxjIQkHbdjnhPIeBElcwrHwzVlhGUSMXrEIzVXoBykSVRsKncNb
103+
[...]
104+
```
105+
106+
### Options
107+
108+
```
109+
-f, --file string JSON file that contains JSON-style domain-specific deployment definition
110+
-h, --help help for create
111+
--name string Overrides the deployment name
112+
--request-id string Optional idempotency token - Can be found in the Stderr device when a previous deployment creation failed, for more information see the examples in the help command page
113+
--version string Overrides all thee deployment's resources to the specified version
114+
```
115+
116+
### Options inherited from parent commands
117+
118+
```
119+
--apikey string API key to use to authenticate (If empty will look for EC_APIKEY environment variable)
120+
--config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config")
121+
--force Do not ask for confirmation
122+
--format string Formats the output using a Go template
123+
--host string Base URL to use (default "https://api.elastic-cloud.com")
124+
--insecure Skips all TLS validation
125+
--message string A message to set on cluster operation
126+
--output string Output format [text|json] (default "text")
127+
--pass string Password to use to authenticate (If empty will look for EC_PASS environment variable)
128+
--pprof Enables pprofing and saves the profile to pprof-20060102150405
129+
-q, --quiet Suppresses the configuration file used for the run, if any
130+
--region string Elastic Cloud region
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+

docs/ecctl_deployment_delete.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## ecctl deployment delete
22

3-
deletes a previously stopped deployment from the platform
3+
Deletes a previously stopped deployment from the platform
44

55
### Synopsis
66

7-
deletes a previously stopped deployment from the platform
7+
Deletes a previously stopped deployment from the platform
88

99
```
1010
ecctl deployment delete <deployment-id> [flags]

0 commit comments

Comments
 (0)