Skip to content

Commit

Permalink
Merge pull request #31035 from GlennChia/f-aws_instance-cpu_options
Browse files Browse the repository at this point in the history
r/aws_instance and r/d/aws_launch_template - cpu options
  • Loading branch information
jar-b authored Apr 28, 2023
2 parents ec52600 + 2aef750 commit 19d172d
Show file tree
Hide file tree
Showing 9 changed files with 831 additions and 24 deletions.
23 changes: 23 additions & 0 deletions .changelog/31035.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```release-note:breaking-change
resource/aws_instance: The `cpu_core_count` argument is deprecated in favor of the `cpu_options` block. The `cpu_options` block can set `core_count`
```

```release-note:breaking-change
resource/aws_instance: The `cpu_threads_per_core` argument is deprecated in favor of the `cpu_options` block. The `cpu_options` block can set `threads_per_core`
```

```release-note:enhancement
resource/aws_instance: Add `cpu_options` argument
```

```release-note:enhancement
resource/aws_instance: Add `amd_sev_snp` argument
```

```release-note:enhancement
resource/aws_launch_template: Add `amd_sev_snp` argument
```

```release-note:enhancement
datasource/aws_launch_template: Add `amd_sev_snp` attribute
```
114 changes: 106 additions & 8 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,60 @@ func ResourceInstance() *schema.Resource {
},
},
},
"cpu_core_count": {
Type: schema.TypeInt,
"cpu_options": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"amd_sev_snp": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(ec2.AmdSevSnpSpecification_Values(), false),
// prevents ForceNew for the case where users launch EC2 instances without cpu_options
// then in a second apply set cpu_options.0.amd_sev_snp to "disabled"
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Id() != "" && old == "" && new == ec2.AmdSevSnpSpecificationDisabled {
return true
}
return false
},
},
"core_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"cpu_core_count"},
},
"threads_per_core": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"cpu_threads_per_core"},
},
},
},
},
"cpu_core_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
Deprecated: "use 'cpu_options' argument instead",
ConflictsWith: []string{"cpu_options.0.core_count"},
},
"cpu_threads_per_core": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
Deprecated: "use 'cpu_options' argument instead",
ConflictsWith: []string{"cpu_options.0.threads_per_core"},
},
"credit_specification": {
Type: schema.TypeList,
Expand Down Expand Up @@ -978,11 +1021,16 @@ func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, meta inte
d.Set("tenancy", v.Tenancy)
}

// preserved to maintain backward compatibility
if v := instance.CpuOptions; v != nil {
d.Set("cpu_core_count", v.CoreCount)
d.Set("cpu_threads_per_core", v.ThreadsPerCore)
}

if err := d.Set("cpu_options", flattenCPUOptions(instance.CpuOptions)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting cpu_options: %s", err)
}

if v := instance.HibernationOptions; v != nil {
d.Set("hibernation", v.Configured)
}
Expand Down Expand Up @@ -2675,7 +2723,10 @@ func buildInstanceOpts(ctx context.Context, d *schema.ResourceData, meta interfa
opts.Placement.HostResourceGroupArn = aws.String(v.(string))
}

if v := d.Get("cpu_core_count").(int); v > 0 {
if v, ok := d.GetOk("cpu_options"); ok {
opts.CpuOptions = expandCPUOptions(v.([]interface{}))
} else if v := d.Get("cpu_core_count").(int); v > 0 {
// preserved to maintain backward compatibility
tc := d.Get("cpu_threads_per_core").(int)
if tc < 0 {
tc = 2
Expand Down Expand Up @@ -2932,6 +2983,31 @@ func expandInstanceMetadataOptions(l []interface{}) *ec2.InstanceMetadataOptions
return opts
}

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

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

opts := &ec2.CpuOptionsRequest{}

if v, ok := m["amd_sev_snp"].(string); ok && v != "" {
opts.AmdSevSnp = aws.String(v)
}

if v, ok := m["core_count"].(int); ok && v > 0 {
tc := m["threads_per_core"].(int)
if tc < 0 {
tc = 2
}
opts.CoreCount = aws.Int64(int64(v))
opts.ThreadsPerCore = aws.Int64(int64(tc))
}

return opts
}

func expandEnclaveOptions(l []interface{}) *ec2.EnclaveOptionsRequest {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down Expand Up @@ -2974,6 +3050,28 @@ func flattenInstanceMetadataOptions(opts *ec2.InstanceMetadataOptionsResponse) [
return []interface{}{m}
}

func flattenCPUOptions(opts *ec2.CpuOptions) []interface{} {
if opts == nil {
return nil
}

m := map[string]interface{}{}

if v := opts.AmdSevSnp; v != nil {
m["amd_sev_snp"] = aws.StringValue(v)
}

if v := opts.CoreCount; v != nil {
m["core_count"] = aws.Int64Value(v)
}

if v := opts.ThreadsPerCore; v != nil {
m["threads_per_core"] = aws.Int64Value(v)
}

return []interface{}{m}
}

func flattenEnclaveOptions(opts *ec2.EnclaveOptions) []interface{} {
if opts == nil {
return nil
Expand Down
Loading

0 comments on commit 19d172d

Please sign in to comment.