mirror of
https://github.com/ansible-collections/google.cloud.git
synced 2025-04-09 04:10:27 -07:00
black code reformat
This commit is contained in:
parent
23cb081de9
commit
72629e09e3
1 changed files with 66 additions and 41 deletions
|
@ -25,9 +25,13 @@ __metaclass__ = type
|
||||||
# Documentation
|
# Documentation
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ["preview"], 'supported_by': 'community'}
|
ANSIBLE_METADATA = {
|
||||||
|
"metadata_version": "1.1",
|
||||||
|
"status": ["preview"],
|
||||||
|
"supported_by": "community",
|
||||||
|
}
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = """
|
||||||
---
|
---
|
||||||
module: gcp_storage_object
|
module: gcp_storage_object
|
||||||
description:
|
description:
|
||||||
|
@ -103,9 +107,9 @@ options:
|
||||||
- This should not be set unless you know what you're doing.
|
- This should not be set unless you know what you're doing.
|
||||||
- This only alters the User Agent string for any API requests.
|
- This only alters the User Agent string for any API requests.
|
||||||
type: str
|
type: str
|
||||||
'''
|
"""
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = """
|
||||||
- name: create a object
|
- name: create a object
|
||||||
google.cloud.gcp_storage_object:
|
google.cloud.gcp_storage_object:
|
||||||
action: download
|
action: download
|
||||||
|
@ -116,9 +120,9 @@ EXAMPLES = '''
|
||||||
auth_kind: serviceaccount
|
auth_kind: serviceaccount
|
||||||
service_account_file: "/tmp/auth.pem"
|
service_account_file: "/tmp/auth.pem"
|
||||||
state: present
|
state: present
|
||||||
'''
|
"""
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = """
|
||||||
acl:
|
acl:
|
||||||
description:
|
description:
|
||||||
- The ACL on the object
|
- The ACL on the object
|
||||||
|
@ -160,13 +164,19 @@ bucket:
|
||||||
- The name of the bucket.
|
- The name of the bucket.
|
||||||
returned: success
|
returned: success
|
||||||
type: str
|
type: str
|
||||||
'''
|
"""
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# Imports
|
# Imports
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, replace_resource_dict
|
from ansible.module_utils.gcp_utils import (
|
||||||
|
navigate_hash,
|
||||||
|
GcpSession,
|
||||||
|
GcpModule,
|
||||||
|
GcpRequest,
|
||||||
|
replace_resource_dict,
|
||||||
|
)
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
@ -177,6 +187,7 @@ try:
|
||||||
from google.cloud import storage
|
from google.cloud import storage
|
||||||
from google.api_core.client_info import ClientInfo
|
from google.api_core.client_info import ClientInfo
|
||||||
from google.cloud.storage import Blob
|
from google.cloud.storage import Blob
|
||||||
|
|
||||||
HAS_GOOGLE_STORAGE_LIBRARY = True
|
HAS_GOOGLE_STORAGE_LIBRARY = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_GOOGLE_STORAGE_LIBRARY = False
|
HAS_GOOGLE_STORAGE_LIBRARY = False
|
||||||
|
@ -190,92 +201,106 @@ def main():
|
||||||
|
|
||||||
module = GcpModule(
|
module = GcpModule(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
action=dict(type='str', choices['download', 'upload', 'delete']),
|
action=dict(type="str", choices=["download", "upload", "delete"]),
|
||||||
src=dict(type='path'),
|
src=dict(type="path"),
|
||||||
dest=dict(type='path'),
|
dest=dict(type="path"),
|
||||||
bucket=dict(type='str'),
|
bucket=dict(type="str"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if not HAS_GOOGLE_STORAGE_LIBRARY:
|
if not HAS_GOOGLE_STORAGE_LIBRARY:
|
||||||
module.fail_json(msg="Please install the google-cloud-storage Python library")
|
module.fail_json(msg="Please install the google-cloud-storage Python library")
|
||||||
|
|
||||||
if not module.params['scopes']:
|
if not module.params["scopes"]:
|
||||||
module.params['scopes'] = ['https://www.googleapis.com/auth/devstorage.full_control']
|
module.params["scopes"] = [
|
||||||
|
"https://www.googleapis.com/auth/devstorage.full_control"
|
||||||
|
]
|
||||||
|
|
||||||
creds = GcpSession(module, 'storage')._credentials()
|
creds = GcpSession(module, "storage")._credentials()
|
||||||
client = storage.Client(credentials=creds,
|
client = storage.Client(
|
||||||
client_info=ClientInfo(user_agent="Google-Ansible-MM-object"))
|
credentials=creds, client_info=ClientInfo(user_agent="Google-Ansible-MM-object")
|
||||||
|
)
|
||||||
|
|
||||||
remote_file_exists = Blob(remote_file_path(module), module.params['bucket']).exists()
|
remote_file_exists = Blob(
|
||||||
|
remote_file_path(module), module.params["bucket"]
|
||||||
|
).exists()
|
||||||
local_file_exists = os.path.isfile(local_file_path(module))
|
local_file_exists = os.path.isfile(local_file_path(module))
|
||||||
|
|
||||||
# Check if files exist.
|
# Check if files exist.
|
||||||
results = {}
|
results = {}
|
||||||
if module.params['action'] == 'delete' and not remote_file_exists:
|
if module.params["action"] == "delete" and not remote_file_exists:
|
||||||
module.fail_json(msg="File does not exist in bucket")
|
module.fail_json(msg="File does not exist in bucket")
|
||||||
|
|
||||||
if module.params['action'] == 'download' and not remote_file_exists:
|
if module.params["action"] == "download" and not remote_file_exists:
|
||||||
module.fail_json(msg="File does not exist in bucket")
|
module.fail_json(msg="File does not exist in bucket")
|
||||||
|
|
||||||
if module.params['action'] == 'upload' and not local_file_exists:
|
if module.params["action"] == "upload" and not local_file_exists:
|
||||||
module.fail_json(msg="File does not exist on disk")
|
module.fail_json(msg="File does not exist on disk")
|
||||||
|
|
||||||
if module.params['action'] == 'delete':
|
if module.params["action"] == "delete":
|
||||||
if remote_file_exists:
|
if remote_file_exists:
|
||||||
results = delete_file(module, client, module.params['src'])
|
results = delete_file(module, client, module.params["src"])
|
||||||
results['changed'] = True
|
results["changed"] = True
|
||||||
module.params['changed'] = True
|
module.params["changed"] = True
|
||||||
|
|
||||||
elif module.params['action'] == 'download':
|
elif module.params["action"] == "download":
|
||||||
results = download_file(module, client, module.params['src'], module.params['dest'])
|
results = download_file(
|
||||||
results['changed'] = True
|
module, client, module.params["src"], module.params["dest"]
|
||||||
|
)
|
||||||
|
results["changed"] = True
|
||||||
|
|
||||||
# Upload
|
# Upload
|
||||||
else:
|
else:
|
||||||
results = upload_file(module, client, module.params['src'], module.params['dest'])
|
results = upload_file(
|
||||||
results['changed'] = True
|
module, client, module.params["src"], module.params["dest"]
|
||||||
|
)
|
||||||
|
results["changed"] = True
|
||||||
|
|
||||||
module.exit_json(**results)
|
module.exit_json(**results)
|
||||||
|
|
||||||
|
|
||||||
def download_file(module, client, name, dest):
|
def download_file(module, client, name, dest):
|
||||||
try:
|
try:
|
||||||
blob = Blob(name, module.params['bucket'])
|
blob = Blob(name, module.params["bucket"])
|
||||||
with open(dest, "wb") as file_obj:
|
with open(dest, "wb") as file_obj:
|
||||||
blob.download_to_file(file_obj)
|
blob.download_to_file(file_obj)
|
||||||
return blob.__dict__
|
return blob.__dict__
|
||||||
except google.cloud.exceptions.NotFound as e:
|
except google.cloud.exceptions.NotFound as e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
|
||||||
def upload_file(module, client, src, dest):
|
def upload_file(module, client, src, dest):
|
||||||
try:
|
try:
|
||||||
blob = Blob(dest, module.params['bucket'])
|
blob = Blob(dest, module.params["bucket"])
|
||||||
with open(src, "wb") as file_obj:
|
with open(src, "wb") as file_obj:
|
||||||
blob.upload_from_file(file_obj)
|
blob.upload_from_file(file_obj)
|
||||||
return blob.__dict__
|
return blob.__dict__
|
||||||
except GoogleCloudError as e:
|
except GoogleCloudError as e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
|
||||||
def delete_file(module, client, name):
|
def delete_file(module, client, name):
|
||||||
try:
|
try:
|
||||||
blob = Blob(name, module.params['bucket'])
|
blob = Blob(name, module.params["bucket"])
|
||||||
blob.delete()
|
blob.delete()
|
||||||
return {}
|
return {}
|
||||||
except google.cloud.exceptions.NotFound as e:
|
except google.cloud.exceptions.NotFound as e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
|
||||||
def local_file_path(module):
|
def local_file_path(module):
|
||||||
if module.params['action'] == 'download':
|
if module.params["action"] == "download":
|
||||||
return module.params['dest']
|
return module.params["dest"]
|
||||||
else:
|
else:
|
||||||
return module.params['src']
|
return module.params["src"]
|
||||||
|
|
||||||
|
|
||||||
def remote_file_path(module):
|
def remote_file_path(module):
|
||||||
if module.params['action'] == 'download':
|
if module.params["action"] == "download":
|
||||||
return module.params['src']
|
return module.params["src"]
|
||||||
else:
|
else:
|
||||||
return module.params['dest']
|
return module.params["dest"]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue