|
6 | 6 |
|
7 | 7 |
|
8 | 8 | class NetboxHTTPAction(NetboxBaseAction): |
9 | | - """Base run action""" |
| 9 | + """ |
| 10 | + Action to call netbox api and return response. |
| 11 | + """ |
10 | 12 |
|
11 | | - def run(self, endpoint_uri, http_verb, get_detail_route_eligible, **kwargs): |
12 | | - """Base run action |
| 13 | + def run(self, endpoint_uri, http_verb, get_detail_route_eligible, fail_non_2xx, **kwargs): |
13 | 14 | """ |
14 | | - |
15 | | - if http_verb == 'get': |
16 | | - if kwargs.get('id', False) and get_detail_route_eligible: |
| 15 | + StackStorm action entry point. |
| 16 | + """ |
| 17 | + if http_verb == "get": |
| 18 | + if kwargs.get("id", False) and get_detail_route_eligible: |
17 | 19 | # modify the `endpoint_uri` to use the detail route |
18 | | - endpoint_uri = '{}{}/'.format(endpoint_uri, str(kwargs.pop('id'))) |
| 20 | + endpoint_uri = "{}{}/".format(endpoint_uri, str(kwargs.pop("id"))) |
19 | 21 | self.logger.debug( |
20 | | - 'endpoint_uri transformed to {} because id was passed'.format(endpoint_uri) |
| 22 | + "endpoint_uri transformed to {} because id was passed".format(endpoint_uri) |
21 | 23 | ) |
22 | 24 |
|
23 | | - if kwargs.get('save_in_key_store') and not kwargs.get('save_in_key_store_key_name'): |
24 | | - return (False, 'save_in_key_store_key_name MUST be used with save_in_key_store!') |
| 25 | + if kwargs.get("save_in_key_store") and not kwargs.get("save_in_key_store_key_name"): |
| 26 | + return (False, "save_in_key_store_key_name MUST be used with save_in_key_store!") |
25 | 27 |
|
26 | 28 | result = self.make_request(endpoint_uri, http_verb, **kwargs) |
27 | 29 |
|
28 | | - if kwargs['save_in_key_store']: |
| 30 | + if fail_non_2xx: |
| 31 | + # Return error rather than storing it in the keystone. |
| 32 | + if result["status"] not in range(200, 300): |
| 33 | + return (False, result) |
| 34 | + |
| 35 | + if kwargs["save_in_key_store"]: |
29 | 36 | # save the result in the st2 keystore |
30 | | - client = Client(base_url='http://localhost') |
31 | | - key_name = kwargs['save_in_key_store_key_name'] |
32 | | - client.keys.update(KeyValuePair(name=key_name, value=json.dumps(result), |
33 | | - ttl=kwargs['save_in_key_store_ttl'])) |
| 37 | + client = Client(base_url="http://localhost") |
| 38 | + key_name = kwargs["save_in_key_store_key_name"] |
| 39 | + client.keys.update( |
| 40 | + KeyValuePair( |
| 41 | + name=key_name, value=json.dumps(result), ttl=kwargs["save_in_key_store_ttl"] |
| 42 | + ) |
| 43 | + ) |
34 | 44 |
|
35 | 45 | return (True, "Result stored in st2 key {}".format(key_name)) |
36 | 46 |
|
37 | 47 | else: |
38 | 48 | result = self.make_request(endpoint_uri, http_verb, **kwargs) |
39 | 49 |
|
40 | | - return result |
| 50 | + # To maintain backward compatibility, this action always returns True (success) event |
| 51 | + # when the netbox http return status is not OK. action_succeeded is set to True and |
| 52 | + # will only be set to False if the `fail_non_2xx` argument is set to True _and_ netbox |
| 53 | + # http return code is not 2xx. |
| 54 | + action_succeeded = True |
| 55 | + |
| 56 | + if fail_non_2xx: |
| 57 | + action_succeeded = result.get("status") in range(200, 300) |
| 58 | + |
| 59 | + return (action_succeeded, result) |
0 commit comments