| Conditions | 14 |
| Total Lines | 69 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like providers.*ECSRAMRoleCredentialsProvider.getCredentials often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package providers |
||
| 142 | func (provider *ECSRAMRoleCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { |
||
| 143 | roleName := provider.roleName |
||
| 144 | if roleName == "" { |
||
| 145 | roleName, err = provider.getRoleName() |
||
| 146 | if err != nil { |
||
| 147 | return |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | requestUrl := securityCredURL + roleName |
||
| 152 | httpRequest, err := hookNewRequest(http.NewRequest)("GET", requestUrl, strings.NewReader("")) |
||
| 153 | if err != nil { |
||
| 154 | err = fmt.Errorf("refresh Ecs sts token err: %s", err.Error()) |
||
| 155 | return |
||
| 156 | } |
||
| 157 | |||
| 158 | if provider.enableIMDSv2 { |
||
| 159 | metadataToken, err := provider.getMetadataToken() |
||
| 160 | if err != nil { |
||
| 161 | return nil, err |
||
| 162 | } |
||
| 163 | httpRequest.Header.Set("x-aliyun-ecs-metadata-token", metadataToken) |
||
| 164 | } |
||
| 165 | |||
| 166 | httpClient := &http.Client{ |
||
| 167 | Timeout: 5 * time.Second, |
||
| 168 | } |
||
| 169 | httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
||
| 170 | if err != nil { |
||
| 171 | err = fmt.Errorf("refresh Ecs sts token err: %s", err.Error()) |
||
| 172 | return |
||
| 173 | } |
||
| 174 | |||
| 175 | defer httpResponse.Body.Close() |
||
| 176 | |||
| 177 | responseBody, err := ioutil.ReadAll(httpResponse.Body) |
||
| 178 | if err != nil { |
||
| 179 | return |
||
| 180 | } |
||
| 181 | |||
| 182 | if httpResponse.StatusCode != http.StatusOK { |
||
| 183 | err = fmt.Errorf("refresh Ecs sts token err, httpStatus: %d, message = %s", httpResponse.StatusCode, string(responseBody)) |
||
| 184 | return |
||
| 185 | } |
||
| 186 | |||
| 187 | var data ecsRAMRoleResponse |
||
| 188 | err = json.Unmarshal(responseBody, &data) |
||
| 189 | if err != nil { |
||
| 190 | err = fmt.Errorf("refresh Ecs sts token err, json.Unmarshal fail: %s", err.Error()) |
||
| 191 | return |
||
| 192 | } |
||
| 193 | |||
| 194 | if data.AccessKeyId == nil || data.AccessKeySecret == nil || data.SecurityToken == nil { |
||
| 195 | err = fmt.Errorf("refresh Ecs sts token err, fail to get credentials") |
||
| 196 | return |
||
| 197 | } |
||
| 198 | |||
| 199 | if *data.Code != "Success" { |
||
| 200 | err = fmt.Errorf("refresh Ecs sts token err, Code is not Success") |
||
| 201 | return |
||
| 202 | } |
||
| 203 | |||
| 204 | session = &sessionCredentials{ |
||
| 205 | AccessKeyId: *data.AccessKeyId, |
||
| 206 | AccessKeySecret: *data.AccessKeySecret, |
||
| 207 | SecurityToken: *data.SecurityToken, |
||
| 208 | Expiration: *data.Expiration, |
||
| 209 | } |
||
| 210 | return |
||
| 211 | } |
||
| 254 |