forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_firewall_rule.go
105 lines (84 loc) · 2.21 KB
/
resource_firewall_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
package triton
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/joyent/gocommon/errors"
"github.com/joyent/gosdc/cloudapi"
)
func resourceFirewallRule() *schema.Resource {
return &schema.Resource{
Create: resourceFirewallRuleCreate,
Exists: resourceFirewallRuleExists,
Read: resourceFirewallRuleRead,
Update: resourceFirewallRuleUpdate,
Delete: resourceFirewallRuleDelete,
Schema: map[string]*schema.Schema{
"rule": {
Description: "firewall rule text",
Type: schema.TypeString,
Required: true,
},
"enabled": {
Description: "Indicates if the rule is enabled",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
rule, err := client.CreateFirewallRule(cloudapi.CreateFwRuleOpts{
Rule: d.Get("rule").(string),
Enabled: d.Get("enabled").(bool),
})
if err != nil {
return err
}
d.SetId(rule.Id)
err = resourceFirewallRuleRead(d, meta)
if err != nil {
return err
}
return nil
}
func resourceFirewallRuleExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*cloudapi.Client)
rule, err := client.GetFirewallRule(d.Id())
if errors.IsResourceNotFound(err) {
return false, nil
}
return rule != nil && err == nil, err
}
func resourceFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
rule, err := client.GetFirewallRule(d.Id())
if err != nil {
return err
}
d.SetId(rule.Id)
d.Set("rule", rule.Rule)
d.Set("enabled", rule.Enabled)
return nil
}
func resourceFirewallRuleUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
_, err := client.UpdateFirewallRule(
d.Id(),
cloudapi.CreateFwRuleOpts{
Rule: d.Get("rule").(string),
Enabled: d.Get("enabled").(bool),
},
)
if err != nil {
return err
}
return resourceFirewallRuleRead(d, meta)
}
func resourceFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
if err := client.DeleteFirewallRule(d.Id()); err != nil {
return err
}
return nil
}