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

resource/aws_ami*: Fix ena_support attribute for copy/from_instance and uncouple common schema function #5433

Merged
merged 2 commits into from
Aug 7, 2018
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
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