-
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 support for powervs image list command (#1127)
- Loading branch information
1 parent
7145288
commit ef8e738
Showing
7 changed files
with
316 additions
and
0 deletions.
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,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 | ||
} |
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,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 { | ||
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 | ||
} |
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,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 | ||
} |
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
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,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> | ||
``` |
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