-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_rolemapping.go
129 lines (109 loc) · 2.87 KB
/
resource_rolemapping.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
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/adeleporte/terraform-provider-hcx/hcx"
)
func resourceRoleMapping() *schema.Resource {
return &schema.Resource{
CreateContext: resourceRoleMappingCreate,
ReadContext: resourceRoleMappingRead,
UpdateContext: resourceRoleMappingUpdate,
DeleteContext: resourceRoleMappingDelete,
Schema: map[string]*schema.Schema{
"admin": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_group": {
Type: schema.TypeString,
Optional: true,
Default: "vsphere.local\\Administrators",
},
},
},
},
"enterprise": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_group": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
},
},
},
"sso": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceRoleMappingCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceRoleMappingUpdate(ctx, d, m)
}
func resourceRoleMappingRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
return diags
}
func resourceRoleMappingUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcx.Client)
admin := d.Get("admin").([]interface{})
enterprise := d.Get("enterprise").([]interface{})
admin_groups := []string{}
for _, j := range admin {
tmp := j.(map[string]interface{})
admin_groups = append(admin_groups, tmp["user_group"].(string))
}
enterprise_groups := []string{}
for _, j := range enterprise {
tmp := j.(map[string]interface{})
enterprise_groups = append(enterprise_groups, tmp["user_group"].(string))
}
body := []hcx.RoleMapping{
{
Role: "System Administrator",
UserGroups: admin_groups,
},
{
Role: "Enterprise Administrator",
UserGroups: enterprise_groups,
},
}
/*
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
return diag.Errorf("%s", buf)
*/
_, err := hcx.PutRoleMapping(client, body)
if err != nil {
return diag.FromErr(err)
}
d.SetId("role_mapping")
return resourceRoleMappingRead(ctx, d, m)
}
func resourceRoleMappingDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*hcx.Client)
body := []hcx.RoleMapping{
{
Role: "System Administrator",
UserGroups: []string{},
},
{
Role: "Enterprise Administrator",
UserGroups: []string{},
},
}
_, err := hcx.PutRoleMapping(client, body)
if err != nil {
return diag.FromErr(err)
}
return diags
}