/
data_source_openstack_identity_group_v3.go
95 lines (75 loc) · 2.42 KB
/
data_source_openstack_identity_group_v3.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
package openstack
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/identity/v3/groups"
)
func dataSourceIdentityGroupV3() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIdentityGroupV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"domain_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
// dataSourceIdentityGroupV3Read performs the group lookup.
func dataSourceIdentityGroupV3Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
identityClient, err := config.IdentityV3Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack identity client: %s", err)
}
listOpts := groups.ListOpts{
DomainID: d.Get("domain_id").(string),
Name: d.Get("name").(string),
}
log.Printf("[DEBUG] openstack_identity_group_v3 list options: %#v", listOpts)
var group groups.Group
allPages, err := groups.List(identityClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to query openstack_identity_group_v3: %s", err)
}
allGroups, err := groups.ExtractGroups(allPages)
if err != nil {
return diag.Errorf("Unable to retrieve openstack_identity_group_v3: %s", err)
}
if len(allGroups) < 1 {
return diag.Errorf("Your openstack_identity_group_v3 query returned no results. " +
"Please change your search criteria and try again")
}
if len(allGroups) > 1 {
return diag.Errorf("Your openstack_identity_group_v3 query returned more than one result")
}
group = allGroups[0]
dataSourceIdentityGroupV3Attributes(d, config, &group)
return nil
}
// dataSourceIdentityRoleV3Attributes populates the fields of an Role resource.
func dataSourceIdentityGroupV3Attributes(d *schema.ResourceData, config *Config, group *groups.Group) {
log.Printf("[DEBUG] openstack_identity_group_v3 details: %#v", group)
d.SetId(group.ID)
d.Set("name", group.Name)
d.Set("description", group.Description)
d.Set("domain_id", group.DomainID)
d.Set("region", GetRegion(d, config))
}