Skip to content

Commit

Permalink
Add common flags for capibmadm cli (#1081)
Browse files Browse the repository at this point in the history
Add validation for output flag
  • Loading branch information
dharaneeshvrd authored Feb 8, 2023
1 parent 5a27e01 commit a38eb0b
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 25 deletions.
5 changes: 1 addition & 4 deletions cmd/capibmadm/clients/powervs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ 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"
)

// 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})
}
3 changes: 0 additions & 3 deletions cmd/capibmadm/cmd/powervs/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -28,7 +26,6 @@ func Commands() *cobra.Command {
Use: "network",
Short: "Perform PowerVS network operations",
}
options.AddPowerVSCommonFlags(cmd)

cmd.AddCommand(CreateCommand())

Expand Down
9 changes: 9 additions & 0 deletions cmd/capibmadm/cmd/powervs/powervs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cmd/capibmadm/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
39 changes: 39 additions & 0 deletions cmd/capibmadm/cmd/vpc/vpc.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 14 additions & 6 deletions cmd/capibmadm/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
46 changes: 34 additions & 12 deletions cmd/capibmadm/utils/printer.go → cmd/capibmadm/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand All @@ -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
Expand Down

0 comments on commit a38eb0b

Please sign in to comment.