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