@@ -19,6 +19,27 @@ class Tag {
1919 return result ;
2020 }
2121
22+ async getTagList ( offset = 0 , limit = 100 ) {
23+ const { Tags, TotalCount } = await this . request ( {
24+ Action : 'DescribeTags' ,
25+ Limit : limit ,
26+ Offset : offset ,
27+ } ) ;
28+ if ( TotalCount > limit ) {
29+ return Tags . concat ( await this . getTagList ( offset + limit , limit ) ) ;
30+ }
31+
32+ return Tags ;
33+ }
34+
35+ async isTagExist ( tag ) {
36+ const tagList = await this . getTagList ( ) ;
37+ const [ exist ] = tagList . filter (
38+ ( item ) => item . TagKey === tag . TagKey && item . TagValue === tag . TagValue ,
39+ ) ;
40+ return ! ! exist ;
41+ }
42+
2243 async getScfResourceTags ( inputs ) {
2344 const data = {
2445 Action : 'DescribeResourceTags' ,
@@ -30,33 +51,97 @@ class Tag {
3051 return Rows ;
3152 }
3253
33- async deploy ( inputs = { } ) {
34- const tagsInputs = {
35- Action : 'ModifyResourceTags' ,
36- Resource : inputs . resource ,
54+ async attachTags ( { serviceType, resourcePrefix, resourceIds, tags } ) {
55+ const commonInputs = {
56+ Action : 'AttachResourcesTag' ,
57+ ResourceIds : resourceIds ,
58+ ServiceType : serviceType ,
59+ ResourceRegion : this . region ,
60+ ResourcePrefix : resourcePrefix ,
3761 } ;
62+ // if tag not exsit, create it
3863
39- const { replaceTags = { } , deleteTags = { } } = inputs ;
64+ for ( let i = 0 ; i < tags . length ; i ++ ) {
65+ const currentTag = tags [ i ] ;
66+ const tagExist = await this . isTagExist ( currentTag ) ;
67+ if ( ! tagExist ) {
68+ await this . createTag ( currentTag ) ;
69+ }
70+ const tagInputs = {
71+ ...commonInputs ,
72+ ...currentTag ,
73+ } ;
74+ await this . request ( tagInputs ) ;
75+ }
76+ }
4077
41- if ( Object . keys ( replaceTags ) . length > 0 ) {
42- tagsInputs . ReplaceTags = Object . entries ( replaceTags ) . map ( ( [ key , val ] ) => ( {
43- TagKey : key ,
44- TagValue : val ,
45- } ) ) ;
78+ async detachTags ( { serviceType, resourcePrefix, resourceIds, tags } ) {
79+ const commonInputs = {
80+ Action : 'DetachResourcesTag' ,
81+ ResourceIds : resourceIds ,
82+ ServiceType : serviceType ,
83+ ResourceRegion : this . region ,
84+ ResourcePrefix : resourcePrefix ,
85+ } ;
86+ for ( let i = 0 ; i < tags . length ; i ++ ) {
87+ const tagInputs = {
88+ ...commonInputs ,
89+ ...tags [ i ] ,
90+ } ;
91+ delete tagInputs . TagValue ;
92+ await this . request ( tagInputs ) ;
4693 }
47- if ( Object . keys ( deleteTags ) . length > 0 ) {
48- tagsInputs . DeleteTags = Object . keys ( deleteTags ) . map ( ( key ) => ( {
49- TagKey : key ,
50- } ) ) ;
94+ }
95+
96+ async createTag ( tag ) {
97+ console . log ( `Creating tag key: ${ tag . TagKey } , value: ${ tag . TagValue } ` ) ;
98+ await this . request ( {
99+ Action : 'CreateTag' ,
100+ ...tag ,
101+ } ) ;
102+
103+ return tag ;
104+ }
105+
106+ async deleteTag ( tag ) {
107+ console . log ( `Deleting tag key: ${ tag . TagKey } , value: ${ tag . TagValue } ` ) ;
108+ await this . request ( {
109+ Action : 'DeleteTag' ,
110+ ...tag ,
111+ } ) ;
112+
113+ return true ;
114+ }
115+
116+ async deleteTags ( tags ) {
117+ for ( let i = 0 ; i < tags . length ; i ++ ) {
118+ await this . deleteTag ( tags [ i ] ) ;
51119 }
52120
121+ return true ;
122+ }
123+
124+ async deploy ( inputs = { } ) {
125+ const { detachTags = [ ] , attachTags = [ ] , serviceType, resourceIds, resourcePrefix } = inputs ;
126+
53127 console . log ( `Updating tags` ) ;
54128 try {
55- await this . request ( tagsInputs ) ;
129+ await this . detachTags ( {
130+ tags : detachTags ,
131+ serviceType,
132+ resourceIds,
133+ resourcePrefix,
134+ } ) ;
135+ await this . attachTags ( {
136+ tags : attachTags ,
137+ serviceType,
138+ resourceIds,
139+ resourcePrefix,
140+ } ) ;
56141 } catch ( e ) {
57142 console . log ( e ) ;
58143 }
59- console . log ( `Update tags success. ` ) ;
144+ console . log ( `Update tags success` ) ;
60145
61146 return true ;
62147 }
0 commit comments