Skip to content

Commit 36d40be

Browse files
authored
cmd: Fix deployment notes (#114)
Fixes the previously broken deployment note commands. With the new deployment api and the introduction of the "deployment resource" concept, the api calls to /api/v1/deployments/{deployment_id}/notes no longer work in the same manner. Until this api endpoint is updated to work using the deployment resource concept, the only deployment IDs it interacts with are the elasticsearch deployments related to the ES IDs (I tested this thoroughly using cURL directly as well as ecctl). So this means we cannot write notes on kibana, apm or appsearch for the time being. I left comments above all related code. In the UI all notes show up on the deployment's main page as well as the ES page. The UI is using a different method, other than calls to this api endpoint, to leave comments on the deployments. This means that comments made through the UI will not show up on deployment note list.
1 parent 772b255 commit 36d40be

File tree

15 files changed

+741
-464
lines changed

15 files changed

+741
-464
lines changed

cmd/deployment/note/command.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ var Command = &cobra.Command{
3838
}
3939

4040
var deploymentNoteCreateCmd = &cobra.Command{
41-
Use: "create <deployment id> --message <message content> --type [elasticsearch|kibana|apm]",
41+
Use: "create <deployment id> --comment <comment content>",
4242
Aliases: []string{"add"},
4343
Short: "Adds a note to a deployment",
4444
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
4545
RunE: func(cmd *cobra.Command, args []string) error {
46+
comment, _ := cmd.Flags().GetString("comment")
4647
return note.Add(note.AddParams{
47-
API: ecctl.Get().API,
48-
ID: args[0],
49-
Type: cmd.Flag("type").Value.String(),
50-
Message: cmd.Flag("message").Value.String(),
48+
Params: note.Params{
49+
Params: deployment.Params{
50+
API: ecctl.Get().API,
51+
ID: args[0],
52+
}},
53+
Message: comment,
5154
UserID: ecctl.Get().Config.User,
5255
})
5356
},
@@ -58,7 +61,7 @@ var deploymentNoteListCmd = &cobra.Command{
5861
Short: "Lists the deployment notes",
5962
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
6063
RunE: func(cmd *cobra.Command, args []string) error {
61-
res, err := note.List(note.ListParams{
64+
res, err := note.List(note.Params{
6265
Params: deployment.Params{
6366
API: ecctl.Get().API,
6467
ID: args[0],
@@ -73,16 +76,18 @@ var deploymentNoteListCmd = &cobra.Command{
7376
}
7477

7578
var deploymentNoteUpdateCmd = &cobra.Command{
76-
Use: "update <deployment id> --id <note id> --message <message content>",
79+
Use: "update <deployment id> --id <note id> --comment <comment content>",
7780
Short: "Updates the deployment notes",
7881
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
7982
RunE: func(cmd *cobra.Command, args []string) error {
83+
comment, _ := cmd.Flags().GetString("comment")
84+
noteID, _ := cmd.Flags().GetString("id")
8085
return util.ReturnErrOnly(
8186
note.Update(note.UpdateParams{
82-
Message: cmd.Flag("message").Value.String(),
87+
Message: comment,
8388
UserID: ecctl.Get().Config.User,
89+
NoteID: noteID,
8490
Params: note.Params{
85-
NoteID: cmd.Flag("id").Value.String(),
8691
Params: deployment.Params{
8792
API: ecctl.Get().API,
8893
ID: args[0],
@@ -98,9 +103,10 @@ var deploymentNoteShowCmd = &cobra.Command{
98103
Short: "Shows a deployment note",
99104
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
100105
RunE: func(cmd *cobra.Command, args []string) error {
106+
noteID, _ := cmd.Flags().GetString("id")
101107
res, err := note.Get(note.GetParams{
108+
NoteID: noteID,
102109
Params: note.Params{
103-
NoteID: cmd.Flag("id").Value.String(),
104110
Params: deployment.Params{
105111
API: ecctl.Get().API,
106112
ID: args[0],
@@ -123,7 +129,12 @@ func init() {
123129
deploymentNoteShowCmd,
124130
)
125131

126-
deploymentNoteCreateCmd.Flags().String("type", "elasticsearch", "Type of deployment to comment, valid options are: [elasticsearch, kibana, apm]")
132+
deploymentNoteCreateCmd.Flags().String("comment", "", "Content of your deployment note")
133+
deploymentNoteCreateCmd.MarkFlagRequired("comment")
134+
deploymentNoteUpdateCmd.Flags().String("comment", "", "Content of your deployment note")
135+
deploymentNoteUpdateCmd.MarkFlagRequired("comment")
127136
deploymentNoteUpdateCmd.Flags().String("id", "", "Note ID")
137+
deploymentNoteUpdateCmd.MarkFlagRequired("id")
128138
deploymentNoteShowCmd.Flags().String("id", "", "Note ID")
139+
deploymentNoteShowCmd.MarkFlagRequired("id")
129140
}

docs/ecctl_deployment_note_create.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ Adds a note to a deployment
77
Adds a note to a deployment
88

99
```
10-
ecctl deployment note create <deployment id> --message <message content> --type [elasticsearch|kibana|apm] [flags]
10+
ecctl deployment note create <deployment id> --comment <comment content> [flags]
1111
```
1212

1313
### Options
1414

1515
```
16-
-h, --help help for create
17-
--type string Type of deployment to comment, valid options are: [elasticsearch, kibana, apm] (default "elasticsearch")
16+
--comment string Content of your deployment note
17+
-h, --help help for create
1818
```
1919

2020
### Options inherited from parent commands

docs/ecctl_deployment_note_update.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ Updates the deployment notes
77
Updates the deployment notes
88

99
```
10-
ecctl deployment note update <deployment id> --id <note id> --message <message content> [flags]
10+
ecctl deployment note update <deployment id> --id <note id> --comment <comment content> [flags]
1111
```
1212

1313
### Options
1414

1515
```
16-
-h, --help help for update
17-
--id string Note ID
16+
--comment string Content of your deployment note
17+
-h, --help help for update
18+
--id string Note ID
1819
```
1920

2021
### Options inherited from parent commands

pkg/deployment/elasticsearch/reallocate.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/elastic/cloud-sdk-go/pkg/plan"
3030
multierror "github.com/hashicorp/go-multierror"
3131

32+
"github.com/elastic/ecctl/pkg/deployment"
3233
"github.com/elastic/ecctl/pkg/deployment/note"
3334
"github.com/elastic/ecctl/pkg/ecctl"
3435
"github.com/elastic/ecctl/pkg/util"
@@ -88,9 +89,11 @@ func Reallocate(params ReallocateParams) error {
8889
// note to the deployment is not critical and is only a nice to have.
8990
//nolint
9091
note.Add(note.AddParams{
91-
API: params.API,
92-
ID: params.ClusterID,
93-
Type: "elasticsearch",
92+
Params: note.Params{
93+
Params: deployment.Params{
94+
API: params.API,
95+
ID: params.ClusterID,
96+
}},
9497
Message: fmt.Sprintf(reallocateMessage, "elasticsearch", strings.Join(params.Instances, " ")),
9598
UserID: params.User,
9699
Commentator: ecctl.GetOperationInstance(),

pkg/deployment/get.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,14 @@ func GetKibana(params GetParams) (*models.KibanaResourceInfo, error) {
173173
}
174174
return res.Payload, nil
175175
}
176+
177+
// GetElasticsearchID returns the deployment's elasticsearch resource ID
178+
func GetElasticsearchID(params GetParams) (string, error) {
179+
res, err := Get(params)
180+
if err != nil {
181+
return "", err
182+
}
183+
184+
esID := *res.Resources.Elasticsearch[0].ID
185+
return esID, nil
186+
}

pkg/deployment/get_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,77 @@ func TestGetKibana(t *testing.T) {
503503
})
504504
}
505505
}
506+
507+
func TestGetElasticsearchID(t *testing.T) {
508+
const getResponse = `{
509+
"healthy": true,
510+
"id": "e3dac8bf3dc64c528c295a94d0f19a77",
511+
"resources": {
512+
"elasticsearch": [{
513+
"id": "418017cd1c7f402cbb7a981b2004ceeb",
514+
"ref_id": "main-elasticsearch",
515+
"region": "ece-region"
516+
}]
517+
}
518+
}`
519+
520+
const getErrorResponse = `{
521+
"errors": null
522+
}`
523+
524+
type args struct {
525+
params GetParams
526+
}
527+
tests := []struct {
528+
name string
529+
args args
530+
want string
531+
wantErr bool
532+
err error
533+
}{
534+
{
535+
name: "Get fails due to API failure",
536+
args: args{
537+
params: GetParams{
538+
DeploymentID: "e3dac8bf3dc64c528c295a94d0f19a77",
539+
API: api.NewMock(mock.Response{Response: http.Response{
540+
Body: mock.NewStringBody(""),
541+
StatusCode: 500,
542+
}}),
543+
},
544+
},
545+
wantErr: true,
546+
err: errors.New(getErrorResponse),
547+
},
548+
{
549+
name: "Get succeeds",
550+
args: args{
551+
params: GetParams{
552+
DeploymentID: "e3dac8bf3dc64c528c295a94d0f19a77",
553+
API: api.NewMock(mock.Response{Response: http.Response{
554+
Body: mock.NewStringBody(getResponse),
555+
StatusCode: 200,
556+
}}),
557+
},
558+
},
559+
want: "418017cd1c7f402cbb7a981b2004ceeb",
560+
},
561+
}
562+
for _, tt := range tests {
563+
t.Run(tt.name, func(t *testing.T) {
564+
got, err := GetElasticsearchID(tt.args.params)
565+
if (err != nil) != tt.wantErr {
566+
t.Errorf("getElasticsearchID() error = %v, wantErr %v", err, tt.wantErr)
567+
return
568+
}
569+
570+
if tt.wantErr && tt.err != nil && err.Error() != tt.err.Error() {
571+
t.Errorf("getElasticsearchID() actual error = '%v', want error '%v'", err, tt.err)
572+
}
573+
574+
if !tt.wantErr && !reflect.DeepEqual(got, tt.want) {
575+
t.Errorf("getElasticsearchID() = %v, want %v", got, tt.want)
576+
}
577+
})
578+
}
579+
}

pkg/deployment/note/list.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 note
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api"
22+
"github.com/elastic/cloud-sdk-go/pkg/client/deployments"
23+
"github.com/elastic/cloud-sdk-go/pkg/models"
24+
)
25+
26+
// ListParams is used by List
27+
type ListParams struct {
28+
Params
29+
}
30+
31+
// List lists all of the notes for the deployment
32+
func List(params Params) (*models.Notes, error) {
33+
if err := params.Validate(); err != nil {
34+
return nil, err
35+
}
36+
37+
if err := params.fillDefaults(); err != nil {
38+
return nil, err
39+
}
40+
41+
res, err := params.API.V1API.Deployments.GetDeploymentNotes(
42+
deployments.NewGetDeploymentNotesParams().
43+
WithDeploymentID(params.ID),
44+
params.AuthWriter,
45+
)
46+
if err != nil {
47+
return nil, api.UnwrapError(err)
48+
}
49+
50+
return res.Payload, nil
51+
}

0 commit comments

Comments
 (0)