Skip to content

Commit a38eb0b

Browse files
Add common flags for capibmadm cli (#1081)
Add validation for output flag
1 parent 5a27e01 commit a38eb0b

File tree

7 files changed

+100
-25
lines changed

7 files changed

+100
-25
lines changed

cmd/capibmadm/clients/powervs/client.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ limitations under the License.
1818
package powervs
1919

2020
import (
21-
"fmt"
22-
2321
"github.com/IBM-Cloud/power-go-client/ibmpisession"
2422

2523
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
2624
)
2725

2826
// NewPISession creates new powervs client.
2927
// To-Do: Need to handle custom endpoint URL if user wants to use staging env.
30-
func NewPISession(accountID string, region string, zone string, debug bool) (*ibmpisession.IBMPISession, error) {
28+
func NewPISession(accountID string, zone string, debug bool) (*ibmpisession.IBMPISession, error) {
3129
return ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{Authenticator: iam.GetIAMAuth(),
3230
Debug: debug,
33-
URL: fmt.Sprintf("https://%s.power-iaas.cloud.ibm.com", region),
3431
UserAccount: accountID,
3532
Zone: zone})
3633
}

cmd/capibmadm/cmd/powervs/network/network.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package network
1818

1919
import (
2020
"github.com/spf13/cobra"
21-
22-
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
2321
)
2422

2523
// Commands function to add PowerVS network commands.
@@ -28,7 +26,6 @@ func Commands() *cobra.Command {
2826
Use: "network",
2927
Short: "Perform PowerVS network operations",
3028
}
31-
options.AddPowerVSCommonFlags(cmd)
3229

3330
cmd.AddCommand(CreateCommand())
3431

cmd/capibmadm/cmd/powervs/powervs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/spf13/cobra"
2121

2222
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network"
23+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
2324
)
2425

2526
// Commands initialises and returns powervs command.
@@ -28,6 +29,14 @@ func Commands() *cobra.Command {
2829
Use: "powervs",
2930
Short: "Commands for operations on PowerVS resources",
3031
}
32+
33+
cmd.PersistentFlags().StringVar(&options.GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id")
34+
cmd.PersistentFlags().StringVar(&options.GlobalOptions.PowerVSZone, "zone", options.GlobalOptions.PowerVSZone, "IBM cloud PowerVS zone (Required)")
35+
cmd.PersistentFlags().BoolVar(&options.GlobalOptions.Debug, "debug", false, "Enable/Disable http transport debugging log")
36+
37+
_ = cmd.MarkPersistentFlagRequired("service-instance-id")
38+
_ = cmd.MarkPersistentFlagRequired("zone")
39+
3140
cmd.AddCommand(network.Commands())
3241

3342
return cmd

cmd/capibmadm/cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
3030

3131
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs"
32+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/vpc"
3233
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
3334
)
3435

@@ -51,8 +52,10 @@ func rootCommand() *cobra.Command {
5152
return nil
5253
},
5354
}
55+
5456
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
5557
cmd.AddCommand(powervs.Commands())
58+
cmd.AddCommand(vpc.Commands())
5659

5760
return cmd
5861
}

cmd/capibmadm/cmd/vpc/vpc.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package vpc contains the commands to operate on vpc resources.
18+
package vpc
19+
20+
import (
21+
"github.com/spf13/cobra"
22+
23+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
24+
)
25+
26+
// Commands initialises and returns VPC command.
27+
func Commands() *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "vpc",
30+
Short: "Commands for operations on VPC resources",
31+
}
32+
33+
cmd.PersistentFlags().StringVar(&options.GlobalOptions.VPCRegion, "region", options.GlobalOptions.VPCRegion, "IBM cloud vpc region. (Required)")
34+
cmd.PersistentFlags().StringVar(&options.GlobalOptions.ResourceGroupName, "resource-group-name", options.GlobalOptions.ResourceGroupName, "IBM cloud resource group name")
35+
36+
_ = cmd.MarkPersistentFlagRequired("region")
37+
38+
return cmd
39+
}

