github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/triton/resource_firewall_rule.go (about) 1 package triton 2 3 import ( 4 "github.com/hashicorp/terraform/helper/schema" 5 "github.com/joyent/gocommon/errors" 6 "github.com/joyent/gosdc/cloudapi" 7 ) 8 9 func resourceFirewallRule() *schema.Resource { 10 return &schema.Resource{ 11 Create: resourceFirewallRuleCreate, 12 Exists: resourceFirewallRuleExists, 13 Read: resourceFirewallRuleRead, 14 Update: resourceFirewallRuleUpdate, 15 Delete: resourceFirewallRuleDelete, 16 Importer: &schema.ResourceImporter{ 17 State: resourceFirewallRuleImporter, 18 }, 19 20 Schema: map[string]*schema.Schema{ 21 "rule": { 22 Description: "firewall rule text", 23 Type: schema.TypeString, 24 Required: true, 25 }, 26 "enabled": { 27 Description: "Indicates if the rule is enabled", 28 Type: schema.TypeBool, 29 Optional: true, 30 Default: false, 31 }, 32 }, 33 } 34 } 35 36 func resourceFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error { 37 client := meta.(*cloudapi.Client) 38 39 rule, err := client.CreateFirewallRule(cloudapi.CreateFwRuleOpts{ 40 Rule: d.Get("rule").(string), 41 Enabled: d.Get("enabled").(bool), 42 }) 43 if err != nil { 44 return err 45 } 46 47 d.SetId(rule.Id) 48 49 err = resourceFirewallRuleRead(d, meta) 50 if err != nil { 51 return err 52 } 53 54 return nil 55 } 56 57 func resourceFirewallRuleExists(d *schema.ResourceData, meta interface{}) (bool, error) { 58 client := meta.(*cloudapi.Client) 59 60 rule, err := client.GetFirewallRule(d.Id()) 61 if errors.IsResourceNotFound(err) { 62 return false, nil 63 } 64 65 return rule != nil && err == nil, err 66 } 67 68 func resourceFirewallRuleRead(d *schema.ResourceData, meta interface{}) error { 69 client := meta.(*cloudapi.Client) 70 71 rule, err := client.GetFirewallRule(d.Id()) 72 if err != nil { 73 return err 74 } 75 76 d.SetId(rule.Id) 77 d.Set("rule", rule.Rule) 78 d.Set("enabled", rule.Enabled) 79 80 return nil 81 } 82 83 func resourceFirewallRuleUpdate(d *schema.ResourceData, meta interface{}) error { 84 client := meta.(*cloudapi.Client) 85 86 _, err := client.UpdateFirewallRule( 87 d.Id(), 88 cloudapi.CreateFwRuleOpts{ 89 Rule: d.Get("rule").(string), 90 Enabled: d.Get("enabled").(bool), 91 }, 92 ) 93 if err != nil { 94 return err 95 } 96 97 return resourceFirewallRuleRead(d, meta) 98 } 99 100 func resourceFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error { 101 client := meta.(*cloudapi.Client) 102 103 if err := client.DeleteFirewallRule(d.Id()); err != nil { 104 return err 105 } 106 107 return nil 108 } 109 110 func resourceFirewallRuleImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 111 return []*schema.ResourceData{d}, nil 112 }