fix(cloudid): sync qcloud collaborators user

This commit is contained in:
Qu Xuan
2020-12-01 20:34:16 +08:00
parent 760854ba91
commit f841fd85ad
3 changed files with 72 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package qcloud
import (
"fmt"
"yunion.io/x/pkg/errors"
)
func (self *SQcloudClient) ListCollaborators(offset, limit int) ([]SUser, int, error) {
if limit < 1 || limit > 50 {
limit = 50
}
params := map[string]string{
"Offset": fmt.Sprintf("%d", offset),
"Limit": fmt.Sprintf("%d", limit),
}
resp, err := self.camRequest("ListCollaborators", params)
if err != nil {
return nil, 0, errors.Wrapf(err, "ListCollaborators")
}
result := []SUser{}
err = resp.Unmarshal(&result, "Data")
if err != nil {
return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
}
total, _ := resp.Float("totalNum")
return result, int(total), nil
}
+16
View File
@@ -191,11 +191,27 @@ func (self *SQcloudClient) GetICloudusers() ([]cloudprovider.IClouduser, error)
if err != nil {
return nil, errors.Wrap(err, "ListUsers")
}
ret := []cloudprovider.IClouduser{}
for i := range users {
users[i].client = self
ret = append(ret, &users[i])
}
collaborators := []SUser{}
for {
part, total, err := self.ListCollaborators(len(collaborators), 50)
if err != nil {
return nil, errors.Wrapf(err, "ListCollaborators")
}
collaborators = append(collaborators, part...)
if len(collaborators) >= total {
break
}
}
for i := range collaborators {
collaborators[i].client = self
ret = append(ret, &collaborators[i])
}
return ret, nil
}
+14
View File
@@ -106,4 +106,18 @@ func init() {
return nil
})
type CollaboratorList struct {
Offset int
Limit int
}
shellutils.R(&CollaboratorList{}, "collaborator-list", "List collaborator", func(cli *qcloud.SRegion, args *CollaboratorList) error {
collaborators, _, err := cli.GetClient().ListCollaborators(args.Offset, args.Limit)
if err != nil {
return err
}
printList(collaborators, 0, 0, 0, nil)
return nil
})
}