Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Ansible - Remove output only from response (#216)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @rambleraptor
  • Loading branch information
modular-magician authored and rambleraptor committed Sep 5, 2019
1 parent 0f62501 commit 84ecdd1
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 163 deletions.
68 changes: 49 additions & 19 deletions plugins/module_utils/gcp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,22 @@ def _compare_lists(self, req_list, resp_list):
# Have to convert each thing over to unicode.
# Python doesn't handle equality checks between unicode + non-unicode well.
difference = []
for index in range(len(list1)):
value1 = list1[index]
if index < len(list2):
value2 = list2[index]
difference.append(self._compare_value(value1, value2))
new_req_list = self._convert_value(req_list)
new_resp_list = self._convert_value(resp_list)

# We have to compare each thing in the request to every other thing
# in the response.
# This is because the request value will be a subset of the response value.
# The assumption is that these lists will be small enough that it won't
# be a performance burden.
for req_item in new_req_list:
found_item = False
for resp_item in new_resp_list:
# Looking for a None value here.
if not self._compare_value(req_item, resp_item):
found_item = True
if not found_item:
difference.append(req_item)

difference2 = []
for value in difference:
Expand All @@ -306,12 +317,12 @@ def _compare_value(self, req_value, resp_value):

# Can assume non-None types at this point.
try:
if isinstance(value1, list):
diff = self._compare_lists(value1, value2)
elif isinstance(value2, dict):
diff = self._compare_dicts(value1, value2)
elif isinstance(value1, bool):
diff = self._compare_boolean(value1, value2)
if isinstance(req_value, list):
diff = self._compare_lists(req_value, resp_value)
elif isinstance(req_value, dict):
diff = self._compare_dicts(req_value, resp_value)
elif isinstance(req_value, bool):
diff = self._compare_boolean(req_value, resp_value)
# Always use to_text values to avoid unicode issues.
elif to_text(req_value) != to_text(resp_value):
diff = req_value
Expand All @@ -322,24 +333,43 @@ def _compare_value(self, req_value, resp_value):

return diff

def _compare_boolean(self, value1, value2):
# Compare two boolean values.
def _compare_boolean(self, req_value, resp_value):
try:
# Both True
if value1 and isinstance(value2, bool) and value2:
if req_value and isinstance(resp_value, bool) and resp_value:
return None
# Value1 True, value2 'true'
elif value1 and to_text(value2) == 'true':
# Value1 True, resp_value 'true'
elif req_value and to_text(resp_value) == 'true':
return None
# Both False
elif not value1 and isinstance(value2, bool) and not value2:
elif not req_value and isinstance(resp_value, bool) and not resp_value:
return None
# Value1 False, value2 'false'
elif not value1 and to_text(value2) == 'false':
# Value1 False, resp_value 'false'
elif not req_value and to_text(resp_value) == 'false':
return None
else:
return value2
return resp_value

# to_text may throw UnicodeErrors.
# These errors shouldn't crash Ansible and should be hidden.
except UnicodeError:
return None

# Python (2 esp.) doesn't do comparisons between unicode + non-unicode well.
# This leads to a lot of false positives when diffing values.
# The Ansible to_text() function is meant to get all strings
# into a standard format.
def _convert_value(self, value):
if isinstance(value, list):
new_list = []
for item in value:
new_list.append(self._convert_value(item))
return new_list
elif isinstance(value, dict):
new_dict = {}
for key in value:
new_dict[key] = self._convert_value(value[key])
return new_dict
else:
return to_text(value)
16 changes: 2 additions & 14 deletions plugins/modules/gcp_bigquery_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,22 +1254,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict(
{
u'estimatedBytes': self.request.get('estimated_bytes'),
u'estimatedRows': self.request.get('estimated_rows'),
u'oldestEntryTime': self.request.get('oldest_entry_time'),
}
)
return remove_nones_from_dict({})

def from_response(self):
return remove_nones_from_dict(
{
u'estimatedBytes': self.request.get(u'estimatedBytes'),
u'estimatedRows': self.request.get(u'estimatedRows'),
u'oldestEntryTime': self.request.get(u'oldestEntryTime'),
}
)
return remove_nones_from_dict({})


class TableSchema(object):
Expand Down
2 changes: 0 additions & 2 deletions plugins/modules/gcp_compute_backend_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,6 @@ def to_request(self):
u'enabled': self.request.get('enabled'),
u'oauth2ClientId': self.request.get('oauth2_client_id'),
u'oauth2ClientSecret': self.request.get('oauth2_client_secret'),
u'oauth2ClientSecretSha256': self.request.get('oauth2_client_secret_sha256'),
}
)

