Skip to content

Commit a075351

Browse files
authored
Merge pull request #11078 from thatderek/r-aws_emr_cluster-arn
Closes #10921
2 parents 4340d41 + c4c08d6 commit a075351

File tree

3 files changed

+20
-88
lines changed

3 files changed

+20
-88
lines changed

aws/resource_aws_emr_cluster.go

Lines changed: 18 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2121
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
2222
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
23+
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
2324
)
2425

2526
func resourceAwsEMRCluster() *schema.Resource {
@@ -84,6 +85,10 @@ func resourceAwsEMRCluster() *schema.Resource {
8485
},
8586

8687
Schema: map[string]*schema.Schema{
88+
"arn": {
89+
Type: schema.TypeString,
90+
Computed: true,
91+
},
8792
"name": {
8893
Type: schema.TypeString,
8994
ForceNew: true,
@@ -802,6 +807,7 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error
802807
ReleaseLabel: aws.String(d.Get("release_label").(string)),
803808
ServiceRole: aws.String(d.Get("service_role").(string)),
804809
VisibleToAllUsers: aws.Bool(d.Get("visible_to_all_users").(bool)),
810+
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().EmrTags(),
805811
}
806812

807813
if v, ok := d.GetOk("additional_info"); ok {
@@ -848,10 +854,6 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error
848854
steps := v.([]interface{})
849855
params.Steps = expandEmrStepConfigs(steps)
850856
}
851-
if v, ok := d.GetOk("tags"); ok {
852-
tagsIn := v.(map[string]interface{})
853-
params.Tags = expandTags(tagsIn)
854-
}
855857
if v, ok := d.GetOk("configurations"); ok {
856858
confUrl := v.(string)
857859
params.Configurations = expandConfigures(confUrl)
@@ -974,6 +976,8 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
974976
}
975977

976978
d.Set("cluster_state", state)
979+
980+
d.Set("arn", aws.StringValue(cluster.ClusterArn))
977981
}
978982

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

1027+
if err := d.Set("tags", keyvaluetags.EmrKeyValueTags(cluster.Tags).IgnoreAws().Map()); err != nil {
1028+
return fmt.Errorf("error settings tags: %s", err)
1029+
}
1030+
10231031
d.Set("name", cluster.Name)
10241032

10251033
d.Set("service_role", cluster.ServiceRole)
@@ -1029,7 +1037,6 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
10291037
d.Set("log_uri", cluster.LogUri)
10301038
d.Set("master_public_dns", cluster.MasterPublicDnsName)
10311039
d.Set("visible_to_all_users", cluster.VisibleToAllUsers)
1032-
d.Set("tags", tagsToMapEMR(cluster.Tags))
10331040
d.Set("ebs_root_volume_size", cluster.EbsRootVolumeSize)
10341041
d.Set("scale_down_behavior", cluster.ScaleDownBehavior)
10351042
d.Set("termination_protection", cluster.TerminationProtected)
@@ -1324,10 +1331,12 @@ func resourceAwsEMRClusterUpdate(d *schema.ResourceData, meta interface{}) error
13241331
}
13251332
}
13261333

