Skip to content

Commit

Permalink
Modify capibmadm to not use GetAPIKeyDetails to fetch the account ID
Browse files Browse the repository at this point in the history
Signed-off-by: Amulyam24 <[email protected]>
  • Loading branch information
Amulyam24 committed Jun 2, 2023
1 parent 023aeb1 commit 9690fe0
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 46 deletions.
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/image/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import (

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/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

type imageImportOptions struct {
Expand Down Expand Up @@ -106,7 +107,7 @@ func importimage(ctx context.Context, imageImportOption imageImportOptions) erro
log := logf.Log
log.Info("Importing PowerVS images: ", "service-instance-id", options.GlobalOptions.ServiceInstanceID)

accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/capibmadm/cmd/powervs/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import (

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"
pkgUtils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListCommand powervs image list command.
Expand All @@ -55,7 +57,7 @@ func listimage(ctx context.Context) error {
log := logf.Log
log.Info("Listing PowerVS images", "service-instance-id", options.GlobalOptions.ServiceInstanceID)

accountID, err := utils.GetAccountID(ctx)
accountID, err := pkgUtils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/key/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (

"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/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

type keyCreateOptions struct {
Expand Down Expand Up @@ -88,7 +89,7 @@ func createSSHKey(ctx context.Context, keyCreateOption keyCreateOptions) error {
logger := log.Log
logger.Info("Creating SSH key...")

accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/key/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (

"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/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// DeleteSSHKeyCommand - child command of 'key' to delete an SSH key.
Expand Down Expand Up @@ -55,7 +56,7 @@ func deleteSSHKey(ctx context.Context, keyName string) error {
logger := log.Log
logger.Info("Deleting SSH key...")

accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/key/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (

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"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListSSHKeyCommand function to list PowerVS SSH Keys.
Expand All @@ -55,7 +56,7 @@ func listSSHKeys(ctx context.Context) error {
log := logf.Log
log.Info("Listing PowerVS SSH Keys", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (

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/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

type networkCreateOptions struct {
Expand Down Expand Up @@ -83,7 +84,7 @@ func createNetwork(ctx context.Context, netCreateOption networkCreateOptions) er
log := logf.Log
log.Info("Creating PowerVS network", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/network/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (

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/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// DeleteCommand function to delete network.
Expand Down Expand Up @@ -55,7 +56,7 @@ func deleteNetwork(ctx context.Context, networkID string) error {
log := logf.Log
log.Info("Deleting PowerVS network", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (

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"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListCommand function to create PowerVS network.
Expand All @@ -55,7 +56,7 @@ func listNetwork(ctx context.Context) error {
log := logf.Log
log.Info("Listing PowerVS networks", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/capibmadm/cmd/powervs/port/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import (

"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"
pkgUtils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

type portCreateOptions struct {
Expand Down Expand Up @@ -66,7 +68,7 @@ capibmadm powervs port create --network <netword-id/network-name> --description
func createPort(ctx context.Context, portCreateOption portCreateOptions) error {
logger := log.Log
logger.Info("Creating Port ", "Network ID/Name", portCreateOption.network, "IP Address", portCreateOption.ipAddress, "Description", portCreateOption.description, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)
accountID, err := utils.GetAccountID(ctx)
accountID, err := pkgUtils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/capibmadm/cmd/powervs/port/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (

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/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

type portDeleteOptions struct {
Expand Down Expand Up @@ -61,7 +62,7 @@ capibmadm powervs port delete --port-id <port-id> --network <network-name/networ
func deletePort(ctx context.Context, portDeleteOption portDeleteOptions) error {
log := logf.Log
log.Info("Deleting PowerVS network port", "network", portDeleteOption.network, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "port-id", portDeleteOption.portID)
accountID, err := utils.GetAccountID(ctx)
accountID, err := utils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/capibmadm/cmd/powervs/port/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (

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"
pkgUtils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListCommand powervs port list command.
Expand Down Expand Up @@ -61,7 +63,7 @@ func listPorts(ctx context.Context, network string) error {
log := logf.Log
log.Info("Listing PowerVS ports", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "network", network)

accountID, err := utils.GetAccountID(ctx)
accountID, err := pkgUtils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/capibmadm/cmd/vpc/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (

"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"
pkgUtils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListCommand vpc image list command.
Expand Down Expand Up @@ -58,7 +59,7 @@ func listImages(ctx context.Context, resourceGroupName string) error {
return err
}

accountID, err := utils.GetAccountID(ctx)
accountID, err := pkgUtils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down Expand Up @@ -95,7 +96,7 @@ func listImages(ctx context.Context, resourceGroupName string) error {
return true, "", nil
}

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

Expand Down
4 changes: 3 additions & 1 deletion cmd/capibmadm/cmd/vpc/key/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import (

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

type keyCreateOptions struct {
Expand Down Expand Up @@ -91,7 +93,7 @@ func createKey(ctx context.Context, keyCreateOption keyCreateOptions) error {
return err
}

accountID, err := utils.GetAccountID(ctx)
accountID, err := pkgUtils.GetAccount(iam.GetIAMAuth())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/capibmadm/cmd/vpc/key/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"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"
pkgUtils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListCommand vpc key list command.
Expand Down Expand Up @@ -79,7 +79,7 @@ func listKeys(ctx context.Context) error {
return true, "", nil
}

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

Expand Down
21 changes: 0 additions & 21 deletions cmd/capibmadm/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,11 @@ import (

"github.com/go-openapi/strfmt"

"github.com/IBM/platform-services-go-sdk/iamidentityv1"
"github.com/IBM/platform-services-go-sdk/resourcemanagerv2"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/platformservices"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
)

// GetAccountID returns IBM cloud account ID of API key used.
func GetAccountID(ctx context.Context) (string, error) {
iamv1, err := platformservices.NewIAMIdentityClient()
if err != nil {
return "", err
}

apiKeyDetailsOpt := iamidentityv1.GetAPIKeysDetailsOptions{IamAPIKey: &options.GlobalOptions.IBMCloudAPIKey}
apiKey, _, err := iamv1.GetAPIKeysDetailsWithContext(ctx, &apiKeyDetailsOpt)
if err != nil {
return "", err
}
if apiKey == nil {
return "", fmt.Errorf("could not retrieve account id")
}

return *apiKey.AccountID, nil
}

// GetResourceGroupID returns ID of given resource group name.
func GetResourceGroupID(ctx context.Context, resourceGroup string, accountID string) (string, error) {
rmv2, err := platformservices.NewResourceManagerV2Client()
Expand Down

0 comments on commit 9690fe0

Please sign in to comment.