@@ -9,13 +9,14 @@ import (
99 "log"
1010 "sort"
1111 "strings"
12+ "time"
1213
13- "github.com/IBM-Cloud/bluemix-go/crn"
14- "github.com/IBM-Cloud/bluemix-go/models"
1514 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
1615 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1716 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
1817
18+ "github.com/IBM/platform-services-go-sdk/iampolicymanagementv1"
19+ rc "github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
1920 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2021)
2122
@@ -111,29 +112,32 @@ func DataSourceIBMResourceKeyValidator() *validate.ResourceValidator {
111112}
112113
113114func dataSourceIBMResourceKeyRead (d * schema.ResourceData , meta interface {}) error {
114- rsContClient , err := meta .(conns.ClientSession ).ResourceControllerAPI ()
115+ rsContClient , err := meta .(conns.ClientSession ).ResourceControllerV2API ()
115116 if err != nil {
116117 return err
117118 }
118- rkAPI := rsContClient .ResourceServiceKey ()
119119 name := d .Get ("name" ).(string )
120120 mostRecent := d .Get ("most_recent" ).(bool )
121121
122- keys , err := rkAPI .GetKeys (name )
122+ resourceKeys := rc.ListResourceKeysOptions {
123+ Name : & name ,
124+ }
125+
126+ keys , _ , err := rsContClient .ListResourceKeys (& resourceKeys )
123127 if err != nil {
124128 return err
125129 }
126- var filteredKeys []models. ServiceKey
130+ var filteredKeys []rc. ResourceKey
127131
128132 if d .Get ("resource_instance_id" ) == "" {
129- filteredKeys = keys
133+ filteredKeys = keys . Resources
130134 } else {
131135 crn , err := getCRN (d , meta )
132- if err != nil {
136+ if err != nil || crn == nil {
133137 return err
134138 }
135- for _ , key := range keys {
136- if key .SourceCrn == * crn {
139+ for _ , key := range keys . Resources {
140+ if & key .CRN == & crn {
137141 filteredKeys = append (filteredKeys , key )
138142 }
139143 }
@@ -144,7 +148,7 @@ func dataSourceIBMResourceKeyRead(d *schema.ResourceData, meta interface{}) erro
144148 return fmt .Errorf ("[ERROR] No resource keys found with name [%s]" , name )
145149 }
146150
147- var key models. ServiceKey
151+ var key rc. ResourceKey
148152
149153 if len (filteredKeys ) > 1 {
150154 if mostRecent {
@@ -158,20 +162,50 @@ func dataSourceIBMResourceKeyRead(d *schema.ResourceData, meta interface{}) erro
158162 key = filteredKeys [0 ]
159163 }
160164
161- d .SetId (key .ID )
165+ d .SetId (* key .ID )
162166
163- if redacted , ok := key .Credentials [ "redacted" ].( string ); ok {
164- log .Printf ("Credentials are redacted with code: %s.The User doesn't have the correct access to view the credentials. Refer to the API documentation for additional details." , redacted )
167+ if key . Credentials != nil && key .Credentials . Redacted != nil {
168+ log .Printf ("Credentials are redacted with code: %s.The User doesn't have the correct access to view the credentials. Refer to the API documentation for additional details." , * key . Credentials . Redacted )
165169 }
166- if roleCrn , ok := key .Parameters ["role_crn" ].(string ); ok {
167- d .Set ("role" , roleCrn [strings .LastIndex (roleCrn , ":" )+ 1 :])
168- } else if roleCrn , ok := key .Credentials ["iam_role_crn" ].(string ); ok {
169- d .Set ("role" , roleCrn [strings .LastIndex (roleCrn , ":" )+ 1 :])
170+
171+ if key .Credentials != nil && key .Credentials .IamRoleCRN != nil {
172+ roleCrn := * key .Credentials .IamRoleCRN
173+ iamPolicyManagementClient , err := meta .(conns.ClientSession ).IAMPolicyManagementV1API ()
174+ if err == nil {
175+ var resourceCRN string
176+ if key .CRN != nil {
177+ serviceName := strings .Split (* key .CRN , ":" )
178+ if len (serviceName ) > 4 {
179+ resourceCRN = serviceName [4 ]
180+ }
181+ }
182+ listRoleOptions := & iampolicymanagementv1.ListRolesOptions {
183+ AccountID : key .AccountID ,
184+ ServiceName : & resourceCRN ,
185+ }
186+ roleList , resp , err := iamPolicyManagementClient .ListRoles (listRoleOptions )
187+ roles := flex .MapRoleListToPolicyRoles (* roleList )
188+ if err == nil && len (roles ) > 0 {
189+ for _ , role := range roles {
190+ if * role .RoleID == roleCrn {
191+ RoleName := role .DisplayName
192+ d .Set ("role" , RoleName )
193+ }
194+ }
195+ }
196+ if err != nil {
197+ log .Printf ("[ERROR] Error listing IAM Roles %s, %s" , err , resp )
198+ }
199+ }
170200 }
171201
172202 // ### Modification for onetime_credientails
173203 d .Set ("onetime_credentials" , key .OnetimeCredentials )
174- d .Set ("credentials" , flex .Flatten (key .Credentials ))
204+ var credInterface map [string ]interface {}
205+ cred , _ := json .Marshal (key .Credentials )
206+ json .Unmarshal (cred , & credInterface )
207+ d .Set ("credentials" , flex .Flatten (credInterface ))
208+
175209 creds , err := json .Marshal (key .Credentials )
176210 if err != nil {
177211 return fmt .Errorf ("[ERROR] Error marshalling resource key credentials: %s" , err )
@@ -180,45 +214,57 @@ func dataSourceIBMResourceKeyRead(d *schema.ResourceData, meta interface{}) erro
180214 return fmt .Errorf ("[ERROR] Error setting the credentials json: %s" , err )
181215 }
182216 d .Set ("status" , key .State )
183- d .Set ("crn" , key .Crn . String () )
217+ d .Set ("crn" , key .CRN )
184218 return nil
185219}
186220
187- func getCRN (d * schema.ResourceData , meta interface {}) (* crn. CRN , error ) {
221+ func getCRN (d * schema.ResourceData , meta interface {}) (* string , error ) {
188222
189- rsContClient , err := meta .(conns.ClientSession ).ResourceControllerAPI ()
223+ rsConClient , err := meta .(conns.ClientSession ).ResourceControllerV2API ()
190224 if err != nil {
191225 return nil , err
192226 }
193227
194228 if insID , ok := d .GetOk ("resource_instance_id" ); ok {
195- instance , err := rsContClient .ResourceServiceInstance ().GetInstance (insID .(string ))
229+ insIdString := insID .(string )
230+ resourceInstanceGet := rc.GetResourceInstanceOptions {
231+ ID : & insIdString ,
232+ }
233+ instance , resp , err := rsConClient .GetResourceInstance (& resourceInstanceGet )
196234 if err != nil {
197- return nil , err
235+ return nil , fmt . Errorf ( "[ERROR] Error retrieving resource instance: %s with resp code: %s" , err , resp )
198236 }
199- return & ( instance .Crn ) , nil
237+ return instance .CRN , nil
200238
201239 }
240+ if insID , ok := d .GetOk ("resource_alias_id" ); ok {
241+ insaliasIdString := insID .(string )
242+ resourceInstanceAliasGet := rc.GetResourceAliasOptions {
243+ ID : & insaliasIdString ,
244+ }
245+ instance , resp , err := rsConClient .GetResourceAlias (& resourceInstanceAliasGet )
246+ if err != nil {
247+ return nil , fmt .Errorf ("[ERROR] Error retrieving resource instance: %s with resp code: %s" , err , resp )
248+ }
249+ return instance .CRN , nil
202250
203- alias , err := rsContClient .ResourceServiceAlias ().Alias (d .Get ("resource_alias_id" ).(string ))
204- if err != nil {
205- return nil , err
206251 }
207- return & (alias .CRN ), nil
252+
253+ return nil , nil
208254
209255}
210256
211- type resourceKeys []models. ServiceKey
257+ type resourceKeys []rc. ResourceKey
212258
213259func (k resourceKeys ) Len () int { return len (k ) }
214260
215261func (k resourceKeys ) Swap (i , j int ) { k [i ], k [j ] = k [j ], k [i ] }
216262
217263func (k resourceKeys ) Less (i , j int ) bool {
218- return (* k [i ].CreatedAt ).Before (* k [j ].CreatedAt )
264+ return (time . Time ( * k [i ].CreatedAt )) .Before (time . Time ( * k [j ].CreatedAt ) )
219265}
220266
221- func mostRecentResourceKey (keys resourceKeys ) models. ServiceKey {
267+ func mostRecentResourceKey (keys resourceKeys ) rc. ResourceKey {
222268 sortedKeys := keys
223269 sort .Sort (sortedKeys )
224270 return sortedKeys [len (sortedKeys )- 1 ]
0 commit comments