2015-02-02 21:36:21 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2016-09-04 04:53:44 +02:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
|
2015-02-02 21:36:21 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWRuleV1() *schema.Resource {
|
2015-02-02 21:36:21 +01:00
|
|
|
return &schema.Resource{
|
2015-02-19 22:55:54 +01:00
|
|
|
Create: resourceFWRuleV1Create,
|
|
|
|
Read: resourceFWRuleV1Read,
|
|
|
|
Update: resourceFWRuleV1Update,
|
|
|
|
Delete: resourceFWRuleV1Delete,
|
2016-07-03 17:37:35 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-02-02 21:36:21 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
|
2015-02-02 21:36:21 +01:00
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"action": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"ip_version": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 4,
|
|
|
|
},
|
|
|
|
"source_ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"destination_ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"source_port": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"destination_port": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"enabled": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2015-02-10 00:19:01 +01:00
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-11-03 13:59:07 +01:00
|
|
|
"value_specs": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-02-02 21:36:21 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWRuleV1Create(d *schema.ResourceData, meta interface{}) error {
|
2015-02-02 21:36:21 +01:00
|
|
|
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-02 21:36:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled := d.Get("enabled").(bool)
|
2016-09-04 04:53:44 +02:00
|
|
|
ipVersion := resourceFWRuleV1DetermineIPVersion(d.Get("ip_version").(int))
|
2016-10-26 04:14:08 +02:00
|
|
|
protocol := resourceFWRuleV1DetermineProtocol(d.Get("protocol").(string))
|
2015-02-02 21:36:21 +01:00
|
|
|
|
2016-11-03 13:59:07 +01:00
|
|
|
ruleConfiguration := RuleCreateOpts{
|
|
|
|
rules.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
Protocol: protocol,
|
|
|
|
Action: d.Get("action").(string),
|
|
|
|
IPVersion: ipVersion,
|
|
|
|
SourceIPAddress: d.Get("source_ip_address").(string),
|
|
|
|
DestinationIPAddress: d.Get("destination_ip_address").(string),
|
|
|
|
SourcePort: d.Get("source_port").(string),
|
|
|
|
DestinationPort: d.Get("destination_port").(string),
|
|
|
|
Enabled: &enabled,
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
},
|
|
|
|
MapValueSpecs(d),
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Create firewall rule: %#v", ruleConfiguration)
|
|
|
|
|
|
|
|
rule, err := rules.Create(networkingClient, ruleConfiguration).Extract()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Firewall rule with id %s : %#v", rule.ID, rule)
|
|
|
|
|
|
|
|
d.SetId(rule.ID)
|
|
|
|
|
2015-02-19 23:44:49 +01:00
|
|
|
return resourceFWRuleV1Read(d, meta)
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWRuleV1Read(d *schema.ResourceData, meta interface{}) error {
|
2015-02-02 21:36:21 +01:00
|
|
|
log.Printf("[DEBUG] Retrieve information about firewall rule: %s", d.Id())
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-02 21:36:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rule, err := rules.Get(networkingClient, d.Id()).Extract()
|
|
|
|
if err != nil {
|
2015-05-05 14:01:49 +02:00
|
|
|
return CheckDeleted(d, err, "FW rule")
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:37:35 +02:00
|
|
|
log.Printf("[DEBUG] Read OpenStack Firewall Rule %s: %#v", d.Id(), rule)
|
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
d.Set("action", rule.Action)
|
2015-03-24 13:59:55 +01:00
|
|
|
d.Set("name", rule.Name)
|
|
|
|
d.Set("description", rule.Description)
|
|
|
|
d.Set("ip_version", rule.IPVersion)
|
|
|
|
d.Set("source_ip_address", rule.SourceIPAddress)
|
|
|
|
d.Set("destination_ip_address", rule.DestinationIPAddress)
|
|
|
|
d.Set("source_port", rule.SourcePort)
|
|
|
|
d.Set("destination_port", rule.DestinationPort)
|
|
|
|
d.Set("enabled", rule.Enabled)
|
2015-02-02 21:36:21 +01:00
|
|
|
|
2016-10-26 04:14:08 +02:00
|
|
|
if rule.Protocol == "" {
|
|
|
|
d.Set("protocol", "any")
|
|
|
|
} else {
|
|
|
|
d.Set("protocol", rule.Protocol)
|
|
|
|
}
|
|
|
|
|
2016-12-04 15:24:07 +01:00
|
|
|
d.Set("region", GetRegion(d))
|
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWRuleV1Update(d *schema.ResourceData, meta interface{}) error {
|
2015-02-02 21:36:21 +01:00
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-02 21:36:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rules.UpdateOpts{}
|
|
|
|
|
|
|
|
if d.HasChange("name") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("name").(string)
|
|
|
|
opts.Name = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("description") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("description").(string)
|
|
|
|
opts.Description = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("protocol") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("protocol").(string)
|
|
|
|
opts.Protocol = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("action") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("action").(string)
|
|
|
|
opts.Action = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("ip_version") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("ip_version").(int)
|
|
|
|
ipVersion := resourceFWRuleV1DetermineIPVersion(v)
|
|
|
|
opts.IPVersion = &ipVersion
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("source_ip_address") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("source_ip_address").(string)
|
|
|
|
opts.SourceIPAddress = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("destination_ip_address") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("destination_ip_address").(string)
|
|
|
|
opts.DestinationIPAddress = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("source_port") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("source_port").(string)
|
|
|
|
opts.SourcePort = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("destination_port") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("destination_port").(string)
|
|
|
|
opts.DestinationPort = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
if d.HasChange("enabled") {
|
2016-09-04 04:53:44 +02:00
|
|
|
v := d.Get("enabled").(bool)
|
|
|
|
opts.Enabled = &v
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating firewall rules: %#v", opts)
|
|
|
|
|
2015-02-19 23:44:49 +01:00
|
|
|
err = rules.Update(networkingClient, d.Id(), opts).Err
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceFWRuleV1Read(d, meta)
|
2015-02-02 21:36:21 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWRuleV1Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-02-02 21:36:21 +01:00
|
|
|
log.Printf("[DEBUG] Destroy firewall rule: %s", d.Id())
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-02 21:36:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-02-18 01:01:46 +01:00
|
|
|
|
|
|
|
rule, err := rules.Get(networkingClient, d.Id()).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.PolicyID != "" {
|
2016-09-04 04:53:44 +02:00
|
|
|
_, err := policies.RemoveRule(networkingClient, rule.PolicyID, rule.ID).Extract()
|
2015-02-18 01:01:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 21:36:21 +01:00
|
|
|
return rules.Delete(networkingClient, d.Id()).Err
|
|
|
|
}
|
2016-09-04 04:53:44 +02:00
|
|
|
|
|
|
|
func resourceFWRuleV1DetermineIPVersion(ipv int) gophercloud.IPVersion {
|
|
|
|
// Determine the IP Version
|
|
|
|
var ipVersion gophercloud.IPVersion
|
|
|
|
switch ipv {
|
|
|
|
case 4:
|
|
|
|
ipVersion = gophercloud.IPv4
|
|
|
|
case 6:
|
|
|
|
ipVersion = gophercloud.IPv6
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipVersion
|
|
|
|
}
|
2016-10-26 04:14:08 +02:00
|
|
|
|
|
|
|
func resourceFWRuleV1DetermineProtocol(p string) rules.Protocol {
|
|
|
|
var protocol rules.Protocol
|
|
|
|
switch p {
|
|
|
|
case "any":
|
|
|
|
protocol = rules.ProtocolAny
|
|
|
|
case "icmp":
|
|
|
|
protocol = rules.ProtocolICMP
|
|
|
|
case "tcp":
|
|
|
|
protocol = rules.ProtocolTCP
|
|
|
|
case "udp":
|
|
|
|
protocol = rules.ProtocolUDP
|
|
|
|
}
|
|
|
|
|
|
|
|
return protocol
|
|
|
|
}
|