diff --git a/portal_client.thrift b/portal_client.thrift index 4dc506e..830cec9 100644 --- a/portal_client.thrift +++ b/portal_client.thrift @@ -587,7 +587,9 @@ service VirtualMachineService { mapget_cluster_status(1:string cluster_id) throws(1:ClusterNotFoundException c) + string get_keypair_public_key_by_name(1:string key_name) + void delete_keypair(1:string key_name) /** diff --git a/simple_vm_client/VirtualMachineHandler.py b/simple_vm_client/VirtualMachineHandler.py index 7b05f1b..2f3712c 100644 --- a/simple_vm_client/VirtualMachineHandler.py +++ b/simple_vm_client/VirtualMachineHandler.py @@ -99,6 +99,14 @@ def resize_volume(self, volume_id: str, size: int) -> None: def get_gateway_ip(self) -> dict[str, str]: return self.openstack_connector.get_gateway_ip() + def get_keypair_public_key_by_name(self, key_name: str): + return self.openstack_connector.get_keypair_public_key_by_name( + key_name=key_name + ) + + def delete_keypair(self, key_name: str): + return self.openstack_connector.delete_keypair(key_name=key_name) + def get_calculation_values(self) -> dict[str, str]: val = self.openstack_connector.get_calculation_values() return val @@ -419,7 +427,6 @@ def create_and_deploy_playbook( ) gateway_ip = self.openstack_connector.get_gateway_ip()["gateway_ip"] if self.openstack_connector.netcat(host=gateway_ip, port=port): - cloud_site = self.openstack_connector.CLOUD_SITE return self.forc_connector.create_and_deploy_playbook( public_key=public_key, diff --git a/simple_vm_client/VirtualMachineService-remote b/simple_vm_client/VirtualMachineService-remote index 68bb76c..7189003 100755 --- a/simple_vm_client/VirtualMachineService-remote +++ b/simple_vm_client/VirtualMachineService-remote @@ -90,6 +90,8 @@ if len(sys.argv) <= 1 or sys.argv[1] == "--help": ) print(" ClusterInfo get_cluster_info(string cluster_id)") print(" get_cluster_status(string cluster_id)") + print(" string get_keypair_public_key_by_name(string key_name)") + print(" void delete_keypair(string key_name)") print(" VM get_server(string openstack_id)") print(" void stop_server(string openstack_id)") print( @@ -664,6 +666,26 @@ elif cmd == "get_cluster_status": ) ) +elif cmd == "get_keypair_public_key_by_name": + if len(args) != 1: + print("get_keypair_public_key_by_name requires 1 args") + sys.exit(1) + pp.pprint( + client.get_keypair_public_key_by_name( + args[0], + ) + ) + +elif cmd == "delete_keypair": + if len(args) != 1: + print("delete_keypair requires 1 args") + sys.exit(1) + pp.pprint( + client.delete_keypair( + args[0], + ) + ) + elif cmd == "get_server": if len(args) != 1: print("get_server requires 1 args") diff --git a/simple_vm_client/VirtualMachineService.py b/simple_vm_client/VirtualMachineService.py index c6c7af2..e21b6b9 100644 --- a/simple_vm_client/VirtualMachineService.py +++ b/simple_vm_client/VirtualMachineService.py @@ -474,6 +474,20 @@ def get_cluster_status(self, cluster_id): """ + def get_keypair_public_key_by_name(self, key_name): + """ + Parameters: + - key_name + + """ + + def delete_keypair(self, key_name): + """ + Parameters: + - key_name + + """ + def get_server(self, openstack_id): """ Get a Server. @@ -2633,6 +2647,73 @@ def recv_get_cluster_status(self): "get_cluster_status failed: unknown result", ) + def get_keypair_public_key_by_name(self, key_name): + """ + Parameters: + - key_name + + """ + self.send_get_keypair_public_key_by_name(key_name) + return self.recv_get_keypair_public_key_by_name() + + def send_get_keypair_public_key_by_name(self, key_name): + self._oprot.writeMessageBegin( + "get_keypair_public_key_by_name", TMessageType.CALL, self._seqid + ) + args = get_keypair_public_key_by_name_args() + args.key_name = key_name + args.write(self._oprot) + self._oprot.writeMessageEnd() + self._oprot.trans.flush() + + def recv_get_keypair_public_key_by_name(self): + iprot = self._iprot + (fname, mtype, rseqid) = iprot.readMessageBegin() + if mtype == TMessageType.EXCEPTION: + x = TApplicationException() + x.read(iprot) + iprot.readMessageEnd() + raise x + result = get_keypair_public_key_by_name_result() + result.read(iprot) + iprot.readMessageEnd() + if result.success is not None: + return result.success + raise TApplicationException( + TApplicationException.MISSING_RESULT, + "get_keypair_public_key_by_name failed: unknown result", + ) + + def delete_keypair(self, key_name): + """ + Parameters: + - key_name + + """ + self.send_delete_keypair(key_name) + self.recv_delete_keypair() + + def send_delete_keypair(self, key_name): + self._oprot.writeMessageBegin("delete_keypair", TMessageType.CALL, self._seqid) + args = delete_keypair_args() + args.key_name = key_name + args.write(self._oprot) + self._oprot.writeMessageEnd() + self._oprot.trans.flush() + + def recv_delete_keypair(self): + iprot = self._iprot + (fname, mtype, rseqid) = iprot.readMessageBegin() + if mtype == TMessageType.EXCEPTION: + x = TApplicationException() + x.read(iprot) + iprot.readMessageEnd() + raise x + result = delete_keypair_result() + result.read(iprot) + iprot.readMessageEnd() + return + def get_server(self, openstack_id): """ Get a Server. @@ -3498,6 +3579,10 @@ def __init__(self, handler): self._processMap["add_cluster_machine"] = Processor.process_add_cluster_machine self._processMap["get_cluster_info"] = Processor.process_get_cluster_info self._processMap["get_cluster_status"] = Processor.process_get_cluster_status + self._processMap["get_keypair_public_key_by_name"] = ( + Processor.process_get_keypair_public_key_by_name + ) + self._processMap["delete_keypair"] = Processor.process_delete_keypair self._processMap["get_server"] = Processor.process_get_server self._processMap["stop_server"] = Processor.process_stop_server self._processMap["create_snapshot"] = Processor.process_create_snapshot @@ -4894,6 +4979,56 @@ def process_get_cluster_status(self, seqid, iprot, oprot): oprot.writeMessageEnd() oprot.trans.flush() + def process_get_keypair_public_key_by_name(self, seqid, iprot, oprot): + args = get_keypair_public_key_by_name_args() + args.read(iprot) + iprot.readMessageEnd() + result = get_keypair_public_key_by_name_result() + try: + result.success = self._handler.get_keypair_public_key_by_name(args.key_name) + msg_type = TMessageType.REPLY + except TTransport.TTransportException: + raise + except TApplicationException as ex: + logging.exception("TApplication exception in handler") + msg_type = TMessageType.EXCEPTION + result = ex + except Exception: + logging.exception("Unexpected exception in handler") + msg_type = TMessageType.EXCEPTION + result = TApplicationException( + TApplicationException.INTERNAL_ERROR, "Internal error" + ) + oprot.writeMessageBegin("get_keypair_public_key_by_name", msg_type, seqid) + result.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + + def process_delete_keypair(self, seqid, iprot, oprot): + args = delete_keypair_args() + args.read(iprot) + iprot.readMessageEnd() + result = delete_keypair_result() + try: + self._handler.delete_keypair(args.key_name) + msg_type = TMessageType.REPLY + except TTransport.TTransportException: + raise + except TApplicationException as ex: + logging.exception("TApplication exception in handler") + msg_type = TMessageType.EXCEPTION + result = ex + except Exception: + logging.exception("Unexpected exception in handler") + msg_type = TMessageType.EXCEPTION + result = TApplicationException( + TApplicationException.INTERNAL_ERROR, "Internal error" + ) + oprot.writeMessageBegin("delete_keypair", msg_type, seqid) + result.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + def process_get_server(self, seqid, iprot, oprot): args = get_server_args() args.read(iprot) @@ -14862,6 +14997,308 @@ def __ne__(self, other): ) +class get_keypair_public_key_by_name_args(object): + """ + Attributes: + - key_name + + """ + + def __init__( + self, + key_name=None, + ): + self.key_name = key_name + + def read(self, iprot): + if ( + iprot._fast_decode is not None + and isinstance(iprot.trans, TTransport.CReadableTransport) + and self.thrift_spec is not None + ): + iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 1: + if ftype == TType.STRING: + self.key_name = ( + iprot.readString().decode("utf-8", errors="replace") + if sys.version_info[0] == 2 + else iprot.readString() + ) + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write( + oprot._fast_encode(self, [self.__class__, self.thrift_spec]) + ) + return + oprot.writeStructBegin("get_keypair_public_key_by_name_args") + if self.key_name is not None: + oprot.writeFieldBegin("key_name", TType.STRING, 1) + oprot.writeString( + self.key_name.encode("utf-8") + if sys.version_info[0] == 2 + else self.key_name + ) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ["%s=%r" % (key, value) for key, value in self.__dict__.items()] + return "%s(%s)" % (self.__class__.__name__, ", ".join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + + +all_structs.append(get_keypair_public_key_by_name_args) +get_keypair_public_key_by_name_args.thrift_spec = ( + None, # 0 + ( + 1, + TType.STRING, + "key_name", + "UTF8", + None, + ), # 1 +) + + +class get_keypair_public_key_by_name_result(object): + """ + Attributes: + - success + + """ + + def __init__( + self, + success=None, + ): + self.success = success + + def read(self, iprot): + if ( + iprot._fast_decode is not None + and isinstance(iprot.trans, TTransport.CReadableTransport) + and self.thrift_spec is not None + ): + iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 0: + if ftype == TType.STRING: + self.success = ( + iprot.readString().decode("utf-8", errors="replace") + if sys.version_info[0] == 2 + else iprot.readString() + ) + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write( + oprot._fast_encode(self, [self.__class__, self.thrift_spec]) + ) + return + oprot.writeStructBegin("get_keypair_public_key_by_name_result") + if self.success is not None: + oprot.writeFieldBegin("success", TType.STRING, 0) + oprot.writeString( + self.success.encode("utf-8") + if sys.version_info[0] == 2 + else self.success + ) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ["%s=%r" % (key, value) for key, value in self.__dict__.items()] + return "%s(%s)" % (self.__class__.__name__, ", ".join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + + +all_structs.append(get_keypair_public_key_by_name_result) +get_keypair_public_key_by_name_result.thrift_spec = ( + ( + 0, + TType.STRING, + "success", + "UTF8", + None, + ), # 0 +) + + +class delete_keypair_args(object): + """ + Attributes: + - key_name + + """ + + def __init__( + self, + key_name=None, + ): + self.key_name = key_name + + def read(self, iprot): + if ( + iprot._fast_decode is not None + and isinstance(iprot.trans, TTransport.CReadableTransport) + and self.thrift_spec is not None + ): + iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 1: + if ftype == TType.STRING: + self.key_name = ( + iprot.readString().decode("utf-8", errors="replace") + if sys.version_info[0] == 2 + else iprot.readString() + ) + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write( + oprot._fast_encode(self, [self.__class__, self.thrift_spec]) + ) + return + oprot.writeStructBegin("delete_keypair_args") + if self.key_name is not None: + oprot.writeFieldBegin("key_name", TType.STRING, 1) + oprot.writeString( + self.key_name.encode("utf-8") + if sys.version_info[0] == 2 + else self.key_name + ) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ["%s=%r" % (key, value) for key, value in self.__dict__.items()] + return "%s(%s)" % (self.__class__.__name__, ", ".join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + + +all_structs.append(delete_keypair_args) +delete_keypair_args.thrift_spec = ( + None, # 0 + ( + 1, + TType.STRING, + "key_name", + "UTF8", + None, + ), # 1 +) + + +class delete_keypair_result(object): + + def read(self, iprot): + if ( + iprot._fast_decode is not None + and isinstance(iprot.trans, TTransport.CReadableTransport) + and self.thrift_spec is not None + ): + iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write( + oprot._fast_encode(self, [self.__class__, self.thrift_spec]) + ) + return + oprot.writeStructBegin("delete_keypair_result") + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ["%s=%r" % (key, value) for key, value in self.__dict__.items()] + return "%s(%s)" % (self.__class__.__name__, ", ".join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + + +all_structs.append(delete_keypair_result) +delete_keypair_result.thrift_spec = () + + class get_server_args(object): """ Attributes: diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index 7f7b07d..85084c7 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -428,6 +428,14 @@ def import_keypair(self, keyname: str, public_key: str) -> dict[str, str]: else: return keypair + def get_keypair_public_key_by_name(self, key_name: str): + logger.info(f"Get keypair: {key_name}") + + key_pair: Keypair = self.openstack_connection.compute.find_keypair(key_name) + if key_pair: + return key_pair.public_key + return "" + def delete_keypair(self, key_name: str) -> None: logger.info(f"Delete keypair: {key_name}")