Conditions | 17 |
Total Lines | 72 |
Code Lines | 58 |
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 credentials.*RAMRoleArnCredentialsProvider.updateCredential 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 credentials |
||
147 | func (r *RAMRoleArnCredentialsProvider) updateCredential() (err error) { |
||
148 | if r.runtime == nil { |
||
149 | r.runtime = new(utils.Runtime) |
||
150 | } |
||
151 | request := request.NewCommonRequest() |
||
152 | request.Domain = "sts.aliyuncs.com" |
||
153 | if r.runtime.STSEndpoint != "" { |
||
154 | request.Domain = r.runtime.STSEndpoint |
||
155 | } |
||
156 | request.Scheme = "HTTPS" |
||
157 | request.Method = "GET" |
||
158 | request.QueryParams["AccessKeyId"] = r.AccessKeyId |
||
159 | if r.SecurityToken != "" { |
||
160 | request.QueryParams["SecurityToken"] = r.SecurityToken |
||
161 | } |
||
162 | request.QueryParams["Action"] = "AssumeRole" |
||
163 | request.QueryParams["Format"] = "JSON" |
||
164 | if r.RoleSessionExpiration > 0 { |
||
165 | if r.RoleSessionExpiration >= 900 && r.RoleSessionExpiration <= 3600 { |
||
166 | request.QueryParams["DurationSeconds"] = strconv.Itoa(r.RoleSessionExpiration) |
||
167 | } else { |
||
168 | err = errors.New("[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr") |
||
169 | return |
||
170 | } |
||
171 | } else { |
||
172 | request.QueryParams["DurationSeconds"] = strconv.Itoa(defaultDurationSeconds) |
||
173 | } |
||
174 | request.QueryParams["RoleArn"] = r.RoleArn |
||
175 | if r.Policy != "" { |
||
176 | request.QueryParams["Policy"] = r.Policy |
||
177 | } |
||
178 | if r.ExternalId != "" { |
||
179 | request.QueryParams["ExternalId"] = r.ExternalId |
||
180 | } |
||
181 | request.QueryParams["RoleSessionName"] = r.RoleSessionName |
||
182 | request.QueryParams["SignatureMethod"] = "HMAC-SHA1" |
||
183 | request.QueryParams["SignatureVersion"] = "1.0" |
||
184 | request.QueryParams["Version"] = "2015-04-01" |
||
185 | request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
186 | request.QueryParams["SignatureNonce"] = utils.GetUUID() |
||
187 | signature := utils.ShaHmac1(request.BuildStringToSign(), r.AccessKeySecret+"&") |
||
188 | request.QueryParams["Signature"] = signature |
||
189 | request.Headers["Host"] = request.Domain |
||
190 | request.Headers["Accept-Encoding"] = "identity" |
||
191 | request.URL = request.BuildURL() |
||
192 | content, err := doAction(request, r.runtime) |
||
193 | if err != nil { |
||
194 | return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error()) |
||
195 | } |
||
196 | var resp *ramRoleArnResponse |
||
197 | err = json.Unmarshal(content, &resp) |
||
198 | if err != nil { |
||
199 | return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error()) |
||
200 | } |
||
201 | if resp == nil || resp.Credentials == nil { |
||
202 | return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty") |
||
203 | } |
||
204 | respCredentials := resp.Credentials |
||
205 | if respCredentials.AccessKeyId == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" { |
||
206 | return fmt.Errorf("refresh RoleArn sts token err: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyId, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration) |
||
207 | } |
||
208 | |||
209 | expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration) |
||
210 | r.lastUpdateTimestamp = time.Now().Unix() |
||
211 | r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
||
212 | r.sessionCredential = &sessionCredential{ |
||
213 | AccessKeyId: respCredentials.AccessKeyId, |
||
214 | AccessKeySecret: respCredentials.AccessKeySecret, |
||
215 | SecurityToken: respCredentials.SecurityToken, |
||
216 | } |
||
217 | |||
218 | return |
||
219 | } |
||
220 |