2016-02-10 21:30:32 +01:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2016-05-07 01:51:20 +02:00
|
|
|
"github.com/joyent/gocommon/errors"
|
2016-02-10 21:30:32 +01:00
|
|
|
"github.com/joyent/gosdc/cloudapi"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
|
|
|
State: resourceFirewallRuleImporter,
|
|
|
|
},
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2016-05-07 01:51:20 +02:00
|
|
|
if errors.IsResourceNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-05-05 01:17:06 +02:00
|
|
|
|
|
|
|
func resourceFirewallRuleImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
}
|