2015-02-03 22:14:56 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2016-09-04 04:53:44 +02:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
|
2017-06-01 07:19:59 +02:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/routerinsertion"
|
2015-02-03 22:14:56 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWFirewallV1() *schema.Resource {
|
2015-02-03 22:14:56 +01:00
|
|
|
return &schema.Resource{
|
2015-02-19 22:55:54 +01:00
|
|
|
Create: resourceFWFirewallV1Create,
|
|
|
|
Read: resourceFWFirewallV1Read,
|
|
|
|
Update: resourceFWFirewallV1Update,
|
|
|
|
Delete: resourceFWFirewallV1Delete,
|
2016-07-03 17:37:35 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-02-03 22:14:56 +01:00
|
|
|
|
2017-03-19 18:09:22 +01:00
|
|
|
Timeouts: &schema.ResourceTimeout{
|
|
|
|
Create: schema.DefaultTimeout(10 * time.Minute),
|
|
|
|
Update: schema.DefaultTimeout(10 * time.Minute),
|
|
|
|
Delete: schema.DefaultTimeout(10 * time.Minute),
|
|
|
|
},
|
|
|
|
|
2015-02-03 22:14:56 +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-03 22:14:56 +01:00
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"policy_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"admin_state_up": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2016-11-03 13:13:54 +01:00
|
|
|
Default: true,
|
2015-02-03 22:14:56 +01:00
|
|
|
},
|
2015-02-10 00:19:01 +01:00
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-05-10 06:38:36 +02:00
|
|
|
Computed: true,
|
2015-02-10 00:19:01 +01:00
|
|
|
},
|
2017-06-01 07:19:59 +02:00
|
|
|
"associated_routers": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
ConflictsWith: []string{"no_routers"},
|
|
|
|
},
|
|
|
|
"no_routers": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"associated_routers"},
|
|
|
|
},
|
2016-11-05 11:06:49 +01:00
|
|
|
"value_specs": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-02-03 22:14:56 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWFirewallV1Create(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 22:14:56 +01:00
|
|
|
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-03 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
var createOpts firewalls.CreateOptsBuilder
|
|
|
|
|
2015-02-03 22:14:56 +01:00
|
|
|
adminStateUp := d.Get("admin_state_up").(bool)
|
2017-06-01 07:19:59 +02:00
|
|
|
createOpts = &firewalls.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
PolicyID: d.Get("policy_id").(string),
|
|
|
|
AdminStateUp: &adminStateUp,
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
}
|
2015-02-03 22:14:56 +01:00
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
associatedRoutersRaw := d.Get("associated_routers").(*schema.Set).List()
|
|
|
|
if len(associatedRoutersRaw) > 0 {
|
|
|
|
log.Printf("[DEBUG] Will attempt to associate Firewall with router(s): %+v", associatedRoutersRaw)
|
|
|
|
|
|
|
|
var routerIds []string
|
|
|
|
for _, v := range associatedRoutersRaw {
|
|
|
|
routerIds = append(routerIds, v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
createOpts = &routerinsertion.CreateOptsExt{
|
|
|
|
CreateOptsBuilder: createOpts,
|
|
|
|
RouterIDs: routerIds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Get("no_routers").(bool) {
|
|
|
|
routerIds := make([]string, 0)
|
|
|
|
log.Println("[DEBUG] No routers specified. Setting to empty slice")
|
|
|
|
createOpts = &routerinsertion.CreateOptsExt{
|
|
|
|
CreateOptsBuilder: createOpts,
|
|
|
|
RouterIDs: routerIds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createOpts = &FirewallCreateOpts{
|
|
|
|
createOpts,
|
2016-11-05 11:06:49 +01:00
|
|
|
MapValueSpecs(d),
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
log.Printf("[DEBUG] Create firewall: %#v", createOpts)
|
2015-02-03 22:14:56 +01:00
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
firewall, err := firewalls.Create(networkingClient, createOpts).Extract()
|
2015-02-03 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Firewall created: %#v", firewall)
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING_CREATE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ACTIVE"},
|
2015-02-19 22:55:54 +01:00
|
|
|
Refresh: waitForFirewallActive(networkingClient, firewall.ID),
|
2017-03-19 18:09:22 +01:00
|
|
|
Timeout: d.Timeout(schema.TimeoutCreate),
|
2015-02-03 22:14:56 +01:00
|
|
|
Delay: 0,
|
|
|
|
MinTimeout: 2 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
2017-06-01 07:19:59 +02:00
|
|
|
log.Printf("[DEBUG] Firewall (%s) is active.", firewall.ID)
|
2015-02-03 22:14:56 +01:00
|
|
|
|
2015-02-18 00:12:04 +01:00
|
|
|
d.SetId(firewall.ID)
|
|
|
|
|
2015-02-19 23:44:49 +01:00
|
|
|
return resourceFWFirewallV1Read(d, meta)
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWFirewallV1Read(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 22:14:56 +01:00
|
|
|
log.Printf("[DEBUG] Retrieve information about firewall: %s", d.Id())
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-03 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
var firewall Firewall
|
|
|
|
err = firewalls.Get(networkingClient, d.Id()).ExtractInto(&firewall)
|
2015-02-03 22:14:56 +01:00
|
|
|
if err != nil {
|
2015-05-05 14:01:49 +02:00
|
|
|
return CheckDeleted(d, err, "firewall")
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:37:35 +02:00
|
|
|
log.Printf("[DEBUG] Read OpenStack Firewall %s: %#v", d.Id(), firewall)
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
d.Set("name", firewall.Name)
|
|
|
|
d.Set("description", firewall.Description)
|
|
|
|
d.Set("policy_id", firewall.PolicyID)
|
|
|
|
d.Set("admin_state_up", firewall.AdminStateUp)
|
|
|
|
d.Set("tenant_id", firewall.TenantID)
|
2016-12-04 15:24:07 +01:00
|
|
|
d.Set("region", GetRegion(d))
|
2017-06-01 07:19:59 +02:00
|
|
|
d.Set("associated_routers", firewall.RouterIDs)
|
2015-02-03 22:14:56 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWFirewallV1Update(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 22:14:56 +01:00
|
|
|
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-03 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
// PolicyID is required
|
|
|
|
opts := firewalls.UpdateOpts{
|
|
|
|
PolicyID: d.Get("policy_id").(string),
|
|
|
|
}
|
2015-02-03 22:14:56 +01:00
|
|
|
|
|
|
|
if d.HasChange("name") {
|
2015-02-17 22:07:01 +01:00
|
|
|
opts.Name = d.Get("name").(string)
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("description") {
|
2015-02-17 22:07:01 +01:00
|
|
|
opts.Description = d.Get("description").(string)
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:12:04 +01:00
|
|
|
if d.HasChange("admin_state_up") {
|
|
|
|
adminStateUp := d.Get("admin_state_up").(bool)
|
|
|
|
opts.AdminStateUp = &adminStateUp
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
var updateOpts firewalls.UpdateOptsBuilder
|
|
|
|
var routerIds []string
|
|
|
|
if d.HasChange("associated_routers") || d.HasChange("no_routers") {
|
|
|
|
// 'no_routers' = true means 'associated_routers' will be empty...
|
|
|
|
if d.Get("no_routers").(bool) {
|
|
|
|
log.Printf("[DEBUG] 'no_routers' is true.")
|
|
|
|
routerIds = make([]string, 0)
|
|
|
|
} else {
|
|
|
|
associatedRoutersRaw := d.Get("associated_routers").(*schema.Set).List()
|
|
|
|
for _, v := range associatedRoutersRaw {
|
|
|
|
routerIds = append(routerIds, v.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOpts = routerinsertion.UpdateOptsExt{
|
|
|
|
UpdateOptsBuilder: opts,
|
|
|
|
RouterIDs: routerIds,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updateOpts = opts
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating firewall with id %s: %#v", d.Id(), updateOpts)
|
|
|
|
|
|
|
|
err = firewalls.Update(networkingClient, d.Id(), updateOpts).Err
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-18 00:12:04 +01:00
|
|
|
|
2015-02-03 22:14:56 +01:00
|
|
|
stateConf := &resource.StateChangeConf{
|
2015-02-18 00:12:04 +01:00
|
|
|
Pending: []string{"PENDING_CREATE", "PENDING_UPDATE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ACTIVE"},
|
2015-02-19 22:55:54 +01:00
|
|
|
Refresh: waitForFirewallActive(networkingClient, d.Id()),
|
2017-03-19 18:09:22 +01:00
|
|
|
Timeout: d.Timeout(schema.TimeoutUpdate),
|
2015-02-03 22:14:56 +01:00
|
|
|
Delay: 0,
|
|
|
|
MinTimeout: 2 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
|
2015-02-19 23:44:49 +01:00
|
|
|
return resourceFWFirewallV1Read(d, meta)
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWFirewallV1Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 22:14:56 +01:00
|
|
|
log.Printf("[DEBUG] Destroy firewall: %s", d.Id())
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(GetRegion(d))
|
2015-02-03 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-19 18:09:22 +01:00
|
|
|
// Ensure the firewall was fully created/updated before being deleted.
|
2015-02-18 00:12:04 +01:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING_CREATE", "PENDING_UPDATE"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ACTIVE"},
|
2015-02-19 22:55:54 +01:00
|
|
|
Refresh: waitForFirewallActive(networkingClient, d.Id()),
|
2017-03-19 18:09:22 +01:00
|
|
|
Timeout: d.Timeout(schema.TimeoutUpdate),
|
2015-02-18 00:12:04 +01:00
|
|
|
Delay: 0,
|
|
|
|
MinTimeout: 2 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
|
2015-02-03 22:14:56 +01:00
|
|
|
err = firewalls.Delete(networkingClient, d.Id()).Err
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-18 00:12:04 +01:00
|
|
|
stateConf = &resource.StateChangeConf{
|
2015-02-03 22:14:56 +01:00
|
|
|
Pending: []string{"DELETING"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"DELETED"},
|
2015-02-19 22:55:54 +01:00
|
|
|
Refresh: waitForFirewallDeletion(networkingClient, d.Id()),
|
2017-03-19 18:09:22 +01:00
|
|
|
Timeout: d.Timeout(schema.TimeoutDelete),
|
2015-02-03 22:14:56 +01:00
|
|
|
Delay: 0,
|
|
|
|
MinTimeout: 2 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func waitForFirewallActive(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc {
|
2015-02-03 22:14:56 +01:00
|
|
|
|
|
|
|
return func() (interface{}, string, error) {
|
2017-06-01 07:19:59 +02:00
|
|
|
var fw Firewall
|
2015-02-03 22:14:56 +01:00
|
|
|
|
2017-06-01 07:19:59 +02:00
|
|
|
err := firewalls.Get(networkingClient, id).ExtractInto(&fw)
|
2015-02-03 22:14:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return fw, fw.Status, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func waitForFirewallDeletion(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc {
|
2015-02-03 22:14:56 +01:00
|
|
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
fw, err := firewalls.Get(networkingClient, id).Extract()
|
2017-06-01 07:19:59 +02:00
|
|
|
log.Printf("[DEBUG] Got firewall %s => %#v", id, fw)
|
2015-02-03 22:14:56 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2015-02-03 22:14:56 +01:00
|
|
|
log.Printf("[DEBUG] Firewall %s is actually deleted", id)
|
|
|
|
return "", "DELETED", nil
|
|
|
|
}
|
2016-09-04 06:57:25 +02:00
|
|
|
return nil, "", fmt.Errorf("Unexpected error: %s", err)
|
2015-02-03 22:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Firewall %s deletion is pending", id)
|
|
|
|
return fw, "DELETING", nil
|
|
|
|
}
|
|
|
|
}
|