-
Notifications
You must be signed in to change notification settings - Fork 33
/
data_source_user.go
58 lines (48 loc) · 1.46 KB
/
data_source_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
package xoa
import (
"log"
"github.com/vatesfr/terraform-provider-xenorchestra/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceXoaUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceUserRead,
Description: "Provides information about a Xen Orchestra user. If the Xen Orchestra user account you are using is not an admin, see the `search_in_session` parameter.",
Schema: map[string]*schema.Schema{
"username": &schema.Schema{
Type: schema.TypeString,
Description: "The username of the XO user.",
Required: true,
},
"search_in_session": &schema.Schema{
Type: schema.TypeBool,
Description: "A boolean which will search for the user in the current session (`session.getUser` Xen Orchestra RPC call). This allows a non admin user to look up their own user account.",
Optional: true,
Default: false,
},
},
}
}
func dataSourceUserRead(d *schema.ResourceData, m interface{}) error {
c := m.(client.XOClient)
username := d.Get("username").(string)
searchInSession := d.Get("search_in_session").(bool)
var user *client.User
var err error
if searchInSession {
user, err = c.GetCurrentUser()
} else {
user, err = c.GetUser(client.User{
Email: username,
})
}
if err != nil {
return err
}
log.Printf("[DEBUG] Found user with %+v", *user)
d.SetId(user.Id)
if err := d.Set("username", user.Email); err != nil {
return err
}
return nil
}