Skip to content

Commit

Permalink
library: flake8 ceph-ansible modules
Browse files Browse the repository at this point in the history
This commit ensure all ceph-ansible modules pass flake8 properly.

Signed-off-by: Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
(cherry picked from commit beda1fe)
  • Loading branch information
hswong3i authored and guits committed Jul 7, 2021
1 parent ec64898 commit 35264b7
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 253 deletions.
76 changes: 38 additions & 38 deletions library/ceph_add_users_buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
default: unlimited
bucketmaxobjects:
description:
- with bucket quota enabled specify maximum number of objects # noqa E501
- with bucket quota enabled specify maximum number of objects # noqa: E501
required: false
default: unlimited
buckets:
Expand Down Expand Up @@ -263,7 +263,7 @@
returned: always
type: list
sample: [
"test2: could not modify user: unable to modify user, cannot add duplicate email\n" # noqa E501
"test2: could not modify user: unable to modify user, cannot add duplicate email\n" # noqa: E501
]
failed_users:
Expand Down Expand Up @@ -334,52 +334,52 @@ def create_users(rgw, users, result):
if email:
if autogenkey:
try:
rgw.create_user(username, fullname, email=email, key_type='s3', # noqa E501
rgw.create_user(username, fullname, email=email, key_type='s3', # noqa: E501
generate_key=autogenkey,
max_buckets=maxbucket, suspended=suspend) # noqa E501
max_buckets=maxbucket, suspended=suspend) # noqa: E501
except radosgw.exception.RadosGWAdminError as e:
result['error_messages'].append(username + ' ' + e.get_code()) # noqa E501
result['error_messages'].append(username + ' ' + e.get_code()) # noqa: E501
fail_flag = True
else:
try:
rgw.create_user(username, fullname, email=email, key_type='s3', # noqa E501
access_key=accesskey, secret_key=secretkey, # noqa E501
max_buckets=maxbucket, suspended=suspend) # noqa E501
rgw.create_user(username, fullname, email=email, key_type='s3', # noqa: E501
access_key=accesskey, secret_key=secretkey, # noqa: E501
max_buckets=maxbucket, suspended=suspend) # noqa: E501
except radosgw.exception.RadosGWAdminError as e:
result['error_messages'].append(username + ' ' + e.get_code()) # noqa E501
result['error_messages'].append(username + ' ' + e.get_code()) # noqa: E501
fail_flag = True
else:
if autogenkey:
try:
rgw.create_user(username, fullname, key_type='s3',
generate_key=autogenkey,
max_buckets=maxbucket, suspended=suspend) # noqa E501
max_buckets=maxbucket, suspended=suspend) # noqa: E501
except radosgw.exception.RadosGWAdminError as e:
result['error_messages'].append(username + ' ' + e.get_code()) # noqa E501
result['error_messages'].append(username + ' ' + e.get_code()) # noqa: E501
fail_flag = True
else:
try:
rgw.create_user(username, fullname, key_type='s3',
access_key=accesskey, secret_key=secretkey, # noqa E501
max_buckets=maxbucket, suspended=suspend) # noqa E501
access_key=accesskey, secret_key=secretkey, # noqa: E501
max_buckets=maxbucket, suspended=suspend) # noqa: E501
except radosgw.exception.RadosGWAdminError as e:
result['error_messages'].append(username + ' ' + e.get_code()) # noqa E501
result['error_messages'].append(username + ' ' + e.get_code()) # noqa: E501
fail_flag = True

if not fail_flag and userquota:
try:
rgw.set_quota(username, 'user', max_objects=usermaxobjects,
max_size_kb=usermaxsize, enabled=True)
except radosgw.exception.RadosGWAdminError as e:
result['error_messages'].append(username + ' ' + e.get_code()) # noqa E501
result['error_messages'].append(username + ' ' + e.get_code()) # noqa: E501
fail_flag = True

if not fail_flag and bucketquota:
try:
rgw.set_quota(username, 'bucket', max_objects=bucketmaxobjects, # noqa E501
rgw.set_quota(username, 'bucket', max_objects=bucketmaxobjects, # noqa: E501
max_size_kb=bucketmaxsize, enabled=True)
except radosgw.exception.RadosGWAdminError as e:
result['error_messages'].append(username + ' ' + e.get_code()) # noqa E501
result['error_messages'].append(username + ' ' + e.get_code()) # noqa: E501
fail_flag = True

if fail_flag:
Expand Down Expand Up @@ -450,12 +450,12 @@ def create_buckets(rgw, buckets, result):
except radosgw.exception.RadosGWAdminError:
pass
failed_buckets.append(bucket)
result['error_messages'].append(bucket + ' could not be linked' + ', NoSuchUser ' + user) # noqa E501
result['error_messages'].append(bucket + ' could not be linked' + ', NoSuchUser ' + user) # noqa: E501

else:
# something went wrong
failed_buckets.append(bucket)
result['error_messages'].append(bucket + ' could not be created') # noqa E501
result['error_messages'].append(bucket + ' could not be created') # noqa: E501

result['added_buckets'] = ", ".join(added_buckets)
result['failed_buckets'] = ", ".join(failed_buckets)
Expand All @@ -467,7 +467,7 @@ def create_bucket(rgw, bucket):
host=rgw._connection[0],
port=rgw.port,
is_secure=rgw.is_secure,
calling_format=boto.s3.connection.OrdinaryCallingFormat(), # noqa E501
calling_format=boto.s3.connection.OrdinaryCallingFormat(), # noqa: E501
)

