pep8 the ansible modules

By ignoring the appropriate tests that pep8 does we can properly run
*most* of the pep8 tests on all of our modules allowing for a more
consistent coding style.

Closes-Bug: #1528431
Change-Id: I33f27a250d06d4f044267aa3ad189e092789b8df
This commit is contained in:
SamYaple 2015-12-21 17:04:09 +00:00
parent ed82afa8e9
commit 4199634e98
8 changed files with 93 additions and 78 deletions

View File

@ -18,9 +18,9 @@ from ConfigParser import ConfigParser
from cStringIO import StringIO from cStringIO import StringIO
import os import os
from ansible.runner.return_data import ReturnData
from ansible import utils from ansible import utils
from ansible.utils import template from ansible.utils import template
from ansible.runner.return_data import ReturnData
class ActionModule(object): class ActionModule(object):
@ -30,7 +30,20 @@ class ActionModule(object):
def __init__(self, runner): def __init__(self, runner):
self.runner = runner self.runner = runner
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): def read_config(self, source, inject, config):
# Only use config if present
if os.access(source, os.R_OK):
# template the source data locally & get ready to transfer
resultant = template.template_from_file(self.runner.basedir,
source, inject)
# Read in new results and merge this with the existing config
fakefile = StringIO(resultant)
config.readfp(fakefile)
fakefile.close()
def run(self, conn, tmp, module_name, module_args, inject,
complex_args=None, **kwargs):
args = {} args = {}
if complex_args: if complex_args:
args.update(complex_args) args.update(complex_args)
@ -47,7 +60,6 @@ class ActionModule(object):
else: else:
inject.update(utils.parse_kv(extra_vars)) inject.update(utils.parse_kv(extra_vars))
# Catch the case where sources is a str() # Catch the case where sources is a str()
if not isinstance(sources, list): if not isinstance(sources, list):
sources = [sources] sources = [sources]
@ -58,24 +70,18 @@ class ActionModule(object):
# template the source string # template the source string
source = template.template(self.runner.basedir, source, inject) source = template.template(self.runner.basedir, source, inject)
# Only use config if present
if os.access(source, os.R_OK):
# template the source data locally & get ready to transfer
try: try:
resultant = template.template_from_file(self.runner.basedir, source, inject) self.read_config(source, inject, config)
except Exception as e: except Exception as e:
return ReturnData(conn=conn, comm_ok=False, result={'failed': True, 'msg': str(e)}) return ReturnData(conn=conn, comm_ok=False,
result={'failed': True, 'msg': str(e)})
# Read in new results and merge this with the existing config
fakefile = StringIO(resultant)
config.readfp(fakefile)
fakefile.close()
# Dump configparser to string via an emulated file # Dump configparser to string via an emulated file
fakefile = StringIO() fakefile = StringIO()
config.write(fakefile) config.write(fakefile)
# Template the file to fill out any variables # Template the file to fill out any variables
content = template.template(self.runner.basedir, fakefile.getvalue(), inject) content = template.template(self.runner.basedir, fakefile.getvalue(),
inject)
fakefile.close() fakefile.close()
# Ship this content over to a new file for use with the copy module # Ship this content over to a new file for use with the copy module
@ -87,4 +93,6 @@ class ActionModule(object):
original_basename=os.path.basename(source), original_basename=os.path.basename(source),
follow=True, follow=True,
) )
return self.runner._execute_module(conn, tmp, 'copy', '', inject=inject, complex_args=copy_module_args) return self.runner._execute_module(conn, tmp, 'copy', '',
inject=inject,
complex_args=copy_module_args)

View File

