Skip to content

Commit 17cbe57

Browse files
authored
added support for resource_groups datasource (IBM-Cloud#6499)
* added support for resource_groups datasource * fixed acceptance tests * updated the error messages
1 parent bf1c267 commit 17cbe57

File tree

4 files changed

+581
-0
lines changed

4 files changed

+581
-0
lines changed

ibm/provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ func Provider() *schema.Provider {
709709

710710
"ibm_resource_quota": resourcecontroller.DataSourceIBMResourceQuota(),
711711
"ibm_resource_group": resourcemanager.DataSourceIBMResourceGroup(),
712+
"ibm_resource_groups": resourcemanager.DataSourceIBMResourceGroups(),
712713
"ibm_resource_instance": resourcecontroller.DataSourceIBMResourceInstance(),
713714
"ibm_resource_key": resourcecontroller.DataSourceIBMResourceKey(),
714715
"ibm_security_group": classicinfrastructure.DataSourceIBMSecurityGroup(),
@@ -2328,6 +2329,7 @@ func Validator() validate.ValidatorDict {
23282329
"ibm_resource_instance": resourcecontroller.DataSourceIBMResourceInstanceValidator(),
23292330
"ibm_resource_key": resourcecontroller.DataSourceIBMResourceKeyValidator(),
23302331
"ibm_resource_group": resourcemanager.DataSourceIBMResourceGroupValidator(),
2332+
"ibm_resource_groups": resourcemanager.DataSourceIBMResourceGroupsValidator(),
23312333

23322334
// bare_metal_server
23332335
"ibm_is_bare_metal_server": vpc.DataSourceIBMIsBareMetalServerValidator(),
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package resourcemanager
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"log"
10+
11+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
13+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
14+
rg "github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17+
)
18+
19+
func DataSourceIBMResourceGroups() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceIBMResourceGroupsRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"name": {
25+
Description: "Resource group name",
26+
Type: schema.TypeString,
27+
Optional: true,
28+
ValidateFunc: validate.InvokeDataSourceValidator("ibm_resource_groups",
29+
"name"),
30+
},
31+
"is_default": {
32+
Description: "Default Resource group",
33+
Type: schema.TypeBool,
34+
Optional: true,
35+
},
36+
"include_deleted": {
37+
Description: "Include deleted resource groups",
38+
Type: schema.TypeBool,
39+
Optional: true,
40+
},
41+
"date": {
42+
Description: "The date in the format of YYYY-MM which returns resource groups. Deleted resource groups will be excluded before this month.",
43+
Type: schema.TypeString,
44+
Optional: true,
45+
},
46+
"resource_groups": {
47+
Type: schema.TypeList,
48+
Description: "List of resource groups",
49+
Computed: true,
50+
Elem: &schema.Resource{
51+
Schema: map[string]*schema.Schema{
52+
"id": {
53+
Type: schema.TypeString,
54+
Description: "The ID of the resource group",
55+
Computed: true,
56+
},
57+
"name": {
58+
Type: schema.TypeString,
59+
Description: "Resource group name",
60+
Computed: true,
61+
},
62+
"is_default": {
63+
Type: schema.TypeBool,
64+
Description: "Default Resource group",
65+
Computed: true,
66+
},
67+
"state": {
68+
Type: schema.TypeString,
69+
Description: "State of the resource group",
70+
Computed: true,
71+
},
72+
"crn": {
73+
Type: schema.TypeString,
74+
Description: "The full CRN associated with the resource group",
75+
Computed: true,
76+
},
77+
"created_at": {
78+
Type: schema.TypeString,
79+
Description: "The date when the resource group was initially created.",
80+
Computed: true,
81+
},
82+
"updated_at": {
83+
Type: schema.TypeString,
84+
Description: "The date when the resource group was last updated.",
85+
Computed: true,
86+
},
87+
"teams_url": {
88+
Type: schema.TypeString,
89+
Description: "The URL to access the team details that associated with the resource group.",
90+
Computed: true,
91+
},
92+
"payment_methods_url": {
93+
Type: schema.TypeString,
94+
Description: "The URL to access the payment methods details that associated with the resource group.",
95+
Computed: true,
96+
},
97+
"quota_url": {
98+
Type: schema.TypeString,
99+
Description: "The URL to access the quota details that associated with the resource group.",
100+
Computed: true,
101+
},
102+
"quota_id": {
103+
Type: schema.TypeString,
104+
Description: "An alpha-numeric value identifying the quota ID associated with the resource group.",
105+
Computed: true,
106+
},
107+
"resource_linkages": {
108+
Type: schema.TypeList,
109+
Description: "An array of the resources that linked to the resource group",
110+
Elem: &schema.Schema{Type: schema.TypeString},
111+
Computed: true,
112+
},
113+
"account_id": {
114+
Type: schema.TypeString,
115+
Description: "Account ID",
116+
Computed: true,
117+
},
118+
},
119+
},
120+
},
121+
},
122+
}
123+
}
124+
125+
func DataSourceIBMResourceGroupsValidator() *validate.ResourceValidator {
126+
validateSchema := make([]validate.ValidateSchema, 0)
127+
validateSchema = append(validateSchema,
128+
validate.ValidateSchema{
129+
Identifier: "name",
130+
ValidateFunctionIdentifier: validate.ValidateCloudData,
131+
Type: validate.TypeString,
132+
CloudDataType: "resource_group",
133+
CloudDataRange: []string{"resolved_to:name"},
134+
Optional: true})
135+
136+
ibmIBMResourceGroupsValidator := validate.ResourceValidator{ResourceName: "ibm_resource_groups", Schema: validateSchema}
137+
return &ibmIBMResourceGroupsValidator
138+
}
139+
140+
func dataSourceIBMResourceGroupsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
141+
var diags diag.Diagnostics
142+
143+
rMgtClient, err := meta.(conns.ClientSession).ResourceManagerV2API()
144+
if err != nil {
145+
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_resource_groups", "read", "initialize-client")
146+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
147+
return tfErr.GetDiag()
148+
}
149+
150+
userDetails, err := meta.(conns.ClientSession).BluemixUserDetails()
151+
if err != nil {
152+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("BluemixUserDetails failed: %s", err.Error()), "ibm_resource_groups", "read")
153+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
154+
return tfErr.GetDiag()
155+
}
156+
accountID := userDetails.UserAccount
157+
158+
// Build the list options with available filters
159+
resourceGroupListOptions := rg.ListResourceGroupsOptions{
160+
AccountID: &accountID,
161+
}
162+
163+
// Apply filters
164+
if name, ok := d.GetOk("name"); ok {
165+
nameStr := name.(string)
166+
resourceGroupListOptions.Name = &nameStr
167+
}
168+
169+
if isDefault, ok := d.GetOk("is_default"); ok {
170+
defaultBool := isDefault.(bool)
171+
resourceGroupListOptions.Default = &defaultBool
172+
}
173+
174+
if includeDeleted, ok := d.GetOk("include_deleted"); ok {
175+
includeDeletedBool := includeDeleted.(bool)
176+
resourceGroupListOptions.IncludeDeleted = &includeDeletedBool
177+
}
178+
179+
if date, ok := d.GetOk("date"); ok {
180+
dateStr := date.(string)
181+
resourceGroupListOptions.Date = &dateStr
182+
}
183+
184+
// List resource groups
185+
rgList, _, err := rMgtClient.ListResourceGroupsWithContext(ctx, &resourceGroupListOptions)
186+
if err != nil {
187+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ListResourceGroupsWithContext failed: %s", err.Error()), "ibm_resource_groups", "read")
188+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
189+
return tfErr.GetDiag()
190+
}
191+
192+
if rgList == nil || rgList.Resources == nil {
193+
tfErr := flex.TerraformErrorf(fmt.Errorf("no resource groups found"), "No resource groups were returned from the API", "ibm_resource_groups", "read")
194+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
195+
return tfErr.GetDiag()
196+
}
197+
198+
// Convert resource groups to list of maps
199+
resourceGroups := make([]map[string]interface{}, 0)
200+
for _, resourceGroup := range rgList.Resources {
201+
rgMap := map[string]interface{}{}
202+
203+
if resourceGroup.ID != nil {
204+
rgMap["id"] = *resourceGroup.ID
205+
}
206+
if resourceGroup.Name != nil {
207+
rgMap["name"] = *resourceGroup.Name
208+
}
209+
if resourceGroup.Default != nil {
210+
rgMap["is_default"] = *resourceGroup.Default
211+
}
212+
if resourceGroup.State != nil {
213+
rgMap["state"] = *resourceGroup.State
214+
}
215+
if resourceGroup.CRN != nil {
216+
rgMap["crn"] = *resourceGroup.CRN
217+
}
218+
if resourceGroup.CreatedAt != nil {
219+
rgMap["created_at"] = resourceGroup.CreatedAt.String()
220+
}
221+
if resourceGroup.UpdatedAt != nil {
222+
rgMap["updated_at"] = resourceGroup.UpdatedAt.String()
223+
}
224+
if resourceGroup.TeamsURL != nil {
225+
rgMap["teams_url"] = *resourceGroup.TeamsURL
226+
}
227+
if resourceGroup.PaymentMethodsURL != nil {
228+
rgMap["payment_methods_url"] = *resourceGroup.PaymentMethodsURL
229+
}
230+
if resourceGroup.QuotaURL != nil {
231+
rgMap["quota_url"] = *resourceGroup.QuotaURL
232+
}
233+
if resourceGroup.QuotaID != nil {
234+
rgMap["quota_id"] = *resourceGroup.QuotaID
235+
}
236+
if resourceGroup.AccountID != nil {
237+
rgMap["account_id"] = *resourceGroup.AccountID
238+
}
239+
if resourceGroup.ResourceLinkages != nil {
240+
rl := make([]string, 0)
241+
for _, r := range resourceGroup.ResourceLinkages {
242+
rl = append(rl, r.(string))
243+
}
244+
rgMap["resource_linkages"] = rl
245+
}
246+
247+
resourceGroups = append(resourceGroups, rgMap)
248+
}
249+
250+
// Set the data source ID and resource groups
251+
d.SetId(accountID)
252+
if err := d.Set("resource_groups", resourceGroups); err != nil {
253+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting resource_groups: %s", err.Error()), "ibm_resource_groups", "read")
254+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
255+
return tfErr.GetDiag()
256+
}
257+
258+
return diags
259+
}

0 commit comments

Comments
 (0)