From f162d8e9c6a8da2249be763ad6af62571e31de1a Mon Sep 17 00:00:00 2001 From: Dharaneeshwaran Ravichandran Date: Tue, 7 Feb 2023 16:59:13 +0530 Subject: [PATCH] Add common flags for capibmadm cli Add validation for output flag --- cmd/capibmadm/clients/powervs/client.go | 5 +-- cmd/capibmadm/cmd/powervs/network/network.go | 3 -- cmd/capibmadm/cmd/powervs/powervs.go | 9 ++++ cmd/capibmadm/cmd/root.go | 3 ++ cmd/capibmadm/cmd/vpc/vpc.go | 39 +++++++++++++++++ cmd/capibmadm/options/options.go | 20 ++++++--- cmd/capibmadm/{utils => printer}/printer.go | 46 +++++++++++++++----- 7 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 cmd/capibmadm/cmd/vpc/vpc.go rename cmd/capibmadm/{utils => printer}/printer.go (70%) diff --git a/cmd/capibmadm/clients/powervs/client.go b/cmd/capibmadm/clients/powervs/client.go index e0d790f16..4393e43cd 100644 --- a/cmd/capibmadm/clients/powervs/client.go +++ b/cmd/capibmadm/clients/powervs/client.go @@ -18,8 +18,6 @@ limitations under the License. package powervs import ( - "fmt" - "github.com/IBM-Cloud/power-go-client/ibmpisession" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam" @@ -27,10 +25,9 @@ import ( // NewPISession creates new powervs client. // To-Do: Need to handle custom endpoint URL if user wants to use staging env. -func NewPISession(accountID string, region string, zone string, debug bool) (*ibmpisession.IBMPISession, error) { +func NewPISession(accountID string, zone string, debug bool) (*ibmpisession.IBMPISession, error) { return ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{Authenticator: iam.GetIAMAuth(), Debug: debug, - URL: fmt.Sprintf("https://%s.power-iaas.cloud.ibm.com", region), UserAccount: accountID, Zone: zone}) } diff --git a/cmd/capibmadm/cmd/powervs/network/network.go b/cmd/capibmadm/cmd/powervs/network/network.go index 910791dbc..715adf03c 100644 --- a/cmd/capibmadm/cmd/powervs/network/network.go +++ b/cmd/capibmadm/cmd/powervs/network/network.go @@ -18,8 +18,6 @@ package network import ( "github.com/spf13/cobra" - - "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" ) // Commands function to add PowerVS network commands. @@ -28,7 +26,6 @@ func Commands() *cobra.Command { Use: "network", Short: "Perform PowerVS network operations", } - options.AddPowerVSCommonFlags(cmd) cmd.AddCommand(CreateCommand()) diff --git a/cmd/capibmadm/cmd/powervs/powervs.go b/cmd/capibmadm/cmd/powervs/powervs.go index 5255302df..fac509f7c 100644 --- a/cmd/capibmadm/cmd/powervs/powervs.go +++ b/cmd/capibmadm/cmd/powervs/powervs.go @@ -20,6 +20,7 @@ import ( "github.com/spf13/cobra" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" ) // Commands initialises and returns powervs command. @@ -28,6 +29,14 @@ func Commands() *cobra.Command { Use: "powervs", Short: "Commands for operations on PowerVS resources", } + + cmd.PersistentFlags().StringVar(&options.GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id") + cmd.PersistentFlags().StringVar(&options.GlobalOptions.PowerVSZone, "zone", options.GlobalOptions.PowerVSZone, "IBM cloud PowerVS zone (Required)") + cmd.PersistentFlags().BoolVar(&options.GlobalOptions.Debug, "debug", false, "Enable/Disable http transport debugging log") + + _ = cmd.MarkPersistentFlagRequired("service-instance-id") + _ = cmd.MarkPersistentFlagRequired("zone") + cmd.AddCommand(network.Commands()) return cmd diff --git a/cmd/capibmadm/cmd/root.go b/cmd/capibmadm/cmd/root.go index f8b703b27..87f0f1b2e 100644 --- a/cmd/capibmadm/cmd/root.go +++ b/cmd/capibmadm/cmd/root.go @@ -29,6 +29,7 @@ import ( logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/vpc" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" ) @@ -51,8 +52,10 @@ func rootCommand() *cobra.Command { return nil }, } + cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) cmd.AddCommand(powervs.Commands()) + cmd.AddCommand(vpc.Commands()) return cmd } diff --git a/cmd/capibmadm/cmd/vpc/vpc.go b/cmd/capibmadm/cmd/vpc/vpc.go new file mode 100644 index 000000000..ec49c7877 --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/vpc.go @@ -0,0 +1,39 @@ +/* +Copyright 2023 The Kubernetes Authors. + +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 vpc contains the commands to operate on vpc resources. +package vpc + +import ( + "github.com/spf13/cobra" + + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" +) + +// Commands initialises and returns VPC command. +func Commands() *cobra.Command { + cmd := &cobra.Command{ + Use: "vpc", + Short: "Commands for operations on VPC resources", + } + + cmd.PersistentFlags().StringVar(&options.GlobalOptions.VPCRegion, "region", options.GlobalOptions.VPCRegion, "IBM cloud vpc region. (Required)") + cmd.PersistentFlags().StringVar(&options.GlobalOptions.ResourceGroupName, "resource-group-name", options.GlobalOptions.ResourceGroupName, "IBM cloud resource group name") + + _ = cmd.MarkPersistentFlagRequired("region") + + return cmd +} diff --git a/cmd/capibmadm/options/options.go b/cmd/capibmadm/options/options.go index 30907b9ea..9b37e9472 100644 --- a/cmd/capibmadm/options/options.go +++ b/cmd/capibmadm/options/options.go @@ -17,7 +17,11 @@ limitations under the License. // Package options contains the reusable and global variables. package options -import "github.com/spf13/cobra" +import ( + "github.com/spf13/cobra" + + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer" +) // IBMCloudAPIKeyEnvName holds the environmental variable name to set PowerVS service instance ID. const IBMCloudAPIKeyEnvName = "IBMCLOUD_API_KEY" //nolint:gosec @@ -28,11 +32,15 @@ var GlobalOptions = &options{} type options struct { IBMCloudAPIKey string ServiceInstanceID string + PowerVSZone string + VPCRegion string + ResourceGroupName string + Debug bool + Output printer.PType } -// AddPowerVSCommonFlags will add a common Power VS flag to the cli. -func AddPowerVSCommonFlags(cmd *cobra.Command) { - cmd.PersistentFlags().StringVar(&GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id") - - _ = cmd.MarkPersistentFlagRequired("service-instance-id") +// AddCommonFlags will add common flags to the cli. +func AddCommonFlags(cmd *cobra.Command) { + GlobalOptions.Output = printer.PrinterTypeTable + cmd.Flags().Var(&GlobalOptions.Output, "output", "Supported printer types: table, json") } diff --git a/cmd/capibmadm/utils/printer.go b/cmd/capibmadm/printer/printer.go similarity index 70% rename from cmd/capibmadm/utils/printer.go rename to cmd/capibmadm/printer/printer.go index ceac9bb14..90309fde2 100644 --- a/cmd/capibmadm/utils/printer.go +++ b/cmd/capibmadm/printer/printer.go @@ -14,7 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -package utils +// Package printer implements printing functionality for cli. +package printer import ( "encoding/json" @@ -27,14 +28,35 @@ import ( "k8s.io/cli-runtime/pkg/printers" ) -// PrinterType is a type declaration for a printer type. -type PrinterType string +// PType is a type declaration for a printer type. +type PType string -var ( - // PrinterTypeTable is a table printer type. - PrinterTypeTable = PrinterType("table") - // PrinterTypeJSON is a json printer type. - PrinterTypeJSON = PrinterType("json") +// String type casts to string. +func (p *PType) String() string { + return string(*p) +} + +// Set sets value for var. +func (p *PType) Set(s string) error { + switch s { + case string(PrinterTypeTable), string(PrinterTypeJSON): + *p = PType(s) + return nil + default: + return ErrUnknowPrinterType + } +} + +// Type returns type in string format. +func (p *PType) Type() string { + return "PType" +} + +const ( + // PrinterTypeTable is a table printer PType. + PrinterTypeTable = PType("table") + // PrinterTypeJSON is a json printer PType. + PrinterTypeJSON = PType("json") ) var ( @@ -51,12 +73,12 @@ type Printer interface { Print(in interface{}) error } -// NewPrinter creates a new printer. -func NewPrinter(printerType string, writer io.Writer) (Printer, error) { +// New creates a new printer. +func New(printerType PType, writer io.Writer) (Printer, error) { switch printerType { - case string(PrinterTypeTable): + case PrinterTypeTable: return &tablePrinter{writer: writer}, nil - case string(PrinterTypeJSON): + case PrinterTypeJSON: return &jsonPrinter{writer: writer}, nil default: return nil, ErrUnknowPrinterType