From ef8e738f513534d6ec3303ac8fe5b559c1d3299f Mon Sep 17 00:00:00 2001 From: Keerthana Arumugam Date: Thu, 2 Mar 2023 17:58:54 +0530 Subject: [PATCH] Add support for powervs image list command (#1127) --- cmd/capibmadm/cmd/powervs/image/image.go | 37 +++++ cmd/capibmadm/cmd/powervs/image/list.go | 126 +++++++++++++++++ cmd/capibmadm/cmd/powervs/image/type.go | 127 ++++++++++++++++++ cmd/capibmadm/cmd/powervs/powervs.go | 2 + docs/book/src/SUMMARY.md | 1 + .../src/topics/capibmadm/powervs/image.md | 21 +++ .../src/topics/capibmadm/powervs/index.md | 2 + 7 files changed, 316 insertions(+) create mode 100644 cmd/capibmadm/cmd/powervs/image/image.go create mode 100644 cmd/capibmadm/cmd/powervs/image/list.go create mode 100644 cmd/capibmadm/cmd/powervs/image/type.go create mode 100644 docs/book/src/topics/capibmadm/powervs/image.md diff --git a/cmd/capibmadm/cmd/powervs/image/image.go b/cmd/capibmadm/cmd/powervs/image/image.go new file mode 100644 index 000000000..10a0dbae6 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/image/image.go @@ -0,0 +1,37 @@ +/* +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 image contains the commands to operate on PowerVS image resources. +package image + +import ( + "github.com/spf13/cobra" + + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" +) + +// Commands function to add PowerVS image commands. +func Commands() *cobra.Command { + cmd := &cobra.Command{ + Use: "image", + Short: "Perform PowerVS image operations", + } + options.AddCommonFlags(cmd) + + cmd.AddCommand(ListCommand()) + + return cmd +} diff --git a/cmd/capibmadm/cmd/powervs/image/list.go b/cmd/capibmadm/cmd/powervs/image/list.go new file mode 100644 index 000000000..56890c4f1 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/image/list.go @@ -0,0 +1,126 @@ +/* +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 image + +import ( + "context" + "fmt" + "os" + + "github.com/go-openapi/strfmt" + "github.com/spf13/cobra" + + powerClient "github.com/IBM-Cloud/power-go-client/clients/instance" + + logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log" + + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils" +) + +// ListCommand powervs image list command. +func ListCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List PowerVS image", + Example: ` +# List PowerVS images +export IBMCLOUD_API_KEY= +capibmadm powervs image list --service-instance-id --zone `, + RunE: func(cmd *cobra.Command, args []string) error { + if err := listimage(cmd.Context()); err != nil { + return err + } + return nil + }, + } + options.AddCommonFlags(cmd) + return cmd +} + +func listimage(ctx context.Context) error { + log := logf.Log + log.Info("Listing PowerVS images", "service-instance-id", options.GlobalOptions.ServiceInstanceID) + + auth := iam.GetIAMAuth() + accountID, err := utils.GetAccountID(ctx, auth) + if err != nil { + return err + } + + sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug) + if err != nil { + return err + } + + imageClient := powerClient.NewIBMPIImageClient(ctx, sess, options.GlobalOptions.ServiceInstanceID) + images, err := imageClient.GetAll() + if err != nil { + return err + } + if len(images.Images) == 0 { + fmt.Println("No images found") + return nil + } + + imageList := ImgList{ + Items: []ImgSpec{}, + } + + for _, image := range images.Images { + imageToAppend := ImgSpec{ + ImageID: utils.DereferencePointer(image.ImageID).(string), + Name: utils.DereferencePointer(image.Name).(string), + Description: utils.DereferencePointer(image.Description).(string), + State: utils.DereferencePointer(image.State).(string), + StoragePool: utils.DereferencePointer(image.StoragePool).(string), + StorageType: utils.DereferencePointer(image.StorageType).(string), + CreationDate: utils.DereferencePointer(image.CreationDate).(strfmt.DateTime), + LastUpdateDate: utils.DereferencePointer(image.LastUpdateDate).(strfmt.DateTime), + } + if image.Specifications != nil { + imageToAppend.Architecture = image.Specifications.Architecture + imageToAppend.ContainerFormat = image.Specifications.ContainerFormat + imageToAppend.DiskFormat = image.Specifications.DiskFormat + imageToAppend.Endianness = image.Specifications.Endianness + imageToAppend.HypervisorType = image.Specifications.HypervisorType + imageToAppend.ImageType = image.Specifications.ImageType + imageToAppend.OperatingSystem = image.Specifications.OperatingSystem + } + + imageList.Items = append(imageList.Items, imageToAppend) + } + + printerObj, err := printer.New(options.GlobalOptions.Output, os.Stdout) + + if err != nil { + return err + } + + switch options.GlobalOptions.Output { + case printer.PrinterTypeJSON: + err = printerObj.Print(imageList) + default: + table := imageList.ToTable() + err = printerObj.Print(table) + } + + return err +} diff --git a/cmd/capibmadm/cmd/powervs/image/type.go b/cmd/capibmadm/cmd/powervs/image/type.go new file mode 100644 index 000000000..43e1e0961 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/image/type.go @@ -0,0 +1,127 @@ +/* +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 image + +import ( + "github.com/go-openapi/strfmt" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// ImgSpec defines a Image. +type ImgSpec struct { + ImageID string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + State string `json:"state"` + StoragePool string `json:"storagePool"` + StorageType string `json:"storageType"` + CreationDate strfmt.DateTime `json:"creationDate"` + LastUpdateDate strfmt.DateTime `json:"lastUpdateDate"` + Architecture string `json:"architecture,omitempty"` + ContainerFormat string `json:"containerFormat,omitempty"` + DiskFormat string `json:"diskFormat,omitempty"` + Endianness string `json:"endianness,omitempty"` + HypervisorType string `json:"hypervisorType,omitempty"` + ImageType string `json:"imageType,omitempty"` + OperatingSystem string `json:"operatingSystem,omitempty"` +} + +// ImgList defines a list of Images. +type ImgList struct { + Items []ImgSpec `json:"items"` +} + +// ToTable converts List to *metav1.Table. +func (imageList *ImgList) 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: "STATE", + Type: "string", + }, + { + Name: "DESCRIPTION", + Type: "string", + }, + { + Name: "STORAGE POOL", + Type: "string", + }, + { + Name: "STORAGE TYPE", + Type: "string", + }, + { + Name: "CREATION DATE", + Type: "string", + }, + { + Name: "LAST UPDATE DATE", + Type: "string", + }, + { + Name: "ARCH", + Type: "string", + }, + { + Name: "CONTAINER FORMAT", + Type: "string", + }, + { + Name: "DISK FORMAT", + Type: "string", + }, + { + Name: "ENDIANNESS", + Type: "string", + }, + { + Name: "HYPERVISOR TYPE ", + Type: "string", + }, + { + Name: "OS", + Type: "string", + }, + { + Name: "IMAGE TYPE", + Type: "string", + }, + }, + } + + for _, image := range imageList.Items { + row := metav1.TableRow{ + Cells: []interface{}{image.ImageID, image.Name, image.State, image.Description, image.StoragePool, image.StorageType, image.CreationDate, image.LastUpdateDate, image.Architecture, image.ContainerFormat, image.DiskFormat, image.Endianness, image.HypervisorType, image.OperatingSystem, image.ImageType}, + } + table.Rows = append(table.Rows, row) + } + return table +} diff --git a/cmd/capibmadm/cmd/powervs/powervs.go b/cmd/capibmadm/cmd/powervs/powervs.go index fd55d04ca..fe9a34b56 100644 --- a/cmd/capibmadm/cmd/powervs/powervs.go +++ b/cmd/capibmadm/cmd/powervs/powervs.go @@ -19,6 +19,7 @@ package powervs import ( "github.com/spf13/cobra" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/image" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/key" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/port" @@ -42,6 +43,7 @@ func Commands() *cobra.Command { cmd.AddCommand(key.Commands()) cmd.AddCommand(network.Commands()) cmd.AddCommand(port.Commands()) + cmd.AddCommand(image.Commands()) return cmd } diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index f696cf3cf..43a693cc5 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -19,6 +19,7 @@ - [Using autoscaler with scaling from 0 machine](./topics/powervs/autoscaler-scalling-from-0.md) - [capibmadm CLI](./topics/capibmadm/index.md) - [Power VS Commands](./topics/capibmadm/powervs/index.md) + - [Image Commands](./topics/capibmadm/powervs/image.md) - [Network Commands](./topics/capibmadm/powervs/network.md) - [Port Commands](./topics/capibmadm/powervs/port.md) - [SSH key Commands](./topics/capibmadm/powervs/key.md) diff --git a/docs/book/src/topics/capibmadm/powervs/image.md b/docs/book/src/topics/capibmadm/powervs/image.md new file mode 100644 index 000000000..d61d59f13 --- /dev/null +++ b/docs/book/src/topics/capibmadm/powervs/image.md @@ -0,0 +1,21 @@ +## PowerVS Image Commands + +### 1. capibmadm powervs image list + +#### Usage: +List PowerVS images. + +#### Environmental Variable: +IBMCLOUD_API_KEY: IBM Cloud API key. + +#### Arguments: +--service-instance-id: PowerVS service instance id. + +--zone: PowerVS service instance zone. + + +#### Example: +```shell +export IBMCLOUD_API_KEY= +capibmadm powervs image list --service-instance-id --zone +``` \ No newline at end of file diff --git a/docs/book/src/topics/capibmadm/powervs/index.md b/docs/book/src/topics/capibmadm/powervs/index.md index e3e445df7..7094ce34c 100644 --- a/docs/book/src/topics/capibmadm/powervs/index.md +++ b/docs/book/src/topics/capibmadm/powervs/index.md @@ -12,4 +12,6 @@ - [create](/topics/capibmadm/powervs/port.html#1-capibmadm-powervs-port-create) - [delete](/topics/capibmadm/powervs/port.html#2-capibmadm-powervs-port-delete) - [list](/topics/capibmadm/powervs/port.html#3-capibmadm-powervs-port-list) +- [image](./image.md) + - [list] (/topics/capibmadm/powervs/image.html#1-capibmadm-powervs-image-list))