Skip to content

Commit

Permalink
Merge pull request #79 from bkhadars/s3_object_check1
Browse files Browse the repository at this point in the history
Fix: prevent existing image overwrite in IBM cloud object store
  • Loading branch information
ltccci authored Feb 2, 2021
2 parents 80ccbeb + 54a3f71 commit ac8ba93
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 170 deletions.
120 changes: 64 additions & 56 deletions cmd/image/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package upload

import (
"fmt"
"path/filepath"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/client/s3utils"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -52,115 +54,120 @@ pvsadm image upload --bucket bucket1320 -f centos-8-latest.ova.gz --resource-gro
#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
apikey := pkg.Options.APIKey
var bucketExists bool = false
var apikey string = pkg.Options.APIKey
opt := pkg.ImageCMDOptions
bucketExists := false

//Create bluemix client
bxCli, err := client.NewClient(apikey)
if err != nil {
return err
}
instances, err := s3utils.GetInstances(bxCli, ServiceType)

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

instanceExists := len(instances) != 0

//check if bucket exists
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 instanceExists {
} else if len(instances) != 0 {
//check for bucket across the instances
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 {
klog.Infof("Found bucket %s in the %s instance\n", opt.BucketName, instanceName)
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 !bucketExists {
klog.Infof("bucket %s not found in the account provided\n", opt.BucketName)
//if bucket doesn't exists,ask user if he wants to use existing cos instance
if s3utils.AskYesOrNo(UseExistingPromptMessage, 3) {
//List of Available COS instances
klog.Infof("Select a COS Instance\n")
instanceNames := []string{}
for name, _ := range instances {
instanceNames = append(instanceNames, name)
}
count := 0
for _, name := range instanceNames {
fmt.Printf("%d. %s (%s)\n", count, name, instances[name])
count = count + 1
}
input := s3utils.SelectCosInstance(len(instanceNames), 3)
if input == -1 {
return fmt.Errorf("Please select a valid COS Instance\n")
}
opt.InstanceName = instanceNames[input]
klog.Infof("Selected InstanceName is %s\n", opt.InstanceName)
s3Cli, err = client.NewS3Client(bxCli, opt.InstanceName, opt.Region)
} else {
if s3utils.AskYesOrNo(CreatePromptMessage, 3) {
name := s3utils.ReadInstanceNameFromUser()
klog.Infof("Creating a new cos %s instance\n", name)
_, err = client.CreateServiceInstance(bxCli.Session, name, ServiceType, opt.ServicePlan,
opt.ResourceGrp, ResourceGroupAPIRegion)
if err != nil {
return err
}
s3Cli, err = client.NewS3Client(bxCli, name, opt.Region)
if err != nil {
return err
}
} else {
return fmt.Errorf("please create cos instance either offline or use the pvsadm command\n")
}
// Ask if user likes to use existing instance
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)
}
} else {
name := s3utils.ReadInstanceNameFromUser()
klog.Infof("Creating a new cos %s instance\n", name)
_, err = client.CreateServiceInstance(bxCli.Session, name, ServiceType, opt.ServicePlan,
opt.ResourceGrp, ResourceGroupAPIRegion)
if err != nil {
return err
}

//Create a new instance
if opt.InstanceName == "" {
if !utils.AskConfirmation(CreatePromptMessage) {
return fmt.Errorf("Create Cloud Object Storage instance either offline or use the pvsadm command\n")
}
s3Cli, err = client.NewS3Client(bxCli, name, opt.Region)
opt.InstanceName = utils.ReadUserInput("Type Name of the Cloud Object Storage instance:")
klog.Infof("Creating a new cos %s instance\n", opt.InstanceName)
_, err = client.CreateServiceInstance(bxCli.Session, opt.InstanceName, ServiceType, opt.ServicePlan,
opt.ResourceGrp, ResourceGroupAPIRegion)
if err != nil {
return err
}
}

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

//Check if object exists or not
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)
}

//Create a new bucket
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
}
}

//upload the Image to S3 bucket
err = s3Cli.UploadObject(opt.ImageName, opt.BucketName)
err = s3Cli.UploadObject(opt.ImageName, opt.ObjectName, opt.BucketName)
if err != nil {
return err
}
Expand All @@ -170,10 +177,11 @@ pvsadm image upload --bucket bucket1320 -f centos-8-latest.ova.gz --region <Regi

