| Conditions | 12 |
| Total Lines | 62 |
| Code Lines | 43 |
| 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 |
||
| 131 | func (provider *ECSRAMRoleCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { |
||
| 132 | roleName := provider.roleName |
||
| 133 | if roleName == "" { |
||
| 134 | roleName, err = provider.getRoleName() |
||
| 135 | if err != nil { |
||
| 136 | return |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | req := &httputil.Request{ |
||
| 141 | Method: "GET", |
||
| 142 | Protocol: "http", |
||
| 143 | Host: "100.100.100.200", |
||
| 144 | Path: "/latest/meta-data/ram/security-credentials/" + roleName, |
||
| 145 | ConnectTimeout: 5 * time.Second, |
||
| 146 | ReadTimeout: 5 * time.Second, |
||
| 147 | Headers: map[string]string{}, |
||
| 148 | } |
||
| 149 | |||
| 150 | if provider.enableIMDSv2 { |
||
| 151 | metadataToken, err := provider.getMetadataToken() |
||
| 152 | if err != nil { |
||
| 153 | return nil, err |
||
| 154 | } |
||
| 155 | req.Headers["x-aliyun-ecs-metadata-token"] = metadataToken |
||
| 156 | } |
||
| 157 | |||
| 158 | res, err := httpDo(req) |
||
| 159 | if err != nil { |
||
| 160 | err = fmt.Errorf("refresh Ecs sts token err: %s", err.Error()) |
||
| 161 | return |
||
| 162 | } |
||
| 163 | |||
| 164 | if res.StatusCode != 200 { |
||
| 165 | err = fmt.Errorf("refresh Ecs sts token err, httpStatus: %d, message = %s", res.StatusCode, string(res.Body)) |
||
| 166 | return |
||
| 167 | } |
||
| 168 | |||
| 169 | var data ecsRAMRoleResponse |
||
| 170 | err = json.Unmarshal(res.Body, &data) |
||
| 171 | if err != nil { |
||
| 172 | err = fmt.Errorf("refresh Ecs sts token err, json.Unmarshal fail: %s", err.Error()) |
||
| 173 | return |
||
| 174 | } |
||
| 175 | |||
| 176 | if data.AccessKeyId == nil || data.AccessKeySecret == nil || data.SecurityToken == nil { |
||
| 177 | err = fmt.Errorf("refresh Ecs sts token err, fail to get credentials") |
||
| 178 | return |
||
| 179 | } |
||
| 180 | |||
| 181 | if *data.Code != "Success" { |
||
| 182 | err = fmt.Errorf("refresh Ecs sts token err, Code is not Success") |
||
| 183 | return |
||
| 184 | } |
||
| 185 | |||
| 186 | session = &sessionCredentials{ |
||
| 187 | AccessKeyId: *data.AccessKeyId, |
||
| 188 | AccessKeySecret: *data.AccessKeySecret, |
||
| 189 | SecurityToken: *data.SecurityToken, |
||
| 190 | Expiration: *data.Expiration, |
||
| 191 | } |
||
| 192 | return |
||
| 193 | } |
||
| 244 |