-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_openstack_identity_user_v3.go
More file actions
136 lines (110 loc) · 3.32 KB
/
Copy pathdata_source_openstack_identity_user_v3.go
File metadata and controls
136 lines (110 loc) · 3.32 KB
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
package openstack
import (
"context"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/identity/v3/users"
)
func dataSourceIdentityUserV3() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIdentityUserV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"default_project_id": {
Type: schema.TypeString,
Computed: true,
},
"domain_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"idp_id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"password_expires_at": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validatePasswordExpiresAtQuery,
},
"protocol_id": {
Type: schema.TypeString,
Optional: true,
},
"unique_id": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
// dataSourceIdentityUserV3Read performs the user lookup.
func dataSourceIdentityUserV3Read(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)
}
enabled := d.Get("enabled").(bool)
listOpts := users.ListOpts{
DomainID: d.Get("domain_id").(string),
Enabled: &enabled,
IdPID: d.Get("idp_id").(string),
Name: d.Get("name").(string),
PasswordExpiresAt: d.Get("password_expires_at").(string),
ProtocolID: d.Get("protocol_id").(string),
UniqueID: d.Get("unique_id").(string),
}
log.Printf("[DEBUG] openstack_identity_user_v3 list options: %#v", listOpts)
var user users.User
allPages, err := users.List(identityClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to query openstack_identity_user_v3: %s", err)
}
allUsers, err := users.ExtractUsers(allPages)
if err != nil {
return diag.Errorf("Unable to retrieve openstack_identity_user_v3: %s", err)
}
if len(allUsers) < 1 {
return diag.Errorf("Your openstack_identity_user_v3 query returned no results. " +
"Please change your search criteria and try again")
}
if len(allUsers) > 1 {
return diag.Errorf("Your openstack_identity_user_v3 query returned more than one result")
}
user = allUsers[0]
dataSourceIdentityUserV3Attributes(d, &user)
return nil
}
// dataSourceIdentityUserV3Attributes populates the fields of an User resource.
func dataSourceIdentityUserV3Attributes(d *schema.ResourceData, user *users.User) {
log.Printf("[DEBUG] openstack_identity_user_v3 details: %#v", user)
d.SetId(user.ID)
d.Set("default_project_id", user.DefaultProjectID)
d.Set("description", user.Description)
d.Set("domain_id", user.DomainID)
d.Set("enabled", user.Enabled)
d.Set("name", user.Name)
d.Set("password_expires_at", user.PasswordExpiresAt.Format(time.RFC3339))
}