|
| 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 | +} |
0 commit comments