def upload_file(module): auth = GcpSession(module, 'storage') with open(module.params['src'], 'r') as f: results = return_if_object( module, auth.post_contents(upload_link(module), f, object_headers(module))) results['changed'] = True return results
def security_policy_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/global/backendServices/{name}/setSecurityPolicy" ]).format(**module.params), {u'securityPolicy': module.params.get('security_policy')}, )
def ip_cidr_range_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/regions/{region}/subnetworks/{name}/expandIpCidrRange" ]).format(**module.params), {u'ipCidrRange': module.params.get('ip_cidr_range')}, )
def quic_override_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/global/targetHttpsProxies/{name}/setQuicOverride" ]).format(**module.params), {u'quicOverride': module.params.get('quic_override')}, )
def target_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/global/forwardingRules/{name}/setTarget" ]).format(**module.params), {u'target': module.params.get('target')}, )
def update(module, link, fetch): auth = GcpSession(module, 'filestore') params = { 'updateMask': updateMask(resource_to_request(module), response_to_hash(module, fetch)) } request = resource_to_request(module) return wait_for_operation(module, auth.patch(link, request, params=params))
def size_gb_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/regions/{region}/disks/{name}/resize" ]).format(**module.params), {u'sizeGb': module.params.get('size_gb')}, )
def update(module, link, fetch): auth = GcpSession(module, 'kms') params = { 'updateMask': updateMask(resource_to_request(module), response_to_hash(module, fetch)) } request = resource_to_request(module) return return_if_object(module, auth.patch(link, request, params=params))
def tensorflow_version_update(module, request, response): auth = GcpSession(module, 'tpu') auth.post( ''.join([ "https://tpu.googleapis.com/v1/", "projects/{project}/locations/{zone}/nodes/{name}:reimage" ]).format(**module.params), {u'tensorflowVersion': module.params.get('tensorflow_version')}, )
def proxy_header_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/global/targetTcpProxies/{name}/setProxyHeader" ]).format(**module.params), {u'proxyHeader': module.params.get('proxy_header')}, )
def update(module, link, fetch): auth = GcpSession(module, 'cloudfunctions') params = { 'updateMask': updateMask(resource_to_request(module), response_to_hash(module, fetch)) } request = resource_to_request(module) del request['name'] return wait_for_operation(module, auth.put(link, request, params=params))
def set_default(module): res = { 'project': module.params['project'], 'model': replace_resource_dict(module.params['model'], 'name'), 'name': module.params['name'] } link = "https://ml.googleapis.com/v1/projects/{project}/models/{model}/versions/{name}:setDefault".format( **res) auth = GcpSession(module, 'mlengine') return_if_object(module, auth.post(link))
def list_instances(self): auth = GcpSession(self.module, 'compute') response = return_if_object( self.module, auth.post(self._list_instances_url(), {'instanceState': 'ALL'}), 'compute#instanceGroupsListInstances') # Transform instance list into a list of selfLinks for diffing with module parameters instances = [] for instance in response.get('items', []): instances.append(instance['instance']) return instances
def labels_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/global/images/{name}/setLabels" ]).format(**module.params), { u'labels': module.params.get('labels'), u'labelFingerprint': response.get('labelFingerprint') }, )
def private_ip_google_access_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/regions/{region}/subnetworks/{name}/setPrivateIpGoogleAccess" ]).format(**module.params), { u'privateIpGoogleAccess': module.params.get('private_ip_google_access') }, )
def kms_encrypt(self, module): payload = {"plaintext": module.params['plaintext']} if module.params['additional_authenticated_data']: payload['additionalAuthenticatedData'] = module.params[ 'additional_authenticated_data'] auth = GcpSession(module, 'cloudkms') url = "https://cloudkms.googleapis.com/v1/projects/{projects}/locations/{locations}/" \ "keyRings/{key_ring}/cryptoKeys/{crypto_key}:encrypt".format(**module.params) response = auth.post(url, body=payload) return response.json()['ciphertext']
def ssl_certificates_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/targetHttpsProxies/{name}/setSslCertificates" ]).format(**module.params), { u'sslCertificates': replace_resource_dict(module.params.get('ssl_certificates', []), 'selfLink') }, )
def ssl_policy_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/global/targetHttpsProxies/{name}/setSslPolicy" ]).format(**module.params), { u'sslPolicy': replace_resource_dict(module.params.get(u'ssl_policy', {}), 'selfLink') }, )
def url_map_update(module, request, response): auth = GcpSession(module, 'compute') auth.post( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/targetHttpsProxies/{name}/setUrlMap" ]).format(**module.params), { u'urlMap': replace_resource_dict(module.params.get(u'url_map', {}), 'selfLink') }, )
def secondary_ip_ranges_update(module, request, response): auth = GcpSession(module, 'compute') auth.patch( ''.join([ "https://www.googleapis.com/compute/v1/", "projects/{project}/regions/{region}/subnetworks/{name}" ]).format(**module.params), { u'secondaryIpRanges': SubnetworkSecondaryiprangesArray( module.params.get('secondary_ip_ranges', []), module).to_request() }, )
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 description_update(module, request, response): auth = GcpSession(module, 'dns') auth.patch( ''.join([ "https://www.googleapis.com/dns/v1/", "projects/{project}/managedZones/{name}" ]).format(**module.params), { u'description': module.params.get('description'), u'labels': module.params.get('labels'), u'privateVisibilityConfig': ManagedZonePrivatevisibilityconfig( module.params.get('private_visibility_config', {}), module).to_request(), }, )
def delete(module, link, kind): auth = GcpSession(module, 'compute') return wait_for_operation(module, auth.delete(link))
def fetch_resource(module, link, kind, allow_not_found=True): auth = GcpSession(module, 'compute') return return_if_object(module, auth.get(link), kind, allow_not_found)
def delete(module, link): auth = GcpSession(module, 'cloudfunctions') return wait_for_operation(module, auth.delete(link))
def create(module, link, kind): auth = GcpSession(module, 'compute') return wait_for_operation(module, auth.post(link, resource_to_request(module)))
def fetch_list(module, link): auth = GcpSession(module, 'iam') return auth.list(link, return_if_object, array_name='roles')
def fetch_resource(module, link, allow_not_found=True): auth = GcpSession(module, 'cloudfunctions') return return_if_object(module, auth.get(link), allow_not_found)
def fetch_list(module, link, query): auth = GcpSession(module, 'compute') return auth.list(link, return_if_object, array_name='items', params={'filter': query})
def create(module, link): auth = GcpSession(module, 'cloudfunctions') return wait_for_operation(module, auth.post(link, resource_to_request(module)))