Skip to content

Commit

Permalink
Merge pull request #4189 from aspectcapital/spot-password-data
Browse files Browse the repository at this point in the history
Support password data on spot requests
  • Loading branch information
bflad authored Apr 12, 2018
2 parents 90a0813 + 3cbbe2c commit 673afb2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
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)
}

0 comments on commit 673afb2

Please sign in to comment.