forked from hashicorp/terraform
/
resource_sec_rule.go
163 lines (140 loc) · 3.82 KB
/
resource_sec_rule.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
package opc
import (
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOPCSecRule() *schema.Resource {
return &schema.Resource{
Create: resourceOPCSecRuleCreate,
Read: resourceOPCSecRuleRead,
Update: resourceOPCSecRuleUpdate,
Delete: resourceOPCSecRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"source_list": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination_list": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"application": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"action": {
Type: schema.TypeString,
Required: true,
},
"disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceOPCSecRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SecRules()
name := d.Get("name").(string)
sourceList := d.Get("source_list").(string)
destinationList := d.Get("destination_list").(string)
application := d.Get("application").(string)
action := d.Get("action").(string)
disabled := d.Get("disabled").(bool)
input := compute.CreateSecRuleInput{
Name: name,
Action: action,
SourceList: sourceList,
DestinationList: destinationList,
Disabled: disabled,
Application: application,
}
desc, descOk := d.GetOk("description")
if descOk {
input.Description = desc.(string)
}
info, err := client.CreateSecRule(&input)
if err != nil {
return fmt.Errorf("Error creating sec rule %s: %s", name, err)
}
d.SetId(info.Name)
return resourceOPCSecRuleRead(d, meta)
}
func resourceOPCSecRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SecRules()
name := d.Id()
input := compute.GetSecRuleInput{
Name: name,
}
result, err := client.GetSecRule(&input)
if err != nil {
// Sec Rule does not exist
if compute.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading sec list %s: %s", name, err)
}
d.Set("name", result.Name)
d.Set("description", result.Description)
d.Set("source_list", result.SourceList)
d.Set("destination_list", result.DestinationList)
d.Set("application", result.Application)
d.Set("action", result.Action)
d.Set("disabled", result.Disabled)
return nil
}
func resourceOPCSecRuleUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SecRules()
name := d.Get("name").(string)
sourceList := d.Get("source_list").(string)
destinationList := d.Get("destination_list").(string)
application := d.Get("application").(string)
action := d.Get("action").(string)
disabled := d.Get("disabled").(bool)
input := compute.UpdateSecRuleInput{
Action: action,
Application: application,
DestinationList: destinationList,
Disabled: disabled,
Name: name,
SourceList: sourceList,
}
desc, descOk := d.GetOk("description")
if descOk {
input.Description = desc.(string)
}
_, err := client.UpdateSecRule(&input)
if err != nil {
return fmt.Errorf("Error updating sec rule %s: %s", name, err)
}
return resourceOPCSecRuleRead(d, meta)
}
func resourceOPCSecRuleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).SecRules()
name := d.Id()
input := compute.DeleteSecRuleInput{
Name: name,
}
if err := client.DeleteSecRule(&input); err != nil {
return fmt.Errorf("Error deleting sec rule %s: %s", name, err)
}
return nil
}