func testAccCheckFWFirewallV1Exists(n, expectedName, expectedDescription string, policyID *string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } if rs.Primary.ID == "" { return fmt.Errorf("No ID is set") } config := testAccProvider.Meta().(*Config) networkingClient, err := config.networkingV2Client(OS_REGION_NAME) if err != nil { return fmt.Errorf("(testAccCheckFirewallExists) Error creating OpenStack networking client: %s", err) } var found *firewalls.Firewall for i := 0; i < 5; i++ { // Firewall creation is asynchronous. Retry some times // if we get a 404 error. Fail on any other error. found, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract() if err != nil { httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) if !ok || httpError.Actual != 404 { time.Sleep(time.Second) continue } } break } if err != nil { return err } if found.Name != expectedName { return fmt.Errorf("Expected Name to be <%s> but found <%s>", expectedName, found.Name) } if found.Description != expectedDescription { return fmt.Errorf("Expected Description to be <%s> but found <%s>", expectedDescription, found.Description) } if found.PolicyID == "" { return fmt.Errorf("Policy should not be empty") } if policyID != nil && found.PolicyID == *policyID { return fmt.Errorf("Policy had not been correctly updated. Went from <%s> to <%s>", expectedName, found.Name) } policyID = &found.PolicyID return nil } }
func waitForFirewallToBeDeleted(t *testing.T, firewallID string) { for i := 0; i < 10; i++ { err := firewalls.Get(base.Client, firewallID).Err if err != nil { httpStatus := err.(*gophercloud.UnexpectedResponseCodeError) if httpStatus.Actual == 404 { return } } time.Sleep(time.Second) } }
func waitForFirewallActive(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc { return func() (interface{}, string, error) { fw, err := firewalls.Get(networkingClient, id).Extract() log.Printf("[DEBUG] Get firewall %s => %#v", id, fw) if err != nil { return nil, "", err } return fw, fw.Status, nil } }
func waitForFirewallDeletion(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc { return func() (interface{}, string, error) { fw, err := firewalls.Get(networkingClient, id).Extract() log.Printf("[DEBUG] Get firewall %s => %#v", id, fw) if err != nil { httpStatus := err.(*gophercloud.UnexpectedResponseCodeError) log.Printf("[DEBUG] Get firewall %s status is %d", id, httpStatus.Actual) if httpStatus.Actual == 404 { log.Printf("[DEBUG] Firewall %s is actually deleted", id) return "", "DELETED", nil } return nil, "", fmt.Errorf("Unexpected status code %d", httpStatus.Actual) } log.Printf("[DEBUG] Firewall %s deletion is pending", id) return fw, "DELETING", nil } }
func testAccCheckFWFirewallV1Destroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) networkingClient, err := config.networkingV2Client(OS_REGION_NAME) if err != nil { return fmt.Errorf("(testAccCheckOpenstackFirewallDestroy) Error creating OpenStack networking client: %s", err) } for _, rs := range s.RootModule().Resources { if rs.Type != "openstack_firewall" { continue } _, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract() if err == nil { return fmt.Errorf("Firewall (%s) still exists.", rs.Primary.ID) } httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) if !ok || httpError.Actual != 404 { return httpError } } return nil }
func resourceFWFirewallV1Read(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] Retrieve information about firewall: %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) } firewall, err := firewalls.Get(networkingClient, d.Id()).Extract() if err != nil { return CheckDeleted(d, err, "firewall") } 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) return nil }
func getFirewall(t *testing.T, firewallID string) *firewalls.Firewall { f, err := firewalls.Get(base.Client, firewallID).Extract() th.AssertNoErr(t, err) t.Logf("Getting firewall ID [%s]", f.ID) return f }