2016-02-10 21:30:32 +01:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-03-31 00:25:27 +02:00
|
|
|
"github.com/joyent/triton-go"
|
2016-02-10 21:30:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceFirewallRule() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceFirewallRuleCreate,
|
|
|
|
Exists: resourceFirewallRuleExists,
|
|
|
|
Read: resourceFirewallRuleRead,
|
|
|
|
Update: resourceFirewallRuleUpdate,
|
|
|
|
Delete: resourceFirewallRuleDelete,
|
2016-05-05 01:17:06 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
2017-03-31 00:25:27 +02:00
|
|
|
State: schema.ImportStatePassthrough,
|
2016-05-05 01:17:06 +02:00
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
2017-03-31 00:25:27 +02:00
|
|
|
"description": {
|
|
|
|
Description: "Human-readable description of the rule",
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"global": {
|
|
|
|
Description: "Indicates whether or not the rule is global",
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
rule, err := client.Firewall().CreateFirewallRule(&triton.CreateFirewallRuleInput{
|
|
|
|
Rule: d.Get("rule").(string),
|
|
|
|
Enabled: d.Get("enabled").(bool),
|
|
|
|
Description: d.Get("description").(string),
|
2016-02-10 21:30:32 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
d.SetId(rule.ID)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return resourceFirewallRuleRead(d, meta)
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFirewallRuleExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return resourceExists(client.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{
|
|
|
|
ID: d.Id(),
|
|
|
|
}))
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
rule, err := client.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{
|
|
|
|
ID: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
d.SetId(rule.ID)
|
2016-02-10 21:30:32 +01:00
|
|
|
d.Set("rule", rule.Rule)
|
|
|
|
d.Set("enabled", rule.Enabled)
|
2017-03-31 00:25:27 +02:00
|
|
|
d.Set("global", rule.Global)
|
|
|
|
d.Set("description", rule.Description)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFirewallRuleUpdate(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
_, err := client.Firewall().UpdateFirewallRule(&triton.UpdateFirewallRuleInput{
|
|
|
|
ID: d.Id(),
|
|
|
|
Rule: d.Get("rule").(string),
|
|
|
|
Enabled: d.Get("enabled").(bool),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceFirewallRuleRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return client.Firewall().DeleteFirewallRule(&triton.DeleteFirewallRuleInput{
|
|
|
|
ID: d.Id(),
|
|
|
|
})
|
2016-05-05 01:17:06 +02:00
|
|
|
}
|