upload

package
v0.1.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UseExistingPromptMessage = "Would you like to use an existing COS Instance for creating bucket?"
	CreatePromptMessage      = "Would you like to create a new COS Instance?"
	ResourceGroupAPIRegion   = "global"
)

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "upload",
	Short: "Upload the image to the IBM COS",
	Long: `Upload the image to the IBM COS
pvsadm image upload --help for information

# Set the API key or feed the --api-key commandline argument
export IBMCLOUD_APIKEY=<IBMCLOUD_APIKEY>

Examples:

# using InstanceName
pvsadm image upload --bucket bucket0711 -f rhcos-461.ova.gz --instance-name pvsadm-cos-instance

#If user is planning to use available cos instance
pvsadm image upload  --bucket bucket0911 -f rhcos-461.ova.gz

#If user intents to create a new COS instance
pvsadm image upload --bucket bucket1320 -f centos-8-latest.ova.gz --resource-group <ResourceGroup_Name>

#if user is planning to create a bucket in particular region
pvsadm image upload --bucket bucket1320 -f centos-8-latest.ova.gz --bucket-region <Region>

#If user likes to give different name to s3 Object
pvsadm image upload --bucket bucket1320 -f centos-8-latest.ova.gz -o centos8latest.ova.gz

#upload using accesskey and secret key
pvsadm image upload --bucket bucket1320 -f centos-8-latest.ova.gz --bucket-region <Region> --accesskey <ACCESSKEY> --secretkey <SECRETKEY>
`,
	PreRunE: func(cmd *cobra.Command, args []string) error {

		if pkg.Options.Environment == "test" {
			return fmt.Errorf("image upload in test/staging env storage bucket is not supported")
		}

		if (len(pkg.ImageCMDOptions.AccessKey) > 0) != (len(pkg.ImageCMDOptions.SecretKey) > 0) {
			return fmt.Errorf("required both --accesskey and --secretkey values")
		}

		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		var s3Cli *client.S3Client
		var bucketExists bool
		opt := pkg.ImageCMDOptions

		if opt.ObjectName == "" {
			opt.ObjectName = filepath.Base(opt.ImageName)
		}

		if pkg.ImageCMDOptions.AccessKey != "" && pkg.ImageCMDOptions.SecretKey != "" {
			s3Cli, err := client.NewS3ClientWithKeys(pkg.ImageCMDOptions.AccessKey, pkg.ImageCMDOptions.SecretKey, opt.Region)
			if err != nil {
				return err
			}

			objectExists, err := s3Cli.CheckIfObjectExists(opt.BucketName, opt.ObjectName)
			if err != nil {
				return err
			}
			if objectExists {
				return fmt.Errorf("%s object already exists in the %s bucket", opt.ObjectName, opt.BucketName)
			}

			return s3Cli.UploadObject(opt.ImageName, opt.ObjectName, opt.BucketName)

		}

		pvsClient, err := client.NewClientWithEnv(pkg.Options.APIKey, pkg.Options.Environment, pkg.Options.Debug)
		if err != nil {
			return err
		}

		instances, err := pvsClient.ListServiceInstances(utils.CosResourceID)
		if err != nil {
			return err
		}

		if opt.COSInstanceName != "" {
			s3Cli, err = client.NewS3Client(pvsClient, opt.COSInstanceName, opt.Region)
			if err != nil {
				return err
			}

			bucketExists, err = s3Cli.CheckBucketExists(opt.BucketName)
			if err != nil {
				return err
			}

			if !bucketExists {
				klog.Infof("Bucket %s not found in the instance %s provided", opt.BucketName, opt.COSInstanceName)
			}

		} else if len(instances) != 0 {

			for instanceName := range instances {
				s3Cli, err = client.NewS3Client(pvsClient, instanceName, opt.Region)
				if err != nil {
					return err
				}

				bucketExists, err = s3Cli.CheckBucketExists(opt.BucketName)
				if err != nil {
					return err
				}

				if bucketExists {
					opt.COSInstanceName = instanceName
					klog.Infof("Found bucket %s in the %s instance", opt.BucketName, opt.COSInstanceName)
					break
				}
			}
		} else if len(instances) == 0 {
			klog.Info("No active Cloud Object Storage instances were found in the account")
		}

		if opt.COSInstanceName == "" && len(instances) != 0 {
			klog.Infof("Bucket %s not found in the account provided", opt.BucketName)
			if utils.AskConfirmation(UseExistingPromptMessage) {
				var availableInstances []string
				for name := range instances {
					availableInstances = append(availableInstances, name)
				}
				opt.COSInstanceName, err = utils.SelectItem("Select Cloud Object Storage Instance:", availableInstances)
				if err != nil {
					return err
				}
				klog.Infof("Selected InstanceName is %s", opt.COSInstanceName)
			}
		}

		if opt.COSInstanceName == "" {
			if !utils.AskConfirmation(CreatePromptMessage) {
				return fmt.Errorf("create Cloud Object Storage instance either offline or use the pvsadm command")
			}
			if opt.ResourceGrp == "" {
				rmv2ListResourceGroupOpt := resourcemanagerv2.ListResourceGroupsOptions{AccountID: ptr.To(pvsClient.User.Account)}
				resourceGroupList, _, err := pvsClient.ResourceManagerClient.ListResourceGroups(&rmv2ListResourceGroupOpt)
				if err != nil {
					return fmt.Errorf("failed to list resource groups: %v", err)
				}

				var resourceGroupNames []string
				for _, resgrp := range resourceGroupList.Resources {
					resourceGroupNames = append(resourceGroupNames, *resgrp.Name)
				}

				opt.ResourceGrp, err = utils.SelectItem("Select a Resource Group having required permissions for creating a COS instance:", resourceGroupNames)
				if err != nil {
					return err
				}
			}

			opt.COSInstanceName = utils.ReadUserInput("Enter the name of the Cloud Object Storage instance to be created:")
			klog.Infof("Creating a new COS instance: %s", opt.COSInstanceName)
			_, err = pvsClient.CreateServiceInstance(opt.COSInstanceName, utils.ServiceTypeCloudObjectStorage, utils.RetrieveValFromMap(utils.CosResourcePlanIDs, opt.ServicePlan),
				opt.ResourceGrp, ResourceGroupAPIRegion)
			if err != nil {
				return err
			}
		}

		s3Cli, err = client.NewS3Client(pvsClient, opt.COSInstanceName, opt.Region)
		if err != nil {
			return err
		}

		if !bucketExists {
			klog.Infof("Creating a new bucket: %s", opt.BucketName)
			s3Cli, err = client.NewS3Client(pvsClient, opt.COSInstanceName, opt.Region)
			if err != nil {
				return err
			}

			err = s3Cli.CreateBucket(opt.BucketName)
			if err != nil {
				return err
			}
		}

		objectExists, err := s3Cli.CheckIfObjectExists(opt.BucketName, opt.ObjectName)
		if err != nil {
			return err
		}
		if objectExists {
			return fmt.Errorf("%s object already exists in the %s bucket", opt.ObjectName, opt.BucketName)
		}

		err = s3Cli.UploadObject(opt.ImageName, opt.ObjectName, opt.BucketName)
		if err != nil {
			return err
		}
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL