~go-bot/goose/test

« back to all changes in this revision

Viewing changes to client/client.go

  • Committer: Tarmac
  • Author(s): Ian Booth
  • Date: 2013-03-28 12:55:16 UTC
  • mfrom: (83.1.1 region-error-fixups)
  • Revision ID: tarmac-20130328125516-y1zpoibtfvl873gg
[r=wallyworld] Simplify some complicated code

This branch cleans up some complicated code which was left after a previous branch landed.

https://codereview.appspot.com/8079043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
        authMode identity.Authenticator
67
67
 
68
68
        auth AuthenticatingClient
 
69
 
69
70
        // Service type to endpoint URLs for each available region
70
71
        regionServiceURLs map[string]identity.ServiceURLs
 
72
 
71
73
        // Service type to endpoint URLs for the authenticated region
72
74
        serviceURLs identity.ServiceURLs
 
75
 
73
76
        // The service types which must be available after authentication,
74
77
        // or else services which use this client will not be able to function as expected.
75
78
        requiredServiceTypes []string
208
211
                        }
209
212
                }
210
213
                missingServiceTypes, possibleRegions = c.possibleRegions(existingServiceTypes)
211
 
                errorPrefix = fmt.Sprintf("the configured region %q does not allow access to all required services, namely: %q\n"+
212
 
                        "access to these services is missing: %q",
 
214
                errorPrefix = fmt.Sprintf("the configured region %q does not allow access to all required services, namely: %s\n"+
 
215
                        "access to these services is missing: %s",
213
216
                        c.creds.Region,
214
217
                        strings.Join(c.requiredServiceTypes, ", "),
215
218
                        strings.Join(missingServiceTypes, ", "))
216
219
        }
217
220
        if len(missingServiceTypes) > 0 {
218
221
                if len(possibleRegions) > 0 {
219
 
                        return fmt.Errorf("%s\none of these regions may be suitable instead: %q",
 
222
                        return fmt.Errorf("%s\none of these regions may be suitable instead: %s",
220
223
                                errorPrefix,
221
224
                                strings.Join(possibleRegions, ", "))
222
225
                } else {
232
235
// required service types. The service types which are accessible, accessibleServiceTypes, is passed in and the
233
236
// method returns what the missing service types are as well as valid regions.
234
237
func (c *authenticatingClient) possibleRegions(accessibleServiceTypes []string) (missingServiceTypes []string, possibleRegions []string) {
235
 
        serviceTypeRegions := make(map[string][]string)
 
238
        var serviceTypeRegions map[string][]string
236
239
        // Figure out the missing service types and build up a map of all service types to regions
237
240
        // obtained from the authentication response.
238
241
        for _, serviceType := range c.requiredServiceTypes {
239
242
                if !containsString(accessibleServiceTypes, serviceType) {
240
243
                        missingServiceTypes = append(missingServiceTypes, serviceType)
241
 
                        for region, serviceURLs := range c.regionServiceURLs {
242
 
                                for regionServiceType := range serviceURLs {
243
 
                                        regions := serviceTypeRegions[regionServiceType]
244
 
                                        if !containsString(regions, region) {
245
 
                                                serviceTypeRegions[regionServiceType] = append(regions, region)
246
 
                                        }
247
 
                                }
248
 
                        }
 
244
                        serviceTypeRegions = c.extractServiceTypeRegions()
249
245
                }
250
246
        }
 
247
 
251
248
        // Look at the region lists for each missing service type and determine which subset of those could
252
249
        // be used to allow access to all required service types. The most specific regions are used.
253
250
        if len(missingServiceTypes) == 1 {
254
251
                possibleRegions = serviceTypeRegions[missingServiceTypes[0]]
255
252
        } else {
256
253
                for _, serviceType := range missingServiceTypes {
257
 
                        regions := serviceTypeRegions[serviceType]
258
254
                        for _, serviceTypeCompare := range missingServiceTypes {
259
255
                                if serviceType == serviceTypeCompare {
260
256
                                        continue
261
257
                                }
262
 
                                for _, region := range regions {
263
 
                                        regionsCompare := serviceTypeRegions[serviceTypeCompare]
264
 
                                        if !containsBaseRegion(regionsCompare, region) && containsSuperRegion(regionsCompare, region) {
265
 
                                                possibleRegions = append(possibleRegions, region)
266
 
                                        }
267
 
                                }
 
258
                                possibleRegions = appendPossibleRegions(serviceType, serviceTypeCompare, serviceTypeRegions, possibleRegions)
268
259
                        }
269
260
                }
270
261
        }
272
263
        return
273
264
}
274
265
 
 
266
// utility function to extract map of service types -> region
 
267
func (c *authenticatingClient) extractServiceTypeRegions() map[string][]string {
 
268
        serviceTypeRegions := make(map[string][]string)
 
269
        for region, serviceURLs := range c.regionServiceURLs {
 
270
                for regionServiceType := range serviceURLs {
 
271
                        regions := serviceTypeRegions[regionServiceType]
 
272
                        if !containsString(regions, region) {
 
273
                                serviceTypeRegions[regionServiceType] = append(regions, region)
 
274
                        }
 
275
                }
 
276
        }
 
277
        return serviceTypeRegions
 
278
}
 
279
 
 
280
// extract the common regions for each service type and append them to the possible regions slice.
 
281
func appendPossibleRegions(serviceType, serviceTypeCompare string, serviceTypeRegions map[string][]string,
 
282
        possibleRegions []string) []string {
 
283
        regions := serviceTypeRegions[serviceType]
 
284
        for _, region := range regions {
 
285
                regionsCompare := serviceTypeRegions[serviceTypeCompare]
 
286
                if !containsBaseRegion(regionsCompare, region) && containsSuperRegion(regionsCompare, region) {
 
287
                        possibleRegions = append(possibleRegions, region)
 
288
                }
 
289
        }
 
290
        return possibleRegions
 
291
}
 
292
 
275
293
// utility function to see if element exists in values slice.
276
294
func containsString(values []string, element string) bool {
277
295
        for _, value := range values {