-
Notifications
You must be signed in to change notification settings - Fork 16
/
data_source_transip_openstack_user.go
63 lines (53 loc) · 1.36 KB
/
data_source_transip_openstack_user.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
package main
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6/openstack"
"github.com/transip/gotransip/v6/repository"
)
func dataSourceOpenstackUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceOpenstackUserRead,
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Description: "Username",
Required: true,
},
"description": {
Type: schema.TypeString,
Description: "Describes this user",
Optional: true,
},
"email": {
Type: schema.TypeString,
Description: "Email",
Optional: true,
},
},
}
}
func dataSourceOpenstackUserRead(d *schema.ResourceData, m interface{}) error {
username := d.Get("username").(string)
client := m.(repository.Client)
repository := openstack.UserRepository{Client: client}
users, err := repository.GetAll()
if err != nil {
return fmt.Errorf("failed to get openstack users: %s", err)
}
// get ID of openstack user
for _, user := range users {
if user.Username == username {
d.SetId(user.ID)
break
}
}
i, err := repository.GetByID(d.Id())
if err != nil {
return fmt.Errorf("failed to lookup openstack user %q: %s", username, err)
}
d.Set("username", i.Username)
d.Set("description", i.Description)
d.Set("email", i.Email)
return nil
}