From 72cc27fec1c046e941ccfbfff914c1453eb1f20c Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 27 Nov 2018 12:29:50 -0800 Subject: [PATCH] Ansible Storage Upload/Download --- plugins/module_utils/gcp_utils.py | 19 +++++-------------- plugins/modules/gcp_storage_object.py | 27 ++++++++++++++------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/plugins/module_utils/gcp_utils.py b/plugins/module_utils/gcp_utils.py index d5a5b18..7a5a363 100644 --- a/plugins/module_utils/gcp_utils.py +++ b/plugins/module_utils/gcp_utils.py @@ -87,25 +87,16 @@ class GcpSession(object): except getattr(requests.exceptions, 'RequestException') as inst: self.module.fail_json(msg=inst.message) - def post(self, url, body=None, headers=None, **kwargs): - if headers: - headers = self.merge_dictionaries(headers, self._headers()) - else: - headers = self._headers() - + def post(self, url, body=None, headers={}, **kwargs): + kwargs.update({'json': body, 'headers': self._merge_dictionaries(headers, self._headers())}) try: - return self.session().post(url, json=body, headers=headers) + return self.session().post(url, data=file_contents, 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() - + def post_contents(self, url, file_contents=None, headers={}, **kwargs): try: - return self.session().post(url, data=file_contents, headers=headers) + return self.session().post(url, data=file_contents, headers=self._headers()) except getattr(requests.exceptions, 'RequestException') as inst: self.module.fail_json(msg=inst.message) diff --git a/plugins/modules/gcp_storage_object.py b/plugins/modules/gcp_storage_object.py index 756b201..ee110a8 100644 --- a/plugins/modules/gcp_storage_object.py +++ b/plugins/modules/gcp_storage_object.py @@ -18,14 +18,15 @@ # ---------------------------------------------------------------------------- from __future__ import absolute_import, division, print_function - __metaclass__ = type ################################################################################ # Documentation ################################################################################ -ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ["preview"], 'supported_by': 'community'} +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ["preview"], + 'supported_by': 'community'} DOCUMENTATION = ''' --- @@ -78,15 +79,15 @@ extends_documentation_fragment: gcp EXAMPLES = ''' - name: create a object gcp_storage_object: - name: ansible-storage-module - action: download - bucket: ansible-bucket - src: modules.zip - dest: "~/modules.zip" - project: test_project - auth_kind: serviceaccount - service_account_file: "/tmp/auth.pem" - state: present + name: ansible-storage-module + action: download + bucket: ansible-bucket + src: modules.zip + dest: "~/modules.zip" + project: "test_project" + auth_kind: "serviceaccount" + service_account_file: "/tmp/auth.pem" + state: present ''' RETURN = ''' @@ -144,7 +145,7 @@ def main(): overwrite=dict(type='bool'), src=dict(type='path'), dest=dict(type='path'), - bucket=dict(type='str'), + bucket=dict(type='str') ) ) @@ -276,7 +277,7 @@ def object_headers(module): return { "name": module.params['dest'], "Content-Type": mimetypes.guess_type(module.params['src'])[0], - "Content-Length": str(os.path.getsize(module.params['src'])), + "Content-Length": str(os.path.getsize(module.params['src'])) }