Skip to content

Commit aaa5d87

Browse files
authored
cmd: Migrate kibana create to deployments API (#71)
This change mainly moves the kibana create action to use the deployment API instead of creating the cluster through the cluster APIs. It makes use of the deployment update API to create the kibana resource within a deployment which should already exist and have an elasticsearch resource. It only provides simple flags and a flat topology with a specified size for the node memory and the number of zones the deployment resource will span. Furthermore, it modifies the depresource.Track parameters to accept an Orphan key which now allows the tracking of both resources which will be created / exist and the ones that will be shut down (Orphans). Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent 88c7938 commit aaa5d87

18 files changed

+808
-651
lines changed

cmd/deployment/elasticsearch/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var createElasticsearchCmd = &cobra.Command{
4242
var size, _ = cmd.Flags().GetInt32("size")
4343
var plugin, _ = cmd.Flags().GetStringSlice("plugin")
4444
var name, _ = cmd.Flags().GetString("name")
45-
var RefID, _ = cmd.Flags().GetString("ref-id")
45+
var refID, _ = cmd.Flags().GetString("ref-id")
4646
var version, _ = cmd.Flags().GetString("version")
4747
var dt, _ = cmd.Flags().GetString("deployment-template")
4848
var te, _ = cmd.Flags().GetStringArray("topology-element")
@@ -62,7 +62,7 @@ var createElasticsearchCmd = &cobra.Command{
6262
payload, err := depresource.ParseElasticsearchInput(depresource.ParseElasticsearchInputParams{
6363
NewElasticsearchParams: depresource.NewElasticsearchParams{
6464
API: ecctl.Get().API,
65-
RefID: RefID,
65+
RefID: refID,
6666
Version: version,
6767
Plugins: plugin,
6868
Region: region,

cmd/deployment/kibana/command.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ var Command = &cobra.Command{
3333

3434
func init() {
3535
Command.AddCommand(
36-
enableKibanaCmd,
3736
kibanaListCmd,
3837
showKibanaClusterCmd,
3938
createKibanaCmd,

cmd/deployment/kibana/create.go

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,84 +18,122 @@
1818
package cmdkibana
1919

2020
import (
21-
"encoding/json"
22-
"os"
23-
"path/filepath"
21+
"fmt"
2422

25-
"github.com/elastic/cloud-sdk-go/pkg/input"
2623
"github.com/elastic/cloud-sdk-go/pkg/models"
24+
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
2725
"github.com/spf13/cobra"
2826

2927
cmdutil "github.com/elastic/ecctl/cmd/util"
30-
"github.com/elastic/ecctl/pkg/deployment/kibana"
28+
"github.com/elastic/ecctl/pkg/deployment"
29+
"github.com/elastic/ecctl/pkg/deployment/depresource"
3130
"github.com/elastic/ecctl/pkg/ecctl"
32-
"github.com/elastic/ecctl/pkg/util"
3331
)
3432

3533
var createKibanaCmd = &cobra.Command{
36-
Use: "create -f <definition.json>",
37-
Short: "Creates a Kibana instance",
38-
34+
Use: "create -f <definition.json>",
35+
Short: "Creates a Kibana instance",
36+
Long: kibanaCreateLong,
37+
Example: kibanaCreateExample,
3938
PreRunE: cobra.MaximumNArgs(0),
4039
RunE: func(cmd *cobra.Command, args []string) error {
41-
if err := cmdutil.FileOrStdin(cmd, "file-template"); err != nil {
42-
return err
40+
var track, _ = cmd.Flags().GetBool("track")
41+
var generatePayload, _ = cmd.Flags().GetBool("generate-payload")
42+
var zoneCount, _ = cmd.Flags().GetInt32("zones")
43+
var size, _ = cmd.Flags().GetInt32("size")
44+
var name, _ = cmd.Flags().GetString("name")
45+
var refID, _ = cmd.Flags().GetString("ref-id")
46+
var esRefID, _ = cmd.Flags().GetString("elasticsearch-ref-id")
47+
var id, _ = cmd.Flags().GetString("id")
48+
var version, _ = cmd.Flags().GetString("version")
49+
var dt, _ = cmd.Flags().GetString("deployment-template")
50+
var region string
51+
if ecctl.Get().Config.Region == "" {
52+
region = cmdutil.DefaultECERegion
4353
}
4454

45-
def, err := parseKibanaDefinitionFile(cmd.Flag("file-template").Value.String())
46-
if err != nil {
47-
return err
55+
var payload *models.KibanaPayload
56+
if err := cmdutil.FileOrStdin(cmd, "file"); err == nil {
57+
err := cmdutil.DecodeDefinition(cmd, "file", &payload)
58+
if err != nil && err != cmdutil.ErrNodefinitionLoaded {
59+
return err
60+
}
4861
}
4962

50-
if id := cmd.Flag("id").Value.String(); id != "" {
51-
def.ElasticsearchClusterID = &id
63+
if payload == nil {
64+
p, err := depresource.NewKibana(depresource.NewKibanaParams{
65+
DeploymentID: id,
66+
ElasticsearchRefID: esRefID,
67+
API: ecctl.Get().API,
68+
RefID: refID,
69+
Version: version,
70+
Region: region,
71+
TemplateID: dt,
72+
Size: size,
73+
ZoneCount: zoneCount,
74+
})
75+
if err != nil {
76+
return err
77+
}
78+
payload = p
5279
}
5380

54-
if name := cmd.Flag("name").Value.String(); name != "" {
55-
def.ClusterName = name
81+
// Returns the KibanaPayload skipping the creation of the resources.
82+
if generatePayload {
83+
return ecctl.Get().Formatter.Format("", payload)
5684
}
5785

58-
track, _ := cmd.Flags().GetBool("track")
59-
r, err := kibana.Create(kibana.CreateParams{
60-
CreateKibanaRequest: &def,
61-
DeploymentParams: kibana.DeploymentParams{
62-
API: ecctl.Get().API,
63-
ID: cmd.Flag("id").Value.String(),
64-
TrackParams: util.TrackParams{
65-
Track: track,
66-
Output: ecctl.Get().Config.OutputDevice,
86+
var updateParams = deployment.UpdateParams{
87+
DeploymentID: id,
88+
API: ecctl.Get().API,
89+
Request: &models.DeploymentUpdateRequest{
90+
// Setting PruneOrphans to false since we don't want any side
91+
// effects on the deployment when only a partial deployment
92+
// definition is sent.
93+
PruneOrphans: ec.Bool(false),
94+
Name: name,
95+
Resources: &models.DeploymentUpdateResources{
96+
Kibana: []*models.KibanaPayload{payload},
6797
},
6898
},
69-
})
99+
}
100+
101+
res, err := deployment.Update(updateParams)
70102
if err != nil {
71103
return err
72104
}
73-
return ecctl.Get().Formatter.Format(
74-
filepath.Join("kibana", "create"),
75-
r,
76-
)
77-
},
78-
}
79105

80-
func parseKibanaDefinitionFile(fp string) (models.CreateKibanaRequest, error) {
81-
var kinabaDef models.CreateKibanaRequest
82-
83-
defFile, err := input.NewFileOrReader(os.Stdin, fp)
84-
if err != nil {
85-
return kinabaDef, err
86-
}
87-
defer defFile.Close()
106+
if err := ecctl.Get().Formatter.Format("", res); err != nil {
107+
if !track {
108+
return err
109+
}
110+
fmt.Fprintln(ecctl.Get().Config.OutputDevice, err)
111+
}
88112

89-
if err := json.NewDecoder(defFile).Decode(&kinabaDef); err != nil {
90-
return kinabaDef, err
91-
}
113+
if !track {
114+
return nil
115+
}
92116

93-
return kinabaDef, nil
117+
return depresource.TrackResources(depresource.TrackResourcesParams{
118+
API: ecctl.Get().API,
119+
Resources: res.Resources,
120+
Orphaned: res.ShutdownResources,
121+
OutputDevice: ecctl.Get().Config.OutputDevice,
122+
})
123+
},
94124
}
95125

96126
func init() {
97127
createKibanaCmd.Flags().BoolP("track", "t", false, cmdutil.TrackFlagMessage)
98-
createKibanaCmd.Flags().StringP("file-template", "f", "", "JSON file that contains the Kibana instance definition")
99-
createKibanaCmd.Flags().String("id", "", "Optional ID to set for the Elasticsearch cluster (Overrides ID if present).")
100-
createKibanaCmd.Flags().String("name", "", "Optional name to set for the Kibana instance (Overrides name if present).")
128+
createKibanaCmd.Flags().StringP("file", "f", "", "KibanaPayload file definition. See help for more information")
129+
createKibanaCmd.Flags().String("deployment-template", "", "Optional deployment template ID, automatically obtained from the current deployment")
130+
createKibanaCmd.Flags().String("version", "", "Version to use, if not specified, the deployment's stack version will be used")
131+
createKibanaCmd.Flags().String("ref-id", "kibana", "RefId for the Kibana deployment")
132+
createKibanaCmd.Flags().String("id", "", "Deployment ID where to create the Kibana deployment")
133+
createKibanaCmd.MarkFlagRequired("id")
134+
createKibanaCmd.Flags().String("elasticsearch-ref-id", "", "Optional Elasticsearch ref ID where the Kibana deployment will connect to")
135+
createKibanaCmd.Flags().String("name", "", "Optional name to set for the Kibana deployment (Overrides name if present)")
136+
createKibanaCmd.Flags().Int32("zones", 1, "Number of zones the deployment will span")
137+
createKibanaCmd.Flags().Int32("size", 1024, "Memory (RAM) in MB that each of the deployment nodes will have")
138+
createKibanaCmd.Flags().Bool("generate-payload", false, "Returns the KibanaPayload without actually creating the deployment resources")
101139
}

cmd/deployment/kibana/create_help.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 cmdkibana
19+
20+
const (
21+
kibanaCreateLong = `Creates a Kibana deployment, limitting the creation scope to Kibana resources.
22+
There's a few ways to create an Kibana deployment, sane default values are provided, making
23+
the command work out of the box even when no parameters are set. When version is not specified,
24+
the matching elasticsearch deployment version will be used. These are the available options:
25+
26+
* Simplified flags: --zones <zone count> --size <node memory in MB>
27+
* File definition: --file=<file path> (shorthand: -f). The definition can be found in:
28+
https://www.elastic.co/guide/en/cloud-enterprise/current/definitions.html#KibanaPayload
29+
30+
As an option "--generate-payload" can be used in order to obtain the generated KibanaPayload
31+
that would be sent as a request, save it, update or extend the topology and create an Kibana
32+
deployment using the saved payload with the "--file" flag.`
33+
34+
kibanaCreateExample = `## Create a single node cluster. The command will exit after the API response has been returned,
35+
## without waiting until the deployment resources have been created. To make the command wait until
36+
the resources have been created use the "--track" flag.
37+
$ ecctl deployment kibana create --id=fc2fe19a8c4f43d385004febe4e63900 --track
38+
{
39+
"id": "fc2fe19a8c4f43d385004febe4e63900",
40+
"name": "fc2fe19a8c4f43d385004febe4e63900",
41+
"resources": [
42+
{
43+
"elasticsearch_cluster_ref_id": "elasticsearch",
44+
"id": "8d37cff942fb482ba7a3cac10eea943a",
45+
"kind": "kibana",
46+
"ref_id": "kibana",
47+
"region": "ece-region"
48+
},
49+
{
50+
"cloud_id": "fc2fe19a8c4f43d385004febe4e63900:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDJjOGRkMWQxYmExNDQyZGU4NDU2NmY0ZTkxY2M1YWJmJDhkMzdjZmY5NDJmYjQ4MmJhN2EzY2FjMTBlZWE5NDNh",
51+
"id": "2c8dd1d1ba1442de84566f4e91cc5abf",
52+
"kind": "elasticsearch",
53+
"ref_id": "elasticsearch",
54+
"region": "ece-region"
55+
}
56+
]
57+
}
58+
Cluster [8d37cff942fb482ba7a3cac10eea943a][Kibana]: running step "wait-until-running" (Plan duration 1.480086699s)...
59+
Cluster [2c8dd1d1ba1442de84566f4e91cc5abf][Elasticsearch]: finished running all the plan steps (Total plan duration: 1.598400189s)
60+
Cluster [8d37cff942fb482ba7a3cac10eea943a][Kibana]: running step "set-maintenance" (Plan duration 1m2.277750264s)...
61+
Cluster [8d37cff942fb482ba7a3cac10eea943a][Kibana]: finished running all the plan steps (Total plan duration: 1m7.544473245s)
62+
63+
## Save the definition to a file for later use.
64+
$ ecctl deployment kibana create --generate-payload --id fc2fe19a8c4f43d385004febe4e63900 --zones 2 --size 2048 > kibana_create_example.json
65+
66+
## Create the deployment piping through the file contents tracking the creation progress
67+
$ cat kibana_create_example.json | dev-cli deployment kibana create --track --id fc2fe19a8c4f43d385004febe4e63900
68+
[...]`
69+
)

cmd/deployment/kibana/enable.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

docs/ecctl_deployment_kibana.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ ecctl deployment kibana [flags]
4141
* [ecctl deployment](ecctl_deployment.md) - Manages deployments
4242
* [ecctl deployment kibana create](ecctl_deployment_kibana_create.md) - Creates a Kibana instance
4343
* [ecctl deployment kibana delete](ecctl_deployment_kibana_delete.md) - Deletes a Kibana instance
44-
* [ecctl deployment kibana enable](ecctl_deployment_kibana_enable.md) - Enables a kibana instance in the selected deployment
4544
* [ecctl deployment kibana list](ecctl_deployment_kibana_list.md) - Returns the list of kibana instances
4645
* [ecctl deployment kibana plan](ecctl_deployment_kibana_plan.md) - Manages Kibana plans
4746
* [ecctl deployment kibana reallocate](ecctl_deployment_kibana_reallocate.md) - Reallocates Kibana instances

0 commit comments

Comments
 (0)