Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for powervs image list command #1127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cmd/capibmadm/cmd/powervs/image/image.go
Original file line number Diff line number Diff line change
@@ -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
}
126 changes: 126 additions & 0 deletions cmd/capibmadm/cmd/powervs/image/list.go
Original file line number Diff line number Diff line change
@@ -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=<api-key>
capibmadm powervs image list --service-instance-id <service-instance-id> --zone <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 {
KeerthanaAP marked this conversation as resolved.
Show resolved Hide resolved
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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line can be omitted here.

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
}
127 changes: 127 additions & 0 deletions cmd/capibmadm/cmd/powervs/image/type.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions cmd/capibmadm/cmd/powervs/powervs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions docs/book/src/topics/capibmadm/powervs/image.md
Original file line number Diff line number Diff line change
@@ -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=<api-key>
capibmadm powervs image list --service-instance-id <service-instance-id> --zone <zone>
```
2 changes: 2 additions & 0 deletions docs/book/src/topics/capibmadm/powervs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))