Expand All @@ -999,7 +998,6 @@ def from_response(self):
u'enabled': self.request.get(u'enabled'),
u'oauth2ClientId': self.request.get(u'oauth2ClientId'),
u'oauth2ClientSecret': self.request.get(u'oauth2ClientSecret'),
u'oauth2ClientSecretSha256': self.request.get(u'oauth2ClientSecretSha256'),
}
)

Expand Down
24 changes: 6 additions & 18 deletions plugins/modules/gcp_compute_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,14 +664,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256'), u'kmsKeyName': self.request.get('kms_key_name')}
)
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'kmsKeyName': self.request.get('kms_key_name')})

def from_response(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256'), u'kmsKeyName': self.request.get(u'kmsKeyName')}
)
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'kmsKeyName': self.request.get(u'kmsKeyName')})


class DiskDiskencryptionkey(object):
Expand All @@ -683,14 +679,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256'), u'kmsKeyName': self.request.get('kms_key_name')}
)
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'kmsKeyName': self.request.get('kms_key_name')})

def from_response(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256'), u'kmsKeyName': self.request.get(u'kmsKeyName')}
)
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'kmsKeyName': self.request.get(u'kmsKeyName')})


class DiskSourcesnapshotencryptionkey(object):
Expand All @@ -702,14 +694,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get('raw_key'), u'kmsKeyName': self.request.get('kms_key_name'), u'sha256': self.request.get('sha256')}
)
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'kmsKeyName': self.request.get('kms_key_name')})

def from_response(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get(u'rawKey'), u'kmsKeyName': self.request.get(u'kmsKeyName'), u'sha256': self.request.get(u'sha256')}
)
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'kmsKeyName': self.request.get(u'kmsKeyName')})


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gcp_compute_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key')})

def from_response(self):
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey')})


class ImageRawdisk(object):
Expand Down Expand Up @@ -764,10 +764,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key')})

def from_response(self):
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey')})


if __name__ == '__main__':
Expand Down
14 changes: 4 additions & 10 deletions plugins/modules/gcp_compute_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,14 +1298,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get('raw_key'), u'rsaEncryptedKey': self.request.get('rsa_encrypted_key'), u'sha256': self.request.get('sha256')}
)
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'rsaEncryptedKey': self.request.get('rsa_encrypted_key')})

def from_response(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get(u'rawKey'), u'rsaEncryptedKey': self.request.get(u'rsaEncryptedKey'), u'sha256': self.request.get(u'sha256')}
)
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'rsaEncryptedKey': self.request.get(u'rsaEncryptedKey')})


class InstanceInitializeparams(object):
Expand Down Expand Up @@ -1348,10 +1344,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key')})

def from_response(self):
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey')})