cmd/capibmadm/options/options.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ limitations under the License.
1717
// Package options contains the reusable and global variables.
1818
package options
1919

20-
import "github.com/spf13/cobra"
20+
import (
21+
"github.com/spf13/cobra"
22+
23+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer"
24+
)
2125

2226
// IBMCloudAPIKeyEnvName holds the environmental variable name to set PowerVS service instance ID.
2327
const IBMCloudAPIKeyEnvName = "IBMCLOUD_API_KEY" //nolint:gosec
@@ -28,11 +32,15 @@ var GlobalOptions = &options{}
2832
type options struct {
2933
IBMCloudAPIKey string
3034
ServiceInstanceID string
35+
PowerVSZone string
36+
VPCRegion string
37+
ResourceGroupName string
38+
Debug bool
39+
Output printer.PType
3140
}
3241

33-
// AddPowerVSCommonFlags will add a common Power VS flag to the cli.
34-
func AddPowerVSCommonFlags(cmd *cobra.Command) {
35-
cmd.PersistentFlags().StringVar(&GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id")
36-
37-
_ = cmd.MarkPersistentFlagRequired("service-instance-id")
42+
// AddCommonFlags will add common flags to the cli.
43+
func AddCommonFlags(cmd *cobra.Command) {
44+
GlobalOptions.Output = printer.PrinterTypeTable
45+
cmd.Flags().Var(&GlobalOptions.Output, "output", "Supported printer types: table, json")
3846
}

cmd/capibmadm/utils/printer.go renamed to cmd/capibmadm/printer/printer.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package utils
17+
// Package printer implements printing functionality for cli.
18+
package printer
1819

1920
import (
2021
"encoding/json"
@@ -27,14 +28,35 @@ import (
2728
"k8s.io/cli-runtime/pkg/printers"
2829
)
2930

30-
// PrinterType is a type declaration for a printer type.
31-
type PrinterType string
31+
// PType is a type declaration for a printer type.
32+
type PType string
3233

33-
var (
34-
// PrinterTypeTable is a table printer type.
35-
PrinterTypeTable = PrinterType("table")
36-
// PrinterTypeJSON is a json printer type.
37-
PrinterTypeJSON = PrinterType("json")
34+
// String type casts to string.
35+
func (p *PType) String() string {
36+
return string(*p)
37+
}
38+
39+
// Set sets value for var.
40+
func (p *PType) Set(s string) error {
41+
switch s {
42+
case string(PrinterTypeTable), string(PrinterTypeJSON):
43+
*p = PType(s)
44+
return nil
45+
default:
46+
return ErrUnknowPrinterType
47+
}
48+
}
49+
50+
// Type returns type in string format.
51+
func (p *PType) Type() string {
52+
return "PType"
53+
}
54+
55+
const (
56+
// PrinterTypeTable is a table printer PType.
57+
PrinterTypeTable = PType("table")
58+
// PrinterTypeJSON is a json printer PType.
59+
PrinterTypeJSON = PType("json")
3860
)
3961

4062
var (
@@ -51,12 +73,12 @@ type Printer interface {
5173
Print(in interface{}) error
5274
}
5375

54-
// NewPrinter creates a new printer.
55-
func NewPrinter(printerType string, writer io.Writer) (Printer, error) {
76+
// New creates a new printer.
77+
func New(printerType PType, writer io.Writer) (Printer, error) {
5678
switch printerType {
57-
case string(PrinterTypeTable):
79+
case PrinterTypeTable:
5880
return &tablePrinter{writer: writer}, nil
59-
case string(PrinterTypeJSON):
81+
case PrinterTypeJSON:
6082
return &jsonPrinter{writer: writer}, nil
6183
default:
6284
return nil, ErrUnknowPrinterType

0 commit comments

Comments
 (0)