@ -109,9 +109,9 @@ Distribute a to file many host without compression; Change permissions on dest:
import base64 import base64
import hashlib import hashlib
import os import os
import sys
import zlib import zlib
def copy_from_host(module): def copy_from_host(module):
compress = module.params.get('compress') compress = module.params.get('compress')
src = module.params.get('src') src = module.params.get('src')
@ -121,7 +121,7 @@ def copy_from_host(module):
if not os.access(src, os.R_OK): if not os.access(src, os.R_OK):
module.fail_json(msg="file is not readable: {}".format(src)) module.fail_json(msg="file is not readable: {}".format(src))
mode = oct(os.stat(src).st_mode & 0777) mode = oct(os.stat(src).st_mode & 0o777)
with open(src, 'rb') as f: with open(src, 'rb') as f:
raw_data = f.read() raw_data = f.read()
@ -162,8 +162,8 @@ def copy_to_host(module):
module.exit_json(changed=True) module.exit_json(changed=True)
def main(): def main():
module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
compress=dict(default=True, type='bool'), compress=dict(default=True, type='bool'),
dest=dict(type='str'), dest=dict(type='str'),
@ -171,7 +171,7 @@ def main():
sha1=dict(default=None, type='str'), sha1=dict(default=None, type='str'),
src=dict(required=True, type='str') src=dict(required=True, type='str')
) )
) module = AnsibleModule(argument_spec)
dest = module.params.get('dest') dest = module.params.get('dest')
@ -183,7 +183,8 @@ def main():
except Exception as e: except Exception as e:
module.exit_json(failed=True, changed=True, msg=repr(e)) module.exit_json(failed=True, changed=True, msg=repr(e))
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import * # noqa
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -45,12 +45,12 @@ EXAMPLES = '''
import json import json
import pyudev import pyudev
def main(): def main():
module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
partition_name=dict(required=True, type='str') partition_name=dict(required=True, type='str')
) )
) module = AnsibleModule(argument_spec)
partition_name = module.params.get('partition_name') partition_name = module.params.get('partition_name')
try: try:
@ -68,6 +68,6 @@ def main():
module.exit_json(failed=True, msg=repr(e)) module.exit_json(failed=True, msg=repr(e))
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import * # noqa
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -14,8 +14,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# This file is a barebones file needed to file a gap until Ansible 2.0. No error # This file is a barebones file needed to file a gap until Ansible 2.0. No
# checking, no deletions, no updates. Idempotent creation only. # error checking, no deletions, no updates. Idempotent creation only.
# If you look closely, you will see we arent _really_ using the shade module # If you look closely, you will see we arent _really_ using the shade module
# we just use it to slightly abstract the authentication model. As patches land # we just use it to slightly abstract the authentication model. As patches land
@ -24,8 +24,8 @@
import shade import shade
def main(): def main():
module = AnsibleModule(
argument_spec = openstack_full_argument_spec( argument_spec = openstack_full_argument_spec(
description=dict(required=True, type='str'), description=dict(required=True, type='str'),
service_name=dict(required=True, type='str'), service_name=dict(required=True, type='str'),
@ -35,7 +35,7 @@ def main():
public_url=dict(required=True, type='str'), public_url=dict(required=True, type='str'),
endpoint_region=dict(required=True, type='str') endpoint_region=dict(required=True, type='str')
) )
) module = AnsibleModule(argument_spec)
try: try:
description = module.params.pop('description') description = module.params.pop('description')
@ -61,13 +61,15 @@ def main():
if _endpoint.service_id == service.id: if _endpoint.service_id == service.id:
endpoint = _endpoint endpoint = _endpoint
else: else:
service = cloud.keystone_client.services.create(name=service_name, service = cloud.keystone_client.services.create(
name=service_name,
service_type=service_type, service_type=service_type,
description=description) description=description)
if endpoint is None: if endpoint is None:
changed = True changed = True
cloud.keystone_client.endpoints.create(service_id=service.id, cloud.keystone_client.endpoints.create(
service_id=service.id,
adminurl=admin_url, adminurl=admin_url,
internalurl=internal_url, internalurl=internal_url,
publicurl=public_url, publicurl=public_url,
@ -78,7 +80,7 @@ def main():
module.exit_json(failed=True, changed=True, msg=e) module.exit_json(failed=True, changed=True, msg=e)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import * # noqa
from ansible.module_utils.openstack import * from ansible.module_utils.openstack import * # noqa
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -14,8 +14,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# This file is a barebones file needed to file a gap until Ansible 2.0. No error # This file is a barebones file needed to file a gap until Ansible 2.0. No
# checking, no deletions, no updates. Idempotent creation only. # error checking, no deletions, no updates. Idempotent creation only.
# If you look closely, you will see we arent _really_ using the shade module # If you look closely, you will see we arent _really_ using the shade module
# we just use it to slightly abstract the authentication model. As patches land # we just use it to slightly abstract the authentication model. As patches land
@ -24,15 +24,15 @@
import shade import shade
def main(): def main():
module = AnsibleModule(
argument_spec = openstack_full_argument_spec( argument_spec = openstack_full_argument_spec(
password=dict(required=True, type='str'), password=dict(required=True, type='str'),
project=dict(required=True, type='str'), project=dict(required=True, type='str'),
role=dict(required=True, type='str'), role=dict(required=True, type='str'),
user=dict(required=True, type='str') user=dict(required=True, type='str')
) )
) module = AnsibleModule(argument_spec)
try: try:
password = module.params.pop('password') password = module.params.pop('password')
@ -61,7 +61,8 @@ def main():
if not project: if not project:
changed = True changed = True
project = cloud.keystone_client.tenants.create(tenant_name=project_name) project = cloud.keystone_client.tenants.create(
tenant_name=project_name)
if not role: if not role:
changed = True changed = True
@ -69,15 +70,19 @@ def main():
if not user: if not user:
changed = True changed = True
user = cloud.keystone_client.users.create(name=user_name, password=password, tenant_id=project.id) user = cloud.keystone_client.users.create(name=user_name,
cloud.keystone_client.roles.add_user_role(role=role.id, user=user.id, tenant=project.id) password=password,
tenant_id=project.id)
cloud.keystone_client.roles.add_user_role(role=role.id,
user=user.id,
tenant=project.id)
module.exit_json(changed=changed) module.exit_json(changed=changed)
except Exception as e: except Exception as e:
module.exit_json(failed=True, changed=True, msg=e) module.exit_json(failed=True, changed=True, msg=e)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import * # noqa
from ansible.module_utils.openstack import * from ansible.module_utils.openstack import * # noqa
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -57,7 +57,7 @@ def main():
module.exit_json(failed=True, changed=True, msg=e) module.exit_json(failed=True, changed=True, msg=e)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import * # noqa
from ansible.module_utils.openstack import * from ansible.module_utils.openstack import * # noqa
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -61,7 +61,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import * # noqa
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -104,4 +104,4 @@ commands =
[flake8] [flake8]
show-source = True show-source = True
exclude=.eggs,.git,.tox,doc,ansible/library,ansible/action_plugins,docker/kolla-ansible exclude=.eggs,.git,.tox,doc