class InstanceGuestacceleratorsArray(object):
Expand Down Expand Up @@ -1406,7 +1402,6 @@ def _request_for_item(self, item):
{
u'accessConfigs': InstanceAccessconfigsArray(item.get('access_configs', []), self.module).to_request(),
u'aliasIpRanges': InstanceAliasiprangesArray(item.get('alias_ip_ranges', []), self.module).to_request(),
u'name': item.get('name'),
u'network': replace_resource_dict(item.get(u'network', {}), 'selfLink'),
u'networkIP': item.get('network_ip'),
u'subnetwork': replace_resource_dict(item.get(u'subnetwork', {}), 'selfLink'),
Expand All @@ -1418,7 +1413,6 @@ def _response_from_item(self, item):
{
u'accessConfigs': InstanceAccessconfigsArray(item.get(u'accessConfigs', []), self.module).from_response(),
u'aliasIpRanges': InstanceAliasiprangesArray(item.get(u'aliasIpRanges', []), self.module).from_response(),
u'name': item.get(u'name'),
u'network': item.get(u'network'),
u'networkIP': item.get(u'networkIP'),
u'subnetwork': item.get(u'subnetwork'),
Expand Down
26 changes: 2 additions & 24 deletions plugins/modules/gcp_compute_instance_group_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,32 +537,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict(
{
u'abandoning': self.request.get('abandoning'),
u'creating': self.request.get('creating'),
u'creatingWithoutRetries': self.request.get('creating_without_retries'),
u'deleting': self.request.get('deleting'),
u'none': self.request.get('none'),
u'recreating': self.request.get('recreating'),
u'refreshing': self.request.get('refreshing'),
u'restarting': self.request.get('restarting'),
}
)
return remove_nones_from_dict({})

def from_response(self):
return remove_nones_from_dict(
{
u'abandoning': self.request.get(u'abandoning'),
u'creating': self.request.get(u'creating'),
u'creatingWithoutRetries': self.request.get(u'creatingWithoutRetries'),
u'deleting': self.request.get(u'deleting'),
u'none': self.request.get(u'none'),
u'recreating': self.request.get(u'recreating'),
u'refreshing': self.request.get(u'refreshing'),
u'restarting': self.request.get(u'restarting'),
}
)
return remove_nones_from_dict({})


class InstanceGroupManagerNamedportsArray(object):
Expand Down
14 changes: 4 additions & 10 deletions plugins/modules/gcp_compute_instance_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,14 +1237,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get('raw_key'), u'rsaEncryptedKey': self.request.get('rsa_encrypted_key'), u'sha256': self.request.get('sha256')}
)
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'rsaEncryptedKey': self.request.get('rsa_encrypted_key')})

def from_response(self):
return remove_nones_from_dict(
{u'rawKey': self.request.get(u'rawKey'), u'rsaEncryptedKey': self.request.get(u'rsaEncryptedKey'), u'sha256': self.request.get(u'sha256')}
)
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'rsaEncryptedKey': self.request.get(u'rsaEncryptedKey')})


class InstanceTemplateInitializeparams(object):
Expand Down Expand Up @@ -1291,10 +1287,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key')})

def from_response(self):
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey')})


class InstanceTemplateGuestacceleratorsArray(object):
Expand Down Expand Up @@ -1349,7 +1345,6 @@ def _request_for_item(self, item):
{
u'accessConfigs': InstanceTemplateAccessconfigsArray(item.get('access_configs', []), self.module).to_request(),
u'aliasIpRanges': InstanceTemplateAliasiprangesArray(item.get('alias_ip_ranges', []), self.module).to_request(),
u'name': item.get('name'),
u'network': replace_resource_dict(item.get(u'network', {}), 'selfLink'),
u'networkIP': item.get('network_ip'),
u'subnetwork': replace_resource_dict(item.get(u'subnetwork', {}), 'selfLink'),
Expand All @@ -1361,7 +1356,6 @@ def _response_from_item(self, item):
{
u'accessConfigs': InstanceTemplateAccessconfigsArray(item.get(u'accessConfigs', []), self.module).from_response(),
u'aliasIpRanges': InstanceTemplateAliasiprangesArray(item.get(u'aliasIpRanges', []), self.module).from_response(),
u'name': item.get(u'name'),
u'network': item.get(u'network'),
u'networkIP': item.get(u'networkIP'),
u'subnetwork': item.get(u'subnetwork'),
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/gcp_compute_interconnect_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'tag8021q': self.request.get('tag8021q')})
return remove_nones_from_dict({})

def from_response(self):
return remove_nones_from_dict({u'tag8021q': self.request.get(u'tag8021q')})
return remove_nones_from_dict({})


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gcp_compute_region_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key')})

def from_response(self):
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey')})


class RegionDiskSourcesnapshotencryptionkey(object):
Expand All @@ -600,10 +600,10 @@ def __init__(self, request, module):
self.request = {}

def to_request(self):
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key'), u'sha256': self.request.get('sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get('raw_key')})

def from_response(self):
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey'), u'sha256': self.request.get(u'sha256')})
return remove_nones_from_dict({u'rawKey': self.request.get(u'rawKey')})


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/gcp_compute_ssl_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ def from_response(self):
return items

def _request_for_item(self, item):
return remove_nones_from_dict({u'code': item.get('code'), u'message': item.get('message')})
return remove_nones_from_dict({})

def _response_from_item(self, item):
return remove_nones_from_dict({u'code': item.get(u'code'), u'message': item.get(u'message')})
return remove_nones_from_dict({})


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 84ecdd1

Please sign in to comment.