Skip to content

Commit

Permalink
Merge pull request #360 from LBGarber/firewall-recreation-on-404
Browse files Browse the repository at this point in the history
Ensure missing resources are recreated
  • Loading branch information
LBGarber authored Jun 23, 2021
2 parents 95c17de + c11aded commit c0d225d
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 0 deletions.
6 changes: 6 additions & 0 deletions linode/resource_linode_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"strconv"
"strings"

Expand Down Expand Up @@ -177,6 +178,11 @@ func resourceLinodeFirewallRead(d *schema.ResourceData, meta interface{}) error

firewall, err := client.GetFirewall(context.Background(), id)
if err != nil {
if apiErr, ok := err.(*linodego.Error); ok && apiErr.Code == 404 {
log.Printf("[WARN] removing Linode Firewall ID %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("failed to get firewall %d: %s", id, err)
}

Expand Down
122 changes: 122 additions & 0 deletions linode/resource_linode_firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package linode
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/linode/linodego"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -332,6 +335,125 @@ func TestAccLinodeFirewall_updates(t *testing.T) {
})
}

func TestAccLinodeFirewall_externalDelete(t *testing.T) {
t.Parallel()

var firewall linodego.Firewall
name := acctest.RandomWithPrefix("tf_test")
devicePrefix := acctest.RandomWithPrefix("tf_test")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLinodeLKEClusterDestroy,
Steps: []resource.TestStep{
{
Config: accTestWithProvider(testAccCheckLinodeFirewallBasic(name, devicePrefix), map[string]interface{}{
providerKeySkipInstanceReadyPoll: true,
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckLinodeFirewallExists(testFirewallResName, &firewall),
resource.TestCheckResourceAttr(testFirewallResName, "label", name),
resource.TestCheckResourceAttr(testFirewallResName, "disabled", "false"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound_policy", "DROP"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.action", "ACCEPT"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.protocol", "TCP"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ports", "80"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv4.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv4.0", "0.0.0.0/0"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv6.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv6.0", "::/0"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound_policy", "DROP"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.action", "ACCEPT"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.protocol", "TCP"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ports", "80"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv4.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv4.0", "0.0.0.0/0"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv6.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv6.0", "2001:db8::/32"),
resource.TestCheckResourceAttr(testFirewallResName, "devices.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "devices.0.type", "linode"),
resource.TestCheckResourceAttr(testFirewallResName, "linodes.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "tags.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "tags.0", "test"),
),
},
{
PreConfig: func() {
// Delete the Firewall external from Terraform
client := testAccProvider.Meta().(*ProviderMeta).Client

if err := client.DeleteFirewall(context.Background(), firewall.ID); err != nil {
t.Fatalf("failed to delete firewall: %s", err)
}
},
Config: accTestWithProvider(testAccCheckLinodeFirewallBasic(name, devicePrefix), map[string]interface{}{
providerKeySkipInstanceReadyPoll: true,
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckLinodeFirewallExists(testFirewallResName, &firewall),
resource.TestCheckResourceAttr(testFirewallResName, "label", name),
resource.TestCheckResourceAttr(testFirewallResName, "disabled", "false"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound_policy", "DROP"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.action", "ACCEPT"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.protocol", "TCP"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ports", "80"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv4.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv4.0", "0.0.0.0/0"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv6.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "inbound.0.ipv6.0", "::/0"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound_policy", "DROP"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.action", "ACCEPT"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.protocol", "TCP"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ports", "80"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv4.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv4.0", "0.0.0.0/0"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv6.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "outbound.0.ipv6.0", "2001:db8::/32"),
resource.TestCheckResourceAttr(testFirewallResName, "devices.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "devices.0.type", "linode"),
resource.TestCheckResourceAttr(testFirewallResName, "linodes.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "tags.#", "1"),
resource.TestCheckResourceAttr(testFirewallResName, "tags.0", "test"),
),
},
},
})
}

func testAccCheckLinodeFirewallExists(name string, firewall *linodego.Firewall) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*ProviderMeta).Client

rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return fmt.Errorf("Error parsing %v to int", rs.Primary.ID)
}

found, err := client.GetFirewall(context.Background(), id)
if err != nil {
return fmt.Errorf("Error retrieving state of Firewall %s: %s", rs.Primary.Attributes["label"], err)
}

*firewall = *found

return nil
}
}

func testAccCheckLinodeFirewallInstance(prefix, identifier string) string {
return fmt.Sprintf(`
resource "linode_instance" "%[1]s" {
Expand Down
6 changes: 6 additions & 0 deletions linode/resource_linode_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"fmt"
"io"
"log"
"os"
"time"

Expand Down Expand Up @@ -134,6 +135,11 @@ func resourceLinodeImageRead(ctx context.Context, d *schema.ResourceData, meta i

image, err := client.GetImage(ctx, d.Id())
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
log.Printf("[WARN] removing Linode Image ID %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return diag.Errorf("Error getting Linode image %s: %s", d.Id(), err)
}

Expand Down
5 changes: 5 additions & 0 deletions linode/resource_linode_lke_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ func resourceLinodeLKEClusterRead(ctx context.Context, d *schema.ResourceData, m

cluster, err := client.GetLKECluster(context.Background(), id)
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
log.Printf("[WARN] removing LKE Cluster ID %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return diag.Errorf("failed to get LKE cluster %d: %s", id, err)
}

Expand Down
6 changes: 6 additions & 0 deletions linode/resource_linode_object_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linode
import (
"context"
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -181,6 +182,11 @@ func resourceLinodeObjectStorageBucketRead(d *schema.ResourceData, meta interfac

bucket, err := client.GetObjectStorageBucket(context.Background(), cluster, label)
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
log.Printf("[WARN] removing Object Storage Bucket %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("failed to find the specified Linode ObjectStorageBucket: %s", err)
}

Expand Down
6 changes: 6 additions & 0 deletions linode/resource_linode_object_storage_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linode
import (
"context"
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -108,6 +109,11 @@ func resourceLinodeObjectStorageKeyRead(d *schema.ResourceData, meta interface{}

objectStorageKey, err := client.GetObjectStorageKey(context.Background(), int(id))
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
log.Printf("[WARN] removing Object Storage Key %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error finding the specified Linode Object Storage Key: %s", err)
}

Expand Down
6 changes: 6 additions & 0 deletions linode/resource_linode_sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linode
import (
"context"
"fmt"
"log"
"strconv"
"time"

Expand Down Expand Up @@ -49,6 +50,11 @@ func resourceLinodeSSHKeyRead(d *schema.ResourceData, meta interface{}) error {

sshkey, err := client.GetSSHKey(context.Background(), int(id))
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
log.Printf("[WARN] removing Linode SSH Key ID %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error finding the specified Linode SSH Key: %s", err)
}

Expand Down
5 changes: 5 additions & 0 deletions linode/resource_linode_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func resourceLinodeTokenRead(d *schema.ResourceData, meta interface{}) error {

token, err := client.GetToken(context.Background(), int(id))
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
log.Printf("[WARN] removing Linode Token ID %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error finding the specified Linode Token: %s", err)
}

Expand Down
6 changes: 6 additions & 0 deletions linode/resource_linode_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"context"
"fmt"
"log"
)

var resourceLinodeUserGrantFields = []string{"global_grants", "domain_grant", "image_grant", "linode_grant",
Expand Down Expand Up @@ -191,6 +192,11 @@ func resourceLinodeUserRead(ctx context.Context, d *schema.ResourceData, meta in
username := d.Get("username").(string)
user, err := client.GetUser(ctx, username)
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
log.Printf("[WARN] removing Linode User %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return diag.Errorf("failed to get user (%s): %s", username, err)
}

Expand Down

0 comments on commit c0d225d

Please sign in to comment.