try:
Expand All @@ -489,23 +489,23 @@ def main():
admin_access_key=dict(type='str', required=True),
admin_secret_key=dict(type='str', required=True),
buckets=dict(type='list', required=False, elements='dict',
options=dict(bucket=dict(type='str', required=True), # noqa E501
user=dict(type='str', required=True))), # noqa E501
options=dict(bucket=dict(type='str', required=True), # noqa: E501
user=dict(type='str', required=True))), # noqa: E501
users=dict(type='list', required=False, elements='dict',
options=dict(username=dict(type='str', required=True), # noqa E501
fullname=dict(type='str', required=True), # noqa E501
email=dict(type='str', required=False), # noqa E501
maxbucket=dict(type='int', required=False, default=1000), # noqa E501
suspend=dict(type='bool', required=False, default=False), # noqa E501
autogenkey=dict(type='bool', required=False, default=True), # noqa E501
accesskey=dict(type='str', required=False), # noqa E501
secretkey=dict(type='str', required=False), # noqa E501
userquota=dict(type='bool', required=False, default=False), # noqa E501
usermaxsize=dict(type='str', required=False, default='-1'), # noqa E501
usermaxobjects=dict(type='int', required=False, default=-1), # noqa E501
bucketquota=dict(type='bool', required=False, default=False), # noqa E501
bucketmaxsize=dict(type='str', required=False, default='-1'), # noqa E501
bucketmaxobjects=dict(type='int', required=False, default=-1)))) # noqa E501
options=dict(username=dict(type='str', required=True), # noqa: E501
fullname=dict(type='str', required=True), # noqa: E501
email=dict(type='str', required=False), # noqa: E501
maxbucket=dict(type='int', required=False, default=1000), # noqa: E501
suspend=dict(type='bool', required=False, default=False), # noqa: E501
autogenkey=dict(type='bool', required=False, default=True), # noqa: E501
accesskey=dict(type='str', required=False), # noqa: E501
secretkey=dict(type='str', required=False), # noqa: E501
userquota=dict(type='bool', required=False, default=False), # noqa: E501
usermaxsize=dict(type='str', required=False, default='-1'), # noqa: E501
usermaxobjects=dict(type='int', required=False, default=-1), # noqa: E501
bucketquota=dict(type='bool', required=False, default=False), # noqa: E501
bucketmaxsize=dict(type='str', required=False, default='-1'), # noqa: E501
bucketmaxobjects=dict(type='int', required=False, default=-1)))) # noqa: E501

# the AnsibleModule object
module = AnsibleModule(argument_spec=fields,
Expand Down Expand Up @@ -533,8 +533,8 @@ def main():
# radosgw connection
rgw = radosgw.connection.RadosGWAdminConnection(host=rgw_host,
port=port,
access_key=admin_access_key, # noqa E501
secret_key=admin_secret_key, # noqa E501
access_key=admin_access_key, # noqa: E501
secret_key=admin_secret_key, # noqa: E501
aws_signature='AWS4',
is_secure=is_secure)

Expand Down
10 changes: 5 additions & 5 deletions library/ceph_crush.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def sort_osd_crush_location(location, module):
"region",
"root",
]
return sorted(location, key=lambda crush: crush_bucket_types.index(crush[0])) # noqa E501
return sorted(location, key=lambda crush: crush_bucket_types.index(crush[0])) # noqa: E501
except ValueError as error:
fatal("{} is not a valid CRUSH bucket, valid bucket types are {}".format(error.args[0].split()[0], crush_bucket_types), module) # noqa E501
fatal("{} is not a valid CRUSH bucket, valid bucket types are {}".format(error.args[0].split()[0], crush_bucket_types), module) # noqa: E501


def create_and_move_buckets_list(cluster, location, containerized=None):
Expand All @@ -122,10 +122,10 @@ def create_and_move_buckets_list(cluster, location, containerized=None):
for item in location:
bucket_type, bucket_name = item
# ceph osd crush add-bucket maroot root
cmd_list.append(generate_cmd(cluster, "add-bucket", bucket_name, bucket_type, containerized)) # noqa E501
cmd_list.append(generate_cmd(cluster, "add-bucket", bucket_name, bucket_type, containerized)) # noqa: E501
if previous_bucket:
# ceph osd crush move monrack root=maroot
cmd_list.append(generate_cmd(cluster, "move", previous_bucket, "%s=%s" % (bucket_type, bucket_name), containerized)) # noqa E501
cmd_list.append(generate_cmd(cluster, "move", previous_bucket, "%s=%s" % (bucket_type, bucket_name), containerized)) # noqa: E501
previous_bucket = item[1]
return cmd_list

