Created
April 19, 2024 10:54
-
-
Save teer823/a27a6a5cbbe15a914bf6e763b88f5e94 to your computer and use it in GitHub Desktop.
FunctionGraph code to use Huaweicloudsdkcce v3 to hibernate or awake cluster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# This code require huaweicloudsdkcce, you may need to build dependency (layer) | |
# https://support.huaweicloud.com/intl/en-us/functiongraph_faq/functiongraph_03_0343.html#section1 | |
import json | |
import os | |
from huaweicloudsdkcore.auth.credentials import BasicCredentials | |
from huaweicloudsdkcce.v3.region.cce_region import CceRegion | |
from huaweicloudsdkcore.exceptions import exceptions | |
from huaweicloudsdkcce.v3 import * | |
def hibernate_cluster(client, cluster_id): | |
print('hibernate cluster id = ', cluster_id) | |
request = HibernateClusterRequest() | |
request.cluster_id = cluster_id | |
response = client.hibernate_cluster(request) | |
return response | |
def awake_cluster(client, cluster_id): | |
print('awake cluster id = ', cluster_id) | |
request = AwakeClusterRequest() | |
request.cluster_id = cluster_id | |
response = client.awake_cluster(request) | |
return response | |
def handler (event, context): | |
print(event) | |
ak = context.getAccessKey() | |
sk = context.getSecretKey() | |
credentials = BasicCredentials(ak, sk) \ | |
client = CceClient.new_builder() \ | |
.with_credentials(credentials) \ | |
.with_region(CceRegion.value_of("ap-southeast-2")) \ | |
.build() | |
try: | |
# Get Cluster ID from environment variable - feel free to change to get from event | |
cluster_id = os.getenv('cluster_id') | |
# Change which function to call awake or hibernate | |
response = awake_cluster(client, cluster_id) | |
print(response) | |
except exceptions.ClientRequestException as e: | |
print(e.status_code) | |
print(e.request_id) | |
print(e.error_code) | |
print(e.error_msg) | |
return { | |
"statusCode": 200, | |
"isBase64Encoded": False, | |
"body": json.dumps(event), | |
"headers": { | |
"Content-Type": "application/json" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment