functiongraph创建函数
华为云搜索functiongraph然后创建函数
选择事件函数,函数运行环境选择python3.9,名称自己命名
创建完成后效果如图
这里为了避免出现明文密码,把iam用户、密码配置到环境变量中,用户可根据具体需要配置,下面为一种实现方式,index.py脚本内容如下:
import requests
import json
def handler (event, context):
# 获取token的url
url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
payload = json.dumps({
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": context.getUserData('name'),
"password": context.getUserData('password'),
"domain": {
"name": context.getUserData('domain')
}
}
}
},
"scope": {
"project": {
"name": "cn-north-4"
}
}
}
})
headers = {
'Content-Type': 'application/json'
}
res = json.dumps({
"status":"allow",
"context":{
"token":requests.request("POST", url, headers=headers, data=payload).headers.get("X-Subject-Token")
}
})
return {
"statusCode": 200,
"body": res,
"headers": {
"Content-Type": "application/json"
}
}