How to delete tags within records in cloud resources table ?

venugopal s
Tera Contributor

Hi everyone,

I need to remove only (AppSvc# Problem, BusApp# Problem) tags from Cloud resource table where Resource type is AWS::Config::Resource Compliance on multiple records with fix script.

Thanks for suggestions

venugopals_0-1680709098777.png

 

1 ACCEPTED SOLUTION

Hi @venugopal s 

 

Try below script. highlighted changes done:

 

var rec = new GlideRecord('cmdb_ci_cmp_resource');
rec.addEncodedQuery('resource_typeSTARTSWITHAWS::Config::ResourceCompliance^sys_tags.ccdca01887f64194abafa7183cbb350a=ccdca01887f64194abafa7183cbb350a^ORsys_tags.ccdca01887f64194abafa7183cbb350c=ccdca01887f64194abafa7183cbb350c');
//rec.setLimit(3);
 rec.query();
while (rec.next()) {
    var gr = new GlideRecord('label_entry');
    gr.addEncodedQuery('table_key=' + rec.sys_id + '^label=ccdca01887f64194abafa7183cbb350a^ORlabel=ccdca01887f64194abafa7183cbb350c');
    gr.setLimit(3);
 gr.query();
 while (gr.next()) {
 gr.deleteRecord();
}
}
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

4 REPLIES 4

Ahmmed Ali
Giga Sage

Hello @venugopal s 

 

You can query Cloud resource table with condition as "Resource type is AWS::Config::Resource Compliance AND tags contain AppSvc# Problem OR tags contain BusApp# Problem". Then for each cloud record, you can query the table label_entry with condition as "Table Key is Cloud resource sys_id AND Label is AppSvc# Problem OR Label is BusApp# Problem". then delete those records in label_entry table.

 

Note: Label field in label_entry is reference to label table. so you need to get sys_id of the labels.

 

in short, label table contains tags and label_entry table contains which tag is added to which record. so you should be able to query label_entry table and delete records there.

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi @Ahmmed Ali 
I have written script, seems it not getting exact result 

var rec = new GlideRecord('cmdb_ci_cmp_resource');
rec.addEncodedQuery('resource_typeSTARTSWITHAWS::Config::ResourceCompliance^sys_tags.ccdca01887f64194abafa7183cbb350a=ccdca01887f64194abafa7183cbb350a^sys_tags.ccdca01887f64194abafa7183cbb350c=ccdca01887f64194abafa7183cbb350c');
//rec.setLimit(3);
 rec.query();
while (rec.next()) {
    var gr = new GlideRecord('label_entry');
    gr.addEncodedQuery('table_key=b5961565dbc30300375d5458dc9619aa^labelLIKEAppSvc# Problem^ORlabelLIKEBusApp# Problem');
    gr.setLimit(3);
 gr.query();
 while (gr.next()) {
 gr.deleteRecord();
}
}
 
Thanks

Hi @venugopal s 

 

Try below script. highlighted changes done:

 

var rec = new GlideRecord('cmdb_ci_cmp_resource');
rec.addEncodedQuery('resource_typeSTARTSWITHAWS::Config::ResourceCompliance^sys_tags.ccdca01887f64194abafa7183cbb350a=ccdca01887f64194abafa7183cbb350a^ORsys_tags.ccdca01887f64194abafa7183cbb350c=ccdca01887f64194abafa7183cbb350c');
//rec.setLimit(3);
 rec.query();
while (rec.next()) {
    var gr = new GlideRecord('label_entry');
    gr.addEncodedQuery('table_key=' + rec.sys_id + '^label=ccdca01887f64194abafa7183cbb350a^ORlabel=ccdca01887f64194abafa7183cbb350c');
    gr.setLimit(3);
 gr.query();
 while (gr.next()) {
 gr.deleteRecord();
}
}
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

venugopal s
Tera Contributor

Thanks for the solution