@@ -19,6 +19,31 @@ class Tag {
1919 return result ;
2020 }
2121
22+ async getResourceTags ( { resourceId, serviceType, resourcePrefix, offset = 0 , limit = 100 } ) {
23+ const { Tags, TotalCount } = await this . request ( {
24+ Action : 'DescribeResourceTagsByResourceIds' ,
25+ Limit : limit ,
26+ Offset : offset ,
27+ ServiceType : serviceType ,
28+ ResourceRegion : this . region ,
29+ ResourcePrefix : resourcePrefix ,
30+ ResourceIds : [ resourceId ] ,
31+ } ) ;
32+ if ( TotalCount > limit ) {
33+ return Tags . concat (
34+ await this . getResourceTags ( {
35+ resourceId,
36+ serviceType,
37+ resourcePrefix,
38+ offset : offset + limit ,
39+ limit,
40+ } ) ,
41+ ) ;
42+ }
43+
44+ return Tags ;
45+ }
46+
2247 async getTagList ( offset = 0 , limit = 100 ) {
2348 const { Tags, TotalCount } = await this . request ( {
2449 Action : 'DescribeTags' ,
@@ -41,14 +66,13 @@ class Tag {
4166 }
4267
4368 async getScfResourceTags ( inputs ) {
44- const data = {
45- Action : 'DescribeResourceTags' ,
46- ResourcePrefix : 'namespace ' ,
47- ResourceId : ` ${ inputs . namespace || 'default' } /function/ ${ inputs . functionName } ` ,
48- } ;
69+ const tags = await this . getResourceTags ( {
70+ resourceId : ` ${ inputs . namespace || 'default' } /function/ ${ inputs . functionName } ` ,
71+ serviceType : 'scf ' ,
72+ resourcePrefix : ' namespace' ,
73+ } ) ;
4974
50- const { Rows } = await this . request ( data ) ;
51- return Rows ;
75+ return tags ;
5276 }
5377
5478 async attachTags ( { serviceType, resourcePrefix, resourceIds, tags } ) {
@@ -145,6 +169,62 @@ class Tag {
145169
146170 return true ;
147171 }
172+
173+ async deployResourceTags ( { tags, resourceId, serviceType, resourcePrefix } ) {
174+ console . log ( `Adding tags for ${ resourceId } in ${ this . region } ` ) ;
175+ const inputKeys = [ ] ;
176+ tags . forEach ( ( { TagKey } ) => {
177+ inputKeys . push ( TagKey ) ;
178+ } ) ;
179+
180+ const oldTags = await this . getResourceTags ( {
181+ resourceId : resourceId ,
182+ serviceType : serviceType ,
183+ resourcePrefix : resourcePrefix ,
184+ } ) ;
185+
186+ const oldTagKeys = [ ] ;
187+ oldTags . forEach ( ( { TagKey } ) => {
188+ oldTagKeys . push ( TagKey ) ;
189+ } ) ;
190+
191+ const detachTags = [ ] ;
192+ const attachTags = [ ] ;
193+ const leftTags = [ ] ;
194+
195+ oldTags . forEach ( ( item ) => {
196+ if ( inputKeys . indexOf ( item . TagKey ) === - 1 ) {
197+ detachTags . push ( {
198+ TagKey : item . TagKey ,
199+ } ) ;
200+ } else {
201+ const [ inputTag ] = tags . filter ( ( t ) => t . TagKey === item . TagKey ) ;
202+ const oldTagVal = item . TagValue ;
203+
204+ if ( inputTag . TagValue !== oldTagVal ) {
205+ attachTags . push ( inputTag ) ;
206+ } else {
207+ leftTags . push ( item ) ;
208+ }
209+ }
210+ } ) ;
211+
212+ tags . forEach ( ( item ) => {
213+ if ( oldTagKeys . indexOf ( item . TagKey ) === - 1 ) {
214+ attachTags . push ( item ) ;
215+ }
216+ } ) ;
217+
218+ await this . deploy ( {
219+ resourceIds : [ resourceId ] ,
220+ resourcePrefix : resourcePrefix ,
221+ serviceType : serviceType ,
222+ detachTags,
223+ attachTags,
224+ } ) ;
225+
226+ return leftTags . concat ( attachTags ) ;
227+ }
148228}
149229
150230module . exports = Tag ;
0 commit comments