Skip to content

Commit

Permalink
edit service can update parameters that weren't originally defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
renan-campos committed Jun 29, 2022
1 parent e0bb518 commit d7e73ea
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 25 deletions.
65 changes: 45 additions & 20 deletions cmd/edit/service/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ limitations under the License.
package service

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/rosa/pkg/arguments"
"github.com/openshift/rosa/pkg/ocm"
"github.com/openshift/rosa/pkg/rosa"
Expand All @@ -39,17 +40,6 @@ var Cmd = &cobra.Command{
Run: run,
Hidden: true,
DisableFlagParsing: true,
Args: func(cmd *cobra.Command, argv []string) error {
err := arguments.ParseUnknownFlags(cmd, argv)
if err != nil {
return err
}

if len(cmd.Flags().Args()) > 0 {
return fmt.Errorf("Unrecognized command line parameter")
}
return nil
},
}

func init() {
Expand All @@ -64,37 +54,72 @@ func init() {
}

func run(cmd *cobra.Command, argv []string) {
r := rosa.NewRuntime().WithOCM()
r := rosa.NewRuntime().WithOCM().WithFlagChecker()
defer r.Cleanup()

// Adding known flags to flag checker before parsing the unknown flags
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
r.FlagChecker.AddValidFlag(flag)
})

err := arguments.ParseUnknownFlags(cmd, argv)
if err != nil {
r.Reporter.Errorf("Failed to parse flags: %v", err)
os.Exit(1)
}

if len(cmd.Flags().Args()) > 0 {
r.Reporter.Errorf("Unrecognized command line parameter")
os.Exit(1)
}

if args.ID == "" {
r.Reporter.Errorf("Service id not specified.")
cmd.Help()
os.Exit(1)
}

// Try to find the cluster:
// Try to find the service:
r.Reporter.Debugf("Loading service %q", args.ID)
service, err := r.OCMClient.GetManagedService(ocm.DescribeManagedServiceArgs{ID: args.ID})
if err != nil {
r.Reporter.Errorf("Failed to get service %q: %v", args.ID, err)
os.Exit(1)
}

parameters := service.Parameters()

if len(parameters) == 0 {
r.Reporter.Errorf("Service %q has no parameters to edit", args.ID)
// Setting parameter flags as valid
addOn, err := r.OCMClient.GetAddOn(service.Service())
if err != nil {
r.Reporter.Errorf("Failed to get add-on %q: %s", service.Service(), err)
os.Exit(1)
}

addonParameters := addOn.Parameters()
addonParameters.Each(func(param *cmv1.AddOnParameter) bool {
r.FlagChecker.AddValidParameter(param.ID())
return true
})

// Now that rosa knows the expected fields to validate,
// Validate that all of the user-specified flags are valid.
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
if !r.FlagChecker.IsValidFlag(flag) {
r.Reporter.Errorf("%q is not a valid flag", flag.Name)
os.Exit(1)
}
})

args.Parameters = map[string]string{}
for _, param := range parameters {
addonParameters.Each(func(param *cmv1.AddOnParameter) bool {
flag := cmd.Flags().Lookup(param.ID())
if flag != nil {
if !param.Editable() {
r.Reporter.Errorf("Cannot edit the parameter %q", param.ID())
}
args.Parameters[param.ID()] = flag.Value.String()
}
}
return true
})

r.Reporter.Debugf("Updating parameters for service %q", args.ID)
err = r.OCMClient.UpdateManagedService(args)
Expand Down
35 changes: 35 additions & 0 deletions pkg/arguments/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file defines an object to check if Flags are valid.

package arguments

import "github.com/spf13/pflag"

type FlagCheck struct {
validFlags map[string]struct{}
}

func NewFlagCheck() *FlagCheck {
return &FlagCheck{
validFlags: map[string]struct{}{},
}
}

func (f *FlagCheck) AddValidFlag(flag *pflag.Flag) *FlagCheck {
f.validFlags[flag.Name] = struct{}{}
return f
}

func (f *FlagCheck) AddValidParameter(parameterName string) *FlagCheck {
f.validFlags[parameterName] = struct{}{}
return f
}

func (f *FlagCheck) IsValidFlag(flag *pflag.Flag) bool {
_, found := f.validFlags[flag.Name]
return found
}

func (f *FlagCheck) IsValidParameter(parameterName string) bool {
_, found := f.validFlags[parameterName]
return found
}
19 changes: 14 additions & 5 deletions pkg/rosa/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rosa
import (
"os"

"github.com/openshift/rosa/pkg/arguments"
"github.com/openshift/rosa/pkg/aws"
"github.com/openshift/rosa/pkg/logging"
"github.com/openshift/rosa/pkg/ocm"
Expand All @@ -11,11 +12,12 @@ import (
)

type Runtime struct {
Reporter *reporter.Object
Logger *logrus.Logger
OCMClient *ocm.Client
AWSClient aws.Client
Creator *aws.Creator
Reporter *reporter.Object
Logger *logrus.Logger
OCMClient *ocm.Client
AWSClient aws.Client
Creator *aws.Creator
FlagChecker *arguments.FlagCheck
}

func NewRuntime() *Runtime {
Expand Down Expand Up @@ -48,6 +50,13 @@ func (r *Runtime) WithAWS() *Runtime {
return r
}

func (r *Runtime) WithFlagChecker() *Runtime {
if r.FlagChecker == nil {
r.FlagChecker = arguments.NewFlagCheck()
}
return r
}

func (r *Runtime) Cleanup() {
if r.OCMClient != nil {
if err := r.OCMClient.Close(); err != nil {
Expand Down

0 comments on commit d7e73ea

Please sign in to comment.