func init() {
Cmd.Flags().StringVar(&pkg.ImageCMDOptions.ResourceGrp, "resource-group", "default", "Name of user resource group.")
Cmd.Flags().StringVar(&pkg.ImageCMDOptions.ServicePlan, "cos-storageclass", "standard", "Cloud Object Storage Class type, available values are [standard, smart, cold, vault].")
Cmd.Flags().StringVar(&pkg.ImageCMDOptions.ServicePlan, "cos-serviceplan", "standard", "Cloud Object Storage Class type, available values are [standard, lite].")
Cmd.Flags().StringVarP(&pkg.ImageCMDOptions.InstanceName, "cos-instance-name", "n", "", "Cloud Object Storage instance name.")
Cmd.Flags().StringVarP(&pkg.ImageCMDOptions.BucketName, "bucket", "b", "", "Cloud Object Storage bucket name.")
Cmd.Flags().StringVarP(&pkg.ImageCMDOptions.ImageName, "file", "f", "", "The PATH to the file to upload.")
Cmd.Flags().StringVarP(&pkg.ImageCMDOptions.ObjectName, "cos-object-name", "o", "", "Cloud Object Storage Object Name(Default: filename from --file|-f option)")
Cmd.Flags().StringVarP(&pkg.ImageCMDOptions.Region, "bucket-region", "r", "us-south", "Cloud Object Storage bucket region.")
_ = Cmd.MarkFlagRequired("bucket")
_ = Cmd.MarkFlagRequired("file")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ppc64le-cloud/pvsadm
go 1.15

require (
github.com/AlecAivazis/survey/v2 v2.2.7
github.com/IBM-Cloud/bluemix-go v0.0.0-20200921095234-26d1d0148c62
github.com/IBM-Cloud/power-go-client v1.0.51
github.com/IBM/go-sdk-core/v4 v4.8.2
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/AlecAivazis/survey v1.8.8 h1:Y4yypp763E8cbqb5RBqZhGgkCFLRFnbRBHrxnpMMsgQ=
github.com/AlecAivazis/survey/v2 v2.2.7 h1:5NbxkF4RSKmpywYdcRgUmos1o+roJY8duCLZXbVjoig=
github.com/AlecAivazis/survey/v2 v2.2.7/go.mod h1:9DYvHgXtiXm6nCn+jXnOXLKbH+Yo9u8fAS/SduGdoPk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/IBM-Cloud/bluemix-go v0.0.0-20200921095234-26d1d0148c62 h1:8I8Z3mRorZhgT31vXPysZNuVQV7vWR3GSVtmh8xJ9L8=
github.com/IBM-Cloud/bluemix-go v0.0.0-20200921095234-26d1d0148c62/go.mod h1:gPJbH1etcDj7qS/hBRiLuYW9CY0bRcostSKusa51xR0=
Expand All @@ -11,6 +14,7 @@ github.com/IBM/ibm-cos-sdk-go v1.5.0/go.mod h1:4mqv/ejW1PKW+Ij6ytU+W8j1UZeLmSEsR
github.com/IBM/platform-services-go-sdk v0.14.4 h1:rK+PhNaTp2Bb5+okvi2ecwC4sHyQxAxbNTPTeLSTz9k=
github.com/IBM/platform-services-go-sdk v0.14.4/go.mod h1:tPxyWC+HYj9WCqCyPv3RpOeO9q27DXxxPriDl8e3z4U=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
Expand Down Expand Up @@ -215,6 +219,7 @@ github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand All @@ -233,6 +238,8 @@ github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand All @@ -248,6 +255,7 @@ github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand All @@ -269,11 +277,17 @@ github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsI
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down Expand Up @@ -357,6 +371,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
Expand Down Expand Up @@ -392,6 +407,7 @@ golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190617133340-57b3e21c3d56/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -437,11 +453,13 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f h1:25KHgbfyiSm6vwQLbM3zZIe1v9p/3ea4Rz+nnM5K/i4=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
20 changes: 20 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ func NewClient(apikey string) (*Client, error) {
c.ResourceClient = ctrlv2.ResourceServiceInstanceV2()
return c, nil
}

//Func GetInstances, list all available instances of particular servicetype
func (c *Client) GetInstances(serviceType string) (map[string]string, error) {
svcs, err := c.ResourceClient.ListInstances(controllerv2.ServiceInstanceQuery{
Type: "service_instance",
})

if err != nil {
return nil, err
}

instances := make(map[string]string)

for _, svc := range svcs {
if svc.Crn.ServiceName == serviceType {
instances[svc.Name] = svc.Guid
}
}
return instances, nil
}
18 changes: 9 additions & 9 deletions pkg/client/s3client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package client

import (
"fmt"
"os"
"time"

"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
"github.com/IBM/ibm-cos-sdk-go/aws"
"github.com/IBM/ibm-cos-sdk-go/aws/credentials/ibmiam"
Expand All @@ -24,9 +27,6 @@ import (
"github.com/IBM/ibm-cos-sdk-go/service/s3/s3manager"
"github.com/ppc64le-cloud/pvsadm/pkg"
"k8s.io/klog/v2"
"os"
"path/filepath"
"time"
)

type S3Client struct {
Expand Down Expand Up @@ -140,12 +140,12 @@ func (c *S3Client) CreateBucket(bucketName string) error {
}

//To upload a object to S3 bucket
func (c *S3Client) UploadObject(object, bucketName string) error {
klog.Infof("uploading the object %s\n", object)
//Read the content of the object
file, err := os.Open(object)
func (c *S3Client) UploadObject(fileName, objectName, bucketName string) error {
klog.Infof("uploading the file %s\n", fileName)
//Read the content of the file
file, err := os.Open(fileName)
if err != nil {
return fmt.Errorf("err opening file %s: %s", object, err)
return fmt.Errorf("err opening file %s: %s", fileName, err)
}
defer file.Close()

Expand All @@ -157,7 +157,7 @@ func (c *S3Client) UploadObject(object, bucketName string) error {
// Upload input parameters
upParams := &s3manager.UploadInput{
Bucket: aws.String(bucketName),
Key: aws.String(filepath.Base(object)),
Key: aws.String(objectName),
Body: file,
}

Expand Down
Loading

0 comments on commit ac8ba93

Please sign in to comment.