|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from veadk.auth.veauth.utils import get_credential_from_vefaas_iam |
| 18 | +from veadk.configs.database_configs import MSENacosConfig |
| 19 | +from veadk.utils.logger import get_logger |
| 20 | +from veadk.utils.volcengine_sign import ve_request |
| 21 | + |
| 22 | +logger = get_logger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +def get_instance_id_by_name(instance_name: str, region: str, project_name: str) -> str: |
| 26 | + logger.info(f"Fetching MSE Nacos instance id by instance name {instance_name} ...") |
| 27 | + |
| 28 | + access_key = os.getenv("VOLCENGINE_ACCESS_KEY") |
| 29 | + secret_key = os.getenv("VOLCENGINE_SECRET_KEY") |
| 30 | + session_token = "" |
| 31 | + |
| 32 | + if not (access_key and secret_key): |
| 33 | + # try to get from vefaas iam |
| 34 | + cred = get_credential_from_vefaas_iam() |
| 35 | + access_key = cred.access_key_id |
| 36 | + secret_key = cred.secret_access_key |
| 37 | + session_token = cred.session_token |
| 38 | + |
| 39 | + res = ve_request( |
| 40 | + request_body={ |
| 41 | + "Filter": {"Status": [], "ProjectName": project_name}, |
| 42 | + "PageNumber": 1, |
| 43 | + "PageSize": 10, |
| 44 | + "ProjectName": project_name, |
| 45 | + }, |
| 46 | + header={"X-Security-Token": session_token}, |
| 47 | + action="ListNacosRegistries", |
| 48 | + ak=access_key, |
| 49 | + sk=secret_key, |
| 50 | + service="mse", |
| 51 | + version="2022-01-01", |
| 52 | + region=region, |
| 53 | + host="open.volcengineapi.com", |
| 54 | + ) |
| 55 | + |
| 56 | + try: |
| 57 | + for item in res["Result"]["Items"]: |
| 58 | + if item["Name"] == instance_name: |
| 59 | + logger.info( |
| 60 | + f"Found MSE Nacos instance id {item['Id']} by instance name {instance_name}" |
| 61 | + ) |
| 62 | + return item["Id"] |
| 63 | + raise ValueError(f"Id by instance name {instance_name} not found: {res}") |
| 64 | + except Exception as e: |
| 65 | + logger.error( |
| 66 | + f"Failed to get MSE Nacos instance id by name {instance_name}: {e}, response: {res}" |
| 67 | + ) |
| 68 | + raise e |
| 69 | + |
| 70 | + |
| 71 | +def get_mse_cridential( |
| 72 | + instance_name: str, |
| 73 | + region: str = "cn-beijing", |
| 74 | + project_name: str = "default", |
| 75 | +) -> MSENacosConfig: |
| 76 | + logger.info("Fetching MSE Nacos token...") |
| 77 | + |
| 78 | + access_key = os.getenv("VOLCENGINE_ACCESS_KEY") |
| 79 | + secret_key = os.getenv("VOLCENGINE_SECRET_KEY") |
| 80 | + session_token = "" |
| 81 | + |
| 82 | + if not (access_key and secret_key): |
| 83 | + # try to get from vefaas iam |
| 84 | + cred = get_credential_from_vefaas_iam() |
| 85 | + access_key = cred.access_key_id |
| 86 | + secret_key = cred.secret_access_key |
| 87 | + session_token = cred.session_token |
| 88 | + |
| 89 | + instance_id = get_instance_id_by_name( |
| 90 | + instance_name=instance_name, region=region, project_name=project_name |
| 91 | + ) |
| 92 | + |
| 93 | + res = ve_request( |
| 94 | + request_body={ |
| 95 | + "Id": instance_id, |
| 96 | + "ProjectName": project_name, |
| 97 | + }, |
| 98 | + header={"X-Security-Token": session_token}, |
| 99 | + action="GetNacosRegistry", |
| 100 | + ak=access_key, |
| 101 | + sk=secret_key, |
| 102 | + service="mse", |
| 103 | + version="2022-01-01", |
| 104 | + region=region, |
| 105 | + host="open.volcengineapi.com", |
| 106 | + ) |
| 107 | + |
| 108 | + try: |
| 109 | + logger.info( |
| 110 | + f"Successfully fetched MSE Nacos endpoint {res['Result']['NacosRegistry']['PublicAddress']} and corresponding password." |
| 111 | + ) |
| 112 | + return MSENacosConfig( |
| 113 | + endpoint=res["Result"]["NacosRegistry"]["PublicAddress"], |
| 114 | + password=res["Result"]["NacosRegistry"]["InitialPassword"], |
| 115 | + ) |
| 116 | + except Exception as e: |
| 117 | + logger.error(f"Failed to get MSE Nacos token: {e}, response: {res}") |
| 118 | + raise e |
0 commit comments