Skip to content

Commit 9adc728

Browse files
committed
review: refactor filterGuests
Used anonymous functions instead of a bunch of named ones.
1 parent 5740ec7 commit 9adc728

File tree

1 file changed

+30
-62
lines changed

1 file changed

+30
-62
lines changed

datasource/proxmox/data.go

Lines changed: 30 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (d *Datasource) Execute() (cty.Value, error) {
214214
return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
215215
}
216216

217-
// Find the latest VM among filtered.
217+
// findLatestConfig finds the latest VM among those passed using `configs`.
218218
// The `meta` field contains info about creation time (but it is not described in API docs).
219219
func findLatestConfig(configs []vmConfig) (vmConfig, error) {
220220
var result vmConfig
@@ -253,87 +253,55 @@ func getVmConfigs(client *proxmox.Client, vmList []proxmox.GuestResource) ([]vmC
253253
return result, nil
254254
}
255255

256-
// Drop guests from list that are not match some filters in the datasource config.
256+
// filterGuests removes guests from the `guests` list that do not match some filters in the datasource config.
257257
func filterGuests(config Config, guests []proxmox.GuestResource) []proxmox.GuestResource {
258-
var result []proxmox.GuestResource
258+
filterFuncs := make([]func(proxmox.GuestResource) bool, 0)
259259

260260
if config.Name != "" {
261-
result = filterByName(guests, config.Name)
262-
} else {
263-
result = guests
261+
filterFuncs = append(filterFuncs, func(vm proxmox.GuestResource) bool {
262+
return vm.Name == config.Name
263+
})
264264
}
265265

266266
if config.NameRegex != "" {
267-
result = filterByNameRegex(guests, config.NameRegex)
268-
} else {
269-
if config.Name == "" {
270-
result = guests
271-
}
267+
filterFuncs = append(filterFuncs, func(vm proxmox.GuestResource) bool {
268+
return regexp.MustCompile(config.NameRegex).MatchString(vm.Name)
269+
})
272270
}
273271

274272
if config.Template {
275-
result = filterByTemplate(result)
276-
}
277-
if config.Node != "" {
278-
result = filterByNode(result, config.Node)
279-
}
280-
if config.VmTags != "" {
281-
result = filterByTags(result, config.VmTags)
282-
}
283-
284-
return result
285-
}
286-
287-
func filterByName(guests []proxmox.GuestResource, name string) []proxmox.GuestResource {
288-
result := make([]proxmox.GuestResource, 0)
289-
for _, i := range guests {
290-
if i.Name == name {
291-
result = append(result, i)
292-
}
293-
}
294-
return result
295-
}
296-
297-
func filterByNameRegex(guests []proxmox.GuestResource, nameRegex string) []proxmox.GuestResource {
298-
re, _ := regexp.Compile(nameRegex)
299-
result := make([]proxmox.GuestResource, 0)
300-
for _, i := range guests {
301-
if re.MatchString(i.Name) {
302-
result = append(result, i)
303-
}
273+
filterFuncs = append(filterFuncs, func(vm proxmox.GuestResource) bool {
274+
return vm.Template
275+
})
304276
}
305-
return result
306-
}
307277

308-
func filterByTemplate(guests []proxmox.GuestResource) []proxmox.GuestResource {
309-
result := make([]proxmox.GuestResource, 0)
310-
for _, i := range guests {
311-
if i.Template {
312-
result = append(result, i)
313-
}
278+
if config.Node != "" {
279+
filterFuncs = append(filterFuncs, func(vm proxmox.GuestResource) bool {
280+
return vm.Node == config.Node
281+
})
314282
}
315-
return result
316-
}
317283

318-
func filterByNode(guests []proxmox.GuestResource, node string) []proxmox.GuestResource {
319-
result := make([]proxmox.GuestResource, 0)
320-
for _, i := range guests {
321-
if i.Node == node {
322-
result = append(result, i)
323-
}
284+
if config.VmTags != "" {
285+
// Split tags string because it can contain several tags separated with ";"
286+
tagsSplitted := strings.Split(config.VmTags, ";")
287+
filterFuncs = append(filterFuncs, func(vm proxmox.GuestResource) bool {
288+
return len(vm.Tags) > 0 && configTagsMatchNodeTags(tagsSplitted, vm.Tags)
289+
})
324290
}
325-
return result
326-
}
327291

328-
func filterByTags(guests []proxmox.GuestResource, tags string) []proxmox.GuestResource {
329292
result := make([]proxmox.GuestResource, 0)
330-
// Split tags string because it can contain several tags separated with ";"
331-
tagsSplitted := strings.Split(tags, ";")
332293
for _, guest := range guests {
333-
if len(guest.Tags) > 0 && configTagsMatchNodeTags(tagsSplitted, guest.Tags) {
294+
var ok bool
295+
for _, guestPassedFilter := range filterFuncs {
296+
if ok = guestPassedFilter(guest); !ok {
297+
break
298+
}
299+
}
300+
if ok {
334301
result = append(result, guest)
335302
}
336303
}
304+
337305
return result
338306
}
339307

0 commit comments

Comments
 (0)