From e1622b7078f8a66d94512db39e5ce3bd1aa02727 Mon Sep 17 00:00:00 2001 From: Pierre Lafievre Date: Tue, 18 Jul 2023 13:25:56 +0200 Subject: [PATCH] B #470: templates datasource shouldn't filter on ID --- CHANGELOG.md | 4 +++- opennebula/data_opennebula_template.go | 25 ++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62ebe1433..cd12340f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,9 @@ DEPRECATION: * `quotas` section of `opennebula_user` resource (#448) * `quotas` section of `opennebula_group` resource (#447) -# 1.2.2 (Unreleased) +BUG FIXES: + +* data/opennebula_template: fix filtering and documentation (#470) # 1.2.2 (May 31st, 2023) diff --git a/opennebula/data_opennebula_template.go b/opennebula/data_opennebula_template.go index 765ebfb2c..b0b4252c3 100644 --- a/opennebula/data_opennebula_template.go +++ b/opennebula/data_opennebula_template.go @@ -129,7 +129,6 @@ func commonTemplatesFilter(d *schema.ResourceData, meta interface{}) ([]*templat } // filter templates with user defined criterias - id := d.Get("id") name, nameOk := d.GetOk("name") hasCPU := d.Get("has_cpu").(bool) hasVCPU := d.Get("has_vcpu").(bool) @@ -143,10 +142,6 @@ func commonTemplatesFilter(d *schema.ResourceData, meta interface{}) ([]*templat match := make([]*templateSc.Template, 0, 1) for i, template := range templates.Templates { - if id != -1 && template.ID != id { - continue - } - if nameOk && template.Name != name { continue } @@ -186,19 +181,31 @@ func commonTemplatesFilter(d *schema.ResourceData, meta interface{}) ([]*templat func templateFilter(d *schema.ResourceData, meta interface{}) (*templateSc.Template, error) { - match, err := commonTemplatesFilter(d, meta) + matched, err := commonTemplatesFilter(d, meta) if err != nil { return nil, err } + newMatched := make([]*templateSc.Template, 0) + + id := d.Get("id").(int) + if id != -1 { + for _, tpl := range matched { + if tpl.ID != id { + continue + } + newMatched = append(newMatched, tpl) + } + } + // the template datasource should match at most one element - if len(match) == 0 { + if len(newMatched) == 0 { return nil, fmt.Errorf("no templates match the constraints") - } else if len(match) > 1 { + } else if len(newMatched) > 1 { return nil, fmt.Errorf("several templates match the constraints") } - return match[0], nil + return newMatched[0], nil } func datasourceOpennebulaTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {