~dave-cheney/goose/003-fix-fd-leak

« back to all changes in this revision

Viewing changes to testservices/novaservice/service.go

  • Committer: Tarmac
  • Author(s): Dimiter Naydenov
  • Date: 2013-02-08 17:35:15 UTC
  • mfrom: (69.1.5 nova-filter-refactoring)
  • Revision ID: tarmac-20130208173515-1olxekdz64n5fw1v
[r=dimitern] nova: Refactor filters

Now nova client and test double use separate filter types for better
isolation and simple code: nova.Filter (by the client), having only
Set() as it's write-only; and novaservice.filter (by the double), being
a thin wrapper over map[string]string.

As discussed, this relays the intent better and makes code cleaner.
Also removed a now redundant test to compare Add() and Set() on nova.Filter
and updated all tests as needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
237
237
        }, nil
238
238
}
239
239
 
 
240
// filter is used internally by matchServers.
 
241
type filter map[string]string
 
242
 
240
243
// matchServers returns a list of matching servers, after applying the
241
244
// given filter. Each separate filter is combined with a logical AND.
242
 
// Each filter can have only one value - the last one set with either
243
 
// Add() or Set(). A nil filter matches all servers.
 
245
// Each filter can have only one value. A nil filter matches all servers.
244
246
//
245
247
// This is tested to match OpenStack behavior. Regular expression
246
248
// matching is supported for FilterServer only, and the supported
249
251
//
250
252
// Example:
251
253
//
252
 
// filter := nova.NewFilter()
253
 
// filter.Set(nova.FilterStatus, nova.StatusActive)
254
 
// filter.Set(nova.FilterServer, `foo.*`)
 
254
// f := filter{
 
255
//     nova.FilterStatus: nova.StatusActive,
 
256
//     nova.FilterServer: `foo.*`,
 
257
// }
255
258
//
256
259
// This will match all servers with status "ACTIVE", and names starting
257
260
// with "foo".
258
 
func (n *Nova) matchServers(filter *nova.Filter) []nova.ServerDetail {
 
261
func (n *Nova) matchServers(f filter) []nova.ServerDetail {
259
262
        var servers []nova.ServerDetail
260
263
        for _, server := range n.servers {
261
264
                servers = append(servers, server)
262
265
        }
263
 
        if filter == nil {
 
266
        if len(f) == 0 {
264
267
                return servers // empty filter matches everything
265
268
        }
266
 
        if status := filter.Get(nova.FilterStatus); status != "" {
 
269
        if status := f[nova.FilterStatus]; status != "" {
267
270
                matched := []nova.ServerDetail{}
268
271
                for _, server := range servers {
269
272
                        if server.Status == status {
276
279
                }
277
280
                servers = matched
278
281
        }
279
 
        if nameRex := filter.Get(nova.FilterServer); nameRex != "" {
 
282
        if nameRex := f[nova.FilterServer]; nameRex != "" {
280
283
                matched := []nova.ServerDetail{}
281
284
                rex, err := regexp.Compile(nameRex)
282
285
                if err != nil {
283
 
                        fmt.Printf("cannot compile regexp filter %q: %v\n", filter, err)
 
286
                        fmt.Printf("cannot compile regexp filter %q: %v\n", nameRex, err)
284
287
                        // effectively nothing matches
285
288
                        return nil
286
289
                }
301
304
}
302
305
 
303
306
// allServers returns a list of all existing servers.
304
 
// Filtering is supported, see nova.Filter* for more info.
305
 
func (n *Nova) allServers(filter *nova.Filter) []nova.ServerDetail {
306
 
        return n.matchServers(filter)
 
307
// Filtering is supported, see filter type for more info.
 
308
func (n *Nova) allServers(f filter) []nova.ServerDetail {
 
309
        return n.matchServers(f)
307
310
}
308
311
 
309
312
// allServersAsEntities returns all servers as Entity structs.
310
 
// Filtering is supported, see nova.Filter* for more info.
311
 
func (n *Nova) allServersAsEntities(filter *nova.Filter) []nova.Entity {
 
313
// Filtering is supported, see filter type for more info.
 
314
func (n *Nova) allServersAsEntities(f filter) []nova.Entity {
312
315
        var entities []nova.Entity
313
 
        servers := n.matchServers(filter)
 
316
        servers := n.matchServers(f)
314
317
        for _, server := range servers {
315
318
                entities = append(entities, nova.Entity{
316
319
                        Id:    server.Id,