This is an automated email from the ASF dual-hosted git repository. vishesh pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/cloudstack-terraform-provider.git
The following commit(s) were added to refs/heads/main by this push: new 8c28240 Support additional parameters for cloudstack_nic resource (#210) 8c28240 is described below commit 8c282403396be3c13270330c9af8921f8dd9e7c9 Author: Pearl Dsilva <pearl1...@gmail.com> AuthorDate: Thu Aug 28 05:44:26 2025 -0400 Support additional parameters for cloudstack_nic resource (#210) --- cloudstack/resource_cloudstack_nic.go | 14 ++++- cloudstack/resource_cloudstack_nic_test.go | 91 ++++++++++++++++++++++++++---- website/docs/r/nic.html.markdown | 16 ++++++ 3 files changed, 110 insertions(+), 11 deletions(-) diff --git a/cloudstack/resource_cloudstack_nic.go b/cloudstack/resource_cloudstack_nic.go index 82f455e..36c25ec 100644 --- a/cloudstack/resource_cloudstack_nic.go +++ b/cloudstack/resource_cloudstack_nic.go @@ -53,6 +53,12 @@ func resourceCloudStackNIC() *schema.Resource { Required: true, ForceNew: true, }, + "mac_address": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, }, } } @@ -66,11 +72,16 @@ func resourceCloudStackNICCreate(d *schema.ResourceData, meta interface{}) error d.Get("virtual_machine_id").(string), ) - // If there is a ipaddres supplied, add it to the parameter struct + // If there is an ipaddress supplied, add it to the parameter struct if ipaddress, ok := d.GetOk("ip_address"); ok { p.SetIpaddress(ipaddress.(string)) } + // If there is a macaddress supplied, add it to the parameter struct + if macaddress, ok := d.GetOk("mac_address"); ok { + p.SetMacaddress(macaddress.(string)) + } + // Create and attach the new NIC r, err := Retry(10, retryableAddNicFunc(cs, p)) if err != nil { @@ -115,6 +126,7 @@ func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error { d.Set("ip_address", n.Ipaddress) d.Set("network_id", n.Networkid) d.Set("virtual_machine_id", vm.Id) + d.Set("mac_address", n.Macaddress) found = true break } diff --git a/cloudstack/resource_cloudstack_nic_test.go b/cloudstack/resource_cloudstack_nic_test.go index 7b66675..d82d542 100644 --- a/cloudstack/resource_cloudstack_nic_test.go +++ b/cloudstack/resource_cloudstack_nic_test.go @@ -79,6 +79,28 @@ func TestAccCloudStackNIC_update(t *testing.T) { }) } +func TestAccCloudStackNIC_macaddress(t *testing.T) { + var nic cloudstack.Nic + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudStackNICDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCloudStackNIC_macaddress, + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudStackNICExists( + "cloudstack_instance.foobar", "cloudstack_nic.foo", &nic), + testAccCheckCloudStackNICMacAddress(&nic), + resource.TestCheckResourceAttr( + "cloudstack_nic.foo", "mac_address", "02:1a:4b:3c:5d:6e"), + ), + }, + }, + }) +} + func testAccCheckCloudStackNICExists( v, n string, nic *cloudstack.Nic) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -122,7 +144,7 @@ func testAccCheckCloudStackNICAttributes( nic *cloudstack.Nic) resource.TestCheckFunc { return func(s *terraform.State) error { - if nic.Networkname != "terraform-network" { + if nic.Networkname != "terraform-network-secondary" { return fmt.Errorf("Bad network name: %s", nic.Networkname) } @@ -134,7 +156,7 @@ func testAccCheckCloudStackNICIPAddress( nic *cloudstack.Nic) resource.TestCheckFunc { return func(s *terraform.State) error { - if nic.Networkname != "terraform-network" { + if nic.Networkname != "terraform-network-secondary" { return fmt.Errorf("Bad network name: %s", nic.Networkname) } @@ -146,6 +168,22 @@ func testAccCheckCloudStackNICIPAddress( } } +func testAccCheckCloudStackNICMacAddress( + nic *cloudstack.Nic) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if nic.Networkname != "terraform-network-secondary" { + return fmt.Errorf("Bad network name: %s", nic.Networkname) + } + + if nic.Macaddress != "02:1a:4b:3c:5d:6e" { + return fmt.Errorf("Bad MAC address: %s", nic.Macaddress) + } + + return nil + } +} + func testAccCheckCloudStackNICDestroy(s *terraform.State) error { cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) @@ -170,16 +208,16 @@ func testAccCheckCloudStackNICDestroy(s *terraform.State) error { const testAccCloudStackNIC_basic = ` resource "cloudstack_network" "foo" { - name = "terraform-network" - display_text = "terraform-network" + name = "terraform-network-primary" + display_text = "terraform-network-primary" cidr = "10.1.1.0/24" network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" zone = "Sandbox-simulator" } resource "cloudstack_network" "bar" { - name = "terraform-network" - display_text = "terraform-network" + name = "terraform-network-secondary" + display_text = "terraform-network-secondary" cidr = "10.1.2.0/24" network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" zone = "Sandbox-simulator" @@ -202,16 +240,16 @@ resource "cloudstack_nic" "foo" { const testAccCloudStackNIC_ipaddress = ` resource "cloudstack_network" "foo" { - name = "terraform-network" - display_text = "terraform-network" + name = "terraform-network-primary" + display_text = "terraform-network-primary" cidr = "10.1.1.0/24" network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" zone = "Sandbox-simulator" } resource "cloudstack_network" "bar" { - name = "terraform-network" - display_text = "terraform-network" + name = "terraform-network-secondary" + display_text = "terraform-network-secondary" cidr = "10.1.2.0/24" network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" zone = "Sandbox-simulator" @@ -232,3 +270,36 @@ resource "cloudstack_nic" "foo" { virtual_machine_id = cloudstack_instance.foobar.id ip_address = "10.1.2.123" }` + +const testAccCloudStackNIC_macaddress = ` +resource "cloudstack_network" "foo" { + name = "terraform-network-primary" + display_text = "terraform-network-primary" + cidr = "10.1.1.0/24" + network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" + zone = "Sandbox-simulator" +} + +resource "cloudstack_network" "bar" { + name = "terraform-network-secondary" + display_text = "terraform-network-secondary" + cidr = "10.1.2.0/24" + network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" + zone = "Sandbox-simulator" +} + +resource "cloudstack_instance" "foobar" { + name = "terraform-test" + display_name = "terraform" + service_offering= "Medium Instance" + network_id = cloudstack_network.foo.id + template = "CentOS 5.6 (64-bit) no GUI (Simulator)" + zone = "Sandbox-simulator" + expunge = true +} + +resource "cloudstack_nic" "foo" { + network_id = cloudstack_network.bar.id + virtual_machine_id = cloudstack_instance.foobar.id + mac_address = "02:1a:4b:3c:5d:6e" +}` diff --git a/website/docs/r/nic.html.markdown b/website/docs/r/nic.html.markdown index 8254b68..a000e70 100644 --- a/website/docs/r/nic.html.markdown +++ b/website/docs/r/nic.html.markdown @@ -22,6 +22,17 @@ resource "cloudstack_nic" "test" { } ``` +With MAC address: + +```hcl +resource "cloudstack_nic" "test" { + network_id = "6eb22f91-7454-4107-89f4-36afcdf33021" + ip_address = "192.168.1.1" + mac_address = "02:1a:4b:3c:5d:6e" + virtual_machine_id = "f8141e2f-4e7e-4c63-9362-986c908b7ea7" +} +``` + ## Argument Reference The following arguments are supported: @@ -32,6 +43,10 @@ The following arguments are supported: * `ip_address` - (Optional) The IP address to assign to the NIC. Changing this forces a new resource to be created. +* `mac_address` - (Optional) The MAC address to assign to the NIC. If not specified, + a MAC address will be automatically generated. Changing this forces a new resource + to be created. + * `virtual_machine_id` - (Required) The ID of the virtual machine to which to attach the NIC. Changing this forces a new resource to be created. @@ -41,3 +56,4 @@ The following attributes are exported: * `id` - The ID of the NIC. * `ip_address` - The assigned IP address. +* `mac_address` - The assigned MAC address.