Skip to content

fix: scan all service provider elements before failing the lookup - #357

Open
rootnomad wants to merge 1 commit into
apache:mainfrom
rootnomad:fix/nsp-vre-lookup-loop
Open

rootnomad wants to merge 1 commit into
apache:mainfrom
rootnomad:fix/nsp-vre-lookup-loop

Conversation

@rootnomad

Copy link
Copy Markdown
Contributor

Description

virtualRouterElementState and internalLbVmElementState look up the service provider element by iterating the list returned by the API, but the not-found error was returned inside the loop:

for _, e := range vre.VirtualRouterElements {
    if nsp.Id == e.Nspid {
        vreID = e.Id
        break
    }
    return fmt.Errorf("Service provider element id (nspod) not found: %s.", nsp.Id)
}

So the lookup fails on the first element that does not match instead of scanning the rest, and an empty list falls through with an empty id into ConfigureVirtualRouterElement. Move the error after the loop behind an empty-id check, so every element is scanned and a genuinely missing element returns a clear error. Same fix for the internal LB element lookup.

Testing

go build ./..., go vet ./... and go test ./cloudstack/ pass (the resource's acceptance test skips without TF_ACC).

There is no fails-before/passes-after acceptance test here: in a standard zone each provider has a single element, so the early return is not reached and the bug does not manifest end to end. The change is a localized control-flow correction in the element lookup, verified by the build, vet and the existing suite.

virtualRouterElementState and internalLbVmElementState returned a not-found
error from inside the loop over the provider elements, so each only examined
the first element and failed whenever the matching element was not at index 0.
Move the not-found error after each loop and trigger it only when no element
matched.

Signed-off-by: Ramgopal Nagaboina <ramgopal.nagaboina.dev@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The localized changes correctly handle later matches and empty result sets without introducing regressions.

0 open findings

What changed in this PR

Corrects service-provider element lookups so all API results are searched before reporting failure.

Changes:

  • Scans all virtual-router and internal-LB elements.
  • Rejects missing or empty element IDs before configuration.
File Description
cloudstack/​resource_cloudstack_network_service_provider_state.go Corrects element lookup control flow.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants