diff --git a/cmd/ocm/gcp/create-wif-config.go b/cmd/ocm/gcp/create-wif-config.go index da299de4..c64d5640 100644 --- a/cmd/ocm/gcp/create-wif-config.go +++ b/cmd/ocm/gcp/create-wif-config.go @@ -19,11 +19,12 @@ import ( var ( // CreateWifConfigOpts captures the options that affect creation of the workload identity configuration CreateWifConfigOpts = options{ - Mode: ModeAuto, - Name: "", - Project: "", - RolePrefix: "", - TargetDir: "", + Mode: ModeAuto, + Name: "", + Project: "", + RolePrefix: "", + TargetDir: "", + OpenshiftVersion: "", } ) @@ -75,6 +76,12 @@ wif-config resource within OCM to represent those resources.`, "", targetDirFlagDescription, ) + createWifConfigCmd.PersistentFlags().StringVar( + &CreateWifConfigOpts.OpenshiftVersion, + "version", + "", + versionFlagDescription, + ) return createWifConfigCmd } @@ -86,6 +93,9 @@ func validationForCreateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, arg if err := promptProjectId(); err != nil { return err } + if err := promptVersion(); err != nil { + return err + } if CreateWifConfigOpts.Mode != ModeAuto && CreateWifConfigOpts.Mode != ModeManual { return fmt.Errorf("Invalid mode. Allowed values are %s", Modes) @@ -137,6 +147,23 @@ func promptProjectId() error { return nil } +func promptVersion() error { + const projectIdHelp = "The OCP version to configure the wif-config for." + if CreateWifConfigOpts.OpenshiftVersion == "" { + if CreateWifConfigOpts.Interactive { + prompt := &survey.Input{ + Message: "Openshift version:", + Help: projectIdHelp, + } + return survey.AskOne( + prompt, + &CreateWifConfigOpts.OpenshiftVersion, + ) + } + } + return nil +} + func createWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { ctx := context.Background() log := log.Default() @@ -225,6 +252,11 @@ func createWorkloadIdentityConfiguration( } wifBuilder.Gcp(gcpBuilder) + if CreateWifConfigOpts.OpenshiftVersion != "" { + wifTemplate := versionToTemplateID(CreateWifConfigOpts.OpenshiftVersion) + wifBuilder.WifTemplates(wifTemplate) + } + wifBuilder.DisplayName(displayName) wifConfigInput, err := wifBuilder.Build() if err != nil { diff --git a/cmd/ocm/gcp/flag_descriptions.go b/cmd/ocm/gcp/flag_descriptions.go index c0edca2c..3267aa8b 100644 --- a/cmd/ocm/gcp/flag_descriptions.go +++ b/cmd/ocm/gcp/flag_descriptions.go @@ -9,4 +9,5 @@ manual: Commands necessary to modify GCP resources will be output ` targetDirFlagDescription = `Directory to place generated files (defaults to current directory)` + versionFlagDescription = `Version of OpenShift to configure the WIF resources for` ) diff --git a/cmd/ocm/gcp/gcp.go b/cmd/ocm/gcp/gcp.go index 31954106..eeee91b2 100644 --- a/cmd/ocm/gcp/gcp.go +++ b/cmd/ocm/gcp/gcp.go @@ -8,6 +8,7 @@ type options struct { Interactive bool Mode string Name string + OpenshiftVersion string Project string Region string RolePrefix string diff --git a/cmd/ocm/gcp/helpers.go b/cmd/ocm/gcp/helpers.go index 88330ab0..95b1749d 100644 --- a/cmd/ocm/gcp/helpers.go +++ b/cmd/ocm/gcp/helpers.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/pkg/errors" @@ -82,3 +83,15 @@ func getPathFromFlag(targetDir string) (string, error) { return targetDir, nil } + +// converts openshift version of form X.Y to template ID of form vX.Y +func versionToTemplateID(version string) string { + // Check if version is a semver in the form X.Y + re := regexp.MustCompile(`^\d+\.\d+$`) + if re.MatchString(version) { + return "v" + version + } + + // Otherwise, return the version as is + return version +} diff --git a/cmd/ocm/gcp/update-wif-config.go b/cmd/ocm/gcp/update-wif-config.go index 66db6ca8..b0c2b0bd 100644 --- a/cmd/ocm/gcp/update-wif-config.go +++ b/cmd/ocm/gcp/update-wif-config.go @@ -8,14 +8,16 @@ import ( "github.com/openshift-online/ocm-cli/pkg/gcp" "github.com/openshift-online/ocm-cli/pkg/ocm" + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/pkg/errors" "github.com/spf13/cobra" ) var ( UpdateWifConfigOpts = options{ - Mode: ModeAuto, - TargetDir: "", + Mode: ModeAuto, + TargetDir: "", + OpenshiftVersion: "", } ) @@ -46,6 +48,12 @@ the wif-config metadata and the GCP resources it represents.`, "", targetDirFlagDescription, ) + updateWifConfigCmd.PersistentFlags().StringVar( + &UpdateWifConfigOpts.OpenshiftVersion, + "version", + "", + versionFlagDescription, + ) return updateWifConfigCmd } @@ -85,6 +93,28 @@ func updateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) e return errors.Wrapf(err, "failed to get wif-config") } + // Update the WIF configuration + if UpdateWifConfigOpts.OpenshiftVersion != "" { + wifTemplate := versionToTemplateID(UpdateWifConfigOpts.OpenshiftVersion) + + wifBuilder := cmv1.NewWifConfig() + existingTemplates, _ := wifConfig.GetWifTemplates() + wifBuilder.WifTemplates(append(existingTemplates, wifTemplate)...) + + updatedWifConfig, err := wifBuilder.Build() + if err != nil { + return errors.Wrapf(err, "failed to create wif-config body") + } + log.Println(append(existingTemplates, wifTemplate)) + + resp, err := connection.ClustersMgmt().V1().GCP().WifConfigs(). + WifConfig(wifConfig.ID()).Update().Body(updatedWifConfig).Send() + if err != nil { + return errors.Wrapf(err, "failed to update wif-config") + } + wifConfig = resp.Body() + } + gcpClient, err := gcp.NewGcpClient(ctx) if err != nil { return errors.Wrapf(err, "failed to initiate GCP client") diff --git a/go.mod b/go.mod index 6b07976b..7f94488e 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/nwidger/jsoncolor v0.3.2 github.com/onsi/ginkgo/v2 v2.11.0 github.com/onsi/gomega v1.27.8 - github.com/openshift-online/ocm-sdk-go v0.1.449 + github.com/openshift-online/ocm-sdk-go v0.1.451 github.com/openshift/rosa v1.2.24 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 73061aec..58b7fdf0 100644 --- a/go.sum +++ b/go.sum @@ -361,8 +361,8 @@ github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM= github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc= github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ= -github.com/openshift-online/ocm-sdk-go v0.1.449 h1:hgegxZuVl8bvR8uA4hCj0/GTfyNLHeQTzBlWXACQX7k= -github.com/openshift-online/ocm-sdk-go v0.1.449/go.mod h1:CiAu2jwl3ITKOxkeV0Qnhzv4gs35AmpIzVABQLtcI2Y= +github.com/openshift-online/ocm-sdk-go v0.1.451 h1:lkMUmReNb7fOLZagnCp5Hu4yY7UyJH47nPEKSUEUVFw= +github.com/openshift-online/ocm-sdk-go v0.1.451/go.mod h1:CiAu2jwl3ITKOxkeV0Qnhzv4gs35AmpIzVABQLtcI2Y= github.com/openshift/rosa v1.2.24 h1:vv0yYnWHx6CCPEAau/0rS54P2ksaf+uWXb1TQPWxiYE= github.com/openshift/rosa v1.2.24/go.mod h1:MVXB27O3PF8WoOic23I03mmq6/9kVxpFx6FKyLMCyrQ= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=