コード例 #1
0
    def get_ssh_key_list(self, vps_isp_id: int) -> List[dict]:
        request = DescribeKeyPairsRequest.DescribeKeyPairsRequest()
        request.set_PageSize(50)
        response = self.handle_request(request)
        ssh_key_list = response["KeyPairs"].get("KeyPair", [])

        ssh_key_list = [
            dict(
                fingerprint=key_pair["KeyPairFingerPrint"],
                date_created=key_pair["CreationTime"],
                name=key_pair["KeyPairName"],
                isp_id=vps_isp_id,
            ) for key_pair in ssh_key_list
        ]

        return ssh_key_list
コード例 #2
0
    def get_or_create_ssh_key_by_public_key(self,
                                            public_key: str,
                                            region_id: str = None) -> str:
        public_key_md5 = gen_ssh_key_fingerprint(public_key).replace(":", "")
        request = DescribeKeyPairsRequest.DescribeKeyPairsRequest()
        request.set_KeyPairFingerPrint(public_key_md5)
        response = self.handle_request(request, region_id=region_id)
        ssh_keys = response["KeyPairs"].get("KeyPair", [])

        ssh_key_name = None

        if ssh_keys:
            ssh_key_name = ssh_keys[0]["KeyPairName"]

        if not ssh_key_name:
            now_time = datetime.now()
            unix_timestamp = f"luwu_{int(now_time.utcnow().timestamp())}"
            ssh_key_name = self.create_ssh_key(unix_timestamp, public_key)

        return ssh_key_name
コード例 #3
0
 def describe_keypairs(self):
     request = DescribeKeyPairsRequest.DescribeKeyPairsRequest()
     key_list = ["KeyPairName", "RegionId"]
     request = self._add_params(request, key_list, self.vm_params)
     return self._send_request(request)