From 52aa522c19724b5b0fedc02d139d3a32d2480ab2 Mon Sep 17 00:00:00 2001 From: Satyajit Bulage Date: Tue, 31 Oct 2017 15:23:31 +0530 Subject: [PATCH] google: PEP8 compliancy fixes (#32309) This PR includes: - PEP8 compliancy fixes - Documentation fixes Signed-off-by: Satyajit Bulage --- lib/ansible/modules/cloud/google/gce.py | 74 +++++++++---------- lib/ansible/modules/cloud/google/gce_eip.py | 3 + lib/ansible/modules/cloud/google/gce_lb.py | 80 ++++++++++----------- lib/ansible/modules/cloud/google/gce_mig.py | 8 +-- lib/ansible/modules/cloud/google/gce_net.py | 50 ++++++------- lib/ansible/modules/cloud/google/gce_pd.py | 45 ++++++------ test/sanity/pep8/legacy-files.txt | 6 -- 7 files changed, 129 insertions(+), 137 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gce.py b/lib/ansible/modules/cloud/google/gce.py index 9aab4cf6ca..36714de861 100644 --- a/lib/ansible/modules/cloud/google/gce.py +++ b/lib/ansible/modules/cloud/google/gce.py @@ -3,14 +3,13 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function -__metaclass__ = type +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} - DOCUMENTATION = ''' --- module: gce @@ -325,6 +324,7 @@ import logging try: from ast import literal_eval + HAS_PYTHON26 = True except ImportError: HAS_PYTHON26 = False @@ -336,6 +336,7 @@ try: from libcloud.common.google import GoogleBaseError, QuotaExceededError, \ ResourceExistsError, ResourceInUseError, ResourceNotFoundError from libcloud.compute.drivers.gce import GCEAddress + _ = Provider.GCE HAS_LIBCLOUD = True except ImportError: @@ -378,7 +379,7 @@ def get_instance_info(inst): else: public_ip = inst.public_ips[0] - return({ + return ({ 'image': inst.image is not None and inst.image.split('/')[-1] or None, 'disks': disk_names, 'machine_type': inst.size, @@ -553,7 +554,7 @@ def create_instances(module, gce, instance_names, number, lc_zone): changed = True except GoogleBaseError as e: module.fail_json(msg='Unexpected error attempting to create ' + - 'instance %s, error: %s' % (instance, e.value)) + 'instance %s, error: %s' % (instance, e.value)) if inst: new_instances.append(inst) @@ -576,7 +577,7 @@ def create_instances(module, gce, instance_names, number, lc_zone): # Work around libcloud bug: attached volumes don't get added # to the instance metadata. get_instance_info() only cares about # source and index. - if len(inst.extra['disks']) != i+1: + if len(inst.extra['disks']) != i + 1: inst.extra['disks'].append( {'source': lc_disk.extra['selfLink'], 'index': i}) @@ -589,6 +590,7 @@ def create_instances(module, gce, instance_names, number, lc_zone): return (changed, instance_json_data, instance_names) + def change_instance_state(module, gce, instance_names, number, zone, state): """Changes the state of a list of instances. For example, change from started to stopped, or started to absent. @@ -633,47 +635,46 @@ def change_instance_state(module, gce, instance_names, number, zone, state): if state in ['absent', 'deleted']: gce.destroy_node(node) changed = True - elif state == 'started' and \ - node.state == libcloud.compute.types.NodeState.STOPPED: + elif state == 'started' and node.state == libcloud.compute.types.NodeState.STOPPED: gce.ex_start_node(node) changed = True - elif state in ['stopped', 'terminated'] and \ - node.state == libcloud.compute.types.NodeState.RUNNING: + elif state in ['stopped', 'terminated'] and node.state == libcloud.compute.types.NodeState.RUNNING: gce.ex_stop_node(node) changed = True return (changed, state_instance_names) + def main(): module = AnsibleModule( - argument_spec = dict( - image = dict(default='debian-8'), - image_family = dict(), - external_projects = dict(type='list'), - instance_names = dict(), - machine_type = dict(default='n1-standard-1'), - metadata = dict(), - name = dict(aliases=['base_name']), - num_instances = dict(type='int'), - network = dict(default='default'), - subnetwork = dict(), - persistent_boot_disk = dict(type='bool', default=False), - disks = dict(type='list'), - state = dict(choices=['active', 'present', 'absent', 'deleted', - 'started', 'stopped', 'terminated'], - default='present'), - tags = dict(type='list'), - zone = dict(default='us-central1-a'), - service_account_email = dict(), - service_account_permissions = dict(type='list'), - pem_file = dict(type='path'), - credentials_file = dict(type='path'), - project_id = dict(), - ip_forward = dict(type='bool', default=False), + argument_spec=dict( + image=dict(default='debian-8'), + image_family=dict(), + external_projects=dict(type='list'), + instance_names=dict(), + machine_type=dict(default='n1-standard-1'), + metadata=dict(), + name=dict(aliases=['base_name']), + num_instances=dict(type='int'), + network=dict(default='default'), + subnetwork=dict(), + persistent_boot_disk=dict(type='bool', default=False), + disks=dict(type='list'), + state=dict(choices=['active', 'present', 'absent', 'deleted', + 'started', 'stopped', 'terminated'], + default='present'), + tags=dict(type='list'), + zone=dict(default='us-central1-a'), + service_account_email=dict(), + service_account_permissions=dict(type='list'), + pem_file=dict(type='path'), + credentials_file=dict(type='path'), + project_id=dict(), + ip_forward=dict(type='bool', default=False), external_ip=dict(default='ephemeral'), - disk_auto_delete = dict(type='bool', default=True), - disk_size = dict(type='int', default=10), - preemptible = dict(type='bool', default=None), + disk_auto_delete=dict(type='bool', default=True), + disk_size=dict(type='int', default=10), + preemptible=dict(type='bool', default=None), ), mutually_exclusive=[('instance_names', 'name')] ) @@ -751,6 +752,7 @@ class LazyDiskImage: Object for lazy instantiation of disk image gce.ex_get_image is a very expensive call, so we want to avoid calling it as much as possible. """ + def __init__(self, module, gce, name, has_pd, family=None, projects=None): self.image = None self.was_called = False diff --git a/lib/ansible/modules/cloud/google/gce_eip.py b/lib/ansible/modules/cloud/google/gce_eip.py index ede640f3f5..20f246a4b8 100644 --- a/lib/ansible/modules/cloud/google/gce_eip.py +++ b/lib/ansible/modules/cloud/google/gce_eip.py @@ -122,6 +122,7 @@ def get_address(gce, name, region): except ResourceNotFoundError: return None + def create_address(gce, params): """ Create a new Address. @@ -147,6 +148,7 @@ def create_address(gce, params): return (changed, return_data) + def delete_address(address): """ Delete an Address. @@ -167,6 +169,7 @@ def delete_address(address): return_data = address.address return (changed, return_data) + def main(): module = AnsibleModule(argument_spec=dict( name=dict(required=True), diff --git a/lib/ansible/modules/cloud/google/gce_lb.py b/lib/ansible/modules/cloud/google/gce_lb.py index 5709e1efd5..0a4b8f6785 100644 --- a/lib/ansible/modules/cloud/google/gce_lb.py +++ b/lib/ansible/modules/cloud/google/gce_lb.py @@ -157,8 +157,8 @@ try: from libcloud.compute.providers import get_driver from libcloud.loadbalancer.types import Provider as Provider_lb from libcloud.loadbalancer.providers import get_driver as get_driver_lb - from libcloud.common.google import GoogleBaseError, QuotaExceededError, \ - ResourceExistsError, ResourceNotFoundError + from libcloud.common.google import GoogleBaseError, QuotaExceededError, ResourceExistsError, ResourceNotFoundError + _ = Provider.GCE HAS_LIBCLOUD = True except ImportError: @@ -170,26 +170,26 @@ from ansible.module_utils.gce import USER_AGENT_PRODUCT, USER_AGENT_VERSION, gce def main(): module = AnsibleModule( - argument_spec = dict( - httphealthcheck_name = dict(), - httphealthcheck_port = dict(default=80), - httphealthcheck_path = dict(default='/'), - httphealthcheck_interval = dict(default=5), - httphealthcheck_timeout = dict(default=5), - httphealthcheck_unhealthy_count = dict(default=2), - httphealthcheck_healthy_count = dict(default=2), - httphealthcheck_host = dict(), - name = dict(), - protocol = dict(default='tcp'), - region = dict(), - external_ip = dict(), - port_range = dict(), - members = dict(type='list'), - state = dict(default='present'), - service_account_email = dict(), - pem_file = dict(type='path'), - credentials_file = dict(type='path'), - project_id = dict(), + argument_spec=dict( + httphealthcheck_name=dict(), + httphealthcheck_port=dict(default=80), + httphealthcheck_path=dict(default='/'), + httphealthcheck_interval=dict(default=5), + httphealthcheck_timeout=dict(default=5), + httphealthcheck_unhealthy_count=dict(default=2), + httphealthcheck_healthy_count=dict(default=2), + httphealthcheck_host=dict(), + name=dict(), + protocol=dict(default='tcp'), + region=dict(), + external_ip=dict(), + port_range=dict(), + members=dict(type='list'), + state=dict(default='present'), + service_account_email=dict(), + pem_file=dict(type='path'), + credentials_file=dict(type='path'), + project_id=dict(), ) ) @@ -203,10 +203,8 @@ def main(): httphealthcheck_path = module.params.get('httphealthcheck_path') httphealthcheck_interval = module.params.get('httphealthcheck_interval') httphealthcheck_timeout = module.params.get('httphealthcheck_timeout') - httphealthcheck_unhealthy_count = \ - module.params.get('httphealthcheck_unhealthy_count') - httphealthcheck_healthy_count = \ - module.params.get('httphealthcheck_healthy_count') + httphealthcheck_unhealthy_count = module.params.get('httphealthcheck_unhealthy_count') + httphealthcheck_healthy_count = module.params.get('httphealthcheck_healthy_count') httphealthcheck_host = module.params.get('httphealthcheck_host') name = module.params.get('name') protocol = module.params.get('protocol') @@ -227,8 +225,7 @@ def main(): json_output = {'name': name, 'state': state} if not name and not httphealthcheck_name: - module.fail_json(msg='Nothing to do, please specify a "name" ' + \ - 'or "httphealthcheck_name" parameter', changed=False) + module.fail_json(msg='Nothing to do, please specify a "name" ' + 'or "httphealthcheck_name" parameter', changed=False) if state in ['active', 'present']: # first, create the httphealthcheck if requested @@ -237,12 +234,12 @@ def main(): json_output['httphealthcheck_name'] = httphealthcheck_name try: hc = gcelb.ex_create_healthcheck(httphealthcheck_name, - host=httphealthcheck_host, path=httphealthcheck_path, - port=httphealthcheck_port, - interval=httphealthcheck_interval, - timeout=httphealthcheck_timeout, - unhealthy_threshold=httphealthcheck_unhealthy_count, - healthy_threshold=httphealthcheck_healthy_count) + host=httphealthcheck_host, path=httphealthcheck_path, + port=httphealthcheck_port, + interval=httphealthcheck_interval, + timeout=httphealthcheck_timeout, + unhealthy_threshold=httphealthcheck_unhealthy_count, + healthy_threshold=httphealthcheck_healthy_count) changed = True except ResourceExistsError: hc = gce.ex_get_healthcheck(httphealthcheck_name) @@ -255,17 +252,15 @@ def main(): json_output['httphealthcheck_port'] = hc.port json_output['httphealthcheck_interval'] = hc.interval json_output['httphealthcheck_timeout'] = hc.timeout - json_output['httphealthcheck_unhealthy_count'] = \ - hc.unhealthy_threshold - json_output['httphealthcheck_healthy_count'] = \ - hc.healthy_threshold + json_output['httphealthcheck_unhealthy_count'] = hc.unhealthy_threshold + json_output['httphealthcheck_healthy_count'] = hc.healthy_threshold # create the forwarding rule (and target pool under the hood) lb = None if name: if not region: module.fail_json(msg='Missing required region name', - changed=False) + changed=False) nodes = [] output_nodes = [] json_output['name'] = name @@ -282,11 +277,11 @@ def main(): try: if hc is not None: lb = gcelb.create_balancer(name, port_range, protocol, - None, nodes, ex_region=region, ex_healthchecks=[hc], - ex_address=external_ip) + None, nodes, ex_region=region, ex_healthchecks=[hc], + ex_address=external_ip) else: lb = gcelb.create_balancer(name, port_range, protocol, - None, nodes, ex_region=region, ex_address=external_ip) + None, nodes, ex_region=region, ex_address=external_ip) changed = True except ResourceExistsError: lb = gcelb.get_balancer(name) @@ -331,7 +326,6 @@ def main(): except Exception as e: module.fail_json(msg=unexpected_error_msg(e), changed=False) - json_output['changed'] = changed module.exit_json(**json_output) diff --git a/lib/ansible/modules/cloud/google/gce_mig.py b/lib/ansible/modules/cloud/google/gce_mig.py index 1040fae636..ca873d8aa7 100644 --- a/lib/ansible/modules/cloud/google/gce_mig.py +++ b/lib/ansible/modules/cloud/google/gce_mig.py @@ -339,7 +339,7 @@ def _validate_autoscaling_params(params): {'name': 'name', 'required': True, 'type': str}, {'name': 'enabled', 'required': True, 'type': bool}, {'name': 'policy', 'required': True, 'type': dict} - ] # yapf: disable + ] # yapf: disable (as_req_valid, as_req_msg) = _check_params(params['autoscaling'], as_req_fields) @@ -351,7 +351,7 @@ def _validate_autoscaling_params(params): {'name': 'max_instances', 'required': True, 'type': int}, {'name': 'min_instances', 'required': False, 'type': int}, {'name': 'cool_down_period', 'required': False, 'type': int} - ] # yapf: disable + ] # yapf: disable (as_policy_valid, as_policy_msg) = _check_params( params['autoscaling']['policy'], as_policy_fields) @@ -385,7 +385,7 @@ def _validate_named_port_params(params): req_fields = [ {'name': 'name', 'required': True, 'type': str}, {'name': 'port', 'required': True, 'type': int} - ] # yapf: disable + ] # yapf: disable for np in params['named_ports']: (valid_named_ports, np_msg) = _check_params(np, req_fields) @@ -772,7 +772,7 @@ def main(): req_create_fields = [ {'name': 'template', 'required': True, 'type': str}, {'name': 'size', 'required': True, 'type': int} - ] # yapf: disable + ] # yapf: disable (valid_create_fields, valid_create_msg) = _check_params( params, req_create_fields) diff --git a/lib/ansible/modules/cloud/google/gce_net.py b/lib/ansible/modules/cloud/google/gce_net.py index 7717777186..d4c298b214 100644 --- a/lib/ansible/modules/cloud/google/gce_net.py +++ b/lib/ansible/modules/cloud/google/gce_net.py @@ -264,8 +264,7 @@ target_tags: try: from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver - from libcloud.common.google import GoogleBaseError, QuotaExceededError, \ - ResourceExistsError, ResourceNotFoundError + from libcloud.common.google import GoogleBaseError, QuotaExceededError, ResourceExistsError, ResourceNotFoundError _ = Provider.GCE HAS_LIBCLOUD = True except ImportError: @@ -293,6 +292,7 @@ def format_allowed_section(allowed): return_val["ports"] = ports return return_val + def format_allowed(allowed): """Format the 'allowed' value so that it is GCE compatible.""" return_value = [] @@ -304,33 +304,34 @@ def format_allowed(allowed): return_value.append(format_allowed_section(section)) return return_value + def sorted_allowed_list(allowed_list): """Sort allowed_list (output of format_allowed) by protocol and port.""" # sort by protocol - allowed_by_protocol = sorted(allowed_list,key=lambda x: x['IPProtocol']) + allowed_by_protocol = sorted(allowed_list, key=lambda x: x['IPProtocol']) # sort the ports list return sorted(allowed_by_protocol, key=lambda y: y.get('ports', []).sort()) def main(): module = AnsibleModule( - argument_spec = dict( - allowed = dict(), - ipv4_range = dict(), - fwname = dict(), - name = dict(), - src_range = dict(default=[], type='list'), - src_tags = dict(default=[], type='list'), - target_tags = dict(default=[], type='list'), - state = dict(default='present'), - service_account_email = dict(), - pem_file = dict(type='path'), - credentials_file = dict(type='path'), - project_id = dict(), - mode = dict(default='legacy', choices=['legacy', 'auto', 'custom']), - subnet_name = dict(), - subnet_region = dict(), - subnet_desc = dict(), + argument_spec=dict( + allowed=dict(), + ipv4_range=dict(), + fwname=dict(), + name=dict(), + src_range=dict(default=[], type='list'), + src_tags=dict(default=[], type='list'), + target_tags=dict(default=[], type='list'), + state=dict(default='present'), + service_account_email=dict(), + pem_file=dict(type='path'), + credentials_file=dict(type='path'), + project_id=dict(), + mode=dict(default='legacy', choices=['legacy', 'auto', 'custom']), + subnet_name=dict(), + subnet_region=dict(), + subnet_desc=dict(), ) ) @@ -379,8 +380,8 @@ def main(): if name and not network: if not ipv4_range and mode != 'auto': module.fail_json(msg="Network '" + name + "' is not found. To create network in legacy or custom mode, 'ipv4_range' parameter is required", - changed=False) - args = [ipv4_range if mode =='legacy' else None] + changed=False) + args = [ipv4_range if mode == 'legacy' else None] kwargs = {} if mode != 'legacy': kwargs['mode'] = mode @@ -414,8 +415,7 @@ def main(): if not allowed and not src_range and not src_tags: if changed and network: module.fail_json( - msg="Network created, but missing required " + \ - "firewall rule parameter(s)", changed=True) + msg="Network created, but missing required " + "firewall rule parameter(s)", changed=True) module.fail_json( msg="Missing required firewall rule parameter(s)", changed=False) @@ -484,7 +484,7 @@ def main(): except ResourceNotFoundError: try: gce.ex_create_firewall(fwname, allowed_list, network=name, - source_ranges=src_range, source_tags=src_tags, target_tags=target_tags) + source_ranges=src_range, source_tags=src_tags, target_tags=target_tags) changed = True except Exception as e: diff --git a/lib/ansible/modules/cloud/google/gce_pd.py b/lib/ansible/modules/cloud/google/gce_pd.py index f69d447c1c..a47eef889f 100644 --- a/lib/ansible/modules/cloud/google/gce_pd.py +++ b/lib/ansible/modules/cloud/google/gce_pd.py @@ -144,8 +144,7 @@ EXAMPLES = ''' try: from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver - from libcloud.common.google import GoogleBaseError, QuotaExceededError, \ - ResourceExistsError, ResourceNotFoundError, ResourceInUseError + from libcloud.common.google import GoogleBaseError, QuotaExceededError, ResourceExistsError, ResourceNotFoundError, ResourceInUseError _ = Provider.GCE HAS_LIBCLOUD = True except ImportError: @@ -157,24 +156,24 @@ from ansible.module_utils.gce import gce_connect, unexpected_error_msg def main(): module = AnsibleModule( - argument_spec = dict( - delete_on_termination = dict(type='bool'), - detach_only = dict(type='bool'), - instance_name = dict(), - mode = dict(default='READ_ONLY', choices=['READ_WRITE', 'READ_ONLY']), - name = dict(required=True), - size_gb = dict(default=10), - disk_type = dict(default='pd-standard'), - image = dict(), - image_family = dict(), - external_projects = dict(type='list'), - snapshot = dict(), - state = dict(default='present'), - zone = dict(default='us-central1-b'), - service_account_email = dict(), - pem_file = dict(type='path'), - credentials_file = dict(type='path'), - project_id = dict(), + argument_spec=dict( + delete_on_termination=dict(type='bool'), + detach_only=dict(type='bool'), + instance_name=dict(), + mode=dict(default='READ_ONLY', choices=['READ_WRITE', 'READ_ONLY']), + name=dict(required=True), + size_gb=dict(default=10), + disk_type=dict(default='pd-standard'), + image=dict(), + image_family=dict(), + external_projects=dict(type='list'), + snapshot=dict(), + state=dict(default='present'), + zone=dict(default='us-central1-b'), + service_account_email=dict(), + pem_file=dict(type='path'), + credentials_file=dict(type='path'), + project_id=dict(), ) ) if not HAS_LIBCLOUD: @@ -209,7 +208,7 @@ def main(): disk = inst = None changed = is_attached = False - json_output = { 'name': name, 'zone': zone, 'state': state, 'disk_type': disk_type } + json_output = {'name': name, 'zone': zone, 'state': state, 'disk_type': disk_type} if detach_only: json_output['detach_only'] = True json_output['detached_from_instance'] = instance_name @@ -248,7 +247,7 @@ def main(): raise Exception except: module.fail_json(msg="Must supply a size_gb larger than 1 GB", - changed=False) + changed=False) if instance_name and inst is None: module.fail_json(msg='Instance %s does not exist in zone %s' % ( @@ -275,7 +274,7 @@ def main(): pass except QuotaExceededError: module.fail_json(msg='Requested disk size exceeds quota', - changed=False) + changed=False) except Exception as e: module.fail_json(msg=unexpected_error_msg(e), changed=False) json_output['size_gb'] = size_gb diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index ed5e0ae8b0..d6d7276010 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -69,12 +69,6 @@ lib/ansible/modules/cloud/docker/docker_network.py lib/ansible/modules/cloud/google/gc_storage.py lib/ansible/modules/cloud/google/gcdns_record.py lib/ansible/modules/cloud/google/gcdns_zone.py -lib/ansible/modules/cloud/google/gce.py -lib/ansible/modules/cloud/google/gce_eip.py -lib/ansible/modules/cloud/google/gce_lb.py -lib/ansible/modules/cloud/google/gce_mig.py -lib/ansible/modules/cloud/google/gce_net.py -lib/ansible/modules/cloud/google/gce_pd.py lib/ansible/modules/cloud/google/gce_tag.py lib/ansible/modules/cloud/google/gcpubsub.py lib/ansible/modules/cloud/google/gcspanner.py