-
Notifications
You must be signed in to change notification settings - Fork 133
/
data_cloud_project_users.go
116 lines (104 loc) · 2.85 KB
/
data_cloud_project_users.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ovh
import (
"context"
"fmt"
"log"
"net/url"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)
func datasourceCloudProjectUsers() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceCloudProjectUsersRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
},
// Computed
"users": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_id": {
Type: schema.TypeString,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"roles": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"permissions": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func datasourceCloudProjectUsersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
users := make([]CloudProjectUser, 0)
log.Printf("[DEBUG] Will read public cloud user %s from project: %s", d.Id(), serviceName)
endpoint := fmt.Sprintf(
"/cloud/project/%s/user",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Get(endpoint, &users); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}
mapUsers := make([]map[string]interface{}, len(users))
ids := make([]string, len(users))
for i, user := range users {
mapUsers[i] = user.ToMap()
mapUsers[i]["user_id"] = strconv.Itoa(user.Id)
ids = append(ids, strconv.Itoa(user.Id))
}
d.SetId(hashcode.Strings(ids))
d.Set("users", mapUsers)
log.Printf("[DEBUG] Read Public Cloud User %s", mapUsers)
return nil
}