Role related code refactoring

Code for operating with roles in release
has to be refactored in order to meet
code consistency and UX:

   fuelclient/objects/role.py
   fuelclient/v1/role.py
   fuelclient/cli/actions/role.py

Change-Id: Ifdfa537f99249d1af8ab857e7f6ada313eb2c578
This commit is contained in:
tivaliy 2016-09-19 20:13:45 +03:00 committed by Vitalii Kulanov
parent 14e8bee51b
commit ab86dfa2dc
3 changed files with 28 additions and 38 deletions

View File

@ -58,7 +58,7 @@ class RoleAction(Action):
fuel role --rel 1
"""
roles = Role.get_all(params.release)
roles = Role(obj_id=params.release).data
acceptable_keys = ("name", )
@ -75,7 +75,7 @@ class RoleAction(Action):
"""Save full role description to file
fuel role --rel 1 --role controller --file some.yaml
"""
role = Role.get_one(params.release, params.role)
role = Role(obj_id=params.release).get_role(params.role)
self.file_serializer.write_to_file(params.file, role)
self.file_serializer.print_to_output(
role,
@ -87,7 +87,7 @@ class RoleAction(Action):
fuel role --rel 1 --create --file some.yaml
"""
role = self.file_serializer.read_from_file(params.file)
role = Role.create(params.release, role)
role = Role(obj_id=params.release).create_role(role)
self.file_serializer.print_to_output(
role,
"Role {0} successfully created from {1}.".format(
@ -99,7 +99,7 @@ class RoleAction(Action):
fuel role --rel 1 --update --file some.yaml
"""
role = self.file_serializer.read_from_file(params.file)
role = Role.update(params.release, role['name'], role)
role = Role(obj_id=params.release).update_role(role['name'], role)
self.file_serializer.print_to_output(
role,
"Role successfully updated from {0}.".format(params.file))
@ -109,7 +109,7 @@ class RoleAction(Action):
"""Delete role from fuel
fuel role --delete --role controller --rel 1
"""
Role.delete(params.release, params.role)
Role(obj_id=params.release).delete_role(params.role)
self.file_serializer.print_to_output(
{},
"Role with id {0} successfully deleted.".format(params.role))

View File

@ -18,33 +18,21 @@ from fuelclient.objects.base import BaseObject
class Role(BaseObject):
class_api_path = "releases/{release_id}/roles/"
instance_api_path = "releases/{release_id}/roles/{role_name}/"
instance_api_path = "releases/{0}/roles/"
role_api_path = "releases/{0}/roles/{1}/"
@classmethod
def get_all(cls, release_id):
return cls.connection.get_request(
cls.class_api_path.format(release_id=release_id))
def get_role(self, role_name):
return self.connection.get_request(
self.role_api_path.format(self.id, role_name))
@classmethod
def get_one(cls, release_id, role_name):
return cls.connection.get_request(
cls.instance_api_path.format(
release_id=release_id, role_name=role_name))
def update_role(self, role_name, data):
return self.connection.put_request(
self.role_api_path.format(self.id, role_name), data)
@classmethod
def update(cls, release_id, role_name, data):
return cls.connection.put_request(
cls.instance_api_path.format(
release_id=release_id, role_name=role_name), data)
def create_role(self, data):
return self.connection.post_request(
self.instance_api_path.format(self.id), data)
@classmethod
def create(cls, release_id, data):
return cls.connection.post_request(
cls.class_api_path.format(release_id=release_id), data)
@classmethod
def delete(cls, release_id, role_name):
return cls.connection.delete_request(
cls.instance_api_path.format(
release_id=release_id, role_name=role_name))
def delete_role(self, role_name):
return self.connection.delete_request(
self.role_api_path.format(self.id, role_name))

View File

@ -31,7 +31,7 @@ class RoleClient(base_v1.BaseV1Client):
:return: roles data as a list of dict
:rtype: list
"""
data = self._entity_wrapper.get_all(release_id=release_id)
data = self._entity_wrapper(obj_id=release_id).data
# Retrieve nested data from 'meta' and add it as a new key-value pair
for role in data:
role_meta = role.pop('meta')
@ -42,18 +42,20 @@ class RoleClient(base_v1.BaseV1Client):
return data
def get_one(self, release_id, role_name):
return self._entity_wrapper.get_one(release_id, role_name)
role = self._entity_wrapper(obj_id=release_id)
return role.get_role(role_name)
def update(self, data, **kwargs):
return self._entity_wrapper.update(kwargs['release_id'],
kwargs['role_name'],
data)
role = self._entity_wrapper(obj_id=kwargs['release_id'])
return role.update_role(kwargs['role_name'], data)
def create(self, data, **kwargs):
return self._entity_wrapper.create(kwargs['release_id'], data)
role = self._entity_wrapper(obj_id=kwargs['release_id'])
return role.create_role(data)
def delete(self, release_id, role_name):
return self._entity_wrapper.delete(release_id, role_name)
role = self._entity_wrapper(obj_id=release_id)
return role.delete_role(role_name)
def get_client(connection):