Skip to content

Commit

Permalink
Improve validation of module return values (#63411)
Browse files Browse the repository at this point in the history
* Add contains: validation for return values.

* Only require returned: on top level.

* Fix various return value problems.

* Update ignore.txt.

* Two more.
  • Loading branch information
felixfontein authored and gundalow committed Oct 18, 2019
1 parent 4de516d commit 04252cf
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 37 deletions.
3 changes: 2 additions & 1 deletion lib/ansible/modules/cloud/alicloud/ali_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,8 @@
security_groups:
description: One or more security groups for the instance.
returned: always
type: list of complex
type: list
elements: dict
contains:
group_id:
description: The ID of the security group.
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/cloud/alicloud/ali_instance_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@
security_groups:
description: One or more security groups for the instance.
returned: always
type: list of complex
type: list
elements: dict
contains:
group_id:
description: The ID of the security group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@
step_adjustments:
description: A set of adjustments that enable you to scale based on the size of the alarm breach
returned: when state present and the policy type is StepScaling
type: list of complex
type: list
elements: dict
target_tracking_scaling_policy_configuration:
description: The target tracking policy.
returned: when state present and the policy type is TargetTrackingScaling
Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/modules/cloud/amazon/ec2_eni.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@
sample: Firewall network interface
groups:
description: list of security groups
type: list of dictionaries
type: list
elements: dict
sample: [ { "sg-f8a8a9da": "default" } ]
id:
description: network interface id
Expand All @@ -229,7 +230,8 @@
sample: 10.20.30.40
private_ip_addresses:
description: list of all private ip addresses associated to this interface
type: list of dictionaries
type: list
elements: dict
sample: [ { "primary_address": true, "private_ip_address": "10.20.30.40" } ]
source_dest_check:
description: value of source/dest check flag
Expand Down
12 changes: 8 additions & 4 deletions lib/ansible/modules/cloud/amazon/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@
groups:
description: One or more security groups.
returned: always
type: list of complex
type: list
elements: dict
contains:
group_id:
description: The ID of the security group.
Expand Down Expand Up @@ -522,7 +523,8 @@
private_ip_addresses:
description: The private IPv4 addresses associated with the network interface.
returned: always
type: list of complex
type: list
elements: dict
contains:
association:
description: The association information for an Elastic IP address (IPv4) associated with the network interface.
Expand Down Expand Up @@ -607,7 +609,8 @@
product_codes:
description: One or more product codes.
returned: always
type: list of complex
type: list
elements: dict
contains:
product_code_id:
description: The product code.
Expand Down Expand Up @@ -642,7 +645,8 @@
security_groups:
description: One or more security groups for the instance.
returned: always
type: list of complex
type: list
elements: dict
contains:
group_id:
description: The ID of the security group.
Expand Down
12 changes: 8 additions & 4 deletions lib/ansible/modules/cloud/amazon/ec2_instance_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@
groups:
description: One or more security groups.
returned: always
type: list of complex
type: list
elements: dict
contains:
group_id:
description: The ID of the security group.
Expand Down Expand Up @@ -308,7 +309,8 @@
private_ip_addresses:
description: The private IPv4 addresses associated with the network interface.
returned: always
type: list of complex
type: list
elements: dict
contains:
association:
description: The association information for an Elastic IP address (IPv4) associated with the network interface.
Expand Down Expand Up @@ -393,7 +395,8 @@
product_codes:
description: One or more product codes.
returned: always
type: list of complex
type: list
elements: dict
contains:
product_code_id:
description: The product code.
Expand Down Expand Up @@ -428,7 +431,8 @@
security_groups:
description: One or more security groups for the instance.
returned: always
type: list of complex
type: list
elements: dict
contains:
group_id:
description: The ID of the security group.
Expand Down
9 changes: 6 additions & 3 deletions lib/ansible/modules/cloud/amazon/ec2_vpc_nacl_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,23 @@
subnets:
description: A list of subnet IDs that are associated with the NACL.
returned: always
type: list of string
type: list
elements: str
ingress:
description:
- A list of NACL ingress rules with the following format.
- "C([rule no, protocol, allow/deny, v4 or v6 cidr, icmp_type, icmp_code, port from, port to])"
returned: always
type: list of list
type: list
elements: list
sample: [[100, 'tcp', 'allow', '0.0.0.0/0', null, null, 22, 22]]
egress:
description:
- A list of NACL egress rules with the following format.
- "C([rule no, protocol, allow/deny, v4 or v6 cidr, icmp_type, icmp_code, port from, port to])"
returned: always
type: list of list
type: list
elements: list
sample: [[100, 'all', 'allow', '0.0.0.0/0', null, null, null, null]]
'''

Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/cloud/amazon/ecs_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
type: str
attributes:
description: list of attributes
type: list of complex
type: list
elements: dict
contains:
name:
description: name of the attribute
Expand Down
12 changes: 8 additions & 4 deletions lib/ansible/modules/cloud/amazon/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@
deployments:
description: list of service deployments
returned: always
type: list of complex
type: list
elements: dict
deploymentConfiguration:
description: dictionary of deploymentConfiguration
returned: always
Expand All @@ -284,11 +285,13 @@
events:
description: list of service events
returned: always
type: list of complex
type: list
elements: dict
placementConstraints:
description: List of placement constraints objects
returned: always
type: list of complex
type: list
elements: dict
contains:
type:
description: The type of constraint. Valid values are distinctInstance and memberOf.
Expand All @@ -302,7 +305,8 @@
placementStrategy:
description: List of placement strategy objects
returned: always
type: list of complex
type: list
elements: dict
contains:
type:
description: The type of placement strategy. Valid values are random, spread and binpack.
Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/modules/cloud/amazon/ecs_service_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@
deployments:
description: list of service deployments
returned: always
type: list of complex
type: list
elements: dict
events:
description: list of service events
returned: when events is true
type: list of complex
type: list
elements: dict
''' # NOQA

try:
Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/modules/cloud/amazon/ecs_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@
overrides:
description: The container overrides set for this task.
returned: only when details is true
type: list of complex
type: list
elements: dict
lastStatus:
description: The last recorded status of the task.
returned: only when details is true
Expand All @@ -161,7 +162,8 @@
containers:
description: The container details.
returned: only when details is true
type: list of complex
type: list
elements: dict
startedBy:
description: The used who started the task.
returned: only when details is true
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/cloud/openstack/os_networks_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
subnets:
description: Subnet(s) included in this network.
returned: success
type: list of strings
type: list
elements: str
tenant_id:
description: Tenant id associated with this network.
returned: success
Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/modules/cloud/openstack/os_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@
sample: "test-stack/97a3f543-8136-4570-920e-fd7605c989d6"
links:
description: Links to the current Stack.
type: list of dict
type: list
elements: dict
sample: "[{'href': 'http://foo:8004/v1/7f6a/stacks/test-stack/97a3f543-8136-4570-920e-fd7605c989d6']"
outputs:
description: Output returned by the Stack.
type: list of dict
type: list
elements: dict
sample: "{'description': 'IP address of server1 in private network',
'output_key': 'server1_private_ip',
'output_value': '10.1.10.103'}"
Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/modules/cloud/openstack/os_subnets_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@
dns_nameservers:
description: DNS name servers for this subnet.
returned: success
type: list of strings
type: list
elements: str
allocation_pools:
description: Allocation pools associated with this subnet.
returned: success
type: list of dicts
type: list
elements: dict
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/modules/network/meraki/meraki_mx_l7_firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@
id:
description: URI of application category.
returned: success
type: string
type: str
sample: Email
name:
description: Descriptive name of application category.
returned: success
type: string
type: str
sample: layer7/category/1
'''

Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/windows/win_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@
message:
description: Any messages returned from the feature subsystem that occurred during installation or removal of this feature.
returned: always
type: list of strings
type: list
elements: str
sample: []
reboot_required:
description: True when the target server requires a reboot as a result of installing or removing this feature.
Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/modules/windows/win_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@
kb:
description: A list of KB article IDs that apply to the update.
returned: always
type: list of strings
type: list
elements: str
sample: [ '3004365' ]
id:
description: Internal Windows Update GUID.
Expand All @@ -231,7 +232,8 @@
categories:
description: A list of category strings for this update.
returned: always
type: list of strings
type: list
elements: str
sample: [ 'Critical Updates', 'Windows Server 2012 R2' ]
failure_hresult_code:
description: The HRESULT code from a failed update.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,30 @@ def return_contains(v):
return v


return_contains_schema = Any(
All(
Schema(
{
Required('description'): Any(list_string_types, *string_types),
'returned': Any(*string_types), # only returned on top level
Required('type'): Any('bool', 'complex', 'dict', 'float', 'int', 'list', 'str'),
'version_added': Any(float, *string_types),
'sample': Any(None, list, dict, int, float, *string_types),
'example': Any(None, list, dict, int, float, *string_types),
'contains': Any(None, *list({str_type: Self} for str_type in string_types)),
# in case of type='list' elements define type of individual item in list
'elements': Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str'),
}
),
Schema(return_contains)
),
Schema(type(None)),
)

# This generates list of dicts with keys from string_types and return_contains_schema value
# for example in Python 3: {str: return_contains_schema}
list_dict_return_contains_schema = [{str_type: return_contains_schema} for str_type in string_types]

return_schema = Any(
All(
Schema(
Expand All @@ -141,7 +165,7 @@ def return_contains(v):
'version_added': Any(float, *string_types),
'sample': Any(None, list, dict, int, float, *string_types),
'example': Any(None, list, dict, int, float, *string_types),
'contains': object,
'contains': Any(None, *list_dict_return_contains_schema),
# in case of type='list' elements define type of individual item in list
'elements': Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str'),
}
Expand Down
Loading

0 comments on commit 04252cf

Please sign in to comment.