upload

package
v0.1.5-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ServiceType              = "cloud-object-storage"
	UseExistingPromptMessage = "Would You Like to use Available COS Instance for creating bucket?"
	CreatePromptMessage      = "Would you like to create 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_API_KEY=<IBM_CLOUD_API_KEY>

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 --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
`,
	RunE: func(cmd *cobra.Command, args []string) error {
		var s3Cli *client.S3Client
		var bucketExists bool = false
		var apikey string = pkg.Options.APIKey
		opt := pkg.ImageCMDOptions

		bxCli, err := client.NewClientWithEnv(apikey, pkg.Options.Environment, pkg.Options.Debug)

		if err != nil {
			return err
		}

		instances, err := bxCli.ListServiceInstances(ServiceType)
		if err != nil {
			return err
		}

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

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

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

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

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

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

		if opt.InstanceName == "" {
			if !utils.AskConfirmation(CreatePromptMessage) {
				return fmt.Errorf("Create Cloud Object Storage instance either offline or use the pvsadm command\n")
			}
			if opt.ResourceGrp == "" {
				resourceGroupQuery := management.ResourceGroupQuery{
					AccountID: bxCli.User.Account,
				}

				resGrpList, err := bxCli.ResGroupAPI.List(&resourceGroupQuery)
				if err != nil {
					return err
				}

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

				opt.ResourceGrp = utils.SelectItem("Select ResourceGroup having required permissions for creating a service instance from the below:", resourceGroupNames)
			}

			opt.InstanceName = utils.ReadUserInput("Type Name of the Cloud Object Storage instance:")
			klog.Infof("Creating a new cos %s instance\n", opt.InstanceName)

			_, err = bxCli.CreateServiceInstance(opt.InstanceName, ServiceType, opt.ServicePlan,
				opt.ResourceGrp, ResourceGroupAPIRegion)
			if err != nil {
				return err
			}
		}

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

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

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

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

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

		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