@@ -5,13 +5,14 @@ package cis
55
66import (
77 "net/url"
8+ "reflect"
89
9- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10-
11- "github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
12- "github.com/IBM-Cloud/bluemix-go/models"
1310 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
1411 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
12+ "github.com/IBM/platform-services-go-sdk/globalcatalogv1"
13+ rc "github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
14+ rg "github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
15+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516)
1617
1718func DataSourceIBMCISInstance () * schema.Resource {
@@ -93,56 +94,59 @@ func DataSourceIBMCISInstance() *schema.Resource {
9394}
9495
9596func dataSourceIBMCISInstanceRead (d * schema.ResourceData , meta interface {}) error {
96- rsConClient , err := meta .(conns.ClientSession ).ResourceControllerAPIV2 ()
97+ var instance rc.ResourceInstance
98+ rsConClient , err := meta .(conns.ClientSession ).ResourceControllerV2API ()
9799 if err != nil {
98100 return err
99101 }
100- rsAPI := rsConClient .ResourceServiceInstanceV2 ()
101102 name := d .Get ("name" ).(string )
102103
103- rsInstQuery := controllerv2. ServiceInstanceQuery {
104- Name : name ,
104+ resourceInstanceListOptions := rc. ListResourceInstancesOptions {
105+ Name : & name ,
105106 }
106107
107108 if rsGrpID , ok := d .GetOk ("resource_group_id" ); ok {
108- rsInstQuery .ResourceGroupID = rsGrpID .(string )
109+ rg := rsGrpID .(string )
110+ resourceInstanceListOptions .ResourceGroupID = & rg
109111 } else {
110112 defaultRg , err := flex .DefaultResourceGroup (meta )
111113 if err != nil {
112114 return err
113115 }
114- rsInstQuery .ResourceGroupID = defaultRg
115- }
116-
117- rsCatClient , err := meta .(conns.ClientSession ).ResourceCatalogAPI ()
118- if err != nil {
119- return err
116+ resourceInstanceListOptions .ResourceGroupID = & defaultRg
120117 }
121- rsCatRepo := rsCatClient .ResourceCatalog ()
122118
123119 if service , ok := d .GetOk ("service" ); ok {
124-
125- serviceOff , err := rsCatRepo .FindByName (service .(string ), true )
120+ name := service .(string )
121+ resourceInstanceListOptions .ResourceID = & name
122+ }
123+ next_url := ""
124+ var instances []rc.ResourceInstance
125+ for {
126+ if next_url != "" {
127+ resourceInstanceListOptions .Start = & next_url
128+ }
129+ listInstanceResponse , resp , err := rsConClient .ListResourceInstances (& resourceInstanceListOptions )
126130 if err != nil {
127- return flex .FmtErrorf ("[ERROR] Error retrieving service offering: %s" , err )
131+ return flex .FmtErrorf ("[ERROR] Error retrieving resource instance: %s with resp code: %s" , err , resp )
132+ }
133+ next_url , err = getInstancesNext (listInstanceResponse .NextURL )
134+ if err != nil {
135+ return flex .FmtErrorf ("[DEBUG] ListResourceInstances failed. Error occurred while parsing NextURL: %s" , err )
136+ }
137+ instances = append (instances , listInstanceResponse .Resources ... )
138+ if next_url == "" {
139+ break
128140 }
129-
130- rsInstQuery .ServiceID = serviceOff [0 ].ID
131141 }
132142
133- var instances []models.ServiceInstanceV2
134-
135- instances , err = rsAPI .ListInstances (rsInstQuery )
136- if err != nil {
137- return err
138- }
139- var filteredInstances []models.ServiceInstanceV2
143+ var filteredInstances []rc.ResourceInstance
140144 var location string
141145
142146 if loc , ok := d .GetOk ("location" ); ok {
143147 location = loc .(string )
144148 for _ , instance := range instances {
145- if flex .GetLocation (instance ) == location {
149+ if flex .GetLocationV2 (instance ) == location {
146150 filteredInstances = append (filteredInstances , instance )
147151 }
148152 }
@@ -154,41 +158,75 @@ func dataSourceIBMCISInstanceRead(d *schema.ResourceData, meta interface{}) erro
154158 return flex .FmtErrorf ("[ERROR] No resource instance found with name [%s]\n If not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service" , name )
155159 }
156160
157- var instance models.ServiceInstanceV2
158-
159161 if len (filteredInstances ) > 1 {
160162 return flex .FmtErrorf ("[ERROR] More than one resource instance found with name matching [%s]\n If not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service" , name )
161163 }
162164 instance = filteredInstances [0 ]
163165
164- d .SetId (instance .ID )
166+ d .SetId (* instance .ID )
165167 d .Set ("status" , instance .State )
166168 d .Set ("resource_group_id" , instance .ResourceGroupID )
167169 d .Set ("location" , instance .RegionID )
168- d .Set ("guid" , instance .Guid )
169- serviceOff , err := rsCatRepo . GetServiceName ( instance . ServiceID )
170+ d .Set ("guid" , instance .GUID )
171+ globalClient , err := meta .(conns. ClientSession ). GlobalCatalogV1API ( )
170172 if err != nil {
171- return flex . FmtErrorf ( "[ERROR] Error retrieving service offering: %s" , err )
173+ return err
172174 }
175+ options := globalcatalogv1.GetCatalogEntryOptions {
173176
174- d .Set ("service" , serviceOff )
177+ ID : instance .ResourceID ,
178+ }
179+ service , _ , err := globalClient .GetCatalogEntry (& options )
180+ if err != nil {
181+ return flex .FmtErrorf ("[ERROR] Error retrieving service offering: %s" , err )
182+ }
183+ d .Set ("service" , service .Name )
184+ planOptions := globalcatalogv1.GetCatalogEntryOptions {
175185
176- servicePlan , err := rsCatRepo .GetServicePlanName (instance .ResourcePlanID )
186+ ID : instance .ResourcePlanID ,
187+ }
188+ plan , _ , err := globalClient .GetCatalogEntry (& planOptions )
177189 if err != nil {
178190 return flex .FmtErrorf ("[ERROR] Error retrieving plan: %s" , err )
179191 }
180- d .Set ("plan" , servicePlan )
192+ d .Set ("plan" , plan . Name )
181193
182194 d .Set (flex .ResourceName , instance .Name )
183- d .Set (flex .ResourceCRN , instance .Crn . String () )
195+ d .Set (flex .ResourceCRN , instance .CRN )
184196 d .Set (flex .ResourceStatus , instance .State )
185- d .Set (flex .ResourceGroupName , instance .ResourceGroupName )
197+
198+ rMgtClient , err := meta .(conns.ClientSession ).ResourceManagerV2API ()
199+ if err != nil {
200+ return err
201+ }
202+ GetResourceGroup := rg.GetResourceGroupOptions {
203+ ID : instance .ResourceGroupID ,
204+ }
205+ resourceGroup , resp , err := rMgtClient .GetResourceGroup (& GetResourceGroup )
206+ if err != nil || resourceGroup == nil {
207+ flex .FmtErrorf ("[ERROR] Error retrieving resource group: %s %s" , err , resp )
208+ }
209+ if resourceGroup != nil && resourceGroup .Name != nil {
210+ d .Set (flex .ResourceGroupName , resourceGroup .Name )
211+ }
186212
187213 rcontroller , err := flex .GetBaseController (meta )
188214 if err != nil {
189215 return err
190216 }
191- d .Set (flex .ResourceControllerURL , rcontroller + "/internet-svcs/" + url .QueryEscape (instance .Crn . String () ))
217+ d .Set (flex .ResourceControllerURL , rcontroller + "/internet-svcs/" + url .QueryEscape (* instance .CRN ))
192218
193219 return nil
194220}
221+
222+ func getInstancesNext (next * string ) (string , error ) {
223+ if reflect .ValueOf (next ).IsNil () {
224+ return "" , nil
225+ }
226+ u , err := url .Parse (* next )
227+ if err != nil {
228+ return "" , err
229+ }
230+ q := u .Query ()
231+ return q .Get ("next_url" ), nil
232+ }
0 commit comments