Skip to content

Fix project-scoped VM lookup in port forward resource - #356

Open
dheeraj12347 wants to merge 1 commit into
apache:mainfrom
dheeraj12347:fix-port-forward-project-vm
Open

dheeraj12347 wants to merge 1 commit into
apache:mainfrom
dheeraj12347:fix-port-forward-project-vm

Conversation

@dheeraj12347

Copy link
Copy Markdown
Contributor

Description

Fixes an issue with project-scoped VM lookup when creating a port forward.

The cloudstack_port_forward resource supports a project attribute, but while creating a port forward, the target virtual machine was previously retrieved using only its VM ID:

vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
    forward["virtual_machine_id"].(string),
)

This lookup does not pass the project context from the port forward resource.

When project-scoped resources are used with account-level credentials, the VM lookup may fail because the API request does not include the project context required to resolve the project-scoped VM.

Changes

The VM lookup in createPortForward has been updated to pass the configured project:

vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
    forward["virtual_machine_id"].(string),
    cloudstack.WithProject(d.Get("project").(string)),
)

This ensures that the project associated with the port forward resource is also used when resolving the target VM.

Regression Test

Added a new acceptance test:

TestAccCloudStackPortForward_project

The test creates:

  1. A project-scoped network.
  2. A project-scoped VM attached to that network.
  3. A port forward with an explicitly configured project.
  4. A port-forward rule targeting the project-scoped VM.

The test verifies that:

  • The port-forward resource is created successfully.
  • The configured project is correctly associated with the resource.
  • A forward rule is created.
  • The forward rule receives a UUID.

Existing Project Handling

The existing project-inheritance behavior remains unchanged.

The new test specifically covers the case where the project is explicitly configured on the port-forward resource and the target VM is also project-scoped.

Validation

The following local checks were successfully completed:

  • go test ./cloudstack — passed
  • go vet ./... — passed
  • git diff --check — passed

The targeted acceptance test was also attempted:

TF_ACC=1 \
CLOUDSTACK_API_URL=http://localhost:8080/client/api \
CLOUDSTACK_API_KEY="$CLOUDSTACK_API_KEY" \
CLOUDSTACK_SECRET_KEY="$CLOUDSTACK_SECRET_KEY" \
go test ./cloudstack \
-run '^TestAccCloudStackPortForward_project$' \
-v \
-timeout 30m

However, the acceptance test could not proceed to the port-forward resource logic because the locally deployed CloudStack simulator did not contain the expected Sandbox-simulator zone.

The failure occurred during network creation:

Error retrieving ID of zone Sandbox-simulator: No match found for Sandbox-simulator

Therefore, the targeted acceptance test was blocked by the local simulator environment before the VM lookup and port-forward creation logic were reached.

Related Issue

Fixes #355

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.

0.7.0 bug

1 participant