Skip to content

Commit a5e41f7

Browse files
authored
cmd: add runner show <runner id> command (#172)
Creates an ecctl runner show <runner-id> command, which shows information about the specified runner.
1 parent cfd6a03 commit a5e41f7

File tree

6 files changed

+268
-0
lines changed

6 files changed

+268
-0
lines changed

cmd/platform/runner/show.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 cmdrunner
19+
20+
import (
21+
"github.com/spf13/cobra"
22+
23+
"github.com/elastic/ecctl/pkg/ecctl"
24+
"github.com/elastic/ecctl/pkg/platform/runner"
25+
)
26+
27+
var showCmd = &cobra.Command{
28+
Use: "show <runner id>",
29+
Short: "Shows information about the specified runner",
30+
PreRunE: cobra.MinimumNArgs(1),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
res, err := runner.Show(runner.ShowParams{
33+
Params: runner.Params{
34+
API: ecctl.Get().API,
35+
},
36+
ID: args[0],
37+
})
38+
if err != nil {
39+
return err
40+
}
41+
42+
return ecctl.Get().Formatter.Format("", res)
43+
},
44+
}
45+
46+
func init() {
47+
Command.AddCommand(showCmd)
48+
}

docs/ecctl_platform_runner.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ ecctl platform runner [flags]
4040

4141
* [ecctl platform](ecctl_platform.md) - Manages the platform
4242
* [ecctl platform runner list](ecctl_platform_runner_list.md) - Lists the existing platform runners
43+
* [ecctl platform runner show](ecctl_platform_runner_show.md) - Shows information about the specified runner
4344

docs/ecctl_platform_runner_show.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## ecctl platform runner show
2+
3+
Shows information about the specified runner
4+
5+
### Synopsis
6+
7+
Shows information about the specified runner
8+
9+
```
10+
ecctl platform runner show <runner id> [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help help for show
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
--apikey string API key to use to authenticate (If empty will look for EC_APIKEY environment variable)
23+
--config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config")
24+
--force Do not ask for confirmation
25+
--format string Formats the output using a Go template
26+
--host string Base URL to use
27+
--insecure Skips all TLS validation
28+
--message string A message to set on cluster operation
29+
--output string Output format [text|json] (default "text")
30+
--pass string Password to use to authenticate (If empty will look for EC_PASS environment variable)
31+
--pprof Enables pprofing and saves the profile to pprof-20060102150405
32+
-q, --quiet Suppresses the configuration file used for the run, if any
33+
--timeout duration Timeout to use on all HTTP calls (default 30s)
34+
--trace Enables tracing saves the trace to trace-20060102150405
35+
--user string Username to use to authenticate (If empty will look for EC_USER environment variable)
36+
--verbose Enable verbose mode
37+
```
38+
39+
### SEE ALSO
40+
41+
* [ecctl platform runner](ecctl_platform_runner.md) - Manages platform runners (for ECE installations only)
42+

pkg/platform/runner/show.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 runner
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api"
22+
"github.com/elastic/cloud-sdk-go/pkg/client/platform_infrastructure"
23+
"github.com/elastic/cloud-sdk-go/pkg/models"
24+
multierror "github.com/hashicorp/go-multierror"
25+
26+
"github.com/elastic/ecctl/pkg/util"
27+
)
28+
29+
// ShowParams is the set of parameters required for
30+
type ShowParams struct {
31+
Params
32+
ID string
33+
}
34+
35+
// Validate checks the parameters
36+
func (params ShowParams) Validate() error {
37+
var merr = new(multierror.Error)
38+
39+
if params.ID == "" {
40+
merr = multierror.Append(merr, util.ErrIDCannotBeEmpty)
41+
}
42+
43+
merr = multierror.Append(merr, params.Params.Validate())
44+
45+
return merr.ErrorOrNil()
46+
}
47+
48+
// Show returns information about a specific runner
49+
func Show(params ShowParams) (*models.RunnerInfo, error) {
50+
if err := params.Validate(); err != nil {
51+
return nil, err
52+
}
53+
54+
res, err := params.API.V1API.PlatformInfrastructure.GetRunner(
55+
platform_infrastructure.NewGetRunnerParams().
56+
WithRunnerID(params.ID),
57+
params.AuthWriter,
58+
)
59+
if err != nil {
60+
return nil, api.UnwrapError(err)
61+
}
62+
63+
return res.Payload, nil
64+
}

pkg/platform/runner/show_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 runner
19+
20+
import (
21+
"errors"
22+
"net/http"
23+
"net/url"
24+
"reflect"
25+
"testing"
26+
27+
"github.com/elastic/cloud-sdk-go/pkg/api"
28+
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
29+
"github.com/elastic/cloud-sdk-go/pkg/models"
30+
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
31+
multierror "github.com/hashicorp/go-multierror"
32+
)
33+
34+
func TestShow(t *testing.T) {
35+
var runnerShow = `
36+
{
37+
"connected": true,
38+
"runner_id": "192.168.44.10"
39+
}`
40+
41+
type args struct {
42+
params ShowParams
43+
}
44+
tests := []struct {
45+
name string
46+
args args
47+
want *models.RunnerInfo
48+
wantErr error
49+
}{
50+
{
51+
name: "Show runner succeeds",
52+
args: args{
53+
params: ShowParams{
54+
ID: "192.168.44.10",
55+
Params: Params{
56+
API: api.NewMock(mock.Response{Response: http.Response{
57+
Body: mock.NewStringBody(runnerShow),
58+
StatusCode: 200,
59+
}}),
60+
},
61+
},
62+
},
63+
want: &models.RunnerInfo{
64+
RunnerID: ec.String("192.168.44.10"),
65+
Connected: ec.Bool(true),
66+
},
67+
},
68+
{
69+
name: "Show runner fails",
70+
args: args{
71+
params: ShowParams{
72+
ID: "192.168.44.10",
73+
Params: Params{
74+
API: api.NewMock(mock.Response{Error: errors.New("error")}),
75+
},
76+
},
77+
},
78+
want: nil,
79+
wantErr: &url.Error{
80+
Op: "Get",
81+
URL: "https://mock-host/mock-path/platform/infrastructure/runners/192.168.44.10",
82+
Err: errors.New("error"),
83+
},
84+
},
85+
{
86+
name: "Show runner fails due to validation",
87+
args: args{
88+
params: ShowParams{},
89+
},
90+
want: nil,
91+
wantErr: &multierror.Error{
92+
Errors: []error{
93+
errors.New("id field cannot be empty"),
94+
errors.New("api reference is required for command"),
95+
},
96+
},
97+
},
98+
}
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
got, err := Show(tt.args.params)
102+
if !reflect.DeepEqual(err, tt.wantErr) {
103+
t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
104+
return
105+
}
106+
if !reflect.DeepEqual(got, tt.want) {
107+
t.Errorf("Show() = %v, want %v", got, tt.want)
108+
}
109+
})
110+
}
111+
}

pkg/util/helper.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ var (
3939
ErrClusterLength = errors.New("cluster id should have a length of 32 characters")
4040
// ErrDeploymentID is the message returned when a provided cluster id is not of the expected length (32 chars)
4141
ErrDeploymentID = errors.New("deployment id should have a length of 32 characters")
42+
// ErrIDCannotBeEmpty is the message returned when an ID field is empty
43+
ErrIDCannotBeEmpty = errors.New("id field cannot be empty")
4244

4345
// SkipMaintenanceHeaders tells the EC proxy layer to still send requests to the
4446
// underlying cluster instances even if they are in maintenance mode

0 commit comments

Comments
 (0)