-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_iam_permission_group.go
167 lines (143 loc) · 3.93 KB
/
resource_iam_permission_group.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package ovh
import (
"context"
"fmt"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceIamPermissionsGroup() *schema.Resource {
return &schema.Resource{
Importer: &schema.ResourceImporter{
State: func(rd *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{rd}, nil
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Required: true,
},
"allow": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"except": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"deny": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"urn": {
Type: schema.TypeString,
Computed: true,
},
},
ReadContext: resourceIamPermissionsGroupsRead,
CreateContext: resourceIamPermissionsGroupCreate,
UpdateContext: resourceIamPermissionsGroupUpdate,
DeleteContext: resourceIamPermissionsGroupDelete,
}
}
func resourceIamPermissionsGroupsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
var pol IamPermissionsGroup
err := config.OVHClient.GetWithContext(ctx, "/v2/iam/permissionsGroup/"+url.PathEscape(d.Id()), &pol)
if err != nil {
return diag.FromErr(err)
}
for k, v := range pol.ToMap() {
d.Set(k, v)
}
return nil
}
func resourceIamPermissionsGroupCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
req := prepareIamPermissionGroupCall(d)
var pol IamPermissionsGroup
err := config.OVHClient.PostWithContext(ctx, "/v2/iam/permissionsGroup", req, &pol)
if err != nil {
warn := diag.Diagnostic{
Severity: diag.Warning,
Summary: "requestDetails",
Detail: fmt.Sprint(req),
}
diag := diag.FromErr(err)
return append(diag, warn)
}
for k, v := range pol.ToMap() {
d.Set(k, v)
}
d.SetId(pol.Urn)
return nil
}
func resourceIamPermissionsGroupUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
req := prepareIamPermissionGroupCall(d)
var pol IamPermissionsGroup
err := config.OVHClient.PutWithContext(ctx, "/v2/iam/permissionsGroup/"+url.PathEscape(d.Id()), req, &pol)
if err != nil {
return diag.FromErr(err)
}
for k, v := range pol.ToMap() {
d.Set(k, v)
}
return nil
}
func resourceIamPermissionsGroupDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
err := config.OVHClient.DeleteWithContext(ctx, "/v2/iam/permissionsGroup/"+url.PathEscape(d.Id()), nil)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func prepareIamPermissionGroupCall(d *schema.ResourceData) IamPermissionsGroup {
var out IamPermissionsGroup
out.Name = d.Get("name").(string)
out.Description = d.Get("description").(string)
if allows, ok := d.GetOk("allow"); ok {
for _, a := range allows.(*schema.Set).List() {
out.Permissions.Allow = append(out.Permissions.Allow, IamAction{Action: a.(string)})
}
}
if except, ok := d.GetOk("except"); ok {
for _, e := range except.(*schema.Set).List() {
out.Permissions.Except = append(out.Permissions.Except, IamAction{Action: e.(string)})
}
}
if deny, ok := d.GetOk("deny"); ok {
for _, e := range deny.(*schema.Set).List() {
out.Permissions.Deny = append(out.Permissions.Deny, IamAction{Action: e.(string)})
}
}
return out
}