Skip to content

Commit

Permalink
improve createUser
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Aug 12, 2024
1 parent 70c76c9 commit f79a02e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
8 changes: 8 additions & 0 deletions integration/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Test_DeleteClusterWithROArequestWithJSONWithDelete(t *testing.T) {
assert.Contains(t, err.Error(), "ErrorClusterNotFound")
}

// 创建安全组
func Test_CreateSecurityGroupWithRPCrequestWithJSONWithNestingparametersWithPOST(t *testing.T) {
client, err := ecs.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand All @@ -79,6 +80,7 @@ func Test_CreateSecurityGroupWithRPCrequestWithJSONWithNestingparametersWithPOST
securityGroupId = response.SecurityGroupId
}

// 查询安全组
func Test_ECS_DescribeSecurityGroupsWithRPCrequestWithJSONWithNestingparametersWithGET(t *testing.T) {
client, err := ecs.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand All @@ -99,6 +101,7 @@ func Test_ECS_DescribeSecurityGroupsWithRPCrequestWithJSONWithNestingparametersW
flag = false
}

// 删除安全组
func Test_ECS_DeleteSecurityGroupWithRPCrequestWithJSONWithPOST(t *testing.T) {
client, err := ecs.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand All @@ -112,6 +115,7 @@ func Test_ECS_DeleteSecurityGroupWithRPCrequestWithJSONWithPOST(t *testing.T) {
securityGroupId = ""
}

// 查询 RDS
func Test_RDS_DescribeDBInstancesWithRPCrequest(t *testing.T) {
client, err := rds.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand All @@ -123,6 +127,7 @@ func Test_RDS_DescribeDBInstancesWithRPCrequest(t *testing.T) {
assert.NotNil(t, response)
}

// 查询 CDN 域名
func Test_CDN_DescribeCdnDomainDetailWithRPCrequest(t *testing.T) {
client, err := cdn.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand All @@ -134,6 +139,7 @@ func Test_CDN_DescribeCdnDomainDetailWithRPCrequest(t *testing.T) {
assert.Equal(t, 36, len(response.RequestId))
}

// 查询角色列表
func Test_RAM_ListRolesWithRPCrequest(t *testing.T) {
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand All @@ -145,6 +151,7 @@ func Test_RAM_ListRolesWithRPCrequest(t *testing.T) {
assert.Equal(t, 36, len(response.RequestId))
}

// 查询 slb regions
func Test_SLB_DescribeRegionsWithRPCrequest(t *testing.T) {
client, err := slb.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand All @@ -156,6 +163,7 @@ func Test_SLB_DescribeRegionsWithRPCrequest(t *testing.T) {
assert.True(t, len(response.Regions.Region) > 0)
}

// 查询 vpc regions
func Test_VPC_DescribeRegionsWithRPCrequest(t *testing.T) {
client, err := vpc.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
assert.Nil(t, err)
Expand Down
52 changes: 35 additions & 17 deletions integration/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"

Expand Down Expand Up @@ -32,10 +33,14 @@ var (

var ecsEndpoint = "ecs." + os.Getenv("REGION_ID") + ".aliyuncs.com"

func newRamClient() (*ram.Client, error) {
return ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
}

func createRole(userid string) (name string, arn string, err error) {
listRequest := ram.CreateListRolesRequest()
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
client, err := newRamClient()
if err != nil {
return
}
Expand Down Expand Up @@ -63,34 +68,47 @@ func createRole(userid string) (name string, arn string, err error) {
return
}

func createUser() (err error) {
listRequest := ram.CreateListUsersRequest()
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
func createUser() (response *ram.CreateUserResponse, err error) {
client, err := newRamClient()
if err != nil {
return
}
listResponse, err := client.ListUsers(listRequest)
createRequest := ram.CreateCreateUserRequest()
createRequest.Scheme = "HTTPS"
createRequest.UserName = username
return client.CreateUser(createRequest)
}

func ensureUser() (err error) {
client, err := newRamClient()
if err != nil {
return
}
for _, user := range listResponse.Users.User {
if user.UserName == username {
return

// 查询用户
getUserRequest := ram.CreateGetUserRequest()
getUserRequest.UserName = username
getUserRequest.Scheme = "HTTPS"
_, err = client.GetUser(getUserRequest)
if err != nil {
if se, ok := err.(*errors.ServerError); ok {
if se.ErrorCode() == "EntityNotExist.User" {
// 如果用户不存在,则创建
_, err = createUser()
return
}
}
return
}
createRequest := ram.CreateCreateUserRequest()
createRequest.Scheme = "HTTPS"
createRequest.UserName = username
_, err = client.CreateUser(createRequest)

return
}

func createAttachPolicyToUser() error {
listRequest := ram.CreateListPoliciesForUserRequest()
listRequest.UserName = username
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
client, err := newRamClient()
if err != nil {
return err
}
Expand Down Expand Up @@ -119,7 +137,7 @@ func createAttachPolicyToRole() error {
listRequest := ram.CreateListPoliciesForRoleRequest()
listRequest.RoleName = rolename
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
client, err := newRamClient()
if err != nil {
return err
}
Expand All @@ -145,7 +163,7 @@ func createAttachPolicyToRole() error {
}

func createAccessKey() (id string, secret string, err error) {
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
client, err := newRamClient()
if err != nil {
return
}
Expand Down Expand Up @@ -183,7 +201,7 @@ func createAccessKey() (id string, secret string, err error) {
}

func createAssumeRole() (response *sts.AssumeRoleResponse, err error) {
err = createUser()
err = ensureUser()
if err != nil {
return
}
Expand Down

0 comments on commit f79a02e

Please sign in to comment.