| Conditions | 18 |
| Total Lines | 75 |
| Code Lines | 51 |
| 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 |
||
| 136 | func (provider *ECSRAMRoleCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { |
||
| 137 | roleName := provider.roleName |
||
| 138 | if roleName == "" { |
||
| 139 | roleName, err = provider.getRoleName() |
||
| 140 | if err != nil { |
||
| 141 | return |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | req := &httputil.Request{ |
||
| 146 | Method: "GET", |
||
| 147 | Protocol: "http", |
||
| 148 | Host: "100.100.100.200", |
||
| 149 | Path: "/latest/meta-data/ram/security-credentials/" + roleName, |
||
| 150 | Headers: map[string]string{}, |
||
| 151 | } |
||
| 152 | |||
| 153 | connectTimeout := 1 * time.Second |
||
| 154 | readTimeout := 1 * time.Second |
||
| 155 | |||
| 156 | if provider.httpOptions != nil && provider.httpOptions.ConnectTimeout > 0 { |
||
| 157 | connectTimeout = time.Duration(provider.httpOptions.ConnectTimeout) * time.Millisecond |
||
| 158 | } |
||
| 159 | if provider.httpOptions != nil && provider.httpOptions.ReadTimeout > 0 { |
||
| 160 | readTimeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Millisecond |
||
| 161 | } |
||
| 162 | if provider.httpOptions != nil && provider.httpOptions.Proxy != "" { |
||
| 163 | req.Proxy = provider.httpOptions.Proxy |
||
| 164 | } |
||
| 165 | req.ConnectTimeout = connectTimeout |
||
| 166 | req.ReadTimeout = readTimeout |
||
| 167 | |||
| 168 | metadataToken, err := provider.getMetadataToken() |
||
| 169 | if err != nil { |
||
| 170 | return nil, err |
||
| 171 | } |
||
| 172 | if metadataToken != "" { |
||
| 173 | req.Headers["x-aliyun-ecs-metadata-token"] = metadataToken |
||
| 174 | } |
||
| 175 | |||
| 176 | res, err := httpDo(req) |
||
| 177 | if err != nil { |
||
| 178 | err = fmt.Errorf("refresh Ecs sts token err: %s", err.Error()) |
||
| 179 | return |
||
| 180 | } |
||
| 181 | |||
| 182 | if res.StatusCode != 200 { |
||
| 183 | err = fmt.Errorf("refresh Ecs sts token err, httpStatus: %d, message = %s", res.StatusCode, string(res.Body)) |
||
| 184 | return |
||
| 185 | } |
||
| 186 | |||
| 187 | var data ecsRAMRoleResponse |
||
| 188 | err = json.Unmarshal(res.Body, &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 | } |
||
| 284 |