def create_server_image(server_instance_id): base_auth_info = BaseAuthInfo() req_path = '/server/v2/createMemberServerImage?serverInstanceNo={0}&responseFormatType=json'. \ format(server_instance_id) base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() res_list = response.read() print(json.loads(res_list.decode('utf-8')))
def block_storage_list(): base_auth_info = BaseAuthInfo() req_path = '/server/v2/getBlockStorageInstanceList?responseFormatType=json' base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() if response.getcode() == 200: res_list = response.read() return res_list else: print("" + response)
def create_block_storage_snapshot(storage_instance_id): base_auth_info = BaseAuthInfo() req_path = '/server/v2/createBlockStorageSnapshotInstance?blockStorageInstanceNo={0}&responseFormatType=json'.\ format(storage_instance_id) base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() if response.getcode() == 200: print(">> snapshot success : " + storage_instance_id) else: print(""+response)
class SmsSender(BaseAuthInfo): auth_info = BaseAuthInfo() ep_path = auth_info.get_sens_ep_path() access_key = auth_info.get_access_key() sens_service_secrete = auth_info.get_sens_service_secrete() def req_sms(self, sms_info): context = ssl._create_unverified_context() req = urllib.request.Request(self.ep_path) req.add_header('X-NCP-auth-key', self.access_key) req.add_header('X-NCP-service-secret', self.sens_service_secrete) req.add_header('Content-Type', 'application/json') json_data = json.dumps(sms_info) json_data_bytes = json_data.encode('utf-8') response = urllib.request.urlopen(req, json_data_bytes, context=context) return response
def server_instance_list(instance_no): base_auth_info = BaseAuthInfo() req_path = '/server/v2/getServerInstanceList?serverInstanceNoList.1='+instance_no+'&responseFormatType=json' base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() if response.getcode() == 200: res_list = response.read() return res_list else: print("" + response)
def create_block_storage_snapshot(storage_instance_id): base_auth_info = BaseAuthInfo() req_path = '/vserver/v2/createBlockStorageSnapshotInstance?originalBlockStorageInstanceNo={0}&responseFormatType=json'.\ format(storage_instance_id) base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() rescode = response.status_code if rescode == 200: print("snapshot success") else: print("Error Code:" + response.text)
def stop_server(server_instance_id): base_auth_info = BaseAuthInfo() req_path = '/vserver/v2/stopServerInstances?serverInstanceNoList.1={0}&responseFormatType=json'. \ format(server_instance_id) base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() rescode = response.status_code if rescode == 200: print(response.text) else: print("Error : " + response.text)
def server_instance_list(instance_no): base_auth_info = BaseAuthInfo() print(">>>>>>>>> " + instance_no) req_path = '/vserver/v2/getServerInstanceList?serverInstanceNoList.1=' + instance_no + '&responseFormatType=json' base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() rescode = response.status_code if rescode == 200: print(response.text) instance_info = response.text return instance_info else: print("Error Code:" + rescode)
def block_storage_list(): base_auth_info = BaseAuthInfo() req_path = '/vserver/v2/getBlockStorageInstanceList?responseFormatType=json' base_auth_info.set_req_path(req_path) sender = APISender(base_auth_info) response = sender.request() rescode = response.status_code if rescode == 200: print(response.text) res_list = response.text return res_list else: print("Error Code:" + rescode)
class MailSender(BaseAuthInfo): auth_info = BaseAuthInfo() ep_path = auth_info.get_mail_ep_path() api_key = auth_info.get_api_key() access_key = auth_info.get_access_key() access_secrete = auth_info.get_access_secrete() def req_email_send(self, mail_info): context = ssl._create_unverified_context() req = urllib.request.Request(self.ep_path) timestamp = self.get_timestamp() req.add_header('x-ncp-apigw-timestamp', timestamp) req.add_header('x-ncp-iam-access-key', self.access_key) req.add_header('x-ncp-apigw-signature-v1', self.make_signature(timestamp)) req.add_header('Content-Type', 'application/json') json_data = json.dumps(mail_info) json_data_bytes = json_data.encode('utf-8') response = urllib.request.urlopen(req, json_data_bytes, context=context) return response @staticmethod def get_timestamp(): timestamp = int(time.time() * 1000) timestamp = str(timestamp) return timestamp def make_signature(self, timestamp): access_secrete_bytes = bytes(self.access_secrete, 'UTF-8') method = "POST" uri = "/api/v1/mails" message = method + " " + uri + "\n" + timestamp + "\n" + self.access_key message = bytes(message, 'UTF-8') signing_key = base64.b64encode( hmac.new(access_secrete_bytes, message, digestmod=hashlib.sha256).digest()) return signing_key