diff --git a/cmd/capibmadm/clients/powervs/client.go b/cmd/capibmadm/clients/powervs/client.go index e0d790f168..4393e43cdc 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/root.go b/cmd/capibmadm/cmd/root.go index f8b703b279..2cce7b33c1 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,11 @@ func rootCommand() *cobra.Command { return nil }, } + + options.AddCommonFlags(cmd) 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 0000000000..19584ca1d5 --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/vpc.go @@ -0,0 +1,36 @@ +/* +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", + } + + options.AddVPCCommonFlags(cmd) + + return cmd +} diff --git a/cmd/capibmadm/options/options.go b/cmd/capibmadm/options/options.go index 30907b9ea9..11e80a173e 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,32 @@ var GlobalOptions = &options{} type options struct { IBMCloudAPIKey string ServiceInstanceID string + PowerVSZone string + VPCRegion string + ResourceGroupName string + Debug bool + Output printer.PType +} + +// AddCommonFlags will add common flags to the cli. +func AddCommonFlags(cmd *cobra.Command) { + cmd.Flags().Var(&GlobalOptions.Output, "output", "Supported printer types: table, json") } // 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.PersistentFlags().StringVar(&GlobalOptions.PowerVSZone, "zone", GlobalOptions.PowerVSZone, "IBM cloud PowerVS zone (Required)") + cmd.PersistentFlags().BoolVar(&GlobalOptions.Debug, "debug", false, "Enable/Disable http transport debugging log") + _ = cmd.MarkPersistentFlagRequired("zone") _ = cmd.MarkPersistentFlagRequired("service-instance-id") } + +// AddVPCCommonFlags will add common VPC flags to the cli. +func AddVPCCommonFlags(cmd *cobra.Command) { + cmd.PersistentFlags().StringVar(&GlobalOptions.VPCRegion, "region", GlobalOptions.VPCRegion, "IBM cloud vpc region. (Required)") + cmd.PersistentFlags().StringVar(&GlobalOptions.ResourceGroupName, "resource-group-name", GlobalOptions.ResourceGroupName, "IBM cloud resource group name") + + _ = cmd.MarkPersistentFlagRequired("region") +} diff --git a/cmd/capibmadm/utils/printer.go b/cmd/capibmadm/printer/printer.go similarity index 71% rename from cmd/capibmadm/utils/printer.go rename to cmd/capibmadm/printer/printer.go index ceac9bb147..d79abf07c9 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,15 +73,13 @@ 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): - return &tablePrinter{writer: writer}, nil - case string(PrinterTypeJSON): + case PrinterTypeJSON: return &jsonPrinter{writer: writer}, nil default: - return nil, ErrUnknowPrinterType + return &tablePrinter{writer: writer}, nil } }