Skip to content

Commit f5d478e

Browse files
authored
deployment: Add template v2 and remove platform commands (#343)
Removes the platform `deployment-template` command and subcommands. Adds a new `deployment template` command and all its CRUD subcommands: * `deployment template create` * `deployment template list` * `deployment template show` * `deployment template delete` * `deployment template update` Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent 982db38 commit f5d478e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1788
-1120
lines changed

build/Makefile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ notice:
5555
.PHONY: lint
5656
lint: deps
5757
@ echo "-> Running linters..."
58-
@ $(GOBIN)/golint -set_exit_status ./...
58+
@ $(GOBIN)/golint -set_exit_status cmd/... pkg/ecctl/... pkg/util/...
5959
@ $(GOBIN)/golangci-lint run --deadline=5m
6060
@ $(GOBIN)/go-licenser -d .
6161
@ echo "-> Done."

cmd/deployment/command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
cmddeploymentnote "github.com/elastic/ecctl/cmd/deployment/note"
2424
cmddeploymentplan "github.com/elastic/ecctl/cmd/deployment/plan"
2525
cmddeploymentresource "github.com/elastic/ecctl/cmd/deployment/resource"
26+
cmddeploymenttemplate "github.com/elastic/ecctl/cmd/deployment/template"
2627
)
2728

2829
// Command is the deployment subcommand
@@ -40,5 +41,6 @@ func init() {
4041
cmddeploymentnote.Command,
4142
cmddeploymentplan.Command,
4243
cmddeploymentresource.Command,
44+
cmddeploymenttemplate.Command,
4345
)
4446
}

cmd/deployment/template/command.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 cmddeploymenttemplate
19+
20+
import "github.com/spf13/cobra"
21+
22+
// Command is the deployment subcommand
23+
var Command = &cobra.Command{
24+
Use: "template",
25+
Short: "Interacts with deployment template APIs",
26+
PreRunE: cobra.NoArgs,
27+
Run: func(cmd *cobra.Command, args []string) {
28+
cmd.Help()
29+
},
30+
}

