github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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/triton-go"
     6  )
     7  
     8  func resourceFirewallRule() *schema.Resource {
     9  	return &schema.Resource{
    10  		Create: resourceFirewallRuleCreate,
    11  		Exists: resourceFirewallRuleExists,
    12  		Read:   resourceFirewallRuleRead,
    13  		Update: resourceFirewallRuleUpdate,
    14  		Delete: resourceFirewallRuleDelete,
    15  		Importer: &schema.ResourceImporter{
    16  			State: schema.ImportStatePassthrough,
    17  		},
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"rule": {
    21  				Description: "firewall rule text",
    22  				Type:        schema.TypeString,
    23  				Required:    true,
    24  			},
    25  			"enabled": {
    26  				Description: "Indicates if the rule is enabled",
    27  				Type:        schema.TypeBool,
    28  				Optional:    true,
    29  				Default:     false,
    30  			},
    31  			"description": {
    32  				Description: "Human-readable description of the rule",
    33  				Type:        schema.TypeString,
    34  				Optional:    true,
    35  			},
    36  			"global": {
    37  				Description: "Indicates whether or not the rule is global",
    38  				Type:        schema.TypeBool,
    39  				Computed:    true,
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
    46  	client := meta.(*triton.Client)
    47  
    48  	rule, err := client.Firewall().CreateFirewallRule(&triton.CreateFirewallRuleInput{
    49  		Rule:        d.Get("rule").(string),
    50  		Enabled:     d.Get("enabled").(bool),
    51  		Description: d.Get("description").(string),
    52  	})
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	d.SetId(rule.ID)
    58  
    59  	return resourceFirewallRuleRead(d, meta)
    60  }
    61  
    62  func resourceFirewallRuleExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    63  	client := meta.(*triton.Client)
    64  
    65  	return resourceExists(client.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{
    66  		ID: d.Id(),
    67  	}))
    68  }
    69  
    70  func resourceFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
    71  	client := meta.(*triton.Client)
    72  
    73  	rule, err := client.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{
    74  		ID: d.Id(),
    75  	})
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	d.SetId(rule.ID)
    81  	d.Set("rule", rule.Rule)
    82  	d.Set("enabled", rule.Enabled)
    83  	d.Set("global", rule.Global)
    84  	d.Set("description", rule.Description)
    85  
    86  	return nil
    87  }
    88  
    89  func resourceFirewallRuleUpdate(d *schema.ResourceData, meta interface{}) error {
    90  	client := meta.(*triton.Client)
    91  
    92  	_, err := client.Firewall().UpdateFirewallRule(&triton.UpdateFirewallRuleInput{
    93  		ID:          d.Id(),
    94  		Rule:        d.Get("rule").(string),
    95  		Enabled:     d.Get("enabled").(bool),
    96  		Description: d.Get("description").(string),
    97  	})
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	return resourceFirewallRuleRead(d, meta)
   103  }
   104  
   105  func resourceFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
   106  	client := meta.(*triton.Client)
   107  
   108  	return client.Firewall().DeleteFirewallRule(&triton.DeleteFirewallRuleInput{
   109  		ID: d.Id(),
   110  	})
   111  }