From 93e5d573ce5c48bf07f7917018ae300ced3e3a54 Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Fri, 28 Apr 2017 12:09:18 +1200 Subject: [PATCH] provider/aws: Exclude aws_instance volume tagging for China and Gov Clouds (#14055) Fixes: #14049 The China and Gov regions do not support the new way of tagging instances and volumes on creation. Therefore, we need to hack this to make sure we don't try and set these on instance creation --- builtin/providers/aws/config.go | 14 ++++ .../providers/aws/resource_aws_instance.go | 66 +++++++++++-------- 2 files changed, 51 insertions(+), 29 deletions(-) diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index 78fa93deb..327090130 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -171,6 +171,20 @@ func (c *AWSClient) DynamoDB() *dynamodb.DynamoDB { return c.dynamodbconn } +func (c *AWSClient) IsGovCloud() bool { + if c.region == "us-gov-west-1" { + return true + } + return false +} + +func (c *AWSClient) IsChinaCloud() bool { + if c.region == "cn-north-1" { + return true + } + return false +} + // Client configures and returns a fully initialized AWSClient func (c *Config) Client() (interface{}, error) { // Get the auth and region. This can fail if keys/regions were not diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index b648b3b35..e6253b3bc 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -432,32 +432,35 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error { runOpts.Ipv6Addresses = ipv6Addresses } - tagsSpec := make([]*ec2.TagSpecification, 0) + restricted := meta.(*AWSClient).IsGovCloud() || meta.(*AWSClient).IsChinaCloud() + if !restricted { + tagsSpec := make([]*ec2.TagSpecification, 0) - if v, ok := d.GetOk("tags"); ok { - tags := tagsFromMap(v.(map[string]interface{})) + if v, ok := d.GetOk("tags"); ok { + tags := tagsFromMap(v.(map[string]interface{})) - spec := &ec2.TagSpecification{ - ResourceType: aws.String("instance"), - Tags: tags, + spec := &ec2.TagSpecification{ + ResourceType: aws.String("instance"), + Tags: tags, + } + + tagsSpec = append(tagsSpec, spec) } - tagsSpec = append(tagsSpec, spec) - } + if v, ok := d.GetOk("volume_tags"); ok { + tags := tagsFromMap(v.(map[string]interface{})) - if v, ok := d.GetOk("volume_tags"); ok { - tags := tagsFromMap(v.(map[string]interface{})) + spec := &ec2.TagSpecification{ + ResourceType: aws.String("volume"), + Tags: tags, + } - spec := &ec2.TagSpecification{ - ResourceType: aws.String("volume"), - Tags: tags, + tagsSpec = append(tagsSpec, spec) } - tagsSpec = append(tagsSpec, spec) - } - - if len(tagsSpec) > 0 { - runOpts.TagSpecifications = tagsSpec + if len(tagsSpec) > 0 { + runOpts.TagSpecifications = tagsSpec + } } // Create the instance @@ -713,19 +716,24 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error { d.Partial(true) - if d.HasChange("tags") && !d.IsNewResource() { - if err := setTags(conn, d); err != nil { - return err - } else { - d.SetPartial("tags") + restricted := meta.(*AWSClient).IsGovCloud() || meta.(*AWSClient).IsChinaCloud() + + if d.HasChange("tags") { + if !d.IsNewResource() || !restricted { + if err := setTags(conn, d); err != nil { + return err + } else { + d.SetPartial("tags") + } } } - - if d.HasChange("volume_tags") && !d.IsNewResource() { - if err := setVolumeTags(conn, d); err != nil { - return err - } else { - d.SetPartial("volume_tags") + if d.HasChange("volume_tags") { + if !d.IsNewResource() || !restricted { + if err := setVolumeTags(conn, d); err != nil { + return err + } else { + d.SetPartial("volume_tags") + } } }