VMware Cloud Community
ITVictor
Contributor
Contributor

What is missing -> vRO creates EC2Client, and returns EC2DescribeTagsRequest, but cannot filter

I've pasted the little code snippet I have been working on below.  It runs with no errors, but if I include the EC2Filter (i.e. filter out all the tags that are not [in this case] security-groups) I get nothing.  If I remark out the line that sets the filters I get dozens of key value pairs for multiple resource types.

============================================================

var ec2client = client.getAmazonEC2Client();

var setResourceType = new EC2Filter();

setResourceType.setName('tag: ResourceType');

setResourceType.setValues(['security-group']);

var tagRequest = new EC2DescribeTagsRequest();

// If I remark out the withFilter line below, the script runs without error

//      and returns tag data for a couple of dozen objects including those with

//      ResourceType = security-group

// If I run it with the line below included I get no errors, but an empty set [] as the output

tagRequest.withFilters([setResourceType]);

var instances = ec2client.describeTags(tagRequest);

System.log(instances);

============================================================

Here is the first part of the output if I don't filter the data:

DynamicWrapper (Instance) : [EC2DescribeTagsResult]-[class com.vmware.o11n.plugin.aws.ec2.model.DescribeTagsResult] -- VALUE :

{Tags: [

{ResourceId: sg-abcd1234,ResourceType: security-group,Key: Name,Value: sg1},

{ResourceId: sg-abcd1234,ResourceType: security-group,Key: aws:cloudformation:logical-id,Value: sg1},

...]}

Here is the entire output if I do try to filter the data:

DynamicWrapper (Instance) : [EC2DescribeTagsResult]-[class com.vmware.o11n.plugin.aws.ec2.model.DescribeTagsResult] -- VALUE : {Tags: [],}

0 Kudos
3 Replies
iiliev
VMware Employee
VMware Employee

I don't have AWS environment to validate it, but are you sure the the filter name format is correct? Instead of 'tag: ResourceType', could you check with something like 'ResourceType' or 'resource-type' ?

0 Kudos
ITVictor
Contributor
Contributor

Thanks and yes that corrected my first issue (setResourceType.setName('resource-type').  So now I get a hash that contains multiple key value pairs, but I can't figure out how to parse the hash and pull only the specific tag key/value pairs I want to work with.  Sample output is below.  How do I read this output and pull only the value of the name key for ResourceIdsubnet-abcd0123 (for example) - without turning it into a string and cobbling together logic to parse on spaces or semicolons.

Output DynamicWrapper (Instance) : 
[EC2DescribeTagsResult]-[class com.vmware.o11n.plugin.aws.ec2.model.DescribeTagsResult] -- VALUE : 
{Tags: [
{ResourceId: subnet-abcd1234,ResourceType: subnet,Key: Name,Value: SB-DB-1a}, 
{ResourceId: subnet-abcd2345,ResourceType: subnet,Key: Name,Value: SB-Web-1a}, 
{ResourceId: subnet-abcd3456,ResourceType: subnet,Key: Name,Value: SB-App-1a}, 
{ResourceId: subnet-bcde1234,ResourceType: subnet,Key: Name,Value: SB-App-1c}, 
{ResourceId: subnet-cdef1234,ResourceType: subnet,Key: Name,Value: SB-Web-1c}, 
{ResourceId: subnet-abcd0123,ResourceType: subnet,Key: Name,Value: SB-DB-1c}],}

Output DynamicWrapper (Instance) : 
[EC2DescribeTagsResult]-[class com.vmware.o11n.plugin.aws.ec2.model.DescribeTagsResult] -- VALUE : 
{Tags: [
{ResourceId: subnet-abcd1234,ResourceType: subnet,Key: Name,Value: SB-DB-1a}, 
{ResourceId: subnet-abcd2345,ResourceType: subnet,Key: Name,Value: SB-Web-1a}, 
{ResourceId: subnet-abcd3456,ResourceType: subnet,Key: Name,Value: SB-App-1a}, 
{ResourceId: subnet-bcde1234,ResourceType: subnet,Key: Name,Value: SB-App-1c}, 
{ResourceId: subnet-cdef1234,ResourceType: subnet,Key: Name,Value: SB-Web-1c}, 
{ResourceId: subnet-abcd0123,ResourceType: subnet,Key: Name,Value: SB-DB-1c}],}
0 Kudos
iiliev
VMware Employee
VMware Employee

EC2DescribeTagsResult scripting object doesn't expose scripting API to access tag list.

Unfortunately, there is no good way to resolve it. One option to try if accessing the underlying Java code will work; something like instances.getNativeObject().getTags(). Another option is to try to parse the string value as JSON, which will require some tweaks as the value is close, but not exactly a valid JSON; for example, the keys/values are not quoted.

0 Kudos