-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add capibmadm vpc image list command (#1071)
- Loading branch information
1 parent
f5d7dc0
commit e542018
Showing
5 changed files
with
317 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters