Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support password data on spot requests #4189

Merged
merged 2 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions aws/resource_aws_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@ func readInstance(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("ipv6_addresses", ipv6Addresses); err != nil {
log.Printf("[WARN] Error setting ipv6_addresses for AWS Spot Instance (%s): %s", d.Id(), err)
}

if d.Get("get_password_data").(bool) {
passwordData, err := getAwsEc2InstancePasswordData(*instance.InstanceId, conn)
if err != nil {
return err
}
d.Set("password_data", passwordData)
} else {
d.Set("get_password_data", false)
d.Set("password_data", nil)
}
}

return nil
Expand Down
54 changes: 54 additions & 0 deletions aws/resource_aws_spot_instance_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ func TestAccAWSSpotInstanceRequest_NetworkInterfaceAttributes(t *testing.T) {
})
}

func TestAccAWSSpotInstanceRequest_getPasswordData(t *testing.T) {
var sir ec2.SpotInstanceRequest
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotInstanceRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotInstanceRequestConfig_getPasswordData(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSpotInstanceRequestExists(
"aws_spot_instance_request.foo", &sir),
resource.TestCheckResourceAttrSet("aws_spot_instance_request.foo", "password_data"),
),
},
},
})
}

func testCheckKeyPair(keyName string, sir *ec2.SpotInstanceRequest) resource.TestCheckFunc {
return func(*terraform.State) error {
if sir.LaunchSpecification.KeyName == nil {
Expand Down Expand Up @@ -535,3 +556,36 @@ func testAccAWSSpotInstanceRequestConfig_SubnetAndSGAndPublicIpAddress(rInt int)
}
}`, rInt, rInt)
}

func testAccAWSSpotInstanceRequestConfig_getPasswordData(rInt int) string {
return fmt.Sprintf(`
# Find latest Microsoft Windows Server 2016 Core image (Amazon deletes old ones)
data "aws_ami" "win2016core" {
most_recent = true

filter {
name = "owner-alias"
values = ["amazon"]
}

filter {
name = "name"
values = ["Windows_Server-2016-English-Core-Base-*"]
}
}

resource "aws_key_pair" "foo" {
key_name = "tf-acctest-%d"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAq6U3HQYC4g8WzU147gZZ7CKQH8TgYn3chZGRPxaGmHW1RUwsyEs0nmombmIhwxudhJ4ehjqXsDLoQpd6+c7BuLgTMvbv8LgE9LX53vnljFe1dsObsr/fYLvpU9LTlo8HgHAqO5ibNdrAUvV31ronzCZhms/Gyfdaue88Fd0/YnsZVGeOZPayRkdOHSpqme2CBrpa8myBeL1CWl0LkDG4+YCURjbaelfyZlIApLYKy3FcCan9XQFKaL32MJZwCgzfOvWIMtYcU8QtXMgnA3/I3gXk8YDUJv5P4lj0s/PJXuTM8DygVAUtebNwPuinS7wwonm5FXcWMuVGsVpG5K7FGQ== tf-acc-winpasswordtest"
}

resource "aws_spot_instance_request" "foo" {
ami = "${data.aws_ami.win2016core.id}"
instance_type = "m1.small"
spot_price = "0.05"
key_name = "${aws_key_pair.foo.key_name}"
wait_for_fulfillment = true
get_password_data = true
}
`, rInt)
}