mirror of
https://github.com/ansible-collections/google.cloud.git
synced 2025-04-25 20:31:24 -07:00
New module: gcp_bigquery_table_facts (#50679)
This commit is contained in:
parent
c585f79756
commit
e3d1e6956d
2 changed files with 611 additions and 9 deletions
|
@ -19,6 +19,7 @@ except ImportError:
|
|||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils._text import to_text
|
||||
import ast
|
||||
import os
|
||||
|
||||
|
||||
|
@ -60,7 +61,15 @@ def replace_resource_dict(item, value):
|
|||
else:
|
||||
if not item:
|
||||
return item
|
||||
return item.get(value)
|
||||
if isinstance(item, dict):
|
||||
return item.get(value)
|
||||
|
||||
# Item could be a string or a string representing a dictionary.
|
||||
try:
|
||||
new_item = ast.literal_eval(item)
|
||||
return replace_resource_dict(new_item, value)
|
||||
except ValueError:
|
||||
return new_item
|
||||
|
||||
|
||||
# Handles all authentation and HTTP sessions for GCP API calls.
|
||||
|
@ -77,9 +86,25 @@ class GcpSession(object):
|
|||
except getattr(requests.exceptions, 'RequestException') as inst:
|
||||
self.module.fail_json(msg=inst.message)
|
||||
|
||||
def post(self, url, body=None):
|
||||
def post(self, url, body=None, headers=None, **kwargs):
|
||||
if headers:
|
||||
headers = self.merge_dictionaries(headers, self._headers())
|
||||
else:
|
||||
headers = self._headers()
|
||||
|
||||
try:
|
||||
return self.session().post(url, json=body, headers=self._headers())
|
||||
return self.session().post(url, json=body, headers=headers)
|
||||
except getattr(requests.exceptions, 'RequestException') as inst:
|
||||
self.module.fail_json(msg=inst.message)
|
||||
|
||||
def post_contents(self, url, file_contents=None, headers=None, **kwargs):
|
||||
if headers:
|
||||
headers = self.merge_dictionaries(headers, self._headers())
|
||||
else:
|
||||
headers = self._headers()
|
||||
|
||||
try:
|
||||
return self.session().post(url, data=file_contents, headers=headers)
|
||||
except getattr(requests.exceptions, 'RequestException') as inst:
|
||||
self.module.fail_json(msg=inst.message)
|
||||
|
||||
|
@ -95,9 +120,16 @@ class GcpSession(object):
|
|||
except getattr(requests.exceptions, 'RequestException') as inst:
|
||||
self.module.fail_json(msg=inst.message)
|
||||
|
||||
def patch(self, url, body=None, **kwargs):
|
||||
kwargs.update({'json': body, 'headers': self._headers()})
|
||||
try:
|
||||
return self.session().patch(url, **kwargs)
|
||||
except getattr(requests.exceptions, 'RequestException') as inst:
|
||||
self.module.fail_json(msg=inst.message)
|
||||
|
||||
def session(self):
|
||||
return AuthorizedSession(
|
||||
self._credentials().with_scopes(self.module.params['scopes']))
|
||||
self._credentials())
|
||||
|
||||
def _validate(self):
|
||||
if not HAS_REQUESTS:
|
||||
|
@ -106,9 +138,6 @@ class GcpSession(object):
|
|||
if not HAS_GOOGLE_LIBRARIES:
|
||||
self.module.fail_json(msg="Please install the google-auth library")
|
||||
|
||||
if 'auth_kind' not in self.module.params:
|
||||
self.module.fail_json(msg="Auth kind parameter is missing")
|
||||
|
||||
if self.module.params.get('service_account_email') is not None and self.module.params['auth_kind'] != 'machineaccount':
|
||||
self.module.fail_json(
|
||||
msg="Service Acccount Email only works with Machine Account-based authentication"
|
||||
|
@ -122,11 +151,11 @@ class GcpSession(object):
|
|||
def _credentials(self):
|
||||
cred_type = self.module.params['auth_kind']
|
||||
if cred_type == 'application':
|
||||
credentials, project_id = google.auth.default()
|
||||
credentials, project_id = google.auth.default(scopes=self.module.params['scopes'])
|
||||
return credentials
|
||||
elif cred_type == 'serviceaccount':
|
||||
path = os.path.realpath(os.path.expanduser(self.module.params['service_account_file']))
|
||||
return service_account.Credentials.from_service_account_file(path)
|
||||
return service_account.Credentials.from_service_account_file(path).with_scopes(self.module.params['scopes'])
|
||||
elif cred_type == 'machineaccount':
|
||||
return google.auth.compute_engine.Credentials(
|
||||
self.module.params['service_account_email'])
|
||||
|
@ -138,6 +167,11 @@ class GcpSession(object):
|
|||
'User-Agent': "Google-Ansible-MM-{0}".format(self.product)
|
||||
}
|
||||
|
||||
def _merge_dictionaries(self, a, b):
|
||||
new = a.copy()
|
||||
new.update(b)
|
||||
return new
|
||||
|
||||
|
||||
class GcpModule(AnsibleModule):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue