Skip to content

Commit

Permalink
Merge pull request #2619 from kl4w/t2_unlimited
Browse files Browse the repository at this point in the history
r/aws_instance: add T2 unlimited to aws instance resources
  • Loading branch information
bflad authored Apr 20, 2018
2 parents fafddd8 + 071ee81 commit 633f9bd
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 0 deletions.
21 changes: 21 additions & 0 deletions aws/data_source_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ func dataSourceAwsInstance() *schema.Resource {
},
},
},
"credit_specification": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cpu_credits": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -389,6 +401,15 @@ func instanceDescriptionAttributes(d *schema.ResourceData, instance *ec2.Instanc
d.Set("user_data", userDataHashSum(*attr.UserData.Value))
}
}
{
creditSpecifications, err := getCreditSpecifications(conn, d.Id())
if err != nil {
return err
}
if err := d.Set("credit_specification", creditSpecifications); err != nil {
return fmt.Errorf("error setting credit_specification: %s", err)
}
}

return nil
}
42 changes: 42 additions & 0 deletions aws/data_source_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ func TestAccAWSInstanceDataSource_getPasswordData_falseToTrue(t *testing.T) {
})
}

func TestAccAWSInstanceDataSource_creditSpecification(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{

Config: testAccInstanceDataSourceConfig_creditSpecification,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_instance.foo", "instance_type", "t2.micro"),
resource.TestCheckResourceAttr("data.aws_instance.foo", "credit_specification.#", "1"),
resource.TestCheckResourceAttr("data.aws_instance.foo", "credit_specification.0.cpu_credits", "unlimited"),
),
},
},
})
}

// Lookup based on InstanceID
const testAccInstanceDataSourceConfig = `
resource "aws_instance" "web" {
Expand Down Expand Up @@ -658,3 +676,27 @@ func testAccInstanceDataSourceConfig_getPasswordData(val bool, rInt int) string
}
`, rInt, val)
}

const testAccInstanceDataSourceConfig_creditSpecification = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
}
resource "aws_instance" "foo" {
ami = "ami-bf4193c7"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.foo.id}"
credit_specification {
cpu_credits = "unlimited"
}
}
data "aws_instance" "foo" {
instance_id = "${aws_instance.foo.id}"
}
`
75 changes: 75 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,27 @@ func resourceAwsInstance() *schema.Resource {
},
},
},

"credit_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cpu_credits": {
Type: schema.TypeString,
Optional: true,
Default: "standard",
},
},
},
},
},
}
}
Expand Down Expand Up @@ -475,6 +496,7 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
SecurityGroups: instanceOpts.SecurityGroups,
SubnetId: instanceOpts.SubnetID,
UserData: instanceOpts.UserData64,
CreditSpecification: instanceOpts.CreditSpecification,
}

_, ipv6CountOk := d.GetOk("ipv6_address_count")
Expand Down Expand Up @@ -783,6 +805,15 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
}
}
}
{
creditSpecifications, err := getCreditSpecifications(conn, d.Id())
if err != nil {
return err
}
if err := d.Set("credit_specification", creditSpecifications); err != nil {
return fmt.Errorf("error setting credit_specification: %s", err)
}
}

if d.Get("get_password_data").(bool) {
passwordData, err := getAwsEc2InstancePasswordData(*instance.InstanceId, conn)
Expand Down Expand Up @@ -1079,6 +1110,24 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("credit_specification") {
if v, ok := d.GetOk("credit_specification"); ok {
creditSpecification := v.([]interface{})[0].(map[string]interface{})
log.Printf("[DEBUG] Modifying credit specification for Instance (%s)", d.Id())
_, err := conn.ModifyInstanceCreditSpecification(&ec2.ModifyInstanceCreditSpecificationInput{
InstanceCreditSpecifications: []*ec2.InstanceCreditSpecificationRequest{
{
InstanceId: aws.String(d.Id()),
CpuCredits: aws.String(creditSpecification["cpu_credits"].(string)),
},
},
})
if err != nil {
return fmt.Errorf("[WARN] Error updating Instance credit specification: %s", err)
}
}
}

// TODO(mitchellh): wait for the attributes we modified to
// persist the change...

Expand Down Expand Up @@ -1631,6 +1680,7 @@ type awsInstanceOpts struct {
SpotPlacement *ec2.SpotPlacement
SubnetID *string
UserData64 *string
CreditSpecification *ec2.CreditSpecificationRequest
}

func buildAwsInstanceOpts(
Expand All @@ -1644,6 +1694,13 @@ func buildAwsInstanceOpts(
InstanceType: aws.String(d.Get("instance_type").(string)),
}

if v, ok := d.GetOk("credit_specification"); ok {
cs := v.([]interface{})[0].(map[string]interface{})
opts.CreditSpecification = &ec2.CreditSpecificationRequest{
CpuCredits: aws.String(cs["cpu_credits"].(string)),
}
}

if v := d.Get("instance_initiated_shutdown_behavior").(string); v != "" {
opts.InstanceInitiatedShutdownBehavior = aws.String(v)
}
Expand Down Expand Up @@ -1833,3 +1890,21 @@ func getAwsInstanceVolumeIds(conn *ec2.EC2, d *schema.ResourceData) ([]*string,

return volumeIds, nil
}

func getCreditSpecifications(conn *ec2.EC2, instanceId string) ([]map[string]interface{}, error) {
var creditSpecifications []map[string]interface{}
creditSpecification := make(map[string]interface{})

attr, err := conn.DescribeInstanceCreditSpecifications(&ec2.DescribeInstanceCreditSpecificationsInput{
InstanceIds: []*string{aws.String(instanceId)},
})
if err != nil {
return nil, err
}
if attr.InstanceCreditSpecifications != nil {
creditSpecification["cpu_credits"] = *attr.InstanceCreditSpecifications[0].CpuCredits
creditSpecifications = append(creditSpecifications, creditSpecification)
}

return creditSpecifications, nil
}
Loading

0 comments on commit 633f9bd

Please sign in to comment.