Skip to content

Commit

Permalink
Merge pull request #5433 from terraform-providers/b-aws_ami-ena_suppo…
Browse files Browse the repository at this point in the history
…rt-computed

resource/aws_ami*: Fix ena_support attribute for copy/from_instance and uncouple common schema function
  • Loading branch information
bflad committed Aug 7, 2018
2 parents 4cfd0d6 + ae0772a commit 27ef09d
Show file tree
Hide file tree
Showing 5 changed files with 672 additions and 380 deletions.
369 changes: 161 additions & 208 deletions aws/resource_aws_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ const (
)

func resourceAwsAmi() *schema.Resource {
// Our schema is shared also with aws_ami_copy and aws_ami_from_instance
resourceSchema := resourceAwsAmiCommonSchema(false)

return &schema.Resource{
Create: resourceAwsAmiCreate,

Expand All @@ -37,7 +34,167 @@ func resourceAwsAmi() *schema.Resource {
Delete: schema.DefaultTimeout(AWSAMIDeleteRetryTimeout),
},

Schema: resourceSchema,
Schema: map[string]*schema.Schema{
"image_location": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"architecture": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "x86_64",
},
"description": {
Type: schema.TypeString,
Optional: true,
},
// The following block device attributes intentionally mimick the
// corresponding attributes on aws_instance, since they have the
// same meaning.
// However, we don't use root_block_device here because the constraint
// on which root device attributes can be overridden for an instance to
// not apply when registering an AMI.
"ebs_block_device": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_on_termination": {
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},

"device_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"encrypted": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"iops": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},

"snapshot_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"volume_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},

"volume_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "standard",
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["snapshot_id"].(string)))
return hashcode.String(buf.String())
},
},
"ena_support": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"ephemeral_block_device": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_name": {
Type: schema.TypeString,
Required: true,
},

"virtual_name": {
Type: schema.TypeString,
Required: true,
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["virtual_name"].(string)))
return hashcode.String(buf.String())
},
},
"kernel_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// Not a public attribute; used to let the aws_ami_copy and aws_ami_from_instance
// resources record that they implicitly created new EBS snapshots that we should
// now manage. Not set by aws_ami, since the snapshots used there are presumed to
// be independently managed.
"manage_ebs_snapshots": {
Type: schema.TypeBool,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ramdisk_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"root_device_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"root_snapshot_id": {
Type: schema.TypeString,
Computed: true,
},
"sriov_net_support": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "simple",
},
"tags": tagsSchema(),
"virtualization_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "paravirtual",
},
},

// The Read, Update and Delete operations are shared with aws_ami_copy
// and aws_ami_from_instance, since they differ only in how the image
Expand Down Expand Up @@ -374,207 +531,3 @@ func resourceAwsAmiWaitForAvailable(timeout time.Duration, id string, client *ec
}
return info.(*ec2.Image), nil
}

func resourceAwsAmiCommonSchema(computed bool) map[string]*schema.Schema {
// The "computed" parameter controls whether we're making
// a schema for an AMI that's been implicitly registered (aws_ami_copy, aws_ami_from_instance)
// or whether we're making a schema for an explicit registration (aws_ami).
// When set, almost every attribute is marked as "computed".
// When not set, only the "id" attribute is computed.
// "name" and "description" are never computed, since they must always
// be provided by the user.

var virtualizationTypeDefault interface{}
var deleteEbsOnTerminationDefault interface{}
var sriovNetSupportDefault interface{}
var architectureDefault interface{}
var volumeTypeDefault interface{}
if !computed {
virtualizationTypeDefault = "paravirtual"
deleteEbsOnTerminationDefault = true
sriovNetSupportDefault = "simple"
architectureDefault = "x86_64"
volumeTypeDefault = "standard"
}

return map[string]*schema.Schema{
"image_location": {
Type: schema.TypeString,
Optional: !computed,
Computed: true,
ForceNew: !computed,
},
"architecture": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
Default: architectureDefault,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"kernel_id": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ramdisk_id": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
},
"root_device_name": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
},
"root_snapshot_id": {
Type: schema.TypeString,
Computed: true,
},
"sriov_net_support": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
Default: sriovNetSupportDefault,
},
"virtualization_type": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
Default: virtualizationTypeDefault,
},

// The following block device attributes intentionally mimick the
// corresponding attributes on aws_instance, since they have the
// same meaning.
// However, we don't use root_block_device here because the constraint
// on which root device attributes can be overridden for an instance to
// not apply when registering an AMI.

"ebs_block_device": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_on_termination": {
Type: schema.TypeBool,
Optional: !computed,
Default: deleteEbsOnTerminationDefault,
ForceNew: !computed,
Computed: computed,
},

"device_name": {
Type: schema.TypeString,
Required: !computed,
ForceNew: !computed,
Computed: computed,
},

"encrypted": {
Type: schema.TypeBool,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
},

"iops": {
Type: schema.TypeInt,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
},

"snapshot_id": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
},

"volume_size": {
Type: schema.TypeInt,
Optional: !computed,
Computed: true,
ForceNew: !computed,
},

"volume_type": {
Type: schema.TypeString,
Optional: !computed,
Computed: computed,
ForceNew: !computed,
Default: volumeTypeDefault,
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["snapshot_id"].(string)))
return hashcode.String(buf.String())
},
},

"ephemeral_block_device": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_name": {
Type: schema.TypeString,
Required: !computed,
Computed: computed,
},

"virtual_name": {
Type: schema.TypeString,
Required: !computed,
Computed: computed,
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["virtual_name"].(string)))
return hashcode.String(buf.String())
},
},

"tags": tagsSchema(),

// Not a public attribute; used to let the aws_ami_copy and aws_ami_from_instance
// resources record that they implicitly created new EBS snapshots that we should
// now manage. Not set by aws_ami, since the snapshots used there are presumed to
// be independently managed.
"manage_ebs_snapshots": {
Type: schema.TypeBool,
Computed: true,
ForceNew: true,
},
"ena_support": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
}
}
Loading

0 comments on commit 27ef09d

Please sign in to comment.