-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_viettelidc_fw_policy_v1.go
204 lines (165 loc) · 5.38 KB
/
resource_viettelidc_fw_policy_v1.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package viettelidc
import (
"context"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
)
func resourceFWPolicyV1() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFWPolicyV1Create,
ReadContext: resourceFWPolicyV1Read,
UpdateContext: resourceFWPolicyV1Update,
DeleteContext: resourceFWPolicyV1Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
DeprecationMessage: "use openstack_fw_policy_v2 resource instead",
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"audited": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"shared": {
Type: schema.TypeBool,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"rules": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"value_specs": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceFWPolicyV1Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
audited := d.Get("audited").(bool)
opts := PolicyCreateOpts{
policies.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Audited: &audited,
TenantID: d.Get("tenant_id").(string),
Rules: expandToStringSlice(d.Get("rules").([]interface{})),
},
MapValueSpecs(d),
}
if r, ok := d.GetOk("shared"); ok {
shared := r.(bool)
opts.Shared = &shared
}
log.Printf("[DEBUG] openstack_fw_policy_v1 create options: %#v", opts)
policy, err := policies.Create(networkingClient, opts).Extract()
if err != nil {
return diag.Errorf("Error creating openstack_fw_policy_v1: %s", err)
}
log.Printf("[DEBUG] openstack_fw_policy_v1 %s created: %#v", policy.ID, policy)
d.SetId(policy.ID)
return resourceFWPolicyV1Read(ctx, d, meta)
}
func resourceFWPolicyV1Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
policy, err := policies.Get(networkingClient, d.Id()).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error retrieving openstack_fw_policy_v1"))
}
log.Printf("[DEBUG] Retrieved openstack_fw_policy_v1 %s: %#v", d.Id(), policy)
d.Set("name", policy.Name)
d.Set("description", policy.Description)
d.Set("shared", policy.Shared)
d.Set("audited", policy.Audited)
d.Set("tenant_id", policy.TenantID)
d.Set("rules", policy.Rules)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceFWPolicyV1Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
opts := policies.UpdateOpts{}
if d.HasChange("name") {
name := d.Get("name").(string)
opts.Name = &name
}
if d.HasChange("description") {
description := d.Get("description").(string)
opts.Description = &description
}
if d.HasChange("rules") {
opts.Rules = expandToStringSlice(d.Get("rules").([]interface{}))
}
log.Printf("[DEBUG] openstack_fw_policy_v1 %s update options: %#v", d.Id(), opts)
err = policies.Update(networkingClient, d.Id(), opts).Err
if err != nil {
return diag.Errorf("Error updating openstack_fw_policy_v1 %s: %s", d.Id(), err)
}
return resourceFWPolicyV1Read(ctx, d, meta)
}
func resourceFWPolicyV1Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
_, err = policies.Get(networkingClient, d.Id()).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error retrieving openstack_fw_policy_v1"))
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Target: []string{"DELETED"},
Refresh: fwPolicyV1DeleteFunc(networkingClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 0,
MinTimeout: 2 * time.Second,
}
if _, err = stateConf.WaitForStateContext(ctx); err != nil {
return diag.Errorf("Error waiting for openstack_fw_policy_v1 %s to be deleted: %s", d.Id(), err)
}
return nil
}