- 목표 : 주말마다 server 끄기!
따라서 트리거는 cron으로 설정했다.
{
"server_instances":[19418816],
"api_url":"/vserver/v2/stopServerInstances",
"access_key":"히히 액세스키!!",
"base_url":"https://ncloud.apigw.ntruss.com",
"REGION":"KR",
"secret_key":"히히 시크릿키!"
}
▲ 이전에 생성한 트리거를 선택한다.
▲ 이전에 생성한 패키지를 선택하고 코드 템플릿을 정한다.
▲ 목표는 인스턴스 켜고 끄기! 'Ncloud Server 인스턴스 관리' 를 선택한다.
import hashlib
import hmac
import base64
import requests
import time
# Signature is the value of `x-ncp-apigw-signature-v2` field in the header.
def make_signature(url, timestamp, access_key, secret_key):
timestamp = int(time.time() * 1000)
timestamp = str(timestamp)
secret_key = bytes(secret_key, "UTF-8")
method = "GET"
message = method + " " + url + "\n" + timestamp + "\n" + access_key
message = bytes(message, "UTF-8")
sign_key = base64.b64encode(
hmac.new(secret_key, message, digestmod=hashlib.sha256).digest()
)
print(sign_key)
return sign_key
# Header is required for authentication to call NAVER Cloud Platform API.
def make_header(timestamp, access_key, sign_key):
headers = {
"Content-Type": "application/json; charset=utf-8",
"x-ncp-apigw-timestamp": timestamp,
"x-ncp-iam-access-key": access_key,
"x-ncp-apigw-signature-v2": sign_key,
}
return headers
"""
Action to start(or stop) server(VPC) instances using Server API
Input parameters that must be defined as action parameters
args:
access_key (str): NAVER Cloud Platform account access key used for API authentication
secret_key (str): NAVER Cloud Platform account secret key used for API authentication
base_url (str): https://ncloud.apigw.ntruss.com
api_url (str): `/vserver/v2/startServerInstances` or `/vserver/v2/stopServerInstances`
server_instances (list): list of instances' Id to start or stop (e.g. [11111111, 22222222])
Please refer to the Server API guide for the accurate `base_url` and `api_url`.
"""
def main(args): # control vpc server instance
api_url_with_params = args["api_url"]
if len(args["server_instances"]) > 0:
api_url_with_params += f'?serverInstanceNoList.1={args["server_instances"][0]}'
for i in range(1, len(args["server_instances"])):
api_url_with_params += (
f'&serverInstanceNoList.{i+1}={args["server_instances"][i]}'
)
url = f'{args["base_url"]}{api_url_with_params}'
timestamp = str(int(time.time() * 1000))
sign_key = make_signature(
api_url_with_params, timestamp, args["access_key"], args["secret_key"]
)
headers = make_header(timestamp, args["access_key"], sign_key)
try:
res = requests.get(url, headers=headers)
if res.status_code == 200:
return {"done": True}
else:
raise Exception({"done": False, "error_message": res.text})
except Exception as e:
raise Exception({"done": False, "error_message": str(e)})
"""
Action to start(or stop) server(VPC) instances using Server API
Input parameters that must be defined as action parameters
args:
access_key (str): NAVER Cloud Platform account access key used for API authentication
secret_key (str): NAVER Cloud Platform account secret key used for API authentication
base_url (str): https://ncloud.apigw.ntruss.com
api_url (str): `/vserver/v2/startServerInstances` or `/vserver/v2/stopServerInstances`
server_instances (list): list of instances' Id to start or stop (e.g. [11111111, 22222222])
Please refer to the Server API guide for the accurate `base_url` and `api_url`.
"""
위 변수가 사용될 예정이며, 위 변수는 이전에 패키지 생성할 때, 정의해주었다.
▼ 이렇게!
{
"server_instances":[19418816],
"api_url":"/vserver/v2/stopServerInstances",
"access_key":"히히 액세스키!!",
"base_url":"https://ncloud.apigw.ntruss.com",
"REGION":"KR",
"secret_key":"히히 시크릿키!"
}
3순위 디폴트 파라미터에 넣으면 해당 액션에만 들어간다.
패키지 만들 때, 넣은 파라미터는, 우선순위에 따라 적용 여부가 갈린다.
자 이제 매일 밤마다 서버가 꺼질거야. 켜는건 API_URI를 start로 바꿔주면된다.("api_url":"/vserver/v2/startServerInstances")
안녕하세요, 네이버 클라우드 플랫폼입니다.
네이버클라우드의 기술 콘텐츠 리워드 프로그램 '이달의 Nclouder(9월)' 도전자로 초대합니다 :)
네이버 클라우드 플랫폼 서비스와 관련된 모든 주제로 10/6(금)까지 신청 가능합니다. (*9월 작성 콘텐츠 한정 신청 가능)
Ncloud 크레딧을 포함한 다양한 리워드가 준비되어 있으니 많은 관심 부탁드립니다!
*자세한 내용은 아래 링크에서 확인부탁드립니다.
https://blog.naver.com/n_cloudplatform/223203012370
*신청 링크
https://navercloud.typeform.com/to/lF8NUaCF