增加斯耀安全组处理
This commit is contained in:
103
alipay/sy_update_ali_ip.go
Normal file
103
alipay/sy_update_ali_ip.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package alipay
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client"
|
||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
"github.com/google/uuid"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 用户登录名称 ecs-ip@1882833282563718.onaliyun.com
|
||||
// 登录密码 ETVuwaiCHBKP36$jvdxkp7&!)Zt4JcWL
|
||||
// AccessKey ID LTAI5tQCWkqiinoXLpC5q5ZG
|
||||
// AccessKey Secret bp6V7H7pkB0bJygJkVVxhlm2i7RiqD
|
||||
var (
|
||||
_syClient *ecs20140526.Client
|
||||
SyRegionId = "cn-nanjing"
|
||||
SyAccessKeyId = "LTAI5tQCWkqiinoXLpC5q5ZG"
|
||||
SyAccessKeySecret = "bp6V7H7pkB0bJygJkVVxhlm2i7RiqD"
|
||||
SyEndPoint = "ecs.cn-nanjing.aliyuncs.com"
|
||||
//SyEndPoint = "ecs-cn-hangzhou.aliyuncs.com"
|
||||
)
|
||||
|
||||
func CreateSyClient() (_result *ecs20140526.Client, _err error) {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
|
||||
config := &openapi.Config{
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
AccessKeyId: tea.String(SyAccessKeyId),
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
AccessKeySecret: tea.String(SyAccessKeySecret),
|
||||
}
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.Endpoint = tea.String(SyEndPoint)
|
||||
_result = &ecs20140526.Client{}
|
||||
_result, _err = ecs20140526.NewClient(config)
|
||||
return _result, _err
|
||||
}
|
||||
|
||||
func UpdateSyAliIp(groupId, ruleId, ip string) (_err error) {
|
||||
if _syClient == nil {
|
||||
client, e := CreateSyClient()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
_syClient = client
|
||||
}
|
||||
|
||||
newUUID, _ := uuid.NewUUID()
|
||||
modifySecurityGroupRuleRequest := &ecs20140526.ModifySecurityGroupRuleRequest{
|
||||
RegionId: tea.String(SyRegionId),
|
||||
ClientToken: tea.String(newUUID.String()),
|
||||
SecurityGroupId: tea.String(groupId),
|
||||
SecurityGroupRuleId: tea.String(ruleId),
|
||||
SourceCidrIp: tea.String(ip),
|
||||
}
|
||||
runtime := &util.RuntimeOptions{}
|
||||
tryErr := func() (_e error) {
|
||||
defer func() {
|
||||
if r := tea.Recover(recover()); r != nil {
|
||||
_e = r
|
||||
}
|
||||
}()
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
_, _err = _syClient.ModifySecurityGroupRuleWithOptions(modifySecurityGroupRuleRequest, runtime)
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
|
||||
return nil
|
||||
}()
|
||||
|
||||
if tryErr != nil {
|
||||
var err = &tea.SDKError{}
|
||||
var _t *tea.SDKError
|
||||
if errors.As(tryErr, &_t) {
|
||||
err = _t
|
||||
}
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
fmt.Println(tea.StringValue(err.Message))
|
||||
// 诊断地址
|
||||
var data interface{}
|
||||
d := json.NewDecoder(strings.NewReader(tea.StringValue(err.Data)))
|
||||
e := d.Decode(&data)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if m, ok := data.(map[string]interface{}); ok {
|
||||
recommend, _ := m["Recommend"]
|
||||
fmt.Println(recommend)
|
||||
}
|
||||
_, _err = util.AssertAsString(err.Message)
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
}
|
||||
return _err
|
||||
}
|
||||
@@ -17,6 +17,7 @@ var (
|
||||
RegionId = "cn-hangzhou"
|
||||
AccessKeyId = "LTAI5tS5pSkUwm6VURuaetxs"
|
||||
AccessKeySecret = "iQFIO4MFa7tZ7HbHs0OXOaMDvMozM5"
|
||||
EndPoint = "ecs-cn-hangzhou.aliyuncs.com"
|
||||
)
|
||||
|
||||
func CreateClient() (_result *ecs20140526.Client, _err error) {
|
||||
@@ -29,7 +30,7 @@ func CreateClient() (_result *ecs20140526.Client, _err error) {
|
||||
AccessKeySecret: tea.String(AccessKeySecret),
|
||||
}
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.Endpoint = tea.String("ecs-cn-hangzhou.aliyuncs.com")
|
||||
config.Endpoint = tea.String(EndPoint)
|
||||
_result = &ecs20140526.Client{}
|
||||
_result, _err = ecs20140526.NewClient(config)
|
||||
return _result, _err
|
||||
|
||||
Reference in New Issue
Block a user