Skip to content

Commit

Permalink
Merge pull request #160 from igoihman/edit-machinepool
Browse files Browse the repository at this point in the history
Implement edit machine pool command
  • Loading branch information
Irit Goihman authored Oct 13, 2020
2 parents 9f997fc + c34f066 commit f4b27f4
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/ocm/edit/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package edit
import (
"github.com/openshift-online/ocm-cli/cmd/ocm/edit/cluster"
"github.com/openshift-online/ocm-cli/cmd/ocm/edit/ingress"
"github.com/openshift-online/ocm-cli/cmd/ocm/edit/machinepool"
"github.com/spf13/cobra"
)

Expand All @@ -29,4 +30,5 @@ var Cmd = &cobra.Command{
func init() {
Cmd.AddCommand(ingress.Cmd)
Cmd.AddCommand(cluster.Cmd)
Cmd.AddCommand(machinepool.Cmd)
}
114 changes: 114 additions & 0 deletions cmd/ocm/edit/machinepool/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright (c) 2020 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package machinepool

import (
"fmt"

c "github.com/openshift-online/ocm-cli/pkg/cluster"
"github.com/openshift-online/ocm-cli/pkg/ocm"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"
)

var args struct {
clusterKey string
replicas int
}

var Cmd = &cobra.Command{
Use: "machinepool",
Aliases: []string{"machine-pool"},
Short: "Edit a cluster machine pool",
Long: "Edit a machine pool size.",
Example: ` # Update the number of replicas for machine pool with ID 'a1b2'
ocm edit machinepool --replicas=3 --cluster=mycluster a1b2`,
RunE: run,
}

func init() {
flags := Cmd.Flags()

flags.StringVarP(
&args.clusterKey,
"cluster",
"c",
"",
"Name or ID of the cluster to edit the machine pool (required).",
)
//nolint:gosec
Cmd.MarkFlagRequired("cluster")

flags.IntVar(
&args.replicas,
"replicas",
-1,
"Restrict application route to direct, private connectivity.",
)
}

func run(cmd *cobra.Command, argv []string) error {

if len(argv) != 1 {
return fmt.Errorf(
"Expected exactly one command line parameter containing the machine pool ID")
}

machinePoolID := argv[0]

// Check that the cluster key (name, identifier or external identifier) given by the user
// is reasonably safe so that there is no risk of SQL injection:
clusterKey := args.clusterKey
if !c.IsValidClusterKey(clusterKey) {
return fmt.Errorf(
"Cluster name, identifier or external identifier '%s' isn't valid: it "+
"must contain only letters, digits, dashes and underscores",
clusterKey,
)
}

// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
return fmt.Errorf("Failed to create OCM connection: %v", err)
}
defer connection.Close()

// Get the client for the cluster management api
clusterCollection := connection.ClustersMgmt().V1().Clusters()

cluster, err := c.GetCluster(clusterCollection, clusterKey)
if err != nil {
return fmt.Errorf("Failed to get cluster '%s': %v", clusterKey, err)
}
machinePool, err := cmv1.NewMachinePool().ID(machinePoolID).
Replicas(args.replicas).
Build()

if err != nil {
return fmt.Errorf("Failed to create machine pool body for cluster '%s': %v", clusterKey, err)
}

_, err = clusterCollection.
Cluster(cluster.ID()).
MachinePools().
MachinePool(machinePoolID).
Update().
Body(machinePool).
Send()
if err != nil {
return fmt.Errorf("Failed to edit machine pool for cluster '%s': %v", clusterKey, err)
}
return nil
}

0 comments on commit f4b27f4

Please sign in to comment.