Skip to content

Commit

Permalink
Add capibmadm vpc image list command (#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharaneeshvrd authored Feb 10, 2023
1 parent f5d7dc0 commit e542018
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 1 deletion.
34 changes: 34 additions & 0 deletions cmd/capibmadm/cmd/vpc/image/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 vpc image resources.
package image

import (
"github.com/spf13/cobra"
)

// Commands function to add VPC image commands.
func Commands() *cobra.Command {
cmd := &cobra.Command{
Use: "image",
Short: "Perform VPC image operations",
}

cmd.AddCommand(ListCommand())

return cmd
}
163 changes: 163 additions & 0 deletions cmd/capibmadm/cmd/vpc/image/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
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"
"os"

"github.com/go-openapi/strfmt"
"github.com/spf13/cobra"

"github.com/IBM/vpc-go-sdk/vpcv1"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/vpc"
"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"
pagingUtil "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListCommand vpc image list command.
func ListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List VPC images",
Example: `
# List images in VPC
export IBMCLOUD_API_KEY=<api-key>
capibmadm vpc image list --region <region> --resource-group-name <resource-group-name>`,
}

options.AddCommonFlags(cmd)

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if err := listImages(cmd.Context(), options.GlobalOptions.ResourceGroupName); err != nil {
return err
}
return nil
}

return cmd
}

func listImages(ctx context.Context, resourceGroupName string) error {
v1, err := vpc.NewV1Client(options.GlobalOptions.VPCRegion)
if err != nil {
return err
}

accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
if err != nil {
return err
}

var resourceGroupID string
if resourceGroupName != "" {
resourceGroupID, err = utils.GetResourceGroupID(ctx, resourceGroupName, accountID)
if err != nil {
return err
}
}

var imageNesList []*vpcv1.ImageCollection
f := func(start string) (bool, string, error) {
var listImageOpt vpcv1.ListImagesOptions

if resourceGroupID != "" {
listImageOpt.ResourceGroupID = &resourceGroupID
}
if start != "" {
listImageOpt.Start = &start
}

imageL, _, err := v1.ListImagesWithContext(ctx, &listImageOpt)
if err != nil {
return false, "", err
}
imageNesList = append(imageNesList, imageL)

if imageL.Next != nil && *imageL.Next.Href != "" {
return false, *imageL.Next.Href, nil
}

return true, "", nil
}

if err = pagingUtil.PagingHelper(f); err != nil {
return err
}

return display(imageNesList)
}

func display(imageNesList []*vpcv1.ImageCollection) error {
var imageListToDisplay List
for _, imageL := range imageNesList {
for _, image := range imageL.Images {
imageToAppend := Image{
ID: utils.DereferencePointer(image.ID).(string),
Name: utils.DereferencePointer(image.Name).(string),
Status: utils.DereferencePointer(image.Status).(string),
CreatedAt: utils.DereferencePointer(image.CreatedAt).(strfmt.DateTime),
Visibility: utils.DereferencePointer(image.Visibility).(string),
Encryption: utils.DereferencePointer(image.Encryption).(string),
}

if image.File != nil {
imageToAppend.FileSize = utils.DereferencePointer(image.File.Size).(int64)
}

if image.ResourceGroup != nil {
imageToAppend.ResourceGroupName = utils.DereferencePointer(image.ResourceGroup.Name).(string)
}

if image.OperatingSystem != nil {
imageToAppend.OperatingSystemName = utils.DereferencePointer(image.OperatingSystem.DisplayName).(string)
imageToAppend.OperatingSystemVersion = utils.DereferencePointer(image.OperatingSystem.Version).(string)
imageToAppend.Arch = utils.DereferencePointer(image.OperatingSystem.Architecture).(string)
}

if image.SourceVolume != nil {
imageToAppend.SourceVolumeName = utils.DereferencePointer(image.SourceVolume.Name).(string)
}

if image.CatalogOffering != nil {
imageToAppend.CatalogOffering = utils.DereferencePointer(image.CatalogOffering.Managed).(bool)
}

imageListToDisplay = append(imageListToDisplay, imageToAppend)
}
}

p, err := printer.New(options.GlobalOptions.Output, os.Stdout)

if err != nil {
return err
}

switch options.GlobalOptions.Output {
case printer.PrinterTypeJSON:
err = p.Print(imageListToDisplay)
default:
table := imageListToDisplay.ToTable()
err = p.Print(table)
}

return err
}
115 changes: 115 additions & 0 deletions cmd/capibmadm/cmd/vpc/image/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
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"
)

// Image vpc image info.
type Image struct {
CatalogOffering bool `json:"catalogOffering"`
CreatedAt strfmt.DateTime `json:"created_at"`
Encryption string `json:"encryption"`
ID string `json:"id"`
Name string `json:"name"`
OperatingSystemName string `json:"operatingSystemName"`
OperatingSystemVersion string `json:"operatingSystemVersion"`
Arch string `json:"arch"`
FileSize int64 `json:"fileSizeInGB"`
SourceVolumeName string `json:"sourceVolumeName"`
ResourceGroupName string `json:"resourceGroupName"`
Status string `json:"status"`
Visibility string `json:"visibility"`
}

// List is list of Image.
type List []Image

// ToTable converts List to *metav1.Table.
func (imageList *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: "STATUS",
Type: "string",
},
{
Name: "CREATED AT",
Type: "string",
},
{
Name: "OS NAME",
Type: "string",
},
{
Name: "OS VERSION",
Type: "string",
},
{
Name: "ARCH",
Type: "string",
},
{
Name: "FILE SIZE(GB)",
Type: "integer",
},
{
Name: "SOURCE VOLUME",
Type: "string",
},
{
Name: "VISIBILITY",
Type: "string",
},
{
Name: "ENCRYPTION",
Type: "string",
},
{
Name: "RESOURCE GROUP",
Type: "string",
},
{
Name: "CATALOG OFFERING",
Type: "boolean",
},
},
}

for _, image := range *imageList {
row := metav1.TableRow{
Cells: []interface{}{image.ID, image.Name, image.Status, image.CreatedAt, image.OperatingSystemName, image.OperatingSystemVersion, image.Arch, image.FileSize, image.SourceVolumeName, image.Visibility, image.Encryption, image.ResourceGroupName, image.CatalogOffering},
}
table.Rows = append(table.Rows, row)
}
return table
}
3 changes: 3 additions & 0 deletions cmd/capibmadm/cmd/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package vpc
import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/vpc/image"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
)

Expand All @@ -35,5 +36,7 @@ func Commands() *cobra.Command {

_ = cmd.MarkPersistentFlagRequired("region")

cmd.AddCommand(image.Commands())

return cmd
}
3 changes: 2 additions & 1 deletion cmd/capibmadm/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ type options struct {

// AddCommonFlags will add common flags to the cli.
func AddCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP((*string)(&GlobalOptions.Output), "output", "o", "table", "The output format of the results. Supported printer types: table, json")
GlobalOptions.Output = printer.PrinterTypeTable
cmd.Flags().Var(&GlobalOptions.Output, "output", "The output format of the results. Supported printer types: table, json")
}

0 comments on commit e542018

Please sign in to comment.