class MockService(): test_nodes = Node.Schema().load([ { '_id': ObjectId(), 'name': 'Test1', 'ip': '1.1.1.1', 'ssh_user': '******', 'tasks': [ { '_id': ObjectId(), 'name': 'Test', 'runtime': 1000, 'deadline': 1000, 'period': 1000 } ], 'devices': [ '/dev/test1', '/dev/test2' ] }, { '_id': ObjectId(), 'name': 'Test2', 'ip': '2.2.2.2', 'ssh_user': '******' } ], many=True) def __init__(self, server_url: str, server_port: str): pass def get_all(self) -> List[Node]: return self.test_nodes def get_one(self, node_key: str) -> Node: for node in self.test_nodes: if str(node._id) == node_key: return node raise StatusError('Node not found.') def create(self, node: Node, ssh_user: str, ssh_pass: str) -> str: for test_node in self.test_nodes: if test_node.name == node.name: raise StatusError('Name already present.') return ObjectId() def update(self, node_key: str, new_values: dict) -> Node: for node in self.test_nodes: if str(node._id) == node_key: return Node.Schema().load(new_values) raise StatusError('Node not found.') def delete(self, node_key: str) -> Node: for node in self.test_nodes: if str(node._id) == node_key: return node raise StatusError('Node not found.')
def get_all(self) -> List[Node]: """Obtain the list of nodes.""" response = requests.get(self.base_url + '/nodes') if response.status_code == HTTPStatus.OK: return Node.Schema().load(response.json(), many=True) else: raise StatusError(response.json()['error'])
def create(self, node: Node, ssh_user: str, ssh_pass: str) -> str: """Create a new node.""" response = requests.post( self.base_url + '/nodes', auth=(ssh_user, ssh_pass), json=Node.Schema(exclude=['_id', 'tasks']).dump(node)) if response.status_code == HTTPStatus.OK: return response.json()['_id'] else: raise StatusError(response.json()['error'])
def get_one(self, node_key: str) -> Node: """ Obtain detailed information about a node. The given node key can be either its ID or name. """ if bson.ObjectId.is_valid(node_key): uri = '/nodes/' + node_key else: uri = '/nodes?name=' + node_key response = requests.get(self.base_url + uri) if response.status_code == HTTPStatus.OK: return Node.Schema().load(response.json()) else: raise StatusError(response.json()['error'])
def delete(self, node_key: str) -> Node: """ Delete a node. The given node key can be either its ID or name. """ if not bson.ObjectId.is_valid(node_key): response = requests.get(self.base_url + '/nodes?name=' + node_key) if response.status_code != HTTPStatus.OK: raise StatusError(response.json()['error']) node_key = response.json()['_id'] response = requests.delete(self.base_url + '/nodes/' + node_key) if response.status_code == HTTPStatus.OK: return Node.Schema().load(response.json()) else: raise StatusError(response.json()['error'])
def update(service, key, values): """ Update a node's values. The node is identified by a given KEY, which can be either the node's ID or its name. The new VALUES must be specified as JSON. """ try: Node.Schema().load(values) service.update(key, values) click.echo('The node\'s values were updated correctly.') except ValidationError as e: click.echo('Unable to update the node\'s values:\n' + str(e.messages)) except Exception as e: click.echo('Unable to update the node\'s values:\n' + str(e))
def add(service, name, address, cpu_cores, cpu, cpu_arch, cpu_freq, ram, device): """Add a new node.""" ssh_user = click.prompt('User', type=str) ssh_pass = click.prompt('Password', type=str, hide_input=True, confirmation_prompt=True) try: node = Node.Schema().load({ 'name': name, 'ip': address, 'cpu': cpu, 'cpu_arch': cpu_arch, 'cpu_cores': cpu_cores, 'cpu_freq': cpu_freq, 'ram': ram, }) node.devices = list(d for d in device) new_id = service.create(node, ssh_user, ssh_pass) click.echo(f'Added new node with ID {new_id}.') except Exception as e: click.echo('Unable to add new node:\n' + str(e))
class MockService(): test_nodes = Node.Schema().load([ { '_id': ObjectId(), 'name': 'Test1', 'ip': '1.1.1.1', 'ssh_user': '******', 'tasks': [ { '_id': ObjectId(), 'name': 'Test', 'runtime': 1000, 'deadline': 1000, 'period': 1000 } ], 'devices': [ '/dev/test1', '/dev/test2' ] }, { '_id': ObjectId(), 'name': 'Test2', 'ip': '2.2.2.2', 'ssh_user': '******', } ], many=True) test_tasks = Task.Schema().load([ { '_id': ObjectId(), 'name': 'Test1', 'runtime': 1000, 'deadline': 1000, 'period': 1000 }, { '_id': ObjectId(), 'name': 'Test2', 'runtime': 1000, 'deadline': 1000, 'period': 1000 } ], many=True) def __init__(self, server_url: str, server_port: str): pass def deploy_task(self, node_key: str, task_key: str): for node in self.test_nodes: if str(node._id) == node_key: for task in self.test_tasks: if str(task._id) == task_key: return raise StatusError('Task not found.') raise StatusError('Node not found.') def remove_task(self, node_key: str, task_key: str): for node in self.test_nodes: if str(node._id) == node_key: for task in self.test_tasks: if str(task._id) == task_key: return raise StatusError('Task not found.') raise StatusError('Node not found.')
def update(self, node_key: str, new_values: dict) -> Node: for node in self.test_nodes: if str(node._id) == node_key: return Node.Schema().load(new_values) raise StatusError('Node not found.')