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

rd/launch_template - add hibernation options #12492

Merged
merged 5 commits into from
Mar 30, 2020
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
16 changes: 16 additions & 0 deletions aws/data_source_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ func dataSourceAwsLaunchTemplate() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"hibernation_options": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"configured": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"tags": tagsSchemaComputed(),
"filter": dataSourceFiltersSchema(),
},
Expand Down Expand Up @@ -492,6 +504,10 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("error setting placement: %s", err)
}

if err := d.Set("hibernation_options", flattenLaunchTemplateHibernationOptions(ltData.HibernationOptions)); err != nil {
return fmt.Errorf("error setting hibernation_options: %s", err)
}

if err := d.Set("tag_specifications", getTagSpecifications(ltData.TagSpecifications)); err != nil {
return fmt.Errorf("error setting tag_specifications: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions aws/data_source_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccAWSLaunchTemplateDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "default_version", dataSourceName, "default_version"),
resource.TestCheckResourceAttrPair(resourceName, "latest_version", dataSourceName, "latest_version"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "hibernation_options", dataSourceName, "hibernation_options"),
),
},
},
Expand Down
46 changes: 46 additions & 0 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,19 @@ func resourceAwsLaunchTemplate() *schema.Resource {
},

"tags": tagsSchema(),
"hibernation_options": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"configured": {
Type: schema.TypeBool,
Required: true,
},
},
},
},
},

CustomizeDiff: customdiff.Sequence(
Expand Down Expand Up @@ -742,6 +755,10 @@ func resourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("error setting placement: %s", err)
}

if err := d.Set("hibernation_options", flattenLaunchTemplateHibernationOptions(ltData.HibernationOptions)); err != nil {
return fmt.Errorf("error setting hibernation_options: %s", err)
}

if err := d.Set("tag_specifications", getTagSpecifications(ltData.TagSpecifications)); err != nil {
return fmt.Errorf("error setting tag_specifications: %s", err)
}
Expand Down Expand Up @@ -1098,6 +1115,31 @@ func getPlacement(p *ec2.LaunchTemplatePlacement) []interface{} {
return s
}

func expandLaunchTemplateHibernationOptions(l []interface{}) *ec2.LaunchTemplateHibernationOptionsRequest {
if len(l) == 0 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

opts := &ec2.LaunchTemplateHibernationOptionsRequest{
Configured: aws.Bool(m["configured"].(bool)),
}

return opts
}

func flattenLaunchTemplateHibernationOptions(m *ec2.LaunchTemplateHibernationOptions) []interface{} {
s := []interface{}{}
if m != nil {
mo := map[string]interface{}{
"configured": aws.BoolValue(m.Configured),
}
s = append(s, mo)
}
return s
}

func getTagSpecifications(t []*ec2.LaunchTemplateTagSpecification) []interface{} {
s := []interface{}{}
for _, v := range t {
Expand Down Expand Up @@ -1288,6 +1330,10 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
}
}

if v, ok := d.GetOk("hibernation_options"); ok {
opts.HibernationOptions = expandLaunchTemplateHibernationOptions(v.([]interface{}))
}

if v, ok := d.GetOk("tag_specifications"); ok {
var tagSpecifications []*ec2.LaunchTemplateTagSpecificationRequest
t := v.([]interface{})
Expand Down
52 changes: 52 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,46 @@ func TestAccAWSLaunchTemplate_metadataOptions(t *testing.T) {
})
}

func TestAccAWSLaunchTemplate_hibernation(t *testing.T) {
var template ec2.LaunchTemplate
resourceName := "aws_launch_template.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateConfigHibernation(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
resource.TestCheckResourceAttr(resourceName, "hibernation_options.0.configured", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSLaunchTemplateConfigHibernation(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
resource.TestCheckResourceAttr(resourceName, "hibernation_options.0.configured", "false"),
),
},
{
Config: testAccAWSLaunchTemplateConfigHibernation(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template),
resource.TestCheckResourceAttr(resourceName, "hibernation_options.0.configured", "true"),
),
},
},
})
}

func testAccCheckAWSLaunchTemplateExists(n string, t *ec2.LaunchTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -1550,3 +1590,15 @@ resource "aws_launch_template" "test" {
}
`, rName)
}

func testAccAWSLaunchTemplateConfigHibernation(rName string, enabled bool) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name = %[1]q

hibernation_options {
configured = %[2]t
}
}
`, rName, enabled)
}
2 changes: 2 additions & 0 deletions website/docs/d/launch_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ In addition to all arguments above, the following attributes are exported:
* `tag_specifications` - The tags to apply to the resources during launch.
* `tags` - (Optional) A mapping of tags to assign to the launch template.
* `user_data` - The Base64-encoded user data to provide when launching the instance.
* `hibernation_options` - The hibernation options for the instance.

7 changes: 7 additions & 0 deletions website/docs/r/launch_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ The following arguments are supported:
* `tag_specifications` - The tags to apply to the resources during launch. See [Tag Specifications](#tag-specifications) below for more details.
* `tags` - (Optional) A mapping of tags to assign to the launch template.
* `user_data` - The Base64-encoded user data to provide when launching the instance.
* `hibernation_options` - The hibernation options for the instance. See [Hibernation Options](#hibernation-options) below for more details.

### Block devices

Expand Down Expand Up @@ -312,6 +313,12 @@ The `placement` block supports the following:
* `spread_domain` - Reserved for future use.
* `tenancy` - The tenancy of the instance (if the instance is running in a VPC). Can be `default`, `dedicated`, or `host`.

### Hibernation Options

The `hibernation_options` block supports the following:

* `configured` - If set to `true`, the launched EC2 instance will hibernation enabled.

### Tag Specifications

The tags to apply to the resources during launch. You can tag instances and volumes. More information can be found in the [EC2 API documentation](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplateTagSpecificationRequest.html).
Expand Down