Expand Down Expand Up @@ -172,7 +172,7 @@ def run_module():
startd = datetime.datetime.now()

# run the Ceph command to add buckets
rc, cmd, out, err = exec_commands(module, create_and_move_buckets_list(cluster, location, containerized)) # noqa E501
rc, cmd, out, err = exec_commands(module, create_and_move_buckets_list(cluster, location, containerized)) # noqa: E501

endd = datetime.datetime.now()
delta = endd - startd
Expand Down
52 changes: 34 additions & 18 deletions library/ceph_crush_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@

from ansible.module_utils.basic import AnsibleModule
try:
from ansible.module_utils.ca_common import exit_module, generate_ceph_cmd, is_containerized, exec_command
from ansible.module_utils.ca_common import exit_module, \
generate_ceph_cmd, \
is_containerized, \
exec_command
except ImportError:
from module_utils.ca_common import exit_module, generate_ceph_cmd, is_containerized, exec_command
from module_utils.ca_common import exit_module, \
generate_ceph_cmd, \
is_containerized, \
exec_command
import datetime
import json

Expand Down Expand Up @@ -71,7 +77,8 @@
description:
- The ceph bucket type for replicated rule.
required: false
choices: ['osd', 'host', 'chassis', 'rack', 'row', 'pdu', 'pod', 'room', 'datacenter', 'zone', 'region', 'root']
choices: ['osd', 'host', 'chassis', 'rack', 'row', 'pdu', 'pod',
'room', 'datacenter', 'zone', 'region', 'root']
device_class:
description:
- The ceph device class for replicated rule.
Expand Down Expand Up @@ -135,7 +142,10 @@ def create_rule(module, container_image=None):
if profile:
args.append(profile)

cmd = generate_ceph_cmd(['osd', 'crush', 'rule'], args, cluster=cluster, container_image=container_image)
cmd = generate_ceph_cmd(['osd', 'crush', 'rule'],
args,
cluster=cluster,
container_image=container_image)

return cmd

Expand All @@ -150,7 +160,10 @@ def get_rule(module, container_image=None):

args = ['dump', name, '--format=json']

cmd = generate_ceph_cmd(['osd', 'crush', 'rule'], args, cluster=cluster, container_image=container_image)
cmd = generate_ceph_cmd(['osd', 'crush', 'rule'],
args,
cluster=cluster,
container_image=container_image)

return cmd

Expand All @@ -165,7 +178,10 @@ def remove_rule(module, container_image=None):

args = ['rm', name]

cmd = generate_ceph_cmd(['osd', 'crush', 'rule'], args, cluster=cluster, container_image=container_image)
cmd = generate_ceph_cmd(['osd', 'crush', 'rule'],
args,
cluster=cluster,
container_image=container_image)

return cmd

Expand All @@ -175,11 +191,11 @@ def main():
argument_spec=dict(
name=dict(type='str', required=True),
cluster=dict(type='str', required=False, default='ceph'),
state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'),
rule_type=dict(type='str', required=False, choices=['replicated', 'erasure']),
state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'), # noqa: E501
rule_type=dict(type='str', required=False, choices=['replicated', 'erasure']), # noqa: E501
bucket_root=dict(type='str', required=False),
bucket_type=dict(type='str', required=False, choices=['osd', 'host', 'chassis', 'rack', 'row', 'pdu', 'pod',
'room', 'datacenter', 'zone', 'region', 'root']),
bucket_type=dict(type='str', required=False, choices=['osd', 'host', 'chassis', 'rack', 'row', 'pdu', 'pod', # noqa: E501
'room', 'datacenter', 'zone', 'region', 'root']), # noqa: E501
device_class=dict(type='str', required=False),
profile=dict(type='str', required=False)
),
Expand Down Expand Up @@ -214,28 +230,28 @@ def main():
container_image = is_containerized()

if state == "present":
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image))
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501
if rc != 0:
rc, cmd, out, err = exec_command(module, create_rule(module, container_image=container_image))
rc, cmd, out, err = exec_command(module, create_rule(module, container_image=container_image)) # noqa: E501
changed = True
else:
rule = json.loads(out)
if (rule['type'] == 1 and rule_type == 'erasure') or (rule['type'] == 3 and rule_type == 'replicated'):
module.fail_json(msg="Can not convert crush rule {} to {}".format(name, rule_type), changed=False, rc=1)
if (rule['type'] == 1 and rule_type == 'erasure') or (rule['type'] == 3 and rule_type == 'replicated'): # noqa: E501
module.fail_json(msg="Can not convert crush rule {} to {}".format(name, rule_type), changed=False, rc=1) # noqa: E501

elif state == "absent":
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image))
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501
if rc == 0:
rc, cmd, out, err = exec_command(module, remove_rule(module, container_image=container_image))
rc, cmd, out, err = exec_command(module, remove_rule(module, container_image=container_image)) # noqa: E501
changed = True
else:
rc = 0
out = "Crush Rule {} doesn't exist".format(name)

elif state == "info":
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image))
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501

exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed)
exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 35264b7

Please sign in to comment.