-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_iam_resource_group.go
153 lines (128 loc) · 3.78 KB
/
resource_iam_resource_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
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 resourceIamResourceGroup() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"resources": {
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,
},
"read_only": {
Type: schema.TypeBool,
Computed: true,
},
"urn": {
Type: schema.TypeString,
Computed: true,
},
},
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// Before importing, check that the resource group is not read-only.
config := meta.(*Config)
id := rd.Id()
var pol IamResourceGroup
err := config.OVHClient.GetWithContext(ctx, fmt.Sprintf("/v2/iam/resourceGroup/%s?details=true", url.PathEscape(id)), &pol)
if err != nil {
return nil, err
}
if pol.ReadOnly {
return nil, fmt.Errorf("resource_group '%s' is read-only", id)
}
return []*schema.ResourceData{rd}, nil
},
},
ReadContext: resourceIamResourceGroupRead,
CreateContext: resourceIamResourceGroupCreate,
UpdateContext: resourceIamResourceGroupUpdate,
DeleteContext: resourceIamResourceGroupDelete,
}
}
func resourceIamResourceGroupRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
id := d.Id()
var pol IamResourceGroup
err := config.OVHClient.GetWithContext(ctx, fmt.Sprintf("/v2/iam/resourceGroup/%s?details=true", url.PathEscape(id)), &pol)
if err != nil {
return diag.FromErr(err)
}
var urns []string
for _, r := range pol.Resources {
urns = append(urns, r.URN)
}
d.Set("resources", urns)
d.Set("name", pol.Name)
d.Set("owner", pol.Owner)
d.Set("created_at", pol.CreatedAt)
d.Set("updated_at", pol.UpdatedAt)
d.Set("read_only", pol.ReadOnly)
d.Set("urn", pol.URN)
return nil
}
func resourceIamResourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
var grp IamResourceGroup
grp.Name = d.Get("name").(string)
urns := d.Get("resources").(*schema.Set)
for _, r := range urns.List() {
urn := r.(string)
grp.Resources = append(grp.Resources, IamResourceDetails{URN: urn})
}
var grpOut IamResourceGroup
err := config.OVHClient.PostWithContext(ctx, "/v2/iam/resourceGroup", grp, &grpOut)
if err != nil {
return diag.FromErr(err)
}
d.SetId(grpOut.ID)
return resourceIamResourceGroupRead(ctx, d, meta)
}
func resourceIamResourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
id := d.Id()
var pol IamResourceGroup
pol.Name = d.Get("name").(string)
urns := d.Get("resources").(*schema.Set)
for _, r := range urns.List() {
urn := r.(string)
pol.Resources = append(pol.Resources, IamResourceDetails{URN: urn})
}
err := config.OVHClient.PutWithContext(ctx, "/v2/iam/resourceGroup/"+url.PathEscape(id), &pol, nil)
if err != nil {
return diag.FromErr(err)
}
return resourceIamResourceGroupRead(ctx, d, meta)
}
func resourceIamResourceGroupDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
id := d.Id()
err := config.OVHClient.DeleteWithContext(ctx, "/v2/iam/resourceGroup/"+url.PathEscape(id), nil)
if err != nil {
return diag.FromErr(err)
}
return nil
}