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

[WIP] Deprecate bluemix-go and clean-up code. #612

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions cmd/create/port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
network, ipaddress, description string
network, ipaddress, description, netID string
)

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -60,8 +60,6 @@ var Cmd = &cobra.Command{
networkNames = append(networkNames, *net.Name)
}

var netID string

if utils.Contains(networkIDs, network) {
netID = network
} else if utils.Contains(networkNames, network) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dhcp-sync/dhcp-sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ var Cmd = &cobra.Command{

err = watcher.Add(file)
if err != nil {
klog.Fatal(err)
klog.Fatalf("cannot sync DHCP server: %+v", err)
}
<-done
return nil
Expand Down
29 changes: 13 additions & 16 deletions cmd/get/cloudconnections/cloudconnections.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cloudconnections

import (
"os"
"strings"

"github.com/IBM-Cloud/power-go-client/ibmpisession"
Expand Down Expand Up @@ -46,21 +45,17 @@ var Cmd = &cobra.Command{
Use: "cloud-connections",
Short: "List the existing cloud connections in the account",
Long: "List the existing cloud connections enabled across all workspaces in the account",
PreRunE: func(md *cobra.Command, args []string) error {
opt := pkg.Options
// TODO: The GetAuthenticatorFromEnvironment function seems to refer to "IBMCLOUD_APIKEY" rather than IBMCLOUD_API_KEY as used in the project.
// In order to use the resourcecontrollerv2's functionality, the IBMCLOUD_API_KEY is re-exported as IBMCLOUD_APIKEY.
os.Setenv("IBMCLOUD_APIKEY", opt.APIKey)
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options
c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
if err != nil {
klog.Errorf("failed to create a session with IBM cloud: %v", err)
return err
}

e, err := client.GetEnvironment(opt.Environment)
kishen-v marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
// Retrieve all workspaces that are available in the account.
workspaceInstances, err := c.ListWorkspaceInstances()
if err != nil {
Expand All @@ -73,18 +68,24 @@ var Cmd = &cobra.Command{
zoneWorkspaces[*workspaceInstance.RegionID] = append(zoneWorkspaces[*workspaceInstance.RegionID], workspaceDetails{name: *workspaceInstance.Name, guid: *workspaceInstance.GUID})
}
cloudConnections := map[string]cloudConnectionDetails{}
authenticator := &core.IamAuthenticator{ApiKey: c.Config.BluemixAPIKey, URL: *c.Config.TokenProviderEndpoint}
authenticator := &core.IamAuthenticator{ApiKey: pkg.Options.APIKey, URL: e["TPEndpoint"]}
kishen-v marked this conversation as resolved.
Show resolved Hide resolved
klog.Info("Listing cloud connections across all workspaces, please wait..")
// Create a IBM PI Session per zone and reuse them across the workspaces in the same zone.
for workspaceZone, workspaces := range zoneWorkspaces {
pvmclientOptions := ibmpisession.IBMPIOptions{Authenticator: authenticator, Debug: pkg.Options.Debug, UserAccount: c.User.Account, Zone: workspaceZone}
pvmclientOptions := ibmpisession.IBMPIOptions{
URL: e["PIEndpoint"],
Authenticator: authenticator,
Debug: pkg.Options.Debug,
UserAccount: c.User.Account,
Zone: workspaceZone,
}
piSession, err := ibmpisession.NewIBMPISession(&pvmclientOptions)
if err != nil {
return err
}
// Iterate over the workspaces available in the zone.
for _, workspace := range workspaces {
pvmClient, err := client.NewGenericPVMClientWithEnv(c, workspace.guid, workspace.name, opt.Environment, piSession)
pvmClient, err := client.NewGenericPVMClientWithEnv(c, workspace.guid, opt.Environment, piSession)
if err != nil {
return err
}
Expand Down Expand Up @@ -130,8 +131,4 @@ var Cmd = &cobra.Command{
klog.Info("There are no active cloud connections in this account.")
return nil
},
PostRunE: func(md *cobra.Command, args []string) error {
os.Unsetenv("IBMCLOUD_APIKEY")
return nil
},
}
2 changes: 1 addition & 1 deletion cmd/get/peravailability/peravailability.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var Cmd = &cobra.Command{
if err != nil {
return err
}
supportsPER := false
var supportsPER bool
for _, datacenter := range ret.Datacenters {
if datacenter.Capabilities[powerEdgeRouter] {
perEnabledRegions = append(perEnabledRegions, *datacenter.Location.Region)
Expand Down
141 changes: 79 additions & 62 deletions cmd/image/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,23 @@ import (
"strings"
"time"

"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/controller"
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
"github.com/IBM-Cloud/bluemix-go/crn"
"github.com/IBM-Cloud/bluemix-go/models"
pmodels "github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
)

const (
serviceCredPrefix = "pvsadm-service-cred"
serviceCredPrefix = "pvsadm-service-cred"
cloudObjectStorage = "cloud-object-storage"
)

// Find COSINSTANCE details of the Provided bucket
func findCOSInstanceDetails(resources []models.ServiceInstanceV2, bxCli *client.Client) (string, string, crn.CRN) {
for _, resource := range resources {
if resource.Crn.ServiceName == "cloud-object-storage" {
s3client, err := client.NewS3Client(bxCli, resource.Name, pkg.ImageCMDOptions.Region)
if err != nil {
continue
}
buckets, err := s3client.S3Session.ListBuckets(nil)
if err != nil {
continue
}
for _, bucket := range buckets.Buckets {
if *bucket.Name == pkg.ImageCMDOptions.BucketName {
return resource.Name, resource.Guid, resource.Crn
}
}
}
}
return "", "", crn.CRN{}
}

var Cmd = &cobra.Command{
Use: "import",
Short: "Import the image into PowerVS instances",
Expand All @@ -69,7 +46,7 @@ pvsadm image import --help for information
# Set the API key or feed the --api-key commandline argument
export IBMCLOUD_API_KEY=<IBM_CLOUD_API_KEY>

# To Import the imge across the two different IBM account use accesskey and secretkey options
# To Import the image across the two different IBM account use accesskey and secretkey options

# To Import the image from public bucket use public-bucket option

Expand All @@ -94,11 +71,8 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object rhel-83-100
if pkg.ImageCMDOptions.InstanceID == "" && pkg.ImageCMDOptions.InstanceName == "" {
return fmt.Errorf("--pvs-instance-name or --pvs-instance-id required")
}

case1 := pkg.ImageCMDOptions.AccessKey == "" && pkg.ImageCMDOptions.SecretKey != ""
case2 := pkg.ImageCMDOptions.AccessKey != "" && pkg.ImageCMDOptions.SecretKey == ""

if case1 || case2 {
// ensure that both, the AccessKey and SecretKey are either both set or unset
kishen-v marked this conversation as resolved.
Show resolved Hide resolved
if (len(pkg.ImageCMDOptions.AccessKey) > 0) != (len(pkg.ImageCMDOptions.SecretKey) > 0) {
kishen-v marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("required both --accesskey and --secretkey values")
}
return nil
Expand All @@ -115,64 +89,108 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object rhel-83-100
os.Exit(1)
}

bxCli, err := client.NewClientWithEnv(apikey, pkg.Options.Environment, pkg.Options.Debug)
pvsClient, err := client.NewClientWithEnv(apikey, pkg.Options.Environment, pkg.Options.Debug)
if err != nil {
return err
}
var accessKey, accessSecret string

//Create AccessKey and SecretKey for the bucket provided if bucket access is private
if (opt.AccessKey == "" || opt.SecretKey == "") && (!opt.Public) {
//Find CosInstance of the bucket
var svcs []models.ServiceInstanceV2
svcs, err = bxCli.ResourceClientV2.ListInstances(controllerv2.ServiceInstanceQuery{
Type: "service_instance",
})
listServiceInstanceOptions := &resourcecontrollerv2.ListResourceInstancesOptions{
Type: ptr.To(cloudObjectStorage),
}
workspaces, _, err := pvsClient.ResouceControllerClient.ListResourceInstances(listServiceInstanceOptions)
if err != nil {
return err
return fmt.Errorf("failed to list the resource instances: %v", err)
}
cosInstanceName, cosID, crn := findCOSInstanceDetails(svcs, bxCli)
if cosInstanceName == "" {
return fmt.Errorf("failed to find the COS instance for the bucket mentioned: %s", opt.BucketName)
if len(workspaces.Resources) == 0 {
return fmt.Errorf("there are no resouces under the resource instance shared")
}
getServiceInstanceOptions := &resourcecontrollerv2.GetResourceInstanceOptions{
// TODO: possibility of workspaces to either be of type service_instance or composite_instance.
ID: workspaces.Resources[0].ID,
}

keys, err := bxCli.GetResourceKeys(cosID)
cosInstance, _, err := pvsClient.ResouceControllerClient.GetResourceInstance(getServiceInstanceOptions)
if err != nil {
return fmt.Errorf("failed to list the service credentials: %v", err)
return fmt.Errorf("failed to list the resource instances: %v", err)
}

var cred map[string]interface{}
key, _, err := pvsClient.ResouceControllerClient.GetResourceKey(
&resourcecontrollerv2.GetResourceKeyOptions{
ID: cosInstance.ID,
},
)
if err != nil {
return fmt.Errorf("cannot find resource")
}
var ok bool
if len(keys) == 0 {

// Create the service credential if does not exist
if key.ID == nil {
if opt.ServiceCredName == "" {
opt.ServiceCredName = serviceCredPrefix + "-" + cosInstanceName
opt.ServiceCredName = serviceCredPrefix + "-" + *cosInstance.Name
}

// Create the service credential if does not exist
klog.V(2).Infof("Auto Generating the COS Service credential for importing the image with name: %s", opt.ServiceCredName)
CreateServiceKeyRequest := controller.CreateServiceKeyRequest{
Name: opt.ServiceCredName,
SourceCRN: crn,
Parameters: map[string]interface{}{"HMAC": true},
}
newKey, err := bxCli.ResourceServiceKey.CreateKey(CreateServiceKeyRequest)
roleCRN := "crn:v1:bluemix:public:iam::::serviceRole:Writer"
params := &resourcecontrollerv2.ResourceKeyPostParameters{}
params.SetProperty("HMAC", true)

key, _, err := pvsClient.ResouceControllerClient.CreateResourceKey(
&resourcecontrollerv2.CreateResourceKeyOptions{
Name: ptr.To(opt.ServiceCredName),
Source: cosInstance.CRN,
Role: ptr.To(roleCRN),
Parameters: params,
},
)
if err != nil {
return err
return fmt.Errorf("unable to create resource key for service instance: %s", err.Error())
}

if key.Credentials != nil {
kishen-v marked this conversation as resolved.
Show resolved Hide resolved
if prop := key.Credentials.GetProperty("cos_hmac_keys"); prop != nil {
if hmacKeys, ok := prop.(map[string]interface{}); ok {
kishen-v marked this conversation as resolved.
Show resolved Hide resolved
accessKey = hmacKeys["access_key_id"].(string)
accessSecret = hmacKeys["secret_access_key"].(string)
} else {
return fmt.Errorf("type assertion for HMAC keys failed")
}
} else {
return fmt.Errorf("HMAC keys are not available with this instance")
}
} else {
return fmt.Errorf("no credentials associated with this instance")
}

if accessKey == "" || accessSecret == "" {
return fmt.Errorf("unknown error occurred setting HMAC credentials")
}
cred, ok = newKey.Credentials["cos_hmac_keys"].(map[string]interface{})
} else {
// Use the service credential already created
klog.V(2).Info("Reading the existing service credential")
cred, ok = keys[0].Credentials["cos_hmac_keys"].(map[string]interface{})

if prop := key.Credentials.GetProperty("cos_hmac_keys"); prop != nil {
klog.V(2).Info("Reading the existing service credential")
cred, ok := prop.(map[string]interface{})
if !ok {
return fmt.Errorf("cannot find cos_hmac_key")
}
accessKey = cred["access_key_id"].(string)
accessSecret = cred["secret_access_key"].(string)
}
}
if !ok {
return fmt.Errorf("failed to get the accessKey and secretKey from service credential")
}
//Assign the Access Key and Secret Key for further operation
opt.AccessKey = cred["access_key_id"].(string)
opt.SecretKey = cred["secret_access_key"].(string)
opt.AccessKey = accessKey
opt.SecretKey = accessSecret
}

pvmclient, err := client.NewPVMClientWithEnv(bxCli, opt.InstanceID, opt.InstanceName, pkg.Options.Environment)
pvmclient, err := client.NewPVMClientWithEnv(pvsClient, opt.InstanceID, opt.InstanceName, pkg.Options.Environment)
if err != nil {
return err
}
Expand Down Expand Up @@ -224,7 +242,6 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object rhel-83-100
klog.Infof("Image import for %s is currently in %s state, Please check the progress in the IBM cloud UI", *image.Name, *image.State)
return nil
}

pollErr = wait.PollImmediate(2*time.Minute, opt.WatchTimeout, func() (bool, error) {
img, err := pvmclient.ImgClient.Get(*image.ImageID)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/image/sync/mock/sync_client_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions cmd/image/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ func copyWorker(copyJobs <-chan copyWorkload, results chan<- bool, workerId int)
}

// Method to create the list of required instances
func createInstanceList(spec []pkg.Spec, bxCli *client.Client) ([]InstanceItem, error) {
func createInstanceList(spec []pkg.Spec, pvsClient *client.Client) ([]InstanceItem, error) {
var instanceList []InstanceItem
for _, item := range spec {
instance := InstanceItem{}
s3Cli, err := NewS3Client(bxCli, item.Source.Cos, item.Source.Region)
s3Cli, err := NewS3Client(pvsClient, item.Source.Cos, item.Source.Region)
if err != nil {
return nil, err
}

instance.Source = s3Cli
for _, targetItem := range item.Target {
s3Cli, err := NewS3Client(bxCli, item.Source.Cos, targetItem.Region)
s3Cli, err := NewS3Client(pvsClient, item.Source.Cos, targetItem.Region)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -257,7 +257,7 @@ Sample spec.yaml file:
start := time.Now()

//Create bluemix client
bxCli, err := client.NewClientWithEnv(apikey, pkg.Options.Environment, pkg.Options.Debug)
pvsClient, err := client.NewClientWithEnv(apikey, pkg.Options.Environment, pkg.Options.Debug)
if err != nil {
return err
}
Expand All @@ -269,7 +269,7 @@ Sample spec.yaml file:
}

// Create necessary objects
instanceList, err := createInstanceList(spec, bxCli)
instanceList, err := createInstanceList(spec, pvsClient)
if err != nil {
return err
}
Expand Down
Loading
Loading