Skip to content

Commit 3fe4656

Browse files
authored
cmd: add --all flag to kibana resync command (#124)
Implements a new `--all` flag for kibana resync
1 parent 3e2c7d1 commit 3fe4656

File tree

5 files changed

+123
-7
lines changed

5 files changed

+123
-7
lines changed

cmd/deployment/kibana/resync.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,32 @@ import (
2828
)
2929

3030
var resyncKibanaCmd = &cobra.Command{
31-
Use: "resync <cluster id>",
32-
Short: "Resynchronizes the search index and cache for the selected Kibana instance",
33-
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
31+
Use: "resync {<deployment id> | --all}",
32+
Short: "Resynchronizes the search index and cache for the selected Kibana instance or all instances",
33+
PreRunE: cmdutil.CheckInputHas1ArgsOr0ArgAndAll,
3434
RunE: func(cmd *cobra.Command, args []string) error {
35+
all, _ := cmd.Flags().GetBool("all")
36+
37+
if all {
38+
fmt.Println("Resynchronizing all Kibana instances")
39+
res, err := kibana.ResyncAll(kibana.ResyncAllParams{
40+
API: ecctl.Get().API,
41+
})
42+
if err != nil {
43+
return err
44+
}
45+
46+
return ecctl.Get().Formatter.Format("", res)
47+
}
48+
3549
fmt.Printf("Resynchronizing Kibana instance: %s\n", args[0])
3650
return kibana.Resync(kibana.DeploymentParams{
3751
API: ecctl.Get().API,
3852
ID: args[0],
3953
})
4054
},
4155
}
56+
57+
func init() {
58+
resyncKibanaCmd.Flags().Bool("all", false, "Resynchronizes the search index for all Kibana instances")
59+
}

docs/ecctl_deployment_kibana.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ecctl deployment kibana [flags]
4545
* [ecctl deployment kibana plan](ecctl_deployment_kibana_plan.md) - Manages Kibana plans
4646
* [ecctl deployment kibana reallocate](ecctl_deployment_kibana_reallocate.md) - Reallocates Kibana instances
4747
* [ecctl deployment kibana restart](ecctl_deployment_kibana_restart.md) - Restarts a Kibana instance
48-
* [ecctl deployment kibana resync](ecctl_deployment_kibana_resync.md) - Resynchronizes the search index and cache for the selected Kibana instance
48+
* [ecctl deployment kibana resync](ecctl_deployment_kibana_resync.md) - Resynchronizes the search index and cache for the selected Kibana instance or all instances
4949
* [ecctl deployment kibana show](ecctl_deployment_kibana_show.md) - Returns the cluster information
5050
* [ecctl deployment kibana start](ecctl_deployment_kibana_start.md) - Starts a Kibana instance
5151
* [ecctl deployment kibana stop](ecctl_deployment_kibana_stop.md) - Downscales a Kibana instance

docs/ecctl_deployment_kibana_resync.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
## ecctl deployment kibana resync
22

3-
Resynchronizes the search index and cache for the selected Kibana instance
3+
Resynchronizes the search index and cache for the selected Kibana instance or all instances
44

55
### Synopsis
66

7-
Resynchronizes the search index and cache for the selected Kibana instance
7+
Resynchronizes the search index and cache for the selected Kibana instance or all instances
88

99
```
10-
ecctl deployment kibana resync <cluster id> [flags]
10+
ecctl deployment kibana resync {<deployment id> | --all} [flags]
1111
```
1212

1313
### Options
1414

1515
```
16+
--all Resynchronizes the search index for all Kibana instances
1617
-h, --help help for resync
1718
```
1819

pkg/deployment/kibana/resync.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,26 @@
1818
package kibana
1919

2020
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api"
2122
"github.com/elastic/cloud-sdk-go/pkg/client/clusters_kibana"
23+
"github.com/elastic/cloud-sdk-go/pkg/models"
2224

2325
"github.com/elastic/ecctl/pkg/util"
2426
)
2527

