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

Closes #10921 #11078

Merged
merged 1 commit into from
Dec 2, 2019
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
106 changes: 18 additions & 88 deletions aws/resource_aws_emr_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsEMRCluster() *schema.Resource {
Expand Down Expand Up @@ -84,6 +85,10 @@ func resourceAwsEMRCluster() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
ForceNew: true,
Expand Down Expand Up @@ -802,6 +807,7 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error
ReleaseLabel: aws.String(d.Get("release_label").(string)),
ServiceRole: aws.String(d.Get("service_role").(string)),
VisibleToAllUsers: aws.Bool(d.Get("visible_to_all_users").(bool)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().EmrTags(),
}

if v, ok := d.GetOk("additional_info"); ok {
Expand Down Expand Up @@ -848,10 +854,6 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error
steps := v.([]interface{})
params.Steps = expandEmrStepConfigs(steps)
}
if v, ok := d.GetOk("tags"); ok {
tagsIn := v.(map[string]interface{})
params.Tags = expandTags(tagsIn)
}
if v, ok := d.GetOk("configurations"); ok {
confUrl := v.(string)
params.Configurations = expandConfigures(confUrl)
Expand Down Expand Up @@ -974,6 +976,8 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("cluster_state", state)

d.Set("arn", aws.StringValue(cluster.ClusterArn))
}

instanceGroups, err := fetchAllEMRInstanceGroups(emrconn, d.Id())
Expand Down Expand Up @@ -1020,6 +1024,10 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting master_instance_group: %s", err)
}

if err := d.Set("tags", keyvaluetags.EmrKeyValueTags(cluster.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error settings tags: %s", err)
}

d.Set("name", cluster.Name)

d.Set("service_role", cluster.ServiceRole)
Expand All @@ -1029,7 +1037,6 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
d.Set("log_uri", cluster.LogUri)
d.Set("master_public_dns", cluster.MasterPublicDnsName)
d.Set("visible_to_all_users", cluster.VisibleToAllUsers)
d.Set("tags", tagsToMapEMR(cluster.Tags))
d.Set("ebs_root_volume_size", cluster.EbsRootVolumeSize)
d.Set("scale_down_behavior", cluster.ScaleDownBehavior)
d.Set("termination_protection", cluster.TerminationProtected)
Expand Down Expand Up @@ -1324,10 +1331,12 @@ func resourceAwsEMRClusterUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if err := setTagsEMR(conn, d); err != nil {
return err
} else {
d.SetPartial("tags")
if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.EmrUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating EMR Cluster (%s) tags: %s", d.Id(), err)
}
}

d.Partial(false)
Expand Down Expand Up @@ -1739,85 +1748,6 @@ func emrCoreInstanceGroup(grps []*emr.InstanceGroup) *emr.InstanceGroup {
return nil
}

func expandTags(m map[string]interface{}) []*emr.Tag {
var result []*emr.Tag
for k, v := range m {
result = append(result, &emr.Tag{
Key: aws.String(k),
Value: aws.String(v.(string)),
})
}

return result
}

func tagsToMapEMR(ts []*emr.Tag) map[string]string {
result := make(map[string]string)
for _, t := range ts {
result[*t.Key] = *t.Value
}

return result
}

func diffTagsEMR(oldTags, newTags []*emr.Tag) ([]*emr.Tag, []*emr.Tag) {
// First, we're creating everything we have
create := make(map[string]interface{})
for _, t := range newTags {
create[*t.Key] = *t.Value
}

// Build the list of what to remove
var remove []*emr.Tag
for _, t := range oldTags {
old, ok := create[*t.Key]
if !ok || old != *t.Value {
// Delete it!
remove = append(remove, t)
}
}

return expandTags(create), remove
}

func setTagsEMR(conn *emr.EMR, d *schema.ResourceData) error {
if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
create, remove := diffTagsEMR(expandTags(o), expandTags(n))

// Set tags
if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %s", remove)
k := make([]*string, len(remove))
for i, t := range remove {
k[i] = t.Key
}

_, err := conn.RemoveTags(&emr.RemoveTagsInput{
ResourceId: aws.String(d.Id()),
TagKeys: k,
})
if err != nil {
return err
}
}
if len(create) > 0 {
log.Printf("[DEBUG] Creating tags: %s", create)
_, err := conn.AddTags(&emr.AddTagsInput{
ResourceId: aws.String(d.Id()),
Tags: create,
})
if err != nil {
return err
}
}
}

return nil
}

func expandBootstrapActions(bootstrapActions []interface{}) []*emr.BootstrapActionConfig {
actionsOut := []*emr.BootstrapActionConfig{}

Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_emr_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func TestAccAWSEMRCluster_basic(t *testing.T) {
testAccCheckAWSEmrClusterExists("aws_emr_cluster.tf-test-cluster", &cluster),
resource.TestCheckResourceAttr("aws_emr_cluster.tf-test-cluster", "scale_down_behavior", "TERMINATE_AT_TASK_COMPLETION"),
resource.TestCheckResourceAttr("aws_emr_cluster.tf-test-cluster", "step.#", "0"),
resource.TestCheckResourceAttrSet("aws_emr_cluster.tf-test-cluster", "arn"),
),
},
{
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/emr_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ Attributes for Hadoop job step configuration

In addition to all arguments above, the following attributes are exported:

* `arn`- The ARN of the cluster.
* `id` - The ID of the EMR Cluster
* `name` - The name of the cluster.
* `release_label` - The release label for the Amazon EMR release.
Expand Down