Skip to content

Commit 44871d3

Browse files
authored
Merge pull request #43 from bkhadars/master
Fix: Verify s3 object exists or not
2 parents cbcbac2 + b08ec77 commit 44871d3

File tree

2 files changed

+67
-51
lines changed

2 files changed

+67
-51
lines changed

cmd/image/import/import.go

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> -r <REGION> --storage
4040
pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object-name rhel-83-10032020.ova.gz --image-name test-image --ostype <OSTYPE> -r <REGION>
4141
`,
4242
RunE: func(cmd *cobra.Command, args []string) error {
43+
var s3client *client.S3Client
4344
opt := pkg.ImageCMDOptions
4445
apikey := pkg.Options.APIKey
4546
//validate inputs
@@ -56,57 +57,64 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object-name rhel-83
5657
os.Exit(1)
5758
}
5859

59-
if opt.AccessKey == "" || opt.SecretKey == "" {
60-
auth, err := core.NewIamAuthenticator(apikey, "", "", "", false, nil)
61-
if err != nil {
62-
return err
63-
}
60+
bxCli, err := client.NewClient(apikey)
61+
if err != nil {
62+
return err
63+
}
6464

65-
bxCli, err := client.NewClient(apikey)
66-
if err != nil {
67-
return err
68-
}
65+
auth, err := core.NewIamAuthenticator(apikey, "", "", "", false, nil)
66+
if err != nil {
67+
return err
68+
}
6969

70-
resourceController, err := client.NewResourceControllerV2(&rcv2.ResourceControllerV2Options{
71-
Authenticator: auth,
72-
})
73-
if err != nil {
74-
return err
75-
}
70+
resourceController, err := client.NewResourceControllerV2(&rcv2.ResourceControllerV2Options{
71+
Authenticator: auth,
72+
})
73+
if err != nil {
74+
return err
75+
}
7676

77-
instances, _, err := resourceController.ResourceControllerV2.ListResourceInstances(resourceController.ResourceControllerV2.NewListResourceInstancesOptions().SetType("service_instance"))
78-
if err != nil {
79-
return err
80-
}
77+
instances, _, err := resourceController.ResourceControllerV2.ListResourceInstances(resourceController.ResourceControllerV2.NewListResourceInstancesOptions().SetType("service_instance"))
78+
if err != nil {
79+
return err
80+
}
8181

82-
// Step 1: Find where COS for the bucket
83-
cosOfBucket := func(resources []rcv2.ResourceInstance) *rcv2.ResourceInstance {
84-
for _, resource := range resources {
85-
if strings.Contains(*resource.Crn, "cloud-object-storage") {
86-
s3client, err := client.NewS3Client(bxCli, *resource.Name, opt.Region)
87-
if err != nil {
88-
continue
89-
}
90-
buckets, err := s3client.S3Session.ListBuckets(nil)
91-
if err != nil {
92-
continue
93-
}
94-
for _, bucket := range buckets.Buckets {
95-
if *bucket.Name == opt.BucketName {
96-
return &resource
97-
}
82+
// Step 1: Find where COS for the bucket
83+
cosOfBucket := func(resources []rcv2.ResourceInstance) *rcv2.ResourceInstance {
84+
for _, resource := range resources {
85+
if strings.Contains(*resource.Crn, "cloud-object-storage") {
86+
s3client, err = client.NewS3Client(bxCli, *resource.Name, opt.Region)
87+
if err != nil {
88+
continue
89+
}
90+
buckets, err := s3client.S3Session.ListBuckets(nil)
91+
if err != nil {
92+
continue
93+
}
94+
for _, bucket := range buckets.Buckets {
95+
if *bucket.Name == opt.BucketName {
96+
return &resource
9897
}
9998
}
10099
}
101-
return nil
102-
}(instances.Resources)
103-
104-
if cosOfBucket == nil {
105-
return fmt.Errorf("failed to find the COS instance for the bucket mentioned: %s", opt.BucketName)
106100
}
107-
klog.Infof("%s bucket found in the %s[ID:%s] COS instance", opt.BucketName, *cosOfBucket.Name, *cosOfBucket.ID)
101+
return nil
102+
}(instances.Resources)
108103

109-
// Step 2: Create the Service Credential in the found COS instance
104+
if cosOfBucket == nil {
105+
return fmt.Errorf("failed to find the COS instance for the bucket mentioned: %s", opt.BucketName)
106+
}
107+
klog.Infof("%s bucket found in the %s[ID:%s] COS instance", opt.BucketName, *cosOfBucket.Name, *cosOfBucket.ID)
108+
109+
//Step 2: Check if s3 object exists
110+
objectExists := s3client.CheckIfObjectExists(opt.BucketName, opt.ImageFilename)
111+
if !objectExists {
112+
return fmt.Errorf("failed to found the object %s in %s bucket", opt.ImageFilename, opt.BucketName)
113+
}
114+
klog.Infof("%s object found in the %s bucket\n", opt.ImageFilename, opt.BucketName)
115+
116+
if opt.AccessKey == "" || opt.SecretKey == "" {
117+
// Step 3: Check if Service Credential exists for the found COS instance
110118
keys, _, err := resourceController.ResourceControllerV2.ListResourceKeys(resourceController.ResourceControllerV2.NewListResourceKeysOptions().SetName(opt.ServiceCredName))
111119
if err != nil {
112120
return fmt.Errorf("failed to list the service credentials: %v", err)
@@ -147,18 +155,13 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> --object-name rhel-83
147155
return err
148156
}
149157

150-
// Step 3: Assign the Access Key and Secret Key for further operation
158+
// Step 4: Assign the Access Key and Secret Key for further operation
151159
opt.AccessKey = h.AccessKeyID
152160
opt.SecretKey = h.SecretKeyID
153161

154162
}
155163

156-
c, err := client.NewClient(apikey)
157-
if err != nil {
158-
return err
159-
}
160-
161-
pvmclient, err := client.NewPVMClient(c, opt.InstanceID, opt.InstanceName)
164+
pvmclient, err := client.NewPVMClient(bxCli, opt.InstanceID, opt.InstanceName)
162165
if err != nil {
163166
return err
164167
}

pkg/client/s3client.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func NewS3Client(c *Client, instanceName, region string) (s3client *S3Client, er
7575
}
7676

7777
//Func CheckBucketExists will verify for the existence of the bucket in the particular account
78-
func (c *S3Client) CheckBucketExists(bucketname string) (bool, error) {
78+
func (c *S3Client) CheckBucketExists(bucketName string) (bool, error) {
7979
result, err := c.S3Session.ListBuckets(nil)
8080
if err != nil {
8181
klog.Infof("Unable to list buckets, %v\n", err)
@@ -84,7 +84,7 @@ func (c *S3Client) CheckBucketExists(bucketname string) (bool, error) {
8484

8585
bucketExists := false
8686
for _, b := range result.Buckets {
87-
if aws.StringValue(b.Name) == bucketname {
87+
if aws.StringValue(b.Name) == bucketName {
8888
bucketExists = true
8989
}
9090
}
@@ -95,6 +95,19 @@ func (c *S3Client) CheckBucketExists(bucketname string) (bool, error) {
9595
return false, nil
9696
}
9797

98+
func (c *S3Client) CheckIfObjectExists(bucketName, objectName string) bool {
99+
input := &s3.GetObjectInput{
100+
Bucket: aws.String(bucketName),
101+
Key: aws.String(objectName),
102+
}
103+
104+
_, err := c.S3Session.GetObject(input)
105+
if err != nil {
106+
return false
107+
}
108+
return true
109+
}
110+
98111
//To create a new bucket in the provided instance
99112
func (c *S3Client) CreateBucket(bucketName string) error {
100113
_, err := c.S3Session.CreateBucket(&s3.CreateBucketInput{

0 commit comments

Comments
 (0)