-
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.
provider/aws: Migrate KeyPair to version 1
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceAwsKeyPairMigrateState( | ||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found AWS Key Pair State v0; migrating to v1") | ||
return migrateKeyPairStateV0toV1(is) | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
|
||
return is, nil | ||
} | ||
|
||
func migrateKeyPairStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) | ||
|
||
// replace public_key with a stripped version, removing `\n` from the end | ||
// see https://github.com/hashicorp/terraform/issues/3455 | ||
is.Attributes["public_key"] = strings.TrimSpace(is.Attributes["public_key"]) | ||
|
||
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,55 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAWSKeyPairMigrateState(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
ID string | ||
Attributes map[string]string | ||
Expected string | ||
Meta interface{} | ||
}{ | ||
"v0_1": { | ||
StateVersion: 0, | ||
ID: "tf-testing-file", | ||
Attributes: map[string]string{ | ||
"fingerprint": "1d:cd:46:31:a9:4a:e0:06:8a:a1:22:cb:3b:bf:8e:42", | ||
"key_name": "tf-testing-file", | ||
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4LBtwcFsQAYWw1cnOwRTZCJCzPSzq0dl3== ctshryock", | ||
}, | ||
Expected: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4LBtwcFsQAYWw1cnOwRTZCJCzPSzq0dl3== ctshryock", | ||
}, | ||
"v0_2": { | ||
StateVersion: 0, | ||
ID: "tf-testing-file", | ||
Attributes: map[string]string{ | ||
"fingerprint": "1d:cd:46:31:a9:4a:e0:06:8a:a1:22:cb:3b:bf:8e:42", | ||
"key_name": "tf-testing-file", | ||
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4LBtwcFsQAYWw1cnOwRTZCJCzPSzq0dl3== ctshryock\n", | ||
}, | ||
Expected: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4LBtwcFsQAYWw1cnOwRTZCJCzPSzq0dl3== ctshryock", | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
is := &terraform.InstanceState{ | ||
ID: tc.ID, | ||
Attributes: tc.Attributes, | ||
} | ||
is, err := resourceAwsKeyPairMigrateState( | ||
tc.StateVersion, is, tc.Meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad: %s, err: %#v", tn, err) | ||
} | ||
|
||
if is.Attributes["public_key"] != tc.Expected { | ||
t.Fatalf("Bad public_key migration: %s\n\n expected: %s", is.Attributes["public_key"], tc.Expected) | ||
} | ||
} | ||
} |