28+
// ResyncAllParams is consumed by ResyncAll
29+
type ResyncAllParams struct {
30+
*api.API
31+
}
32+
33+
// Validate ensures the parameters are usable by the consuming function.
34+
func (params ResyncAllParams) Validate() error {
35+
if params.API == nil {
36+
return util.ErrAPIReq
37+
}
38+
return nil
39+
}
40+
2641
// Resync forces indexer to immediately resynchronize the search index
2742
// and cache for a given Kibana instance.
2843
func Resync(params DeploymentParams) error {
@@ -38,3 +53,20 @@ func Resync(params DeploymentParams) error {
3853
),
3954
)
4055
}
56+
57+
// ResyncAll asynchronously resynchronizes the search index for all Kibana instances.
58+
func ResyncAll(params ResyncAllParams) (*models.ModelVersionIndexSynchronizationResults, error) {
59+
if err := params.Validate(); err != nil {
60+
return nil, err
61+
}
62+
63+
res, err := params.API.V1API.ClustersKibana.ResyncKibanaClusters(
64+
clusters_kibana.NewResyncKibanaClustersParams(),
65+
params.API.AuthWriter,
66+
)
67+
if err != nil {
68+
return nil, api.UnwrapError(err)
69+
}
70+
71+
return res.Payload, nil
72+
}

pkg/deployment/kibana/resync_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/elastic/cloud-sdk-go/pkg/api"
2828
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
29+
"github.com/elastic/cloud-sdk-go/pkg/models"
2930
multierror "github.com/hashicorp/go-multierror"
3031

3132
"github.com/elastic/ecctl/pkg/util"
@@ -102,3 +103,67 @@ func TestResync(t *testing.T) {
102103
})
103104
}
104105
}
106+
107+
func TestResyncAll(t *testing.T) {
108+
type args struct {
109+
params ResyncAllParams
110+
}
111+
tests := []struct {
112+
name string
113+
args args
114+
wantErr error
115+
want *models.ModelVersionIndexSynchronizationResults
116+
}{
117+
{
118+
name: "Fails due to parameter validation (API)",
119+
args: args{params: ResyncAllParams{}},
120+
wantErr: errors.New("api reference is required for command"),
121+
},
122+
{
123+
name: "Fails due to unknown API response",
124+
args: args{params: ResyncAllParams{
125+
API: api.NewMock(mock.Response{Response: http.Response{
126+
StatusCode: http.StatusForbidden,
127+
Body: mock.NewStringBody(`{"error": "some forbidden error"}`),
128+
}}),
129+
}},
130+
wantErr: errors.New(`{"error": "some forbidden error"}`),
131+
},
132+
{
133+
name: "Fails due to API error",
134+
args: args{params: ResyncAllParams{
135+
API: api.NewMock(mock.Response{
136+
Error: errors.New("error with API"),
137+
}),
138+
}},
139+
wantErr: &url.Error{
140+
Op: "Post",
141+
URL: "https://mock-host/mock-path/clusters/kibana/_resync?skip_matching_version=true",
142+
Err: errors.New("error with API"),
143+
},
144+
},
145+
{
146+
name: "Succeeds to re-synchronize all Kibana instances without errors",
147+
args: args{params: ResyncAllParams{
148+
API: api.NewMock(mock.Response{Response: http.Response{
149+
StatusCode: http.StatusAccepted,
150+
Body: mock.NewStringBody(`{}`),
151+
}}),
152+
}},
153+
want: &models.ModelVersionIndexSynchronizationResults{},
154+
},
155+
}
156+
157+
for _, tt := range tests {
158+
t.Run(tt.name, func(t *testing.T) {
159+
got, err := ResyncAll(tt.args.params)
160+
if !reflect.DeepEqual(tt.wantErr, err) {
161+
t.Errorf("ResyncAll() error = %v, wantErr %v", err, tt.wantErr)
162+
return
163+
}
164+
if !reflect.DeepEqual(got, tt.want) {
165+
t.Errorf("ResyncAll() = %v, want %v", got, tt.want)
166+
}
167+
})
168+
}
169+
}

0 commit comments

Comments
 (0)