Skip to content

Commit 73c0fac

Browse files
authored
cmd: Add deployment resource upgrade command (#76)
Adds a command which allows upgrading a stateless resource as specified in the help and long description for commands. The supported type is any of the stateless resource types, which for now are apm, appsearch and kibana. Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent d1409c8 commit 73c0fac

File tree

9 files changed

+390
-0
lines changed

9 files changed

+390
-0
lines changed

cmd/deployment/command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
cmdkibana "github.com/elastic/ecctl/cmd/deployment/kibana"
2626
cmddeploymentnote "github.com/elastic/ecctl/cmd/deployment/note"
2727
cmddeploymentplan "github.com/elastic/ecctl/cmd/deployment/plan"
28+
cmddeploymentresource "github.com/elastic/ecctl/cmd/deployment/resource"
2829
)
2930

3031
// Command is the deployment subcommand
@@ -44,5 +45,6 @@ func init() {
4445
cmdkibana.Command,
4546
cmdapm.Command,
4647
cmddeploymentplan.Command,
48+
cmddeploymentresource.Command,
4749
)
4850
}

cmd/deployment/resource/command.go

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

cmd/deployment/resource/upgrade.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 cmddeploymentresource
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/models"
22+
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
23+
"github.com/spf13/cobra"
24+
25+
cmdutil "github.com/elastic/ecctl/cmd/util"
26+
"github.com/elastic/ecctl/pkg/deployment/depresource"
27+
"github.com/elastic/ecctl/pkg/ecctl"
28+
)
29+
30+
// upgradeCmd is the deployment subcommand
31+
var upgradeCmd = &cobra.Command{
32+
Use: "upgrade <deployment id> --type <type> --ref-id <ref-id>",
33+
Short: "Upgrades a deployment resource",
34+
Long: upgradeLong,
35+
PreRunE: cmdutil.MinimumNArgsAndUUID(1),
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
resType, _ := cmd.Flags().GetString("type")
38+
refID, _ := cmd.Flags().GetString("ref-id")
39+
40+
res, err := depresource.UpgradeStateless(depresource.UpgradeStatelessParams{
41+
API: ecctl.Get().API,
42+
DeploymentID: args[0],
43+
Type: resType,
44+
RefID: refID,
45+
})
46+
47+
if err != nil {
48+
return err
49+
}
50+
51+
if track, _ := cmd.Flags().GetBool("track"); !track {
52+
return nil
53+
}
54+
55+
return depresource.TrackResources(depresource.TrackResourcesParams{
56+
API: ecctl.Get().API,
57+
OutputDevice: ecctl.Get().Config.OutputDevice,
58+
Resources: []*models.DeploymentResource{{
59+
ID: ec.String(res.ResourceID),
60+
Kind: ec.String(resType),
61+
RefID: ec.String(refID),
62+
}},
63+
})
64+
},
65+
}
66+
67+
func init() {
68+
Command.AddCommand(upgradeCmd)
69+
upgradeCmd.Flags().BoolP("track", "t", false, cmdutil.TrackFlagMessage)
70+
upgradeCmd.Flags().String("type", "", "Optional stateless deployment type to upgrade (kibana, apm, or appsearch)")
71+
upgradeCmd.MarkFlagRequired("type")
72+
upgradeCmd.Flags().String("ref-id", "", "Optional deployment RefId, if not set, the RefId will be auto-discovered")
73+
upgradeCmd.MarkFlagRequired("ref-id")
74+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 cmddeploymentresource
19+
20+
const (
21+
upgradeLong = `Upgrades a stateless deployment resource so it matches the Elasticsearch
22+
deployment version. Only stateless resources are supported in the --type flag`
23+
)

docs/ecctl_deployment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ecctl deployment [flags]
4747
* [ecctl deployment list](ecctl_deployment_list.md) - Lists the platform's deployments
4848
* [ecctl deployment note](ecctl_deployment_note.md) - Manages a deployment's notes
4949
* [ecctl deployment plan](ecctl_deployment_plan.md) - Manages deployment plans
50+
* [ecctl deployment resource](ecctl_deployment_resource.md) - Manages deployment resources
5051
* [ecctl deployment restore](ecctl_deployment_restore.md) - Restores a previously shut down deployment and all of its associated sub-resources
5152
* [ecctl deployment search](ecctl_deployment_search.md) - Performs advanced deployment search using the Elasticsearch Query DSL
5253
* [ecctl deployment show](ecctl_deployment_show.md) - Shows the specified deployment resources

docs/ecctl_deployment_resource.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## ecctl deployment resource
2+
3+
Manages deployment resources
4+
5+
### Synopsis
6+
7+
Manages deployment resources
8+
9+
```
10+
ecctl deployment resource [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help help for resource
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 deployment](ecctl_deployment.md) - Manages deployments
42+
* [ecctl deployment resource upgrade](ecctl_deployment_resource_upgrade.md) - Upgrades a deployment resource
43+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## ecctl deployment resource upgrade
2+
3+
Upgrades a deployment resource
4+
5+
### Synopsis
6+
7+
Upgrades a stateless deployment resource so it matches the Elasticsearch
8+
deployment version. Only stateless resources are supported in the --type flag
9+
10+
```
11+
ecctl deployment resource upgrade <deployment id> --type <type> --ref-id <ref-id> [flags]
12+
```
13+
14+
### Options
15+
16+
```
17+
-h, --help help for upgrade
18+
--ref-id string Optional deployment RefId, if not set, the RefId will be auto-discovered
19+
-t, --track Tracks the progress of the performed task
20+
--type string Optional stateless deployment type to upgrade (kibana, apm, or appsearch)
21+
```
22+
23+
### Options inherited from parent commands
24+
25+
```
26+
--apikey string API key to use to authenticate (If empty will look for EC_APIKEY environment variable)
27+
--config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config")
28+
--force Do not ask for confirmation
29+
--format string Formats the output using a Go template
30+
--host string Base URL to use
31+
--insecure Skips all TLS validation
32+
--message string A message to set on cluster operation
33+
--output string Output format [text|json] (default "text")
34+
--pass string Password to use to authenticate (If empty will look for EC_PASS environment variable)
35+
--pprof Enables pprofing and saves the profile to pprof-20060102150405
36+
-q, --quiet Suppresses the configuration file used for the run, if any
37+
--timeout duration Timeout to use on all HTTP calls (default 30s)
38+
--trace Enables tracing saves the trace to trace-20060102150405
39+
--user string Username to use to authenticate (If empty will look for EC_USER environment variable)
40+
--verbose Enable verbose mode
41+
```
42+
43+
### SEE ALSO
44+
45+
* [ecctl deployment resource](ecctl_deployment_resource.md) - Manages deployment resources
46+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 depresource
19+
20+
import (
21+
"errors"
22+
23+
"github.com/elastic/cloud-sdk-go/pkg/api"
24+
"github.com/elastic/cloud-sdk-go/pkg/client/deployments"
25+
"github.com/elastic/cloud-sdk-go/pkg/models"
26+
"github.com/hashicorp/go-multierror"
27+
28+
"github.com/elastic/ecctl/pkg/deployment/deputil"
29+
"github.com/elastic/ecctl/pkg/util"
30+
)
31+
32+
// UpgradeStatelessParams is consumed by UpgradeStateless
33+
type UpgradeStatelessParams struct {
34+
*api.API
35+
36+
DeploymentID string
37+
Type string
38+
RefID string
39+
}
40+
41+
// Validate ensures the parameters are usable by the consuming function.
42+
func (params UpgradeStatelessParams) Validate() error {
43+
var merr = new(multierror.Error)
44+
45+
if params.API == nil {
46+
merr = multierror.Append(merr, util.ErrAPIReq)
47+
}
48+
49+
if len(params.DeploymentID) != 32 {
50+
merr = multierror.Append(merr, deputil.NewInvalidDeploymentIDError(params.DeploymentID))
51+
}
52+
53+
if params.Type == "" {
54+
merr = multierror.Append(merr, errors.New("deployment resource type cannot be empty"))
55+
}
56+
57+
return merr.ErrorOrNil()
58+
}
59+
60+
// UpgradeStateless upgrades a stateless deployment resource like APM, Kibana
61+
// and AppSearch.
62+
func UpgradeStateless(params UpgradeStatelessParams) (*models.DeploymentResourceUpgradeResponse, error) {
63+
if err := params.Validate(); err != nil {
64+
return nil, err
65+
}
66+
67+
res, err := params.V1API.Deployments.UpgradeDeploymentStatelessResource(
68+
deployments.NewUpgradeDeploymentStatelessResourceParams().
69+
WithStatelessResourceKind(params.Type).
70+
WithDeploymentID(params.DeploymentID).
71+
WithRefID(params.RefID),
72+
params.AuthWriter,
73+
)
74+
if err != nil {
75+
return nil, api.UnwrapError(err)
76+
}
77+
78+
return res.Payload, nil
79+
}

0 commit comments

Comments
 (0)