Changing name_pattern -> pattern (#290)

Signed-off-by: Modular Magician <magic-modules@google.com>
This commit is contained in:
The Magician 2019-07-11 12:24:00 -07:00 committed by Alex Stephen
parent da912bcb6c
commit baafbbe715
3 changed files with 43 additions and 40 deletions

View file

@ -295,6 +295,7 @@ expirationPolicy:
from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, remove_nones_from_dict, replace_resource_dict
import json
import re
################################################################################
# Main
@ -385,8 +386,8 @@ def delete(module, link):
def resource_to_request(module):
request = {
u'name': module.params.get('name'),
u'topic': replace_resource_dict(module.params.get(u'topic', {}), 'name'),
u'name': name_pattern(module.params.get('name'), module),
u'topic': topic_pattern(replace_resource_dict(module.params.get(u'topic', {}), 'name'), module),
u'labels': module.params.get('labels'),
u'pushConfig': SubscriptionPushconfig(module.params.get('push_config', {}), module).to_request(),
u'ackDeadlineSeconds': module.params.get('ack_deadline_seconds'),
@ -394,7 +395,6 @@ def resource_to_request(module):
u'retainAckedMessages': module.params.get('retain_acked_messages'),
u'expirationPolicy': SubscriptionExpirationpolicy(module.params.get('expiration_policy', {}), module).to_request(),
}
request = encode_request(request, module)
return_vals = {}
for k, v in request.items():
if v or v is False:
@ -431,8 +431,6 @@ def return_if_object(module, response, allow_not_found=False):
except getattr(json.decoder, 'JSONDecodeError', ValueError):
module.fail_json(msg="Invalid JSON response with error: %s" % response.text)
result = decode_request(result, module)
if navigate_hash(result, ['error', 'errors']):
module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
@ -442,7 +440,6 @@ def return_if_object(module, response, allow_not_found=False):
def is_different(module, response):
request = resource_to_request(module)
response = response_to_hash(module, response)
request = decode_request(request, module)
# Remove all output-only from response.
response_vals = {}
@ -462,8 +459,8 @@ def is_different(module, response):
# This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response):
return {
u'name': module.params.get('name'),
u'topic': replace_resource_dict(module.params.get(u'topic', {}), 'name'),
u'name': name_pattern(module.params.get('name'), module),
u'topic': topic_pattern(replace_resource_dict(module.params.get(u'topic', {}), 'name'), module),
u'labels': response.get(u'labels'),
u'pushConfig': SubscriptionPushconfig(response.get(u'pushConfig', {}), module).from_response(),
u'ackDeadlineSeconds': response.get(u'ackDeadlineSeconds'),
@ -473,25 +470,29 @@ def response_to_hash(module, response):
}
def decode_request(response, module):
if 'name' in response:
response['name'] = response['name'].split('/')[-1]
def name_pattern(name, module):
if name is None:
return
if 'topic' in response:
response['topic'] = response['topic'].split('/')[-1]
regex = r"projects/.*/subscriptions/.*"
return response
if not re.match(regex, name):
name = "projects/{project}/subscriptions/{name}".format(**module.params)
return name
def encode_request(request, module):
if isinstance(module.params.get('topic'), dict):
request['topic'] = '/'.join(['projects', module.params['project'], 'topics', replace_resource_dict(module.params.get(u'topic', {}), 'name')])
else:
request['topic'] = '/'.join(['projects', module.params['project'], 'topics', module.params['topic']])
def topic_pattern(name, module):
if name is None:
return
request['name'] = '/'.join(['projects', module.params['project'], 'subscriptions', module.params['name']])
regex = r"projects/.*/topics/.*"
return request
if not re.match(regex, name):
formatted_params = {'project': module.params['project'], 'topic': replace_resource_dict(module.params['topic'], 'name')}
name = "projects/{project}/topics/{topic}".format(**formatted_params)
return name
class SubscriptionPushconfig(object):

View file

@ -107,6 +107,7 @@ labels:
from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, replace_resource_dict
import json
import re
################################################################################
# Main
@ -181,8 +182,11 @@ def delete(module, link):
def resource_to_request(module):
request = {u'name': module.params.get('name'), u'kmsKeyName': module.params.get('kms_key_name'), u'labels': module.params.get('labels')}
request = encode_request(request, module)
request = {
u'name': name_pattern(module.params.get('name'), module),
u'kmsKeyName': module.params.get('kms_key_name'),
u'labels': module.params.get('labels'),
}
return_vals = {}
for k, v in request.items():
if v or v is False:
@ -219,8 +223,6 @@ def return_if_object(module, response, allow_not_found=False):
except getattr(json.decoder, 'JSONDecodeError', ValueError):
module.fail_json(msg="Invalid JSON response with error: %s" % response.text)
result = decode_request(result, module)
if navigate_hash(result, ['error', 'errors']):
module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
@ -230,7 +232,6 @@ def return_if_object(module, response, allow_not_found=False):
def is_different(module, response):
request = resource_to_request(module)
response = response_to_hash(module, response)
request = decode_request(request, module)
# Remove all output-only from response.
response_vals = {}
@ -249,18 +250,19 @@ def is_different(module, response):
# Remove unnecessary properties from the response.
# This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response):
return {u'name': module.params.get('name'), u'kmsKeyName': module.params.get('kms_key_name'), u'labels': response.get(u'labels')}
return {u'name': name_pattern(module.params.get('name'), module), u'kmsKeyName': module.params.get('kms_key_name'), u'labels': response.get(u'labels')}
def decode_request(response, module):
if 'name' in response:
response['name'] = response['name'].split('/')[-1]
return response
def name_pattern(name, module):
if name is None:
return
regex = r"projects/.*/topics/.*"
def encode_request(request, module):
request['name'] = '/'.join(['projects', module.params['project'], 'topics', module.params['name']])
return request
if not re.match(regex, name):
name = "projects/{project}/topics/{name}".format(**module.params)
return name
if __name__ == '__main__':

View file

@ -153,7 +153,7 @@ def delete(module, link):
def resource_to_request(module):
request = {u'name': name_partial_to_full(module.params.get('name'), module.params)}
request = {u'name': name_pattern(module.params.get('name'), module)}
return_vals = {}
for k, v in request.items():
if v or v is False:
@ -217,17 +217,17 @@ def is_different(module, response):
# Remove unnecessary properties from the response.
# This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response):
return {u'name': name_partial_to_full(module.params.get('name'), module.params), u'url': response.get(u'url'), u'size': response.get(u'size')}
return {u'name': name_pattern(module.params.get('name'), module), u'url': response.get(u'url'), u'size': response.get(u'size')}
def name_partial_to_full(name, params):
def name_pattern(name, module):
if name is None:
return
url = r"projects/.*/repos/.*"
regex = r"projects/.*/repos/.*"
if not re.match(url, name):
name = "projects/{project}/repos/{name}".format(**params)
if not re.match(regex, name):
name = "projects/{project}/repos/{name}".format(**module.params)
return name