StackStorm-Exchange / stackstorm-aws

st2 content pack containing Amazon Web Services integrations.
https://exchange.stackstorm.org/
Apache License 2.0
15 stars 26 forks source link

Invalid type for parameter Filters[0] when filtering for tags #108

Open drksnw opened 4 years ago

drksnw commented 4 years ago

Hello,

I have a problem with the ec2_describe_instances action.

Calling the action without any filter works well, however I would need to only get instances with a specific tag.

I tried adding the Filters parameter like this : Filters=tag:Name=test-instance however, it crashes at botocore as it need to have a dict instead of a string.

I've also tried to write the filter like a dict (with { and }), but it seems that the parameter is always casted to a string, which breaks the functionality.

Here is the full stack trace :

st2.actions.python.ActionManager: DEBUG    Calling method "boto3.ec2.describe_instances" with kwargs: {u'Filters': [u'tag:Name=test-instance']}
Traceback (most recent call last):
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/python_runner/python_action_wrapper.py", line 333, in <module>
    obj.run()
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/python_runner/python_action_wrapper.py", line 192, in run
    output = action.run(**self._parameters)
  File "/opt/stackstorm/packs/aws/actions/run.py", line 36, in run
    return self.do_method(module_path, cls, aws_action, **kwargs)
  File "/opt/stackstorm/packs/aws/actions/lib/action.py", line 226, in do_method
    resultset = getattr(obj, action)(**kwargs)
  File "/opt/stackstorm/virtualenvs/aws/lib/python2.7/site-packages/botocore/client.py", line 251, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/opt/stackstorm/virtualenvs/aws/lib/python2.7/site-packages/botocore/client.py", line 513, in _make_api_call
    api_params, operation_model, context=request_context)
  File "/opt/stackstorm/virtualenvs/aws/lib/python2.7/site-packages/botocore/client.py", line 566, in _convert_to_request_dict
    api_params, operation_model)
  File "/opt/stackstorm/virtualenvs/aws/lib/python2.7/site-packages/botocore/validate.py", line 270, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Filters[0], value: tag:Name=test-instance, type: <type 'unicode'>, valid types: <type 'dict'>

Do you know if I'm using the Filters parameter wrong, or is there any other problem ?

Thanks in advance for your help !

Best regards

winem commented 4 years ago

Hi,

try it like this: ec2_client.describe_instances(Filters=[{'Name': 'tag:Key', 'Values': [ test-instance ]})

This will return all instances with a tag test-instance regardless of the value. If you want to check for a specific value the query would look like this: (Filters=[{'Name': 'tag:test-instance', 'Values': [ <the-value-you-are-looking-for> ]}).

Cheers, Marcel

drksnw commented 4 years ago

Hi @winem, Thanks for your quick reply.

It seems that it doesn't work either with this syntax, now I have these error messages :

Invalid type for parameter Filters[0], value: Name:tag:Key, type: <type 'unicode'>, valid types: <type 'dict'>
Invalid type for parameter Filters[1], value: Values:[test-instance], type: <type 'unicode'>, valid types: <type 'dict'>

I'm trying to call it directly with st2 run, maybe have I to use a different syntax ?

The command I'm running is the following :

st2 run aws.ec2_describe_instances Filters={'Name':'tag:Key','Values':[test-instance]} account_id=xxxxxxxxxxxx region=eu-central-1

Thanks !

Best regards

winem commented 4 years ago

Hi, can't test it right now but as far as I remember Filters need to be an array, too. So wrap the filter in [] please -> [{'Name':'tag:Key','Values':[test-instance]}]

If that's not working either I'll try to find some time test it later today.

drksnw commented 4 years ago

Hi, Mmh, still not :

botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Filters[0], value: [Name:tag:Key], type: <type 'unicode'>, valid types: <type 'dict'>
Invalid type for parameter Filters[1], value: [Values:[test-instance]], type: <type 'unicode'>, valid types: <type 'dict'>

I will try to dig deeper in the code too, maybe I will find something interesting.

Thanks for your help !

winem commented 4 years ago

Please post your current filter again. Is it {'Name':'tag:Key'],['Values':[test-instance]]} now?

drksnw commented 4 years ago

My filter is : [{'Name':'tag:Key','Values':[test-instance]}]

drksnw commented 4 years ago

Ok, I've found out what's wrong.

I have modified the action.py script in order to get the filters in an user-friendlier way (like Filters=Key1=Value1,Key2=Value2...)

I will edit it in order to have multiples values for a key, and I will do a pull request.

Best regards

satellite-no commented 2 years ago

Has this gone anywhere? What is the current way to implement filters or is this just simply broke?

I've tried every way I can think of.

st2 run aws.ec2_describe_instances Filters=instance-state-name=running
st2 run aws.ec2_describe_instances Filters='instance-state-name'='running'
st2 run aws.ec2_describe_instances Filters="{instance-state-name='running'}"
st2 run aws.ec2_describe_instances Filters={instance-state-name='running'}
st2 run aws.ec2_describe_instances Filters={instance-state-name=running}
st2 run aws.ec2_describe_instances Filters=[{'Name': 'instance-state-name', 'Values': 'running'}]
st2 run aws.ec2_describe_instances Filters=[{'Name':'instance-state-name','Values':'running'}]
st2 run aws.ec2_describe_instances Filters={Name:'instance-state-name',Values:'running'}
amanda11 commented 2 years ago

I think Values is an array. I'm not using AWS pack, but the AWS_boto3 pack, and with that I have:

    action: aws_boto3.boto3action
    input:
      action_name: describe_instances
      service: ec2
      params: 
        Filters: 
          - Name: instance-state-name
            Values: 
              - running

So that would imply that you might want to try: st2 run aws.ec2_describe_instances Filters=[{'Name':'instance-state-name','Values':['running']}]

satellite-no commented 2 years ago

Hey @amanda11, Thanks for the suggestion, looks like that gives the same errors as the others. I'm really thinking this action is just bugged.

botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Filters[0], value: [Name:instance-state-name], type: <class 'str'>, valid types: <class 'dict'>
Invalid type for parameter Filters[1], value: [Values:[running]], type: <class 'str'>, valid types: <class 'dict'>
cwilson21 commented 2 years ago

Has anyone poked further at this? I am running into the same issue when trying to use ec2_describe_instances where the filter is right however it is still throwing a validation error @drksnw ?

cwilson21 commented 2 years ago

So just for the record here. I got this to work.... The problem being the single quotes. The filter I used looked sort of like this:

[{"Name":"tag:Name","Values":["value"]}]