2015-02-03 20:56:13 +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/policies"
|
2015-02-03 20:56:13 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWPolicyV1() *schema.Resource {
|
2015-02-03 20:56:13 +01:00
|
|
|
return &schema.Resource{
|
2015-02-19 22:55:54 +01:00
|
|
|
Create: resourceFWPolicyV1Create,
|
|
|
|
Read: resourceFWPolicyV1Read,
|
|
|
|
Update: resourceFWPolicyV1Update,
|
|
|
|
Delete: resourceFWPolicyV1Delete,
|
2016-07-03 17:37:35 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-02-03 20:56:13 +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 20:56:13 +01:00
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"audited": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
"shared": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
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
|
|
|
},
|
2015-02-03 20:56:13 +01:00
|
|
|
"rules": &schema.Schema{
|
2016-06-16 13:27:01 +02:00
|
|
|
Type: schema.TypeList,
|
2015-02-03 20:56:13 +01:00
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWPolicyV1Create(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 20:56:13 +01:00
|
|
|
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-16 13:27:01 +02:00
|
|
|
v := d.Get("rules").([]interface{})
|
2015-02-03 20:56:13 +01:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Rules found : %#v", v)
|
2016-06-16 13:27:01 +02:00
|
|
|
log.Printf("[DEBUG] Rules count : %d", len(v))
|
2015-02-03 20:56:13 +01:00
|
|
|
|
2016-06-16 13:27:01 +02:00
|
|
|
rules := make([]string, len(v))
|
|
|
|
for i, v := range v {
|
2015-02-03 20:56:13 +01:00
|
|
|
rules[i] = v.(string)
|
|
|
|
}
|
|
|
|
|
2015-02-10 00:19:01 +01:00
|
|
|
audited := d.Get("audited").(bool)
|
|
|
|
shared := d.Get("shared").(bool)
|
|
|
|
|
2015-02-03 20:56:13 +01:00
|
|
|
opts := policies.CreateOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
2015-02-10 00:19:01 +01:00
|
|
|
Audited: &audited,
|
|
|
|
Shared: &shared,
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
Rules: rules,
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Create firewall policy: %#v", opts)
|
|
|
|
|
|
|
|
policy, err := policies.Create(networkingClient, opts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Firewall policy created: %#v", policy)
|
2015-02-03 20:56:13 +01:00
|
|
|
|
|
|
|
d.SetId(policy.ID)
|
|
|
|
|
2015-02-19 23:44:49 +01:00
|
|
|
return resourceFWPolicyV1Read(d, meta)
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWPolicyV1Read(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 20:56:13 +01:00
|
|
|
log.Printf("[DEBUG] Retrieve information about firewall policy: %s", d.Id())
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := policies.Get(networkingClient, d.Id()).Extract()
|
|
|
|
if err != nil {
|
2015-05-05 14:01:49 +02:00
|
|
|
return CheckDeleted(d, err, "FW policy")
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:37:35 +02:00
|
|
|
log.Printf("[DEBUG] Read OpenStack Firewall Policy %s: %#v", d.Id(), policy)
|
|
|
|
|
2015-03-24 13:59:55 +01:00
|
|
|
d.Set("name", policy.Name)
|
|
|
|
d.Set("description", policy.Description)
|
|
|
|
d.Set("shared", policy.Shared)
|
|
|
|
d.Set("audited", policy.Audited)
|
|
|
|
d.Set("tenant_id", policy.TenantID)
|
2016-07-03 17:37:35 +02:00
|
|
|
d.Set("rules", policy.Rules)
|
2015-02-03 20:56:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWPolicyV1Update(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 20:56:13 +01:00
|
|
|
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := policies.UpdateOpts{}
|
|
|
|
|
|
|
|
if d.HasChange("name") {
|
2015-02-17 22:07:01 +01:00
|
|
|
opts.Name = d.Get("name").(string)
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("description") {
|
2015-02-17 22:07:01 +01:00
|
|
|
opts.Description = d.Get("description").(string)
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("rules") {
|
2016-06-16 13:27:01 +02:00
|
|
|
v := d.Get("rules").([]interface{})
|
2015-02-03 20:56:13 +01:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Rules found : %#v", v)
|
2016-06-16 13:27:01 +02:00
|
|
|
log.Printf("[DEBUG] Rules count : %d", len(v))
|
2015-02-03 20:56:13 +01:00
|
|
|
|
2016-06-16 13:27:01 +02:00
|
|
|
rules := make([]string, len(v))
|
|
|
|
for i, v := range v {
|
2015-02-03 20:56:13 +01:00
|
|
|
rules[i] = v.(string)
|
|
|
|
}
|
|
|
|
opts.Rules = rules
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating firewall policy with id %s: %#v", d.Id(), opts)
|
|
|
|
|
2015-02-19 23:44:49 +01:00
|
|
|
err = policies.Update(networkingClient, d.Id(), opts).Err
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceFWPolicyV1Read(d, meta)
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func resourceFWPolicyV1Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-02-03 20:56:13 +01:00
|
|
|
log.Printf("[DEBUG] Destroy firewall policy: %s", d.Id())
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 15; i++ {
|
|
|
|
|
|
|
|
err = policies.Delete(networkingClient, d.Id()).Err
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-09-05 04:52:41 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
|
|
|
return nil
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 04:52:41 +02:00
|
|
|
if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
|
|
|
|
if errCode.Actual == 409 {
|
|
|
|
// This error usually means that the policy is attached
|
|
|
|
// to a firewall. At this point, the firewall is probably
|
|
|
|
// being delete. So, we retry a few times.
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 20:56:13 +01:00
|
|
|
|
2016-09-05 04:52:41 +02:00
|
|
|
return err
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 04:52:41 +02:00
|
|
|
return nil
|
2015-02-03 20:56:13 +01:00
|
|
|
}
|