2014-08-26 07:09:38 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
2014-08-26 07:09:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeFirewall() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeFirewallCreate,
|
|
|
|
Read: resourceComputeFirewallRead,
|
2014-08-26 21:50:08 +02:00
|
|
|
Update: resourceComputeFirewallUpdate,
|
2014-08-26 07:09:38 +02:00
|
|
|
Delete: resourceComputeFirewallDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-02-05 11:37:52 +01:00
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2014-08-26 07:09:38 +02:00
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"allow": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ports": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceComputeFirewallAllowHash,
|
|
|
|
},
|
|
|
|
|
|
|
|
"source_ranges": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"source_tags": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2014-09-26 07:15:31 +02:00
|
|
|
|
|
|
|
"target_tags": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2014-09-28 20:51:49 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2014-09-26 07:15:31 +02:00
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2015-02-10 12:14:15 +01:00
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-08-26 07:09:38 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeFirewallAllowHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["protocol"].(string)))
|
|
|
|
|
|
|
|
// We need to make sure to sort the strings below so that we always
|
|
|
|
// generate the same hash code no matter what is in the set.
|
|
|
|
if v, ok := m["ports"]; ok {
|
|
|
|
vs := v.(*schema.Set).List()
|
|
|
|
s := make([]string, len(vs))
|
|
|
|
for i, raw := range vs {
|
|
|
|
s[i] = raw.(string)
|
|
|
|
}
|
|
|
|
sort.Strings(s)
|
|
|
|
|
|
|
|
for _, v := range s {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeFirewallCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2014-08-26 21:50:08 +02:00
|
|
|
firewall, err := resourceFirewall(d, meta)
|
2014-08-26 07:09:38 +02:00
|
|
|
if err != nil {
|
2014-08-26 21:50:08 +02:00
|
|
|
return err
|
2014-08-26 07:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientCompute.Firewalls.Insert(
|
|
|
|
config.Project, firewall).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating firewall: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(firewall.Name)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Creating Firewall")
|
2014-08-26 07:44:27 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
2014-08-26 07:09:38 +02:00
|
|
|
|
|
|
|
return resourceComputeFirewallRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeFirewallRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2015-02-10 12:14:15 +01:00
|
|
|
firewall, err := config.clientCompute.Firewalls.Get(
|
2014-08-26 07:09:38 +02:00
|
|
|
config.Project, d.Id()).Do()
|
|
|
|
if err != nil {
|
2014-08-26 07:49:14 +02:00
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 07:09:38 +02:00
|
|
|
return fmt.Errorf("Error reading firewall: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-02-10 12:14:15 +01:00
|
|
|
d.Set("self_link", firewall.SelfLink)
|
|
|
|
|
2014-08-26 07:09:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 21:50:08 +02:00
|
|
|
func resourceComputeFirewallUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2014-08-27 05:33:53 +02:00
|
|
|
d.Partial(true)
|
|
|
|
|
2014-08-26 21:50:08 +02:00
|
|
|
firewall, err := resourceFirewall(d, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientCompute.Firewalls.Update(
|
|
|
|
config.Project, d.Id(), firewall).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating firewall: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Updating Firewall")
|
2014-08-26 21:50:08 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 21:50:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-27 05:33:53 +02:00
|
|
|
d.Partial(false)
|
|
|
|
|
2014-08-26 21:50:08 +02:00
|
|
|
return resourceComputeFirewallRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2014-08-26 07:09:38 +02:00
|
|
|
func resourceComputeFirewallDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
// Delete the firewall
|
|
|
|
op, err := config.clientCompute.Firewalls.Delete(
|
|
|
|
config.Project, d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting firewall: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Deleting Firewall")
|
2014-08-26 07:44:27 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
2014-08-26 07:09:38 +02:00
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-26 21:50:08 +02:00
|
|
|
|
|
|
|
func resourceFirewall(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{}) (*compute.Firewall, error) {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
// Look up the network to attach the firewall to
|
|
|
|
network, err := config.clientCompute.Networks.Get(
|
|
|
|
config.Project, d.Get("network").(string)).Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading network: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the list of allowed entries
|
|
|
|
var allowed []*compute.FirewallAllowed
|
|
|
|
if v := d.Get("allow").(*schema.Set); v.Len() > 0 {
|
|
|
|
allowed = make([]*compute.FirewallAllowed, 0, v.Len())
|
|
|
|
for _, v := range v.List() {
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
var ports []string
|
|
|
|
if v := m["ports"].(*schema.Set); v.Len() > 0 {
|
|
|
|
ports = make([]string, v.Len())
|
|
|
|
for i, v := range v.List() {
|
|
|
|
ports[i] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allowed = append(allowed, &compute.FirewallAllowed{
|
|
|
|
IPProtocol: m["protocol"].(string),
|
|
|
|
Ports: ports,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the list of sources
|
|
|
|
var sourceRanges, sourceTags []string
|
|
|
|
if v := d.Get("source_ranges").(*schema.Set); v.Len() > 0 {
|
|
|
|
sourceRanges = make([]string, v.Len())
|
|
|
|
for i, v := range v.List() {
|
|
|
|
sourceRanges[i] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v := d.Get("source_tags").(*schema.Set); v.Len() > 0 {
|
|
|
|
sourceTags = make([]string, v.Len())
|
|
|
|
for i, v := range v.List() {
|
|
|
|
sourceTags[i] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 07:15:31 +02:00
|
|
|
// Build up the list of targets
|
|
|
|
var targetTags []string
|
|
|
|
if v := d.Get("target_tags").(*schema.Set); v.Len() > 0 {
|
|
|
|
targetTags = make([]string, v.Len())
|
2014-09-28 20:51:49 +02:00
|
|
|
for i, v := range v.List() {
|
2014-09-26 07:15:31 +02:00
|
|
|
targetTags[i] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 21:50:08 +02:00
|
|
|
// Build the firewall parameter
|
|
|
|
return &compute.Firewall{
|
|
|
|
Name: d.Get("name").(string),
|
2015-02-05 11:37:52 +01:00
|
|
|
Description: d.Get("description").(string),
|
2014-08-26 21:50:08 +02:00
|
|
|
Network: network.SelfLink,
|
|
|
|
Allowed: allowed,
|
|
|
|
SourceRanges: sourceRanges,
|
|
|
|
SourceTags: sourceTags,
|
2014-09-26 07:15:31 +02:00
|
|
|
TargetTags: targetTags,
|
2014-08-26 21:50:08 +02:00
|
|
|
}, nil
|
|
|
|
}
|