|
| 1 | +package cloudsso |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/aliyun/aliyun-cli/v3/cli" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | +) |
| 11 | + |
| 12 | +// AccessConfigurationsParameter is a struct that holds the parameters for accessing configurations. |
| 13 | +type AccessConfigurationsParameter struct { |
| 14 | + UrlPrefix string `json:"urlPrefix"` |
| 15 | + AccessToken string `json:"accessToken"` |
| 16 | + AccountId string `json:"accountId"` |
| 17 | + HttpClient *http.Client `json:"-"` |
| 18 | +} |
| 19 | + |
| 20 | +// AccessConfigurationsRequest 表示获取访问配置的请求参数 |
| 21 | +type AccessConfigurationsRequest struct { |
| 22 | + AccountId string |
| 23 | + NextToken string |
| 24 | + MaxResults int |
| 25 | +} |
| 26 | + |
| 27 | +// AccessConfigurationsResponse 表示访问配置的响应 |
| 28 | +type AccessConfigurationsResponse struct { |
| 29 | + AccessConfigurationsForAccount []AccessConfiguration `json:"AccessConfigurationsForAccount"` |
| 30 | + NextToken string `json:"NextToken"` |
| 31 | + IsTruncated bool `json:"IsTruncated"` |
| 32 | +} |
| 33 | + |
| 34 | +// AccessConfiguration 表示单个访问配置 |
| 35 | +type AccessConfiguration struct { |
| 36 | + AccessConfigurationId string `json:"AccessConfigurationId"` |
| 37 | + AccessConfigurationName string `json:"AccessConfigurationName"` |
| 38 | + AccessConfigurationDescription string `json:"AccessConfigurationDescription"` |
| 39 | +} |
| 40 | + |
| 41 | +// ListAccessConfigurationsForAccount 获取单次访问配置列表 |
| 42 | +func (p *AccessConfigurationsParameter) ListAccessConfigurationsForAccount(req AccessConfigurationsRequest) (*AccessConfigurationsResponse, error) { |
| 43 | + // 构建URL |
| 44 | + url := fmt.Sprintf("%s/access-assignments/access-configurations", p.UrlPrefix) |
| 45 | + |
| 46 | + // 添加查询参数 |
| 47 | + query := url + "?AccountId=" + req.AccountId |
| 48 | + if req.NextToken != "" { |
| 49 | + query += "&NextToken=" + req.NextToken |
| 50 | + } |
| 51 | + if req.MaxResults > 0 { |
| 52 | + query += "&MaxResults=" + strconv.Itoa(req.MaxResults) |
| 53 | + } |
| 54 | + |
| 55 | + // 创建HTTP请求 |
| 56 | + httpReq, err := http.NewRequest("GET", query, nil) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + // 设置请求头 |
| 62 | + httpReq.Header.Set("Accept", "application/json") |
| 63 | + httpReq.Header.Set("Content-Type", "application/json") |
| 64 | + httpReq.Header.Set("Authorization", "Bearer "+p.AccessToken) |
| 65 | + httpReq.Header.Set("User-Agent", "aliyun/CLI-"+cli.Version) |
| 66 | + |
| 67 | + // 发送请求 |
| 68 | + client := p.HttpClient |
| 69 | + if client == nil { |
| 70 | + client = http.DefaultClient |
| 71 | + } |
| 72 | + |
| 73 | + resp, err := client.Do(httpReq) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + defer resp.Body.Close() |
| 78 | + |
| 79 | + // 读取响应体 |
| 80 | + body, err := io.ReadAll(resp.Body) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + // 检查错误 |
| 86 | + if resp.StatusCode >= 400 && resp.StatusCode < 500 { |
| 87 | + var errResp struct { |
| 88 | + ErrorCode string `json:"ErrorCode"` |
| 89 | + ErrorMessage string `json:"ErrorMessage"` |
| 90 | + RequestId string `json:"RequestId"` |
| 91 | + } |
| 92 | + |
| 93 | + if err := json.Unmarshal(body, &errResp); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + return nil, fmt.Errorf("%s: %s %s", errResp.ErrorCode, errResp.ErrorMessage, errResp.RequestId) |
| 98 | + } |
| 99 | + |
| 100 | + // 解析响应 |
| 101 | + var result AccessConfigurationsResponse |
| 102 | + if err := json.Unmarshal(body, &result); err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + return &result, nil |
| 107 | +} |
| 108 | + |
| 109 | +// ListAllAccessConfigurations 获取所有访问配置列表 |
| 110 | +func (p *AccessConfigurationsParameter) ListAllAccessConfigurations(req AccessConfigurationsRequest) ([]AccessConfiguration, error) { |
| 111 | + var configurations []AccessConfiguration |
| 112 | + |
| 113 | + // 获取第一页数据 |
| 114 | + response, err := p.ListAccessConfigurationsForAccount(req) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + configurations = append(configurations, response.AccessConfigurationsForAccount...) |
| 120 | + |
| 121 | + // 如果有更多页,继续请求 |
| 122 | + for response.IsTruncated { |
| 123 | + req.NextToken = response.NextToken |
| 124 | + |
| 125 | + response, err = p.ListAccessConfigurationsForAccount(req) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + configurations = append(configurations, response.AccessConfigurationsForAccount...) |
| 131 | + } |
| 132 | + |
| 133 | + return configurations, nil |
| 134 | +} |
0 commit comments