diff --git a/cmd/capibmadm/cmd/vpc/doc b/cmd/capibmadm/cmd/vpc/doc new file mode 100644 index 0000000000..85238357c0 --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/doc @@ -0,0 +1,15 @@ +/* +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 diff --git a/cmd/capibmadm/cmd/vpc/key/key.go b/cmd/capibmadm/cmd/vpc/key/key.go new file mode 100644 index 0000000000..02713f4249 --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/key/key.go @@ -0,0 +1,30 @@ +/* +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 key + +import ( + "github.com/spf13/cobra" +) + +// Commands function to add VPC key commands. +func Commands() *cobra.Command { + cmd := &cobra.Command{ + Use: "key", + Short: "Perform VPC key operations", + } + + cmd.AddCommand(ListCommand()) + + return cmd +} diff --git a/cmd/capibmadm/cmd/vpc/key/list.go b/cmd/capibmadm/cmd/vpc/key/list.go new file mode 100644 index 0000000000..cc01a38625 --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/key/list.go @@ -0,0 +1,148 @@ +/* +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 key + +import ( + "context" + "os" + + "github.com/go-openapi/strfmt" + "github.com/spf13/cobra" + + "github.com/IBM/vpc-go-sdk/vpcv1" + + "sigs.k8s.io/cluster-api-provider-aws/v2/cmd/clusterawsadm/printers" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils" + pagingUtil "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils" +) + +var resourceGroupName string + +// ListCommand vpc image list command. +func ListCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List VPC key", + Example: ` +# List key in VPC +export IBMCLOUD_API_KEY= +capibmadm vpc key list --region --resource-group-name `, + } + + options.AddCommonFlags(cmd) + options.AddVPCCommonFlags(cmd) + + cmd.Flags().StringVar(&resourceGroupName, "resource-group-name", resourceGroupName, "IBM cloud resource group name") + + cmd.RunE = func(cmd *cobra.Command, args []string) error { + if err := listKeys(cmd.Context(), resourceGroupName); err != nil { + return err + } + return nil + } + + return cmd +} + +func listKeys(ctx context.Context, resourceGroupName string) error { + v1, err := utils.CreateVPCService(options.GlobalOptions.VPCRegion) + if err != nil { + return err + } + + accountID, err := utils.GetAccountID(ctx, utils.GetIAMAuth()) + if err != nil { + return err + } + + if resourceGroupName != "" { + _, err = utils.GetResourceGroupID(ctx, resourceGroupName, accountID) + if err != nil { + return err + } + } + + var keyNesList []*vpcv1.KeyCollection + f := func(start string) (bool, string, error) { + var listKeyOpt vpcv1.ListKeysOptions + + if start != "" { + listKeyOpt.Start = &start + } + + keyL, _, err := v1.ListKeysWithContext(ctx, &listKeyOpt) + if err != nil { + return false, "", err + } + keyNesList = append(keyNesList, keyL) + + if keyL.Next != nil && *keyL.Next.Href != "" { + return false, *keyL.Next.Href, nil + } + + return true, "", nil + } + + if err = pagingUtil.PagingHelper(f); err != nil { + return err + } + + return display(keyNesList) +} + +func display(keyNesList []*vpcv1.KeyCollection) error { + var keyListToDisplay List + for _, keyL := range keyNesList { + for _, key := range keyL.Keys { + keyToAppend := Key{ + CreatedAt: utils.DereferencePointer(key.CreatedAt).(strfmt.DateTime), + ID: utils.DereferencePointer(key.ID).(string), + Name: utils.DereferencePointer(key.Name).(string), + Type: utils.DereferencePointer(key.Type).(string), + Length: utils.DereferencePointer(key.Length).(int64), + FingerPrint: utils.DereferencePointer(key.Fingerprint).(string), + ResourceGroup: utils.DereferencePointer(key.ResourceGroup.Name).(string), + } + + if key.ResourceGroup != nil { + keyToAppend.ResourceGroup = utils.DereferencePointer(key.ResourceGroup.Name).(string) + } + + keyListToDisplay = append(keyListToDisplay, keyToAppend) + } + } + + var printer printers.Printer + var err error + + if options.GlobalOptions.Output == "json" { + printer, err = printers.New("json", os.Stdout) + } else { + printer, err = printers.New("table", os.Stdout) + } + + if err != nil { + return err + } + + switch options.GlobalOptions.Output { + case "json": + err = printer.Print(keyListToDisplay) + default: + table := keyListToDisplay.ToTable() + err = printer.Print(table) + } + return err +} diff --git a/cmd/capibmadm/cmd/vpc/key/type.go b/cmd/capibmadm/cmd/vpc/key/type.go new file mode 100644 index 0000000000..be0c88cce3 --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/key/type.go @@ -0,0 +1,81 @@ +/* +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 key + +import ( + "github.com/go-openapi/strfmt" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// Image vpc image info. +type Key struct { + ID string `json:"id"` + CreatedAt strfmt.DateTime `json:"created_at"` + Name string `json:"name"` + Type string `json:"type"` + ResourceGroup string `json:"resourceGroup"` + FingerPrint string `json:"fingerPrint"` + Length int64 `json:"length"` +} + +// List is list of Image. +type List []Key + +// ToTable converts List to *metav1.Table. +func (keyList *List) ToTable() *metav1.Table { + table := &metav1.Table{ + TypeMeta: metav1.TypeMeta{ + APIVersion: metav1.SchemeGroupVersion.String(), + Kind: "Table", + }, + ColumnDefinitions: []metav1.TableColumnDefinition{ + { + Name: "ID", + Type: "string", + }, + { + Name: "Name", + Type: "string", + }, + { + Name: "Type", + Type: "string", + }, + { + Name: "Created At", + Type: "string", + }, + { + Name: "Length", + Type: "string", + }, + { + Name: "FingerPrint", + Type: "string", + }, + { + Name: "Resource Group", + Type: "string", + }, + }, + } + + for _, key := range *keyList { + row := metav1.TableRow{ + Cells: []interface{}{key.ID, key.Name, key.Type, key.CreatedAt, key.Length, key.FingerPrint, key.ResourceGroup}, + } + table.Rows = append(table.Rows, row) + } + return table +} diff --git a/cmd/capibmadm/cmd/vpc/vpc.go b/cmd/capibmadm/cmd/vpc/vpc.go index ec49c7877e..0038f1976d 100644 --- a/cmd/capibmadm/cmd/vpc/vpc.go +++ b/cmd/capibmadm/cmd/vpc/vpc.go @@ -1,6 +1,7 @@ /* 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 @@ -15,11 +16,13 @@ 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/cmd/vpc/key" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" ) @@ -34,6 +37,7 @@ func Commands() *cobra.Command { cmd.PersistentFlags().StringVar(&options.GlobalOptions.ResourceGroupName, "resource-group-name", options.GlobalOptions.ResourceGroupName, "IBM cloud resource group name") _ = cmd.MarkPersistentFlagRequired("region") + cmd.AddCommand(key.Commands()) return cmd } diff --git a/cmd/capibmadm/options/options.go b/cmd/capibmadm/options/options.go index 9b37e94726..1d360d37f5 100644 --- a/cmd/capibmadm/options/options.go +++ b/cmd/capibmadm/options/options.go @@ -1,12 +1,9 @@ /* 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. @@ -19,7 +16,6 @@ package options import ( "github.com/spf13/cobra" - "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer" ) @@ -44,3 +40,10 @@ func AddCommonFlags(cmd *cobra.Command) { GlobalOptions.Output = printer.PrinterTypeTable cmd.Flags().Var(&GlobalOptions.Output, "output", "Supported printer types: table, json") } + +// 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.MarkPersistentFlagRequired("region") +} diff --git a/cmd/capibmadm/utils/utils.go b/cmd/capibmadm/utils/utils.go index c258d7cabd..144d1dac2e 100644 --- a/cmd/capibmadm/utils/utils.go +++ b/cmd/capibmadm/utils/utils.go @@ -1,12 +1,9 @@ /* 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. @@ -14,7 +11,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package utils contains utility and printer functions for cli. package utils import ( @@ -26,14 +22,37 @@ import ( "github.com/IBM/go-sdk-core/v5/core" "github.com/IBM/platform-services-go-sdk/iamidentityv1" "github.com/IBM/platform-services-go-sdk/resourcemanagerv2" + "github.com/IBM/vpc-go-sdk/vpcv1" - "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/platformservices" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" ) -// GetAccountID returns IBM cloud account ID of API key used. +func CreateVPCService(region string) (*vpcv1.VpcV1, error) { + svcEndpoint := "https://" + region + ".iaas.cloud.ibm.com/v1" + + v1, err := vpcv1.NewVpcV1(&vpcv1.VpcV1Options{ + ServiceName: "vpcs", + Authenticator: GetIAMAuth(), + URL: svcEndpoint, + }) + + return v1, err +} + +func CreateResourceManagerV2Client() (*resourcemanagerv2.ResourceManagerV2, error) { + rmv2, err := resourcemanagerv2.NewResourceManagerV2(&resourcemanagerv2.ResourceManagerV2Options{ + Authenticator: GetIAMAuth(), + URL: resourcemanagerv2.DefaultServiceURL, + }) + + return rmv2, err +} + func GetAccountID(ctx context.Context, auth *core.IamAuthenticator) (string, error) { - iamv1, err := platformservices.NewIAMIdentityClient() + iamv1, err := iamidentityv1.NewIamIdentityV1(&iamidentityv1.IamIdentityV1Options{ + Authenticator: auth, + URL: iamidentityv1.DefaultServiceURL, + }) if err != nil { return "", err } @@ -50,9 +69,9 @@ func GetAccountID(ctx context.Context, auth *core.IamAuthenticator) (string, err return *apiKey.AccountID, nil } -// GetResourceGroupID returns ID of given resource group name. func GetResourceGroupID(ctx context.Context, resourceGroup string, accountID string) (string, error) { - rmv2, err := platformservices.NewResourceManagerV2Client() + + rmv2, err := CreateResourceManagerV2Client() if err != nil { return "", err @@ -78,7 +97,12 @@ func GetResourceGroupID(ctx context.Context, resourceGroup string, accountID str return "", err } -// DereferencePointer dereferences pointer. +func GetIAMAuth() *core.IamAuthenticator { + return &core.IamAuthenticator{ + ApiKey: options.GlobalOptions.IBMCloudAPIKey, + } +} + func DereferencePointer(value interface{}) interface{} { switch v := value.(type) { case *string: diff --git a/go.mod b/go.mod index 924fd5f317..4fe74b9067 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( k8s.io/klog/v2 v2.80.1 k8s.io/utils v0.0.0-20220823124924-e9cbc92d1a73 sigs.k8s.io/cluster-api v1.3.3 + sigs.k8s.io/cluster-api-provider-aws/v2 v2.0.2 sigs.k8s.io/cluster-api/test v1.3.3 sigs.k8s.io/controller-runtime v0.13.1 ) @@ -109,8 +110,8 @@ require ( github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.13.0 // indirect - github.com/prometheus/client_model v0.2.0 // indirect + github.com/prometheus/client_golang v1.14.0 // indirect + github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/common v0.37.0 // indirect github.com/prometheus/procfs v0.8.0 // indirect github.com/shopspring/decimal v1.3.1 // indirect diff --git a/go.sum b/go.sum index 27a2f51c0c..bff087d7fe 100644 --- a/go.sum +++ b/go.sum @@ -661,13 +661,14 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= -github.com/prometheus/client_golang v1.13.0 h1:b71QUfeo5M8gq2+evJdTPfZhYMAU0uKPkyPJ7TPsloU= -github.com/prometheus/client_golang v1.13.0/go.mod h1:vTeo+zgvILHsnnj/39Ou/1fPN5nJFOEMgftOUOmlvYQ= +github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= +github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= +github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= @@ -1405,6 +1406,8 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/cluster-api v1.3.3 h1:sHRAbuev6+bz3OAySmdmT62md/D/UUIy0EYwvP38H/4= sigs.k8s.io/cluster-api v1.3.3/go.mod h1:nnXmR51rHshpMEXmB4LZIwdiXWKXV6yaooB1KzrL0Qs= +sigs.k8s.io/cluster-api-provider-aws/v2 v2.0.2 h1:WjTt0uyUG+FJBodnrSzb1L3mHfkIgtgMnNQQ+NJMuT8= +sigs.k8s.io/cluster-api-provider-aws/v2 v2.0.2/go.mod h1:ZKM3W39Pl7uPuuB6mT/dWakgy1O8OcivGRCAZqSP+Hs= sigs.k8s.io/cluster-api/test v1.3.3 h1:AzX6zWneNOv3umK9PnhON5kjt2MJaw0jvFl39ReWL28= sigs.k8s.io/cluster-api/test v1.3.3/go.mod h1:HKkh1O4lALo51GsXVIJ1rQNyLRE+CbEVcA+f/1gU5Rw= sigs.k8s.io/controller-runtime v0.13.1 h1:tUsRCSJVM1QQOOeViGeX3GMT3dQF1eePPw6sEE3xSlg=