@@ -40,6 +40,7 @@ pvsadm image import -n upstream-core-lon04 -b <BUCKETNAME> -r <REGION> --storage
4040pvsadm 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 }
0 commit comments