Conditions | 14 |
Total Lines | 63 |
Code Lines | 52 |
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.*RAMRoleArnCredential.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 |
||
99 | func (r *RAMRoleArnCredential) updateCredential() (err error) { |
||
100 | if r.runtime == nil { |
||
101 | r.runtime = new(utils.Runtime) |
||
102 | } |
||
103 | request := request.NewCommonRequest() |
||
104 | request.Domain = "sts.aliyuncs.com" |
||
105 | request.Scheme = "HTTPS" |
||
106 | request.Method = "GET" |
||
107 | request.QueryParams["AccessKeyId"] = r.AccessKeyID |
||
108 | request.QueryParams["Action"] = "AssumeRole" |
||
109 | request.QueryParams["Format"] = "JSON" |
||
110 | if r.RoleSessionExpiration > 0 { |
||
111 | if r.RoleSessionExpiration >= 900 && r.RoleSessionExpiration <= 3600 { |
||
112 | request.QueryParams["DurationSeconds"] = strconv.Itoa(r.RoleSessionExpiration) |
||
113 | } else { |
||
114 | err = errors.New("[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr") |
||
115 | return |
||
116 | } |
||
117 | } else { |
||
118 | request.QueryParams["DurationSeconds"] = strconv.Itoa(defaultDurationSeconds) |
||
119 | } |
||
120 | request.QueryParams["RoleArn"] = r.RoleArn |
||
121 | if r.Policy != "" { |
||
122 | request.QueryParams["Policy"] = r.Policy |
||
123 | } |
||
124 | request.QueryParams["RoleSessionName"] = r.RoleSessionName |
||
125 | request.QueryParams["SignatureMethod"] = "HMAC-SHA1" |
||
126 | request.QueryParams["SignatureVersion"] = "1.0" |
||
127 | request.QueryParams["Version"] = "2015-04-01" |
||
128 | request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
129 | request.QueryParams["SignatureNonce"] = utils.GetUUID() |
||
130 | signature := utils.ShaHmac1(request.BuildStringToSign(), r.AccessKeySecret+"&") |
||
131 | request.QueryParams["Signature"] = signature |
||
132 | request.Headers["Host"] = request.Domain |
||
133 | request.Headers["Accept-Encoding"] = "identity" |
||
134 | request.URL = request.BuildURL() |
||
135 | content, err := doAction(request, r.runtime) |
||
136 | if err != nil { |
||
137 | return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error()) |
||
138 | } |
||
139 | var resp *ramRoleArnResponse |
||
140 | err = json.Unmarshal(content, &resp) |
||
141 | if err != nil { |
||
142 | return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error()) |
||
143 | } |
||
144 | if resp == nil || resp.Credentials == nil { |
||
145 | return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty") |
||
146 | } |
||
147 | respCredentials := resp.Credentials |
||
148 | if respCredentials.AccessKeyID == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" { |
||
149 | return fmt.Errorf("refresh RoleArn sts token err: AccessKeyID: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyID, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration) |
||
150 | } |
||
151 | |||
152 | expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration) |
||
153 | r.lastUpdateTimestamp = time.Now().Unix() |
||
154 | r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
||
155 | r.sessionCredential = &sessionCredential{ |
||
156 | AccessKeyID: respCredentials.AccessKeyID, |
||
157 | AccessKeySecret: respCredentials.AccessKeySecret, |
||
158 | SecurityToken: respCredentials.SecurityToken, |
||
159 | } |
||
160 | |||
161 | return |
||
162 | } |
||
163 |