def main(): """Main function""" module = GcpModule(argument_spec=dict( state=dict( default='present', choices=['present', 'absent'], type='str'), name=dict(required=True, type='str'), type=dict(required=True, type='str'), ttl=dict(type='int'), target=dict(type='list', elements='str'), managed_zone=dict(required=True, type='dict'), )) if not module.params['scopes']: module.params['scopes'] = [ 'https://www.googleapis.com/auth/ndev.clouddns.readwrite' ] state = module.params['state'] kind = 'dns#resourceRecordSet' fetch = fetch_wrapped_resource(module, 'dns#resourceRecordSet', 'dns#resourceRecordSetsListResponse', 'rrsets') changed = False if 'dnsName' not in module.params.get( 'managed_zone') or 'name' not in module.params.get('managed_zone'): module.fail_json( msg= "managed_zone dictionary must contain both the name of the zone and the dns name of the zone" ) if fetch: if state == 'present': if is_different(module, fetch): update(module, self_link(module), kind, fetch) fetch = fetch_resource(module, self_link(module), kind) changed = True else: delete(module, self_link(module), kind, fetch) fetch = {} changed = True else: if state == 'present': fetch = create(module, collection(module), kind) changed = True else: fetch = {} fetch.update({'changed': changed}) module.exit_json(**fetch)
def main(): """Main function""" module = GcpModule(argument_spec=dict( state=dict( default='present', choices=['present', 'absent'], type='str'), name=dict(required=True, type='str'), disable_dependent_services=dict(type='bool'), )) if not module.params['scopes']: module.params['scopes'] = [ 'https://www.googleapis.com/auth/cloud-platform' ] state = module.params['state'] fetch = fetch_resource(module, self_link(module)) changed = False if module.params['state'] == 'present' and module.params[ 'disable_dependent_services']: module.fail_json( msg= "You cannot enable a service and use the disable_dependent_service option" ) if fetch and fetch.get('state') == 'DISABLED': fetch = {} if fetch: if state == 'present': if is_different(module, fetch): update(module, self_link(module)) fetch = fetch_resource(module, self_link(module)) changed = True else: delete(module, delete_link(module)) fetch = {} changed = True else: if state == 'present': fetch = create(module, create_link(module)) changed = True else: fetch = {} fetch.update({'changed': changed}) module.exit_json(**fetch)
def main(): """Main function""" module = GcpModule(argument_spec=dict( state=dict( default='present', choices=['present', 'absent'], type='str'), action=dict(type='str'), overwrite=dict(type='bool'), src=dict(type='path'), dest=dict(type='path'), bucket=dict(type='str'), )) if not module.params['scopes']: module.params['scopes'] = [ 'https://www.googleapis.com/auth/devstorage.full_control' ] remote_object = fetch_resource(module, self_link(module)) local_file_exists = os.path.isfile(local_file_path(module)) # Check if files exist. if module.params['action'] == 'download' and not remote_object: module.fail_json(msg="File does not exist in bucket") if module.params['action'] == 'upload' and not local_file_exists: module.fail_json(msg="File does not exist on disk") # Check if we'll be overwriting files. if not module.params['overwrite']: remote_object['changed'] = False if module.params['action'] == 'download' and local_file_exists: # If files differ, throw an error if get_md5_local( local_file_path(module)) != remote_object['md5Hash']: module.fail_json( msg="Local file is different than remote file") # If files are the same, module is done running. else: module.exit_json(**remote_object) elif module.params['action'] == 'upload' and remote_object: # If files differ, throw an error if get_md5_local( local_file_path(module)) != remote_object['md5Hash']: module.fail_json( msg="Local file is different than remote file") # If files are the same, module is done running. else: module.exit_json(**remote_object) # Upload/download the files auth = GcpSession(module, 'storage') if module.params['action'] == 'download': results = download_file(module) else: results = upload_file(module) module.exit_json(**results)
def main(): """Main function""" module = GcpModule(argument_spec=dict( action=dict(type="str", choices=["download", "upload", "delete"]), src=dict(type="path"), dest=dict(type="path"), bucket=dict(type="str"), )) if not HAS_GOOGLE_STORAGE_LIBRARY: module.fail_json( msg="Please install the google-cloud-storage Python library") if not module.params["scopes"]: module.params["scopes"] = [ "https://www.googleapis.com/auth/devstorage.full_control" ] creds = GcpSession(module, "storage")._credentials() client = storage.Client( project=module.params['project'], credentials=creds, client_info=ClientInfo(user_agent="Google-Ansible-MM-object")) bucket = client.get_bucket(module.params['bucket']) remote_file_exists = Blob(remote_file_path(module), bucket).exists() local_file_exists = os.path.isfile(local_file_path(module)) # Check if files exist. results = {} if module.params["action"] == "delete" and not remote_file_exists: module.fail_json(msg="File does not exist in bucket") if module.params["action"] == "download" and not remote_file_exists: module.fail_json(msg="File does not exist in bucket") if module.params["action"] == "upload" and not local_file_exists: module.fail_json(msg="File does not exist on disk") if module.params["action"] == "delete": if remote_file_exists: results = delete_file(module, client, module.params["src"]) results["changed"] = True module.params["changed"] = True elif module.params["action"] == "download": results = download_file(module, client, module.params["src"], module.params["dest"]) results["changed"] = True # Upload else: results = upload_file(module, client, module.params["src"], module.params["dest"]) results["changed"] = True module.exit_json(**results)
def main(): module = GcpModule(argument_spec=dict( path=dict(type='path'), bucket=dict(type='str'), )) if not module.params['scopes']: module.params['scopes'] = [ 'https://www.googleapis.com/auth/devstorage.full_control' ] session = GcpSession(module, 'storage') remote_object = session.get( "https://www.googleapis.com/storage/v1/b/{bucket}/o/{path}".format( **module.params)) if remote_object.status_code == 404 or remote_object.status_code == 204: module.fail_json(msg="File not found") data = session.get(remote_object.json()['mediaLink']) module.exit_json(changed=False, value=data.content)