-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r/security_group: Add option to forcefully revoke rules before deleti…
…on (#2074) * initial work for force revoking rules, basic vpc and sg sweeper * add docs, migration * huh * remove timeout for security groups
- Loading branch information
Showing
8 changed files
with
668 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceAwsSecurityGroupMigrateState( | ||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found AWS SecurityGroup State v0; migrating to v1") | ||
return migrateAwsSecurityGroupStateV0toV1(is) | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func migrateAwsSecurityGroupStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
if is.Empty() || is.Attributes == nil { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) | ||
|
||
// set default for revoke_rules_on_delete | ||
is.Attributes["revoke_rules_on_delete"] = "false" | ||
|
||
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes) | ||
return is, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAWSSecurityGroupMigrateState(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
Attributes map[string]string | ||
Expected map[string]string | ||
Meta interface{} | ||
}{ | ||
"v0": { | ||
StateVersion: 0, | ||
Attributes: map[string]string{ | ||
"name": "test", | ||
}, | ||
Expected: map[string]string{ | ||
"name": "test", | ||
"revoke_rules_on_delete": "false", | ||
}, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
is := &terraform.InstanceState{ | ||
ID: "i-abc123", | ||
Attributes: tc.Attributes, | ||
} | ||
is, err := resourceAwsSecurityGroupMigrateState( | ||
tc.StateVersion, is, tc.Meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad: %s, err: %#v", tn, err) | ||
} | ||
|
||
for k, v := range tc.Expected { | ||
if is.Attributes[k] != v { | ||
t.Fatalf( | ||
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v", | ||
tn, k, v, k, is.Attributes[k], is.Attributes) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestAWSSecurityGroupMigrateState_empty(t *testing.T) { | ||
var is *terraform.InstanceState | ||
var meta interface{} | ||
|
||
// should handle nil | ||
is, err := resourceAwsSecurityGroupMigrateState(0, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("err: %#v", err) | ||
} | ||
if is != nil { | ||
t.Fatalf("expected nil instancestate, got: %#v", is) | ||
} | ||
|
||
// should handle non-nil but empty | ||
is = &terraform.InstanceState{} | ||
is, err = resourceAwsSecurityGroupMigrateState(0, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("err: %#v", err) | ||
} | ||
} |
Oops, something went wrong.