Skip to content

Commit

Permalink
Add common flags for capibmadm cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dharaneeshvrd committed Feb 7, 2023
1 parent 5eff1b0 commit 0db0561
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 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: 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
36 changes: 36 additions & 0 deletions cmd/capibmadm/cmd/vpc/vpc.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 26 additions & 1 deletion 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,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")
}
48 changes: 34 additions & 14 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,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
}
}

Expand Down

0 comments on commit 0db0561

Please sign in to comment.