cmd/deployment/template/create.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 cmddeploymenttemplate
19+
20+
import (
21+
"errors"
22+
"fmt"
23+
"io"
24+
25+
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/deptemplateapi"
26+
"github.com/elastic/cloud-sdk-go/pkg/models"
27+
sdkcmdutil "github.com/elastic/cloud-sdk-go/pkg/util/cmdutil"
28+
"github.com/spf13/cobra"
29+
30+
cmdutil "github.com/elastic/ecctl/cmd/util"
31+
"github.com/elastic/ecctl/pkg/ecctl"
32+
)
33+
34+
var (
35+
errReadingDefPrefix = "failed reading deployment template definition"
36+
errReadingDefMessage = "provide a valid deployment template definition using the --file flag"
37+
)
38+
39+
var createCmd = &cobra.Command{
40+
Use: "create --file <definition.json>",
41+
Short: cmdutil.AdminReqDescription("Creates a new deployment template"),
42+
PreRunE: cobra.NoArgs,
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
var req *models.DeploymentTemplateRequestBody
45+
if err := sdkcmdutil.DecodeDefinition(cmd, "file", &req); err != nil {
46+
if errors.Is(err, io.EOF) {
47+
return fmt.Errorf("%s: %s", errReadingDefPrefix, errReadingDefMessage)
48+
}
49+
return fmt.Errorf("%s: %s", errReadingDefPrefix, err.Error())
50+
}
51+
52+
templateID, _ := cmd.Flags().GetString("template-id")
53+
res, err := deptemplateapi.Create(deptemplateapi.CreateParams{
54+
API: ecctl.Get().API,
55+
Region: ecctl.Get().Config.Region,
56+
TemplateID: templateID,
57+
Request: req,
58+
})
59+
if err != nil {
60+
return err
61+
}
62+
63+
return ecctl.Get().Formatter.Format("id", models.IDResponse{ID: &res})
64+
},
65+
}
66+
67+
func init() {
68+
Command.AddCommand(createCmd)
69+
createCmd.Flags().Bool("hide-instance-configurations", false, "Hides instance configurations - only visible when using the JSON output.")
70+
createCmd.Flags().StringP("file", "f", "", "Deployment template definition.")
71+
createCmd.Flags().String("template-id", "", "Optional deployment template ID. Otherwise the deployment template will be created with an auto-generated ID.")
72+
cobra.MarkFlagFilename(createCmd.Flags(), "file", "json")
73+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 cmddeploymenttemplate
19+
20+
import (
21+
"errors"
22+
"io/ioutil"
23+
"net/url"
24+
"testing"
25+
26+
"github.com/elastic/cloud-sdk-go/pkg/api"
27+
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
28+
"github.com/elastic/cloud-sdk-go/pkg/models"
29+
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
30+
31+
"github.com/elastic/ecctl/cmd/util/testutils"
32+
)
33+
34+
func Test_createCmd(t *testing.T) {
35+
createRawResp, err := ioutil.ReadFile("./testdata/create.json")
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
var succeedResp = new(models.DeploymentTemplateRequestBody)
41+
if err := succeedResp.UnmarshalBinary(createRawResp); err != nil {
42+
t.Fatal(err)
43+
}
44+
45+
createBody, err := succeedResp.MarshalBinary()
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
tests := []struct {
51+
name string
52+
args testutils.Args
53+
want testutils.Assertion
54+
}{
55+
{
56+
name: "fails due empty argument",
57+
args: testutils.Args{
58+
Cmd: createCmd,
59+
Args: []string{
60+
"create",
61+
},
62+
Cfg: testutils.MockCfg{Responses: []mock.Response{
63+
mock.SampleInternalError(),
64+
}},
65+
},
66+
want: testutils.Assertion{
67+
Err: errors.New("failed reading deployment template definition: provide a valid deployment template definition using the --file flag"),
68+
},
69+
},
70+
{
71+
name: "fails due to API error",
72+
args: testutils.Args{
73+
Cmd: createCmd,
74+
Args: []string{
75+
"create", "--file=testdata/create.json",
76+
},
77+
Cfg: testutils.MockCfg{Responses: []mock.Response{
78+
mock.SampleInternalError(),
79+
}},
80+
},
81+
want: testutils.Assertion{
82+
Err: mock.MultierrorInternalError,
83+
},
84+
},
85+
{
86+
name: "succeeds with JSON format",
87+
args: testutils.Args{
88+
Cmd: createCmd,
89+
Args: []string{
90+
"create", "--file=testdata/create.json",
91+
},
92+
Cfg: testutils.MockCfg{
93+
OutputFormat: "json",
94+
Responses: []mock.Response{
95+
mock.New201ResponseAssertion(
96+
&mock.RequestAssertion{
97+
Header: api.DefaultWriteMockHeaders,
98+
Method: "POST",
99+
Path: "/api/v1/deployments/templates",
100+
Host: api.DefaultMockHost,
101+
Query: url.Values{
102+
"region": []string{"ece-region"},
103+
},
104+
Body: mock.NewStringBody(string(createBody) + "\n"),
105+
},
106+
mock.NewStructBody(models.IDResponse{
107+
ID: ec.String("some-randomly-generated-id"),
108+
}),
109+
),
110+
},
111+
},
112+
},
113+
want: testutils.Assertion{
114+
Stdout: "{\n \"id\": \"some-randomly-generated-id\"\n}\n",
115+
},
116+
},
117+
{
118+
name: "succeeds with text format",
119+
args: testutils.Args{
120+
Cmd: createCmd,
121+
Args: []string{
122+
"create", "--file=testdata/create.json",
123+
},
124+
Cfg: testutils.MockCfg{
125+
OutputFormat: "text",
126+
Responses: []mock.Response{
127+
mock.New201ResponseAssertion(
128+
&mock.RequestAssertion{
129+
Header: api.DefaultWriteMockHeaders,
130+
Method: "POST",
131+
Path: "/api/v1/deployments/templates",
132+
Host: api.DefaultMockHost,
133+
Query: url.Values{
134+
"region": []string{"ece-region"},
135+
},
136+
Body: mock.NewStringBody(string(createBody) + "\n"),
137+
},
138+
mock.NewStructBody(models.IDResponse{
139+
ID: ec.String("some-randomly-generated-id"),
140+
}),
141+
),
142+
},
143+
},
144+
},
145+
want: testutils.Assertion{
146+
Stdout: "ID\nsome-randomly-generated-id\n",
147+
},
148+
},
149+
}
150+
for _, tt := range tests {
151+
t.Run(tt.name, func(t *testing.T) {
152+
testutils.RunCmdAssertion(t, tt.args, tt.want)
153+
})
154+
}
155+
}

cmd/platform/deployment-template/delete.go renamed to cmd/deployment/template/delete.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,32 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package cmddeploymentdemplate
18+
package cmddeploymenttemplate
1919

2020
import (
21-
"fmt"
22-
23-
"github.com/elastic/cloud-sdk-go/pkg/api/platformapi/configurationtemplateapi"
21+
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/deptemplateapi"
2422
"github.com/spf13/cobra"
2523

24+
cmdutil "github.com/elastic/ecctl/cmd/util"
2625
"github.com/elastic/ecctl/pkg/ecctl"
2726
)
2827

2928
var deleteCmd = &cobra.Command{
30-
Use: "delete <template id>",
31-
Short: "Deletes a specific platform deployment template",
32-
PreRunE: cobra.ExactArgs(1),
29+
Use: "delete --template-id <template id>",
30+
Short: cmdutil.AdminReqDescription("Deletes an existing deployment template"),
31+
PreRunE: cobra.NoArgs,
3332
RunE: func(cmd *cobra.Command, args []string) error {
34-
err := configurationtemplateapi.DeleteTemplate(configurationtemplateapi.DeleteTemplateParams{
35-
API: ecctl.Get().API,
36-
Region: ecctl.Get().Config.Region,
37-
ID: args[0],
33+
templateID, _ := cmd.Flags().GetString("template-id")
34+
return deptemplateapi.Delete(deptemplateapi.DeleteParams{
35+
API: ecctl.Get().API,
36+
Region: ecctl.Get().Config.Region,
37+
TemplateID: templateID,
3838
})
39-
40-
if err != nil {
41-
return err
42-
}
43-
44-
fmt.Printf("Successfully deleted deployment template %v \n", args[0])
45-
return nil
4639
},
4740
}
4841

4942
func init() {
5043
Command.AddCommand(deleteCmd)
44+
deleteCmd.Flags().String("template-id", "", "Required template ID to update.")
45+
deleteCmd.MarkFlagRequired("template-id")
5146
}

0 commit comments

Comments
 (0)