1327-
if err := setTagsEMR(conn, d); err != nil {
1328-
return err
1329-
} else {
1330-
d.SetPartial("tags")
1334+
if d.HasChange("tags") {
1335+
o, n := d.GetChange("tags")
1336+
1337+
if err := keyvaluetags.EmrUpdateTags(conn, d.Id(), o, n); err != nil {
1338+
return fmt.Errorf("error updating EMR Cluster (%s) tags: %s", d.Id(), err)
1339+
}
13311340
}
13321341

13331342
d.Partial(false)
@@ -1739,85 +1748,6 @@ func emrCoreInstanceGroup(grps []*emr.InstanceGroup) *emr.InstanceGroup {
17391748
return nil
17401749
}
17411750

1742-
func expandTags(m map[string]interface{}) []*emr.Tag {
1743-
var result []*emr.Tag
1744-
for k, v := range m {
1745-
result = append(result, &emr.Tag{
1746-
Key: aws.String(k),
1747-
Value: aws.String(v.(string)),
1748-
})
1749-
}
1750-
1751-
return result
1752-
}
1753-
1754-
func tagsToMapEMR(ts []*emr.Tag) map[string]string {
1755-
result := make(map[string]string)
1756-
for _, t := range ts {
1757-
result[*t.Key] = *t.Value
1758-
}
1759-
1760-
return result
1761-
}
1762-
1763-
func diffTagsEMR(oldTags, newTags []*emr.Tag) ([]*emr.Tag, []*emr.Tag) {
1764-
// First, we're creating everything we have
1765-
create := make(map[string]interface{})
1766-
for _, t := range newTags {
1767-
create[*t.Key] = *t.Value
1768-
}
1769-
1770-
// Build the list of what to remove
1771-
var remove []*emr.Tag
1772-
for _, t := range oldTags {
1773-
old, ok := create[*t.Key]
1774-
if !ok || old != *t.Value {
1775-
// Delete it!
1776-
remove = append(remove, t)
1777-
}
1778-
}
1779-
1780-
return expandTags(create), remove
1781-
}
1782-
1783-
func setTagsEMR(conn *emr.EMR, d *schema.ResourceData) error {
1784-
if d.HasChange("tags") {
1785-
oraw, nraw := d.GetChange("tags")
1786-
o := oraw.(map[string]interface{})
1787-
n := nraw.(map[string]interface{})
1788-
create, remove := diffTagsEMR(expandTags(o), expandTags(n))
1789-
1790-
// Set tags
1791-
if len(remove) > 0 {
1792-
log.Printf("[DEBUG] Removing tags: %s", remove)
1793-
k := make([]*string, len(remove))
1794-
for i, t := range remove {
1795-
k[i] = t.Key
1796-
}
1797-
1798-
_, err := conn.RemoveTags(&emr.RemoveTagsInput{
1799-
ResourceId: aws.String(d.Id()),
1800-
TagKeys: k,
1801-
})
1802-
if err != nil {
1803-
return err
1804-
}
1805-
}
1806-
if len(create) > 0 {
1807-
log.Printf("[DEBUG] Creating tags: %s", create)
1808-
_, err := conn.AddTags(&emr.AddTagsInput{
1809-
ResourceId: aws.String(d.Id()),
1810-
Tags: create,
1811-
})
1812-
if err != nil {
1813-
return err
1814-
}
1815-
}
1816-
}
1817-
1818-
return nil
1819-
}
1820-
18211751
func expandBootstrapActions(bootstrapActions []interface{}) []*emr.BootstrapActionConfig {
18221752
actionsOut := []*emr.BootstrapActionConfig{}
18231753

aws/resource_aws_emr_cluster_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func TestAccAWSEMRCluster_basic(t *testing.T) {
9191
testAccCheckAWSEmrClusterExists("aws_emr_cluster.tf-test-cluster", &cluster),
9292
resource.TestCheckResourceAttr("aws_emr_cluster.tf-test-cluster", "scale_down_behavior", "TERMINATE_AT_TASK_COMPLETION"),
9393
resource.TestCheckResourceAttr("aws_emr_cluster.tf-test-cluster", "step.#", "0"),
94+
resource.TestCheckResourceAttrSet("aws_emr_cluster.tf-test-cluster", "arn"),
9495
),
9596
},
9697
{

website/docs/r/emr_cluster.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ Attributes for Hadoop job step configuration
383383

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

386+
* `arn`- The ARN of the cluster.
386387
* `id` - The ID of the EMR Cluster
387388
* `name` - The name of the cluster.
388389
* `release_label` - The release label for the Amazon EMR release.

0 commit comments

Comments
 (0)