/
data_source_ibm_resource_instance.go
234 lines (196 loc) · 6.12 KB
/
data_source_ibm_resource_instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package ibm
import (
"fmt"
"github.com/IBM-Cloud/bluemix-go/models"
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/controller"
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/managementv2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceIBMResourceInstance() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMResourceInstanceRead,
Schema: map[string]*schema.Schema{
"name": {
Description: "Resource instance name for example, myobjectstorage",
Type: schema.TypeString,
Required: true,
},
"resource_group_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The id of the resource group in which the instance is present",
},
"location": {
Description: "The location or the environment in which instance exists",
Optional: true,
Type: schema.TypeString,
Computed: true,
},
"service": {
Description: "The service type of the instance",
Optional: true,
Type: schema.TypeString,
Computed: true,
},
"plan": {
Description: "The plan type of the instance",
Type: schema.TypeString,
Computed: true,
},
"status": {
Description: "The resource instance status",
Type: schema.TypeMap,
Computed: true,
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "CRN of resource instance",
},
"guid": {
Type: schema.TypeString,
Computed: true,
Description: "Guid of resource instance",
},
ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},
ResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
ResourceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the resource",
},
ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about the resource",
},
"extensions": {
Type: schema.TypeMap,
Computed: true,
Description: "The extended metadata as a map associated with the resource instance.",
},
},
}
}
func dataSourceIBMResourceInstanceRead(d *schema.ResourceData, meta interface{}) error {
rsConClient, err := meta.(ClientSession).ResourceControllerAPI()
if err != nil {
return err
}
rsAPI := rsConClient.ResourceServiceInstance()
name := d.Get("name").(string)
rsInstQuery := controller.ServiceInstanceQuery{
Name: name,
}
if rsGrpID, ok := d.GetOk("resource_group_id"); ok {
rsInstQuery.ResourceGroupID = rsGrpID.(string)
} else {
rsMangClient, err := meta.(ClientSession).ResourceManagementAPIv2()
if err != nil {
return err
}
resourceGroupQuery := managementv2.ResourceGroupQuery{
Default: true,
}
grpList, err := rsMangClient.ResourceGroup().List(&resourceGroupQuery)
if err != nil {
return err
}
if len(grpList) <= 0 {
return fmt.Errorf("The targeted resource group could not be found. Make sure you have required permissions to access the resource group.")
}
rsInstQuery.ResourceGroupID = grpList[0].ID
}
rsCatClient, err := meta.(ClientSession).ResourceCatalogAPI()
if err != nil {
return err
}
rsCatRepo := rsCatClient.ResourceCatalog()
if service, ok := d.GetOk("service"); ok {
serviceOff, err := rsCatRepo.FindByName(service.(string), true)
if err != nil {
return fmt.Errorf("Error retrieving service offering: %s", err)
}
rsInstQuery.ServiceID = serviceOff[0].ID
}
var instances []models.ServiceInstance
instances, err = rsAPI.ListInstances(rsInstQuery)
if err != nil {
return err
}
var filteredInstances []models.ServiceInstance
var location string
if loc, ok := d.GetOk("location"); ok {
location = loc.(string)
for _, instance := range instances {
if getLocation(instance) == location {
filteredInstances = append(filteredInstances, instance)
}
}
} else {
filteredInstances = instances
}
if len(filteredInstances) == 0 {
return fmt.Errorf("No resource instance found with name [%s]\nIf not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service", name)
}
var instance models.ServiceInstance
if len(filteredInstances) > 1 {
return fmt.Errorf(
"More than one resource instance found with name matching [%s]\nIf not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service", name)
}
instance = filteredInstances[0]
d.SetId(instance.ID)
d.Set("status", instance.State)
d.Set("resource_group_id", instance.ResourceGroupID)
d.Set("location", instance.RegionID)
serviceOff, err := rsCatRepo.GetServiceName(instance.ServiceID)
if err != nil {
return fmt.Errorf("Error retrieving service offering: %s", err)
}
d.Set("service", serviceOff)
d.Set(ResourceName, instance.Name)
d.Set(ResourceCRN, instance.Crn.String())
d.Set(ResourceStatus, instance.State)
d.Set(ResourceGroupName, instance.ResourceGroupName)
d.Set("guid", instance.Guid)
if len(instance.Extensions) == 0 {
d.Set("extensions", instance.Extensions)
} else {
d.Set("extensions", Flatten(instance.Extensions))
}
rcontroller, err := getBaseController(meta)
if err != nil {
return err
}
d.Set(ResourceControllerURL, rcontroller+"/services/")
servicePlan, err := rsCatRepo.GetServicePlanName(instance.ServicePlanID)
if err != nil {
return fmt.Errorf("Error retrieving plan: %s", err)
}
d.Set("plan", servicePlan)
d.Set("crn", instance.Crn.String())
return nil
}
func getLocation(instance models.ServiceInstance) string {
region := instance.Crn.Region
cName := instance.Crn.CName
if cName == "bluemix" || cName == "staging" {
return region
} else {
return cName + "-" + region
}
}