From 585ecc2867af03ea90ee5fd9f975cf89723573e1 Mon Sep 17 00:00:00 2001 From: Nikhil Kathole Date: Tue, 7 Nov 2017 18:49:17 +0530 Subject: [PATCH] gce: PEP8 compliancy Fixes (#32311) * gce: PEP8 compliancy Fixes * Documentation updates * Documentation updates * Documentation update --- lib/ansible/modules/cloud/google/gce_tag.py | 96 ++++----- lib/ansible/modules/cloud/google/gcpubsub.py | 202 +++++++++--------- lib/ansible/modules/cloud/google/gcspanner.py | 110 +++++----- test/sanity/pep8/legacy-files.txt | 3 - 4 files changed, 196 insertions(+), 215 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gce_tag.py b/lib/ansible/modules/cloud/google/gce_tag.py index 1223629c30..e2591901c8 100644 --- a/lib/ansible/modules/cloud/google/gce_tag.py +++ b/lib/ansible/modules/cloud/google/gce_tag.py @@ -1,17 +1,16 @@ #!/usr/bin/python -# -# Copyright: Ansible Project +# -*- coding: utf-8 -*- + +# Copyright: (c) 2017, Ansible Project # 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 - ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} - DOCUMENTATION = ''' --- module: gce_tag @@ -19,64 +18,43 @@ version_added: "2.0" short_description: add or remove tag(s) to/from GCE instances description: - This module can add or remove tags U(https://cloud.google.com/compute/docs/label-or-tag-resources#tags) - to/from GCE instances. Use 'instance_pattern' to update multiple instances in a specify zone + to/from GCE instances. Use 'instance_pattern' to update multiple instances in a specify zone. options: instance_name: description: - - The name of the GCE instance to add/remove tags. Required if instance_pattern is not specified. - required: false - default: null - aliases: [] + - The name of the GCE instance to add/remove tags. + - Required if C(instance_pattern) is not specified. instance_pattern: description: - The pattern of GCE instance names to match for adding/removing tags. Full-Python regex is supported. See U(https://docs.python.org/2/library/re.html) for details. - If instance_name is not specified, this field is required. - required: false - default: null - aliases: [] + - If C(instance_name) is not specified, this field is required. version_added: "2.3" tags: description: - - comma-separated list of tags to add or remove - required: true - default: null - aliases: [] + - Comma-separated list of tags to add or remove. + required: yes state: description: - - desired state of the tags - required: false - default: "present" - choices: ["present", "absent"] - aliases: [] + - Desired state of the tags. + choices: [ absent, present ] + default: present zone: description: - - the zone of the disk specified by source - required: false - default: "us-central1-a" - aliases: [] + - The zone of the disk specified by source. + default: us-central1-a service_account_email: description: - - service account email - required: false - default: null - aliases: [] + - Service account email. pem_file: description: - - path to the pem file associated with the service account email - required: false - default: null - aliases: [] + - Path to the PEM file associated with the service account email. project_id: description: - - your GCE project ID - required: false - default: null - aliases: [] - + - Your GCE project ID. requirements: - - "python >= 2.6" - - "apache-libcloud >= 0.17.0" + - python >= 2.6 + - apache-libcloud >= 0.17.0 notes: - Either I(instance_name) or I(instance_pattern) is required. author: @@ -85,27 +63,27 @@ author: ''' EXAMPLES = ''' -# Add tags 'http-server', 'https-server', 'staging' to instance name 'staging-server' in zone us-central1-a. -- gce_tag: +- name: Add tags to instance + gce_tag: instance_name: staging-server tags: http-server,https-server,staging zone: us-central1-a state: present -# Remove tags 'foo', 'bar' from instance 'test-server' in default zone (us-central1-a) -- gce_tag: +- name: Remove tags from instance in default zone (us-central1-a) + gce_tag: instance_name: test-server tags: foo,bar state: absent -# Add tags 'foo', 'bar' to instances in zone that match pattern -- gce_tag: +- name: Add tags to instances in zone that match pattern + gce_tag: instance_pattern: test-server-* tags: foo,bar zone: us-central1-a state: present - ''' + import re import traceback @@ -128,14 +106,17 @@ def _union_items(baselist, comparelist): """Combine two lists, removing duplicates.""" return list(set(baselist) | set(comparelist)) + def _intersect_items(baselist, comparelist): """Return matching items in both lists.""" return list(set(baselist) & set(comparelist)) + def _get_changed_items(baselist, comparelist): """Return changed items as they relate to baselist.""" return list(set(baselist) & set(set(baselist) ^ set(comparelist))) + def modify_tags(gce, module, node, tags, state='present'): """Modify tags on an instance.""" @@ -164,24 +145,25 @@ def modify_tags(gce, module, node, tags, state='present'): except (GoogleBaseError, InvalidRequestError) as e: module.fail_json(msg=str(e), changed=False) + def main(): module = AnsibleModule( argument_spec=dict( - instance_name=dict(required=False), - instance_pattern=dict(required=False), + instance_name=dict(type='str'), + instance_pattern=dict(type='str'), tags=dict(type='list', required=True), - state=dict(default='present', choices=['present', 'absent']), - zone=dict(default='us-central1-a'), - service_account_email=dict(), + state=dict(type='str', default='present', choices=['absent', 'present']), + zone=dict(type='str', default='us-central1-a'), + service_account_email=dict(type='str'), pem_file=dict(type='path'), - project_id=dict(), + project_id=dict(type='str'), ), mutually_exclusive=[ - [ 'instance_name', 'instance_pattern' ] + ['instance_name', 'instance_pattern'] ], required_one_of=[ - [ 'instance_name', 'instance_pattern' ] - ] + ['instance_name', 'instance_pattern'] + ], ) instance_name = module.params.get('instance_name') diff --git a/lib/ansible/modules/cloud/google/gcpubsub.py b/lib/ansible/modules/cloud/google/gcpubsub.py index 2c27754a9c..882f694853 100644 --- a/lib/ansible/modules/cloud/google/gcpubsub.py +++ b/lib/ansible/modules/cloud/google/gcpubsub.py @@ -1,49 +1,46 @@ #!/usr/bin/python -# Copyright 2016 Google Inc. +# -*- coding: utf-8 -*- + +# Copyright: (c) 2016, Google Inc. # 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 - ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} - DOCUMENTATION = ''' --- module: gcpubsub version_added: "2.3" -short_description: Create and Delete Topics/Subscriptions, Publish and pull messages on PubSub. +short_description: Create and Delete Topics/Subscriptions, Publish and pull messages on PubSub description: - Create and Delete Topics/Subscriptions, Publish and pull messages on PubSub. See U(https://cloud.google.com/pubsub/docs) for an overview. requirements: - - "python >= 2.6" - - "google-auth >= 0.5.0" - - "google-cloud-pubsub >= 0.22.0" + - google-auth >= 0.5.0 + - google-cloud-pubsub >= 0.22.0 notes: - Subscription pull happens before publish. You cannot publish and pull in the same task. author: - - "Tom Melendez (@supertom) " + - Tom Melendez (@supertom) options: topic: description: - - GCP pubsub topic name. Only the name, not the full path, is required. - required: True + - GCP pubsub topic name. + - Only the name, not the full path, is required. + required: yes subscription: description: - Dictionary containing a subscripton name associated with a topic (required), along with optional ack_deadline, push_endpoint and pull. For pulling from a subscription, message_ack (bool), max_messages (int) and return_immediate are available as subfields. See subfields name, push_endpoint and ack_deadline for more information. - required: False name: description: Subfield of subscription. Required if subscription is specified. See examples. - required: False ack_deadline: description: Subfield of subscription. Not required. Default deadline for subscriptions to ACK the message before it is resent. See examples. - required: False pull: description: - Subfield of subscription. Not required. If specified, messages will be retrieved from topic via the provided subscription name. @@ -53,95 +50,93 @@ options: description: - Subfield of subscription. Not required. If specified, message will be sent to an endpoint. See U(https://cloud.google.com/pubsub/docs/advanced#push_endpoints) for more information. - required: False publish: description: - List of dictionaries describing messages and attributes to be published. Dictionary is in message(str):attributes(dict) format. Only message is required. - required: False state: description: - - State of the topic or queue (absent, present). Applies to the most granular resource. Remove the most granular resource. If subcription is - specified we remove it. If only topic is specified, that is what is removed. Note that a topic can be removed without first removing the - subscription. - required: False - default: "present" + - State of the topic or queue. + - Applies to the most granular resource. + - If subscription isspecified we remove it. + - If only topic is specified, that is what is removed. + - NOTE - A topic can be removed without first removing the subscription. + choices: [ absent, present ] + default: present ''' + EXAMPLES = ''' -# Create a topic and publish a message to it # (Message will be pushed; there is no check to see if the message was pushed before -# Topics: -## Create Topic -gcpubsub: - topic: ansible-topic-example - state: present +- name: Create a topic and publish a message to it + gcpubsub: + topic: ansible-topic-example + state: present -## Delete Topic -### Subscriptions associated with topic are not deleted. -gcpubsub: - topic: ansible-topic-example - state: absent +# Subscriptions associated with topic are not deleted. +- name: Delete Topic + gcpubsub: + topic: ansible-topic-example + state: absent -## Messages: publish multiple messages, with attributes (key:value available with the message) -### setting absent will keep the messages from being sent -gcpubsub: - topic: "{{ topic_name }}" - state: present - publish: - - message: "this is message 1" - attributes: - mykey1: myvalue - mykey2: myvalu2 - mykey3: myvalue3 - - message: "this is message 2" - attributes: - server: prod - sla: "99.9999" - owner: fred +# Setting absent will keep the messages from being sent +- name: Publish multiple messages, with attributes (key:value available with the message) + gcpubsub: + topic: '{{ topic_name }}' + state: present + publish: + - message: this is message 1 + attributes: + mykey1: myvalue + mykey2: myvalu2 + mykey3: myvalue3 + - message: this is message 2 + attributes: + server: prod + sla: "99.9999" + owner: fred -# Subscriptions -## Create Subscription (pull) -gcpubsub: - topic: ansible-topic-example - subscription: - - name: mysub - state: present +- name: Create Subscription (pull) + gcpubsub: + topic: ansible-topic-example + subscription: + - name: mysub + state: present -## Create Subscription with ack_deadline and push endpoint -### pull is default, ack_deadline is not required -gcpubsub: - topic: ansible-topic-example - subscription: - - name: mysub - ack_deadline: "60" - push_endpoint: http://pushendpoint.example.com - state: present +# pull is default, ack_deadline is not required +- name: Create Subscription with ack_deadline and push endpoint + gcpubsub: + topic: ansible-topic-example + subscription: + - name: mysub + ack_deadline: "60" + push_endpoint: http://pushendpoint.example.com + state: present -## Subscription change from push to pull -### setting push_endpoint to "None" converts subscription to pull. -gcpubsub: - topic: ansible-topic-example - subscription: - name: mysub - push_endpoint: "None" +# Setting push_endpoint to "None" converts subscription to pull. +- name: Subscription change from push to pull + gcpubsub: + topic: ansible-topic-example + subscription: + name: mysub + push_endpoint: "None" -## Delete subscription ### Topic will not be deleted -gcpubsub: - topic: ansible-topic-example - subscription: - - name: mysub - state: absent +- name: Delete subscription + gcpubsub: + topic: ansible-topic-example + subscription: + - name: mysub + state: absent -## Pull messages from subscription -### only pull keyword is required. -gcpubsub: - topic: ansible-topic-example - subscription: - name: ansible-topic-example-sub - pull: - message_ack: yes - max_messages: "100" +# only pull keyword is required. +- name: Pull messages from subscription + gcpubsub: + topic: ansible-topic-example + subscription: + name: ansible-topic-example-sub + pull: + message_ack: yes + max_messages: "100" ''' RETURN = ''' @@ -208,24 +203,24 @@ def publish_messages(message_list, topic): batch.publish(bytes(msg), **attrs) return True + def pull_messages(pull_params, sub): """ :rtype: tuple (output, changed) """ changed = False - max_messages=pull_params.get('max_messages', None) + max_messages = pull_params.get('max_messages', None) message_ack = pull_params.get('message_ack', 'no') return_immediately = pull_params.get('return_immediately', False) - output= [] - pulled = sub.pull(return_immediately=return_immediately, - max_messages=max_messages) + output = [] + pulled = sub.pull(return_immediately=return_immediately, max_messages=max_messages) for ack_id, msg in pulled: msg_dict = {'message_id': msg.message_id, 'attributes': msg.attributes, 'data': msg.data, - 'ack_id': ack_id } + 'ack_id': ack_id} output.append(msg_dict) if message_ack: @@ -238,14 +233,17 @@ def pull_messages(pull_params, sub): def main(): - module = AnsibleModule(argument_spec=dict( - topic=dict(required=True), - state=dict(choices=['absent', 'present'], default='present'), - publish=dict(type='list', default=None), - subscription=dict(type='dict', default=None), - service_account_email=dict(), - credentials_file=dict(), - project_id=dict(), ),) + module = AnsibleModule( + argument_spec=dict( + topic=dict(type='str', required=True), + state=dict(type='str', default='present', choices=['absent', 'present']), + publish=dict(type='list'), + subscription=dict(type='dict'), + service_account_email=dict(type='str'), + credentials_file=dict(type='str'), + project_id=dict(type='str'), + ), + ) if not HAS_PYTHON26: module.fail_json( @@ -280,7 +278,7 @@ def main(): ack_deadline=mod_params['subscription'].get('ack_deadline', None), push_endpoint=mod_params['subscription'].get('push_endpoint', None)) - if mod_params['state'] == 'absent': + if mod_params['state'] == 'absent': # Remove the most granular resource. If subcription is specified # we remove it. If only topic is specified, that is what is removed. # Note that a topic can be removed without first removing the subscription. @@ -294,7 +292,7 @@ def main(): if t.exists(): t.delete() changed = True - elif mod_params['state'] == 'present': + elif mod_params['state'] == 'present': if not t.exists(): t.create() changed = True @@ -307,7 +305,7 @@ def main(): # Subscription operations # TODO(supertom): if more 'update' operations arise, turn this into a function. s.reload() - push_endpoint=mod_params['subscription'].get('push_endpoint', None) + push_endpoint = mod_params['subscription'].get('push_endpoint', None) if push_endpoint is not None: if push_endpoint != s.push_endpoint: if push_endpoint == 'None': @@ -326,11 +324,9 @@ def main(): if mod_params['publish'] and len(mod_params['publish']) > 0: changed = publish_messages(mod_params['publish'], t) - json_output['changed'] = changed json_output.update(mod_params) module.exit_json(**json_output) - if __name__ == '__main__': main() diff --git a/lib/ansible/modules/cloud/google/gcspanner.py b/lib/ansible/modules/cloud/google/gcspanner.py index fbd502ec8b..8311bbd324 100644 --- a/lib/ansible/modules/cloud/google/gcspanner.py +++ b/lib/ansible/modules/cloud/google/gcspanner.py @@ -1,11 +1,12 @@ #!/usr/bin/python -# Copyright 2017 Google Inc. +# -*- coding: utf-8 -*- + +# Copyright: (c) 2017, Google Inc. # 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 - ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} @@ -14,73 +15,74 @@ DOCUMENTATION = ''' --- module: gcspanner version_added: "2.3" -short_description: Create and Delete Instances/Databases on Spanner. +short_description: Create and Delete Instances/Databases on Spanner description: - Create and Delete Instances/Databases on Spanner. See U(https://cloud.google.com/spanner/docs) for an overview. requirements: - - "python >= 2.6" - - "google-auth >= 0.5.0" - - "google-cloud-spanner >= 0.23.0" + - python >= 2.6 + - google-auth >= 0.5.0 + - google-cloud-spanner >= 0.23.0 notes: - Changing the configuration on an existing instance is not supported. author: - - "Tom Melendez (@supertom) " + - Tom Melendez (@supertom) options: configuration: description: - - Configuration the instance should use. Examples are us-central1, asia-east1 and europe-west1. - required: True + - Configuration the instance should use. + - Examples are us-central1, asia-east1 and europe-west1. + required: yes instance_id: description: - GCP spanner instance name. - required: True + required: yes database_name: description: - Name of database contained on the instance. - required: False force_instance_delete: description: - To delete an instance, this argument must exist and be true (along with state being equal to absent). - required: False - default: False + type: bool + default: 'no' instance_display_name: description: - - Name of Instance to display. If not specified, instance_id will be used instead. - required: False + - Name of Instance to display. + - If not specified, instance_id will be used instead. node_count: description: - - Number of nodes in the instance. If not specified while creating an instance, - node_count will be set to 1. - required: False + - Number of nodes in the instance. + default: 1 state: - description: State of the instance or database (absent, present). Applies to the most granular - resource. If a database_name is specified we remove it. If only instance_id - is specified, that is what is removed. - required: False - default: "present" + description: + - State of the instance or database. Applies to the most granular resource. + - If a C(database_name) is specified we remove it. + - If only C(instance_id) is specified, that is what is removed. + choices: [ absent, present ] + default: present ''' + EXAMPLES = ''' -# Create instance. -gcspanner: - instance_id: "{{ instance_id }}" - configuration: "{{ configuration }}" - state: present - node_count: 1 +- name: Create instance + gcspanner: + instance_id: '{{ instance_id }}' + configuration: '{{ configuration }}' + state: present + node_count: 1 -# Create database. -gcspanner: - instance_id: "{{ instance_id }}" - configuration: "{{ configuration }}" - database_name: "{{ database_name }}" - state: present +- name: Create database + gcspanner: + instance_id: '{{ instance_id }}' + configuration: '{{ configuration }}' + database_name: '{{ database_name }}' + state: present -# Delete instance (and all databases) -gcspanner: - instance_id: "{{ instance_id }}" - configuration: "{{ configuration }}" - state: absent - force_instance_delete: yes +- name: Delete instance (and all databases) +- gcspanner: + instance_id: '{{ instance_id }}' + configuration: '{{ configuration }}' + state: absent + force_instance_delete: yes ''' RETURN = ''' @@ -136,6 +138,7 @@ CLOUD_CLIENT = 'google-cloud-spanner' CLOUD_CLIENT_MINIMUM_VERSION = '0.23.0' CLOUD_CLIENT_USER_AGENT = 'ansible-spanner-0.1' + def get_spanner_configuration_name(config_name, project_name): config_name = 'projects/%s/instanceConfigs/regional-%s' % (project_name, config_name) @@ -177,17 +180,20 @@ def instance_update(instance): def main(): - module = AnsibleModule(argument_spec=dict( - instance_id=dict(type='str', required=True), - state=dict(choices=['absent', 'present'], default='present'), - database_name=dict(type='str', default=None), - configuration=dict(type='str', required=True), - node_count=dict(type='int'), - instance_display_name=dict(type='str', default=None), - force_instance_delete=dict(type='bool', default=False), - service_account_email=dict(), - credentials_file=dict(), - project_id=dict(), ), ) + module = AnsibleModule( + argument_spec=dict( + instance_id=dict(type='str', required=True), + state=dict(type='str', default='present', choices=['absent', 'present']), + database_name=dict(type='str'), + configuration=dict(type='str', required=True), + node_count=dict(type='int', default=1), + instance_display_name=dict(type='str'), + force_instance_delete=dict(type='bool', default=False), + service_account_email=dict(type='str'), + credentials_file=dict(type='str'), + project_id=dict(type='str'), + ), + ) if not HAS_PYTHON26: module.fail_json( diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index d253720f44..63ea92970b 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -65,9 +65,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_tag.py -lib/ansible/modules/cloud/google/gcpubsub.py -lib/ansible/modules/cloud/google/gcspanner.py lib/ansible/modules/cloud/lxc/lxc_container.py lib/ansible/modules/cloud/lxd/lxd_container.py lib/ansible/modules/cloud/misc/rhevm.py