Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google: PEP8 compliancy fixes #32309

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 38 additions & 36 deletions lib/ansible/modules/cloud/google/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -325,6 +324,7 @@

try:
from ast import literal_eval

HAS_PYTHON26 = True
except ImportError:
HAS_PYTHON26 = False
Expand All @@ -336,6 +336,7 @@
from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
ResourceExistsError, ResourceInUseError, ResourceNotFoundError
from libcloud.compute.drivers.gce import GCEAddress

_ = Provider.GCE
HAS_LIBCLOUD = True
except ImportError:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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})

Expand All @@ -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.
Expand Down Expand Up @@ -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')]
)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/ansible/modules/cloud/google/gce_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def get_address(gce, name, region):
except ResourceNotFoundError:
return None


def create_address(gce, params):
"""
Create a new Address.
Expand All @@ -147,6 +148,7 @@ def create_address(gce, params):

return (changed, return_data)


def delete_address(address):
"""
Delete an Address.
Expand All @@ -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),
Expand Down
80 changes: 37 additions & 43 deletions lib/ansible/modules/cloud/google/gce_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@
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:
Expand All @@ -170,26 +170,26 @@

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(),
)
)

Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions lib/ansible/modules/cloud/google/gce_mig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down