providers/aws: aws_instance id-only

This commit is contained in:
Mitchell Hashimoto 2016-04-22 09:37:27 -07:00
parent 46430cd384
commit d85df63526
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 93 additions and 32 deletions

View File

@ -533,6 +533,9 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("vpc_security_group_ids", sgs); err != nil { if err := d.Set("vpc_security_group_ids", sgs); err != nil {
return err return err
} }
if err := d.Set("security_groups", []string{}); err != nil {
return err
}
} else { } else {
for _, sg := range instance.SecurityGroups { for _, sg := range instance.SecurityGroups {
sgs = append(sgs, *sg.GroupName) sgs = append(sgs, *sg.GroupName)
@ -541,11 +544,29 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("security_groups", sgs); err != nil { if err := d.Set("security_groups", sgs); err != nil {
return err return err
} }
if err := d.Set("vpc_security_group_ids", []string{}); err != nil {
return err
}
} }
if err := readBlockDevices(d, instance, conn); err != nil { if err := readBlockDevices(d, instance, conn); err != nil {
return err return err
} }
if _, ok := d.GetOk("ephemeral_block_device"); !ok {
d.Set("ephemeral_block_device", []interface{}{})
}
// Instance attributes
{
attr, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{
Attribute: aws.String("disableApiTermination"),
InstanceId: aws.String(d.Id()),
})
if err != nil {
return err
}
d.Set("disable_api_termination", attr.DisableApiTermination.Value)
}
return nil return nil
} }
@ -696,8 +717,17 @@ func readBlockDevices(d *schema.ResourceData, instance *ec2.Instance, conn *ec2.
if err := d.Set("ebs_block_device", ibds["ebs"]); err != nil { if err := d.Set("ebs_block_device", ibds["ebs"]); err != nil {
return err return err
} }
// This handles the import case which needs to be defaulted to empty
if _, ok := d.GetOk("root_block_device"); !ok {
if err := d.Set("root_block_device", []interface{}{}); err != nil {
return err
}
}
if ibds["root"] != nil { if ibds["root"] != nil {
if err := d.Set("root_block_device", []interface{}{ibds["root"]}); err != nil { roots := []interface{}{ibds["root"]}
if err := d.Set("root_block_device", roots); err != nil {
return err return err
} }
} }

View File

@ -33,7 +33,14 @@ func TestAccAWSInstance_basic(t *testing.T) {
} }
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
// We ignore security groups because even with EC2 classic
// we'll import as VPC security groups, which is fine. We verify
// VPC security group import in other tests
IDRefreshName: "aws_instance.foo",
IDRefreshIgnore: []string{"user_data", "security_groups", "vpc_security_group_ids"},
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy, CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
@ -135,7 +142,10 @@ func TestAccAWSInstance_blockDevices(t *testing.T) {
} }
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_instance.foo",
IDRefreshIgnore: []string{
"ephemeral_block_device", "user_data", "security_groups", "vpc_security_groups"},
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy, CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
@ -202,9 +212,10 @@ func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
} }
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo",
CheckDestroy: testAccCheckInstanceDestroy, Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceConfigSourceDestDisable, Config: testAccInstanceConfigSourceDestDisable,
@ -255,9 +266,10 @@ func TestAccAWSInstance_disableApiTermination(t *testing.T) {
} }
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo",
CheckDestroy: testAccCheckInstanceDestroy, Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceConfigDisableAPITermination(true), Config: testAccInstanceConfigDisableAPITermination(true),
@ -282,9 +294,11 @@ func TestAccAWSInstance_vpc(t *testing.T) {
var v ec2.Instance var v ec2.Instance
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo",
CheckDestroy: testAccCheckInstanceDestroy, IDRefreshIgnore: []string{"associate_public_ip_address", "user_data"},
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceConfigVPC, Config: testAccInstanceConfigVPC,
@ -337,9 +351,11 @@ func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) {
var v ec2.Instance var v ec2.Instance
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo_instance",
CheckDestroy: testAccCheckInstanceDestroy, IDRefreshIgnore: []string{"associate_public_ip_address"},
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceNetworkInstanceSecurityGroups, Config: testAccInstanceNetworkInstanceSecurityGroups,
@ -356,9 +372,10 @@ func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) {
var v ec2.Instance var v ec2.Instance
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo_instance",
CheckDestroy: testAccCheckInstanceDestroy, Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs, Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs,
@ -419,9 +436,10 @@ func TestAccAWSInstance_privateIP(t *testing.T) {
} }
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo",
CheckDestroy: testAccCheckInstanceDestroy, Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceConfigPrivateIP, Config: testAccInstanceConfigPrivateIP,
@ -448,9 +466,11 @@ func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) {
} }
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo",
CheckDestroy: testAccCheckInstanceDestroy, IDRefreshIgnore: []string{"associate_public_ip_address"},
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP,
@ -482,9 +502,11 @@ func TestAccAWSInstance_keyPairCheck(t *testing.T) {
} }
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo",
CheckDestroy: testAccCheckInstanceDestroy, IDRefreshIgnore: []string{"source_dest_check"},
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceConfigKeyPair, Config: testAccInstanceConfigKeyPair,
@ -530,9 +552,10 @@ func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) {
var v ec2.Instance var v ec2.Instance
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, IDRefreshName: "aws_instance.foo",
CheckDestroy: testAccCheckInstanceDestroy, Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccInstanceConfigForceNewAndTagsDrift, Config: testAccInstanceConfigForceNewAndTagsDrift,

View File

@ -317,8 +317,16 @@ func testIDOnlyRefresh(c TestCase, opts terraform.ContextOpts, step TestStep, r
expected := r.Primary.Attributes expected := r.Primary.Attributes
// Remove fields we're ignoring // Remove fields we're ignoring
for _, v := range c.IDRefreshIgnore { for _, v := range c.IDRefreshIgnore {
delete(actual, v) for k, _ := range actual {
delete(expected, v) if strings.HasPrefix(k, v) {
delete(actual, k)
}
}
for k, _ := range expected {
if strings.HasPrefix(k, v) {
delete(expected, k)
}
}
} }
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {