Skip to content

Conversation

ianc769
Copy link
Contributor

@ianc769 ianc769 commented Aug 18, 2025

Adding cloudstack_physicalnetwork , cloudstack_traffic_type and cloudstack_physicalnetwork as terraform managed resource options:

cloudstack_physicalnetwork -> https://cloudstack.apache.org/api/apidocs-4.20/apis/createPhysicalNetwork.html

cloudstack_traffic_type -> https://cloudstack.apache.org/api/apidocs-4.20/apis/addTrafficType.html

cloudstack_network_service_provider -> https://cloudstack.apache.org/api/apidocs-4.20/apis/addNetworkServiceProvider.html

Using this code for example:

resource "cloudstack_zone" "foo" {
  name          = "terraform-zone"
  dns1          = "8.8.8.8"
  internal_dns1 = "8.8.4.4"
  network_type  = "Advanced"
}

resource "cloudstack_physicalnetwork" "foo" {
  name                   = "terraform-physical-network"
  zone                   = cloudstack_zone.foo.name
  broadcast_domain_range = "ZONE"
  isolation_methods      = ["VLAN"]
}

resource "cloudstack_traffic_type" "foo" {
  physical_network_id = cloudstack_physicalnetwork.foo.id
  type                = "Management"
  kvm_network_label   = "cloudbr0"
}

resource "cloudstack_network_service_provider" "securitygroup" {
  name                = "SecurityGroupProvider"
  physical_network_id = cloudstack_physicalnetwork.foo.id
  state               = "Enabled"
}
  # cloudstack_network_service_provider.securitygroup will be created
  + resource "cloudstack_network_service_provider" "securitygroup" {
      + id                  = (known after apply)
      + name                = "SecurityGroupProvider"
      + physical_network_id = (known after apply)
      + state               = "Enabled"
    }

  # cloudstack_physicalnetwork.foo will be created
  + resource "cloudstack_physicalnetwork" "foo" {
      + broadcast_domain_range = "ZONE"
      + id                     = (known after apply)
      + isolation_methods      = [
          + "VLAN",
        ]
      + name                   = "terraform-physical-network"
      + zone                   = "terraform-zone"
    }

  # cloudstack_traffic_type.foo will be created
  + resource "cloudstack_traffic_type" "foo" {
      + id                  = (known after apply)
      + kvm_network_label   = "cloudbr0"
      + physical_network_id = (known after apply)
      + type                = "Management"
    }

  # cloudstack_zone.foo will be created
  + resource "cloudstack_zone" "foo" {
      + allocationstate = (known after apply)
      + dns1            = "8.8.8.8"
      + id              = (known after apply)
      + internal_dns1   = "8.8.4.4"
      + name            = "terraform-zone"
      + network_type    = "Advanced"
    }

Plan: 4 to add, 0 to change, 0 to destroy.

cloudstack_zone.foo: Creating...
cloudstack_zone.foo: Creation complete after 1s [id=da8a5cb8-3dbf-4c9d-9fe6-f28a8a00f7ed]
cloudstack_physicalnetwork.foo: Creating...
cloudstack_physicalnetwork.foo: Creation complete after 1s [id=bc5ff5dc-28a5-4dcf-b14c-af176a3fdc45]
cloudstack_network_service_provider.securitygroup: Creating...
cloudstack_traffic_type.foo: Creating...
cloudstack_traffic_type.foo: Creation complete after 1s [id=ff508483-8ee7-4a7b-b62a-4f22cb36c937]
cloudstack_network_service_provider.securitygroup: Creation complete after 1s [id=5be9b3d5-612a-42b0-8d8d-8d4f534437d0]

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

ianc769 added 3 commits July 28, 2025 10:28
- Implement data source for cloudstack_physicalnetwork to retrieve physical network details.
- Create resource for managing cloudstack_physicalnetwork, including CRUD operations.
- Add tests for both data source and resource functionalities.
- Update documentation for cloudstack_physicalnetwork data source and resource.
@kiranchavala kiranchavala added this to the v0.6.0 milestone Aug 19, 2025
@vishesh92 vishesh92 closed this Aug 26, 2025
@vishesh92 vishesh92 reopened this Aug 26, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for three new CloudStack resources that manage physical network infrastructure: cloudstack_physicalnetwork, cloudstack_traffic_type, and cloudstack_network_service_provider. These resources allow Terraform to manage physical network configurations in CloudStack zones.

  • Implements complete CRUD operations for physical networks, traffic types, and network service providers
  • Adds corresponding data source for querying physical networks
  • Includes comprehensive test coverage for all new resources

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
cloudstack/provider.go Registers new resources and data source with provider
cloudstack/resource_cloudstack_physicalnetwork.go Physical network resource implementation
cloudstack/resource_cloudstack_traffic_type.go Traffic type resource implementation
cloudstack/resource_cloudstack_network_service_provider.go Network service provider resource implementation
cloudstack/data_source_cloudstack_physicalnetwork.go Physical network data source implementation
website/docs/r/*.html.markdown Documentation for new resources
website/docs/d/physicalnetwork.html.markdown Documentation for new data source
*_test.go files Test suites for all new resources and data source

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +268 to +275
if d.Get("kvm_network_label") == "" {
d.Set("kvm_network_label", "cloudbr0")
}

if d.Get("xen_network_label") == "" {
d.Set("xen_network_label", "xenbr0")
}

Copy link
Preview

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

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

Setting default values during import can cause configuration drift. These hardcoded defaults may not match the actual CloudStack configuration and will cause Terraform to detect changes on subsequent plans. Consider removing these default assignments or making them optional.

Suggested change
if d.Get("kvm_network_label") == "" {
d.Set("kvm_network_label", "cloudbr0")
}
if d.Get("xen_network_label") == "" {
d.Set("xen_network_label", "xenbr0")
}
// Do not set hardcoded defaults for kvm_network_label or xen_network_label during import.
// If the API provides these values, set them; otherwise, leave them unset.

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ianc769 , I think we can considder this co-pilot comment in a future improvement. the magic values here are a bit ugly and rigid (albeit correct)

Copy link
Collaborator

@kiranchavala kiranchavala left a comment

Choose a reason for hiding this comment

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

LGTM, Tested manually the resources

network_service_provider,
cloudstack_physicalnetwork,
cloudstack_traffic_type

Also the data_source_cloudstack_physicalnetwork

resource "cloudstack_zone" "foo" {
  name          = "terraform-zone"
  dns1          = "8.8.8.8"
  internal_dns1 = "8.8.4.4"
  network_type  = "Advanced"
}

resource "cloudstack_physicalnetwork" "foo" {
  name                   = "terraform-physical-network"
  zone                   = cloudstack_zone.foo.name
  broadcast_domain_range = "ZONE"
  isolation_methods      = ["VLAN"]
}

resource "cloudstack_traffic_type" "foo" {
  physical_network_id = cloudstack_physicalnetwork.foo.id
  type                = "Management"
  kvm_network_label   = "cloudbr0"
}

resource "cloudstack_network_service_provider" "securitygroup" {
  name                = "SecurityGroupProvider"
  physical_network_id = cloudstack_physicalnetwork.foo.id
  state               = "Enabled"
}


terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # cloudstack_network_service_provider.securitygroup will be created
  + resource "cloudstack_network_service_provider" "securitygroup" {
      + id                  = (known after apply)
      + name                = "SecurityGroupProvider"
      + physical_network_id = (known after apply)
      + state               = "Enabled"
    }

  # cloudstack_physicalnetwork.foo will be created
  + resource "cloudstack_physicalnetwork" "foo" {
      + broadcast_domain_range = "ZONE"
      + id                     = (known after apply)
      + isolation_methods      = [
          + "VLAN",
        ]
      + name                   = "terraform-physical-network"
      + zone                   = "terraform-zone"
    }

  # cloudstack_traffic_type.foo will be created
  + resource "cloudstack_traffic_type" "foo" {
      + id                  = (known after apply)
      + kvm_network_label   = "cloudbr0"
      + physical_network_id = (known after apply)
      + type                = "Management"
    }

  # cloudstack_zone.foo will be created
  + resource "cloudstack_zone" "foo" {
      + dns1          = "8.8.8.8"
      + id            = (known after apply)
      + internal_dns1 = "8.8.4.4"
      + name          = "terraform-zone"
      + network_type  = "Advanced"
    }

Plan: 4 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudstack_zone.foo: Creating...
cloudstack_zone.foo: Creation complete after 1s [id=760d4c0d-cc59-4245-bb30-18d3448ae672]
cloudstack_physicalnetwork.foo: Creating...
cloudstack_physicalnetwork.foo: Creation complete after 1s [id=318ce63d-7642-4174-8f3e-69b00f38c98a]
cloudstack_network_service_provider.securitygroup: Creating...
cloudstack_traffic_type.foo: Creating...
cloudstack_network_service_provider.securitygroup: Creation complete after 1s [id=8cebfeed-7df8-444a-9802-6d9a86dd4de8]
cloudstack_traffic_type.foo: Creation complete after 1s [id=abcb7c9f-05f6-4ff1-a90f-3b16394cef1e]

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
╭─ ~/Desktop/cloudstack-India-demo/cloudstack-terraform copy                                                                                            ✔ ╱ 5s ╱ Azure subscription 1  ╱ 03:52:39 PM 
╰─ terraform destroy
cloudstack_zone.foo: Refreshing state... [id=760d4c0d-cc59-4245-bb30-18d3448ae672]
cloudstack_physicalnetwork.foo: Refreshing state... [id=318ce63d-7642-4174-8f3e-69b00f38c98a]
cloudstack_network_service_provider.securitygroup: Refreshing state... [id=8cebfeed-7df8-444a-9802-6d9a86dd4de8]
cloudstack_traffic_type.foo: Refreshing state... [id=abcb7c9f-05f6-4ff1-a90f-3b16394cef1e]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # cloudstack_network_service_provider.securitygroup will be destroyed
  - resource "cloudstack_network_service_provider" "securitygroup" {
      - id                  = "8cebfeed-7df8-444a-9802-6d9a86dd4de8" -> null
      - name                = "SecurityGroupProvider" -> null
      - physical_network_id = "318ce63d-7642-4174-8f3e-69b00f38c98a" -> null
      - state               = "Enabled" -> null
    }

  # cloudstack_physicalnetwork.foo will be destroyed
  - resource "cloudstack_physicalnetwork" "foo" {
      - broadcast_domain_range = "ZONE" -> null
      - id                     = "318ce63d-7642-4174-8f3e-69b00f38c98a" -> null
      - isolation_methods      = [
          - "VLAN",
        ] -> null
      - name                   = "terraform-physical-network" -> null
      - zone                   = "terraform-zone" -> null
        # (2 unchanged attributes hidden)
    }

  # cloudstack_traffic_type.foo will be destroyed
  - resource "cloudstack_traffic_type" "foo" {
      - id                  = "abcb7c9f-05f6-4ff1-a90f-3b16394cef1e" -> null
      - kvm_network_label   = "cloudbr0" -> null
      - physical_network_id = "318ce63d-7642-4174-8f3e-69b00f38c98a" -> null
      - type                = "Management" -> null
    }

  # cloudstack_zone.foo will be destroyed
  - resource "cloudstack_zone" "foo" {
      - dns1          = "8.8.8.8" -> null
      - id            = "760d4c0d-cc59-4245-bb30-18d3448ae672" -> null
      - internal_dns1 = "8.8.4.4" -> null
      - name          = "terraform-zone" -> null
      - network_type  = "Advanced" -> null
    }

Plan: 0 to add, 0 to change, 4 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

cloudstack_network_service_provider.securitygroup: Destroying... [id=8cebfeed-7df8-444a-9802-6d9a86dd4de8]
cloudstack_traffic_type.foo: Destroying... [id=abcb7c9f-05f6-4ff1-a90f-3b16394cef1e]
cloudstack_traffic_type.foo: Destruction complete after 1s
cloudstack_network_service_provider.securitygroup: Destruction complete after 1s
cloudstack_physicalnetwork.foo: Destroying... [id=318ce63d-7642-4174-8f3e-69b00f38c98a]
cloudstack_physicalnetwork.foo: Destruction complete after 0s
cloudstack_zone.foo: Destroying... [id=760d4c0d-cc59-4245-bb30-18d3448ae672]
cloudstack_zone.foo: Destruction complete after 0s

Destroy complete! Resources: 4 destroyed.

data "cloudstack_physicalnetwork" "default" {
  filter {
    name = "name"
    value = "terraform-physical-network"
  }
}

output "zome" {
  value = data.cloudstack_physicalnetwork.default.zone
}

terraform apply
data.cloudstack_physicalnetwork.default: Reading...
data.cloudstack_physicalnetwork.default: Read complete after 0s [id=7f434f12-dece-4705-b08f-980d97fc542d]

Changes to Outputs:
  + zome = "terraform-zone"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

zome = "terraform-zone"

Screenshot 2025-08-29 at 3 54 23 PM

@DaanHoogland DaanHoogland merged commit a8c34cd into apache:main Aug 31, 2025
24 checks passed
ianc769 added a commit to ianc769/cloudstack-terraform-provider that referenced this pull request Sep 3, 2025
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.

4 participants