github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/profitbricks/resource_profitbricks_firewall.go (about)

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"github.com/profitbricks/profitbricks-sdk-go"
     7  )
     8  
     9  func resourceProfitBricksFirewall() *schema.Resource {
    10  	return &schema.Resource{
    11  		Create: resourceProfitBricksFirewallCreate,
    12  		Read:   resourceProfitBricksFirewallRead,
    13  		Update: resourceProfitBricksFirewallUpdate,
    14  		Delete: resourceProfitBricksFirewallDelete,
    15  		Schema: map[string]*schema.Schema{
    16  
    17  			"name": {
    18  				Type:     schema.TypeString,
    19  				Optional: true,
    20  			},
    21  
    22  			"protocol": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  			"source_mac": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  			},
    30  			"source_ip": {
    31  				Type:     schema.TypeString,
    32  				Optional: true,
    33  			},
    34  			"target_ip": {
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  			},
    38  			"port_range_start": {
    39  				Type:     schema.TypeInt,
    40  				Optional: true,
    41  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    42  					if v.(int) < 1 && v.(int) > 65534 {
    43  						errors = append(errors, fmt.Errorf("Port start range must be between 1 and 65534"))
    44  					}
    45  					return
    46  				},
    47  			},
    48  
    49  			"port_range_end": {
    50  				Type:     schema.TypeInt,
    51  				Optional: true,
    52  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    53  					if v.(int) < 1 && v.(int) > 65534 {
    54  						errors = append(errors, fmt.Errorf("Port end range must be between 1 and 65534"))
    55  					}
    56  					return
    57  				},
    58  			},
    59  			"icmp_type": {
    60  				Type:     schema.TypeString,
    61  				Optional: true,
    62  			},
    63  			"icmp_code": {
    64  				Type:     schema.TypeString,
    65  				Optional: true,
    66  			},
    67  			"datacenter_id": {
    68  				Type:     schema.TypeString,
    69  				Required: true,
    70  			},
    71  			"server_id": {
    72  				Type:     schema.TypeString,
    73  				Required: true,
    74  			},
    75  			"nic_id": {
    76  				Type:     schema.TypeString,
    77  				Required: true,
    78  			},
    79  		},
    80  	}
    81  }
    82  
    83  func resourceProfitBricksFirewallCreate(d *schema.ResourceData, meta interface{}) error {
    84  	fw := profitbricks.FirewallRule{
    85  		Properties: profitbricks.FirewallruleProperties{
    86  			Protocol: d.Get("protocol").(string),
    87  		},
    88  	}
    89  
    90  	if _, ok := d.GetOk("name"); ok {
    91  		fw.Properties.Name = d.Get("name").(string)
    92  	}
    93  	if _, ok := d.GetOk("source_mac"); ok {
    94  		fw.Properties.SourceMac = d.Get("source_mac").(string)
    95  	}
    96  	if _, ok := d.GetOk("source_ip"); ok {
    97  		fw.Properties.SourceIp = d.Get("source_ip").(string)
    98  	}
    99  	if _, ok := d.GetOk("target_ip"); ok {
   100  		fw.Properties.TargetIp = d.Get("target_ip").(string)
   101  	}
   102  	if _, ok := d.GetOk("port_range_start"); ok {
   103  		fw.Properties.PortRangeStart = d.Get("port_range_start").(int)
   104  	}
   105  	if _, ok := d.GetOk("port_range_end"); ok {
   106  		fw.Properties.PortRangeEnd = d.Get("port_range_end").(int)
   107  	}
   108  	if _, ok := d.GetOk("icmp_type"); ok {
   109  		fw.Properties.IcmpType = d.Get("icmp_type").(string)
   110  	}
   111  	if _, ok := d.GetOk("icmp_code"); ok {
   112  		fw.Properties.IcmpCode = d.Get("icmp_code").(string)
   113  	}
   114  
   115  	fw = profitbricks.CreateFirewallRule(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), fw)
   116  
   117  	if fw.StatusCode > 299 {
   118  		return fmt.Errorf("An error occured while creating a firewall rule: %s", fw.Response)
   119  	}
   120  
   121  	err := waitTillProvisioned(meta, fw.Headers.Get("Location"))
   122  	if err != nil {
   123  		return err
   124  	}
   125  	d.SetId(fw.Id)
   126  
   127  	return resourceProfitBricksFirewallRead(d, meta)
   128  }
   129  
   130  func resourceProfitBricksFirewallRead(d *schema.ResourceData, meta interface{}) error {
   131  	fw := profitbricks.GetFirewallRule(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), d.Id())
   132  
   133  	if fw.StatusCode > 299 {
   134  		return fmt.Errorf("An error occured while fetching a firewall rule  dcId: %s server_id: %s  nic_id: %s ID: %s %s", d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), d.Id(), fw.Response)
   135  	}
   136  
   137  	d.Set("protocol", fw.Properties.Protocol)
   138  	d.Set("name", fw.Properties.Name)
   139  	d.Set("source_mac", fw.Properties.SourceMac)
   140  	d.Set("source_ip", fw.Properties.SourceIp)
   141  	d.Set("target_ip", fw.Properties.TargetIp)
   142  	d.Set("port_range_start", fw.Properties.PortRangeStart)
   143  	d.Set("port_range_end", fw.Properties.PortRangeEnd)
   144  	d.Set("icmp_type", fw.Properties.IcmpType)
   145  	d.Set("icmp_code", fw.Properties.IcmpCode)
   146  	d.Set("nic_id", d.Get("nic_id").(string))
   147  
   148  	return nil
   149  }
   150  
   151  func resourceProfitBricksFirewallUpdate(d *schema.ResourceData, meta interface{}) error {
   152  	properties := profitbricks.FirewallruleProperties{}
   153  
   154  	if d.HasChange("name") {
   155  		_, new := d.GetChange("name")
   156  
   157  		properties.Name = new.(string)
   158  	}
   159  	if d.HasChange("source_mac") {
   160  		_, new := d.GetChange("source_mac")
   161  
   162  		properties.SourceMac = new.(string)
   163  	}
   164  	if d.HasChange("source_ip") {
   165  		_, new := d.GetChange("source_ip")
   166  
   167  		properties.SourceIp = new.(string)
   168  	}
   169  	if d.HasChange("target_ip") {
   170  		_, new := d.GetChange("target_ip")
   171  
   172  		properties.TargetIp = new.(string)
   173  	}
   174  	if d.HasChange("port_range_start") {
   175  		_, new := d.GetChange("port_range_start")
   176  
   177  		properties.PortRangeStart = new.(int)
   178  	}
   179  	if d.HasChange("port_range_end") {
   180  		_, new := d.GetChange("port_range_end")
   181  
   182  		properties.PortRangeEnd = new.(int)
   183  	}
   184  	if d.HasChange("icmp_type") {
   185  		_, new := d.GetChange("icmp_type")
   186  
   187  		properties.IcmpType = new.(int)
   188  	}
   189  	if d.HasChange("icmp_code") {
   190  		_, new := d.GetChange("icmp_code")
   191  
   192  		properties.IcmpCode = new.(int)
   193  	}
   194  
   195  	resp := profitbricks.PatchFirewallRule(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), d.Id(), properties)
   196  
   197  	if resp.StatusCode > 299 {
   198  		return fmt.Errorf("An error occured while deleting a firewall rule ID %s %s", d.Id(), resp.Response)
   199  	}
   200  
   201  	err := waitTillProvisioned(meta, resp.Headers.Get("Location"))
   202  	if err != nil {
   203  		return err
   204  	}
   205  	return resourceProfitBricksFirewallRead(d, meta)
   206  }
   207  
   208  func resourceProfitBricksFirewallDelete(d *schema.ResourceData, meta interface{}) error {
   209  	resp := profitbricks.DeleteFirewallRule(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Get("nic_id").(string), d.Id())
   210  
   211  	if resp.StatusCode > 299 {
   212  		return fmt.Errorf("An error occured while deleting a firewall rule ID %s %s", d.Id(), string(resp.Body))
   213  	}
   214  
   215  	err := waitTillProvisioned(meta, resp.Headers.Get("Location"))
   216  	if err != nil {
   217  		return err
   218  	}
   219  	d.SetId("")
   220  
   221  	return nil
   222  }