@@ -20,6 +20,7 @@ import (
20
20
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
21
21
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
22
22
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
23
+ "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
23
24
)
24
25
25
26
func resourceAwsEMRCluster () * schema.Resource {
@@ -84,6 +85,10 @@ func resourceAwsEMRCluster() *schema.Resource {
84
85
},
85
86
86
87
Schema : map [string ]* schema.Schema {
88
+ "arn" : {
89
+ Type : schema .TypeString ,
90
+ Computed : true ,
91
+ },
87
92
"name" : {
88
93
Type : schema .TypeString ,
89
94
ForceNew : true ,
@@ -802,6 +807,7 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error
802
807
ReleaseLabel : aws .String (d .Get ("release_label" ).(string )),
803
808
ServiceRole : aws .String (d .Get ("service_role" ).(string )),
804
809
VisibleToAllUsers : aws .Bool (d .Get ("visible_to_all_users" ).(bool )),
810
+ Tags : keyvaluetags .New (d .Get ("tags" ).(map [string ]interface {})).IgnoreAws ().EmrTags (),
805
811
}
806
812
807
813
if v , ok := d .GetOk ("additional_info" ); ok {
@@ -848,10 +854,6 @@ func resourceAwsEMRClusterCreate(d *schema.ResourceData, meta interface{}) error
848
854
steps := v .([]interface {})
849
855
params .Steps = expandEmrStepConfigs (steps )
850
856
}
851
- if v , ok := d .GetOk ("tags" ); ok {
852
- tagsIn := v .(map [string ]interface {})
853
- params .Tags = expandTags (tagsIn )
854
- }
855
857
if v , ok := d .GetOk ("configurations" ); ok {
856
858
confUrl := v .(string )
857
859
params .Configurations = expandConfigures (confUrl )
@@ -974,6 +976,8 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
974
976
}
975
977
976
978
d .Set ("cluster_state" , state )
979
+
980
+ d .Set ("arn" , aws .StringValue (cluster .ClusterArn ))
977
981
}
978
982
979
983
instanceGroups , err := fetchAllEMRInstanceGroups (emrconn , d .Id ())
@@ -1020,6 +1024,10 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
1020
1024
return fmt .Errorf ("error setting master_instance_group: %s" , err )
1021
1025
}
1022
1026
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
+
1023
1031
d .Set ("name" , cluster .Name )
1024
1032
1025
1033
d .Set ("service_role" , cluster .ServiceRole )
@@ -1029,7 +1037,6 @@ func resourceAwsEMRClusterRead(d *schema.ResourceData, meta interface{}) error {
1029
1037
d .Set ("log_uri" , cluster .LogUri )
1030
1038
d .Set ("master_public_dns" , cluster .MasterPublicDnsName )
1031
1039
d .Set ("visible_to_all_users" , cluster .VisibleToAllUsers )
1032
- d .Set ("tags" , tagsToMapEMR (cluster .Tags ))
1033
1040
d .Set ("ebs_root_volume_size" , cluster .EbsRootVolumeSize )
1034
1041
d .Set ("scale_down_behavior" , cluster .ScaleDownBehavior )
1035
1042
d .Set ("termination_protection" , cluster .TerminationProtected )
@@ -1324,10 +1331,12 @@ func resourceAwsEMRClusterUpdate(d *schema.ResourceData, meta interface{}) error
1324
1331
}
1325
1332
}
1326
1333
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
+ }
1331
1340
}
1332
1341
1333
1342
d .Partial (false )
@@ -1739,85 +1748,6 @@ func emrCoreInstanceGroup(grps []*emr.InstanceGroup) *emr.InstanceGroup {
1739
1748
return nil
1740
1749
}
1741
1750
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
-
1821
1751
func expandBootstrapActions (bootstrapActions []interface {}) []* emr.BootstrapActionConfig {
1822
1752
actionsOut := []* emr.BootstrapActionConfig {}
1823
1753
0 commit comments