github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/builtin/providers/openstack/resource_openstack_fw_policy_v1.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/hashcode" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/rackspace/gophercloud" 11 "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/policies" 12 ) 13 14 func resourceFWPolicyV1() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceFWPolicyV1Create, 17 Read: resourceFWPolicyV1Read, 18 Update: resourceFWPolicyV1Update, 19 Delete: resourceFWPolicyV1Delete, 20 21 Schema: map[string]*schema.Schema{ 22 "region": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 DefaultFunc: envDefaultFunc("OS_REGION_NAME"), 27 }, 28 "name": &schema.Schema{ 29 Type: schema.TypeString, 30 Optional: true, 31 }, 32 "description": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 }, 36 "audited": &schema.Schema{ 37 Type: schema.TypeBool, 38 Optional: true, 39 Default: false, 40 }, 41 "shared": &schema.Schema{ 42 Type: schema.TypeBool, 43 Optional: true, 44 Default: false, 45 }, 46 "tenant_id": &schema.Schema{ 47 Type: schema.TypeString, 48 Optional: true, 49 ForceNew: true, 50 }, 51 "rules": &schema.Schema{ 52 Type: schema.TypeSet, 53 Optional: true, 54 Elem: &schema.Schema{Type: schema.TypeString}, 55 Set: func(v interface{}) int { 56 return hashcode.String(v.(string)) 57 }, 58 }, 59 }, 60 } 61 } 62 63 func resourceFWPolicyV1Create(d *schema.ResourceData, meta interface{}) error { 64 65 config := meta.(*Config) 66 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 67 if err != nil { 68 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 69 } 70 71 v := d.Get("rules").(*schema.Set) 72 73 log.Printf("[DEBUG] Rules found : %#v", v) 74 log.Printf("[DEBUG] Rules count : %d", v.Len()) 75 76 rules := make([]string, v.Len()) 77 for i, v := range v.List() { 78 rules[i] = v.(string) 79 } 80 81 audited := d.Get("audited").(bool) 82 shared := d.Get("shared").(bool) 83 84 opts := policies.CreateOpts{ 85 Name: d.Get("name").(string), 86 Description: d.Get("description").(string), 87 Audited: &audited, 88 Shared: &shared, 89 TenantID: d.Get("tenant_id").(string), 90 Rules: rules, 91 } 92 93 log.Printf("[DEBUG] Create firewall policy: %#v", opts) 94 95 policy, err := policies.Create(networkingClient, opts).Extract() 96 if err != nil { 97 return err 98 } 99 100 log.Printf("[DEBUG] Firewall policy created: %#v", policy) 101 102 d.SetId(policy.ID) 103 104 return resourceFWPolicyV1Read(d, meta) 105 } 106 107 func resourceFWPolicyV1Read(d *schema.ResourceData, meta interface{}) error { 108 log.Printf("[DEBUG] Retrieve information about firewall policy: %s", d.Id()) 109 110 config := meta.(*Config) 111 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 112 if err != nil { 113 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 114 } 115 116 policy, err := policies.Get(networkingClient, d.Id()).Extract() 117 118 if err != nil { 119 return CheckDeleted(d, err, "LB pool") 120 } 121 122 d.Set("name", policy.Name) 123 d.Set("description", policy.Description) 124 d.Set("shared", policy.Shared) 125 d.Set("audited", policy.Audited) 126 d.Set("tenant_id", policy.TenantID) 127 return nil 128 } 129 130 func resourceFWPolicyV1Update(d *schema.ResourceData, meta interface{}) error { 131 132 config := meta.(*Config) 133 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 134 if err != nil { 135 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 136 } 137 138 opts := policies.UpdateOpts{} 139 140 if d.HasChange("name") { 141 opts.Name = d.Get("name").(string) 142 } 143 144 if d.HasChange("description") { 145 opts.Description = d.Get("description").(string) 146 } 147 148 if d.HasChange("rules") { 149 v := d.Get("rules").(*schema.Set) 150 151 log.Printf("[DEBUG] Rules found : %#v", v) 152 log.Printf("[DEBUG] Rules count : %d", v.Len()) 153 154 rules := make([]string, v.Len()) 155 for i, v := range v.List() { 156 rules[i] = v.(string) 157 } 158 opts.Rules = rules 159 } 160 161 log.Printf("[DEBUG] Updating firewall policy with id %s: %#v", d.Id(), opts) 162 163 err = policies.Update(networkingClient, d.Id(), opts).Err 164 if err != nil { 165 return err 166 } 167 168 return resourceFWPolicyV1Read(d, meta) 169 } 170 171 func resourceFWPolicyV1Delete(d *schema.ResourceData, meta interface{}) error { 172 log.Printf("[DEBUG] Destroy firewall policy: %s", d.Id()) 173 174 config := meta.(*Config) 175 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 176 if err != nil { 177 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 178 } 179 180 for i := 0; i < 15; i++ { 181 182 err = policies.Delete(networkingClient, d.Id()).Err 183 if err == nil { 184 break 185 } 186 187 httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) 188 if !ok || httpError.Actual != 409 { 189 return err 190 } 191 192 // This error usualy means that the policy is attached 193 // to a firewall. At this point, the firewall is probably 194 // being delete. So, we retry a few times. 195 196 time.Sleep(time.Second * 2) 197 } 198 199 return err 200 }