Skip to content

Commit 8c28240

Browse files
authored
Support additional parameters for cloudstack_nic resource (#210)
1 parent d131aa1 commit 8c28240

File tree

3 files changed

+110
-11
lines changed

3 files changed

+110
-11
lines changed

cloudstack/resource_cloudstack_nic.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func resourceCloudStackNIC() *schema.Resource {
5353
Required: true,
5454
ForceNew: true,
5555
},
56+
"mac_address": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
Computed: true,
60+
ForceNew: true,
61+
},
5662
},
5763
}
5864
}
@@ -66,11 +72,16 @@ func resourceCloudStackNICCreate(d *schema.ResourceData, meta interface{}) error
6672
d.Get("virtual_machine_id").(string),
6773
)
6874

69-
// If there is a ipaddres supplied, add it to the parameter struct
75+
// If there is an ipaddress supplied, add it to the parameter struct
7076
if ipaddress, ok := d.GetOk("ip_address"); ok {
7177
p.SetIpaddress(ipaddress.(string))
7278
}
7379

80+
// If there is a macaddress supplied, add it to the parameter struct
81+
if macaddress, ok := d.GetOk("mac_address"); ok {
82+
p.SetMacaddress(macaddress.(string))
83+
}
84+
7485
// Create and attach the new NIC
7586
r, err := Retry(10, retryableAddNicFunc(cs, p))
7687
if err != nil {
@@ -115,6 +126,7 @@ func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error {
115126
d.Set("ip_address", n.Ipaddress)
116127
d.Set("network_id", n.Networkid)
117128
d.Set("virtual_machine_id", vm.Id)
129+
d.Set("mac_address", n.Macaddress)
118130
found = true
119131
break
120132
}

cloudstack/resource_cloudstack_nic_test.go

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ func TestAccCloudStackNIC_update(t *testing.T) {
7979
})
8080
}
8181

82+
func TestAccCloudStackNIC_macaddress(t *testing.T) {
83+
var nic cloudstack.Nic
84+
85+
resource.Test(t, resource.TestCase{
86+
PreCheck: func() { testAccPreCheck(t) },
87+
Providers: testAccProviders,
88+
CheckDestroy: testAccCheckCloudStackNICDestroy,
89+
Steps: []resource.TestStep{
90+
{
91+
Config: testAccCloudStackNIC_macaddress,
92+
Check: resource.ComposeTestCheckFunc(
93+
testAccCheckCloudStackNICExists(
94+
"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
95+
testAccCheckCloudStackNICMacAddress(&nic),
96+
resource.TestCheckResourceAttr(
97+
"cloudstack_nic.foo", "mac_address", "02:1a:4b:3c:5d:6e"),
98+
),
99+
},
100+
},
101+
})
102+
}
103+
82104
func testAccCheckCloudStackNICExists(
83105
v, n string, nic *cloudstack.Nic) resource.TestCheckFunc {
84106
return func(s *terraform.State) error {
@@ -122,7 +144,7 @@ func testAccCheckCloudStackNICAttributes(
122144
nic *cloudstack.Nic) resource.TestCheckFunc {
123145
return func(s *terraform.State) error {
124146

125-
if nic.Networkname != "terraform-network" {
147+
if nic.Networkname != "terraform-network-secondary" {
126148
return fmt.Errorf("Bad network name: %s", nic.Networkname)
127149
}
128150

@@ -134,7 +156,7 @@ func testAccCheckCloudStackNICIPAddress(
134156
nic *cloudstack.Nic) resource.TestCheckFunc {
135157
return func(s *terraform.State) error {
136158

137-
if nic.Networkname != "terraform-network" {
159+
if nic.Networkname != "terraform-network-secondary" {
138160
return fmt.Errorf("Bad network name: %s", nic.Networkname)
139161
}
140162

@@ -146,6 +168,22 @@ func testAccCheckCloudStackNICIPAddress(
146168
}
147169
}
148170

171+
func testAccCheckCloudStackNICMacAddress(
172+
nic *cloudstack.Nic) resource.TestCheckFunc {
173+
return func(s *terraform.State) error {
174+
175+
if nic.Networkname != "terraform-network-secondary" {
176+
return fmt.Errorf("Bad network name: %s", nic.Networkname)
177+
}
178+
179+
if nic.Macaddress != "02:1a:4b:3c:5d:6e" {
180+
return fmt.Errorf("Bad MAC address: %s", nic.Macaddress)
181+
}
182+
183+
return nil
184+
}
185+
}
186+
149187
func testAccCheckCloudStackNICDestroy(s *terraform.State) error {
150188
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
151189

@@ -170,16 +208,16 @@ func testAccCheckCloudStackNICDestroy(s *terraform.State) error {
170208

171209
const testAccCloudStackNIC_basic = `
172210
resource "cloudstack_network" "foo" {
173-
name = "terraform-network"
174-
display_text = "terraform-network"
211+
name = "terraform-network-primary"
212+
display_text = "terraform-network-primary"
175213
cidr = "10.1.1.0/24"
176214
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
177215
zone = "Sandbox-simulator"
178216
}
179217
180218
resource "cloudstack_network" "bar" {
181-
name = "terraform-network"
182-
display_text = "terraform-network"
219+
name = "terraform-network-secondary"
220+
display_text = "terraform-network-secondary"
183221
cidr = "10.1.2.0/24"
184222
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
185223
zone = "Sandbox-simulator"
@@ -202,16 +240,16 @@ resource "cloudstack_nic" "foo" {
202240

203241
const testAccCloudStackNIC_ipaddress = `
204242
resource "cloudstack_network" "foo" {
205-
name = "terraform-network"
206-
display_text = "terraform-network"
243+
name = "terraform-network-primary"
244+
display_text = "terraform-network-primary"
207245
cidr = "10.1.1.0/24"
208246
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
209247
zone = "Sandbox-simulator"
210248
}
211249
212250
resource "cloudstack_network" "bar" {
213-
name = "terraform-network"
214-
display_text = "terraform-network"
251+
name = "terraform-network-secondary"
252+
display_text = "terraform-network-secondary"
215253
cidr = "10.1.2.0/24"
216254
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
217255
zone = "Sandbox-simulator"
@@ -232,3 +270,36 @@ resource "cloudstack_nic" "foo" {
232270
virtual_machine_id = cloudstack_instance.foobar.id
233271
ip_address = "10.1.2.123"
234272
}`
273+
274+
const testAccCloudStackNIC_macaddress = `
275+
resource "cloudstack_network" "foo" {
276+
name = "terraform-network-primary"
277+
display_text = "terraform-network-primary"
278+
cidr = "10.1.1.0/24"
279+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
280+
zone = "Sandbox-simulator"
281+
}
282+
283+
resource "cloudstack_network" "bar" {
284+
name = "terraform-network-secondary"
285+
display_text = "terraform-network-secondary"
286+
cidr = "10.1.2.0/24"
287+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
288+
zone = "Sandbox-simulator"
289+
}
290+
291+
resource "cloudstack_instance" "foobar" {
292+
name = "terraform-test"
293+
display_name = "terraform"
294+
service_offering= "Medium Instance"
295+
network_id = cloudstack_network.foo.id
296+
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
297+
zone = "Sandbox-simulator"
298+
expunge = true
299+
}
300+
301+
resource "cloudstack_nic" "foo" {
302+
network_id = cloudstack_network.bar.id
303+
virtual_machine_id = cloudstack_instance.foobar.id
304+
mac_address = "02:1a:4b:3c:5d:6e"
305+
}`

website/docs/r/nic.html.markdown

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ resource "cloudstack_nic" "test" {
2222
}
2323
```
2424

25+
With MAC address:
26+
27+
```hcl
28+
resource "cloudstack_nic" "test" {
29+
network_id = "6eb22f91-7454-4107-89f4-36afcdf33021"
30+
ip_address = "192.168.1.1"
31+
mac_address = "02:1a:4b:3c:5d:6e"
32+
virtual_machine_id = "f8141e2f-4e7e-4c63-9362-986c908b7ea7"
33+
}
34+
```
35+
2536
## Argument Reference
2637

2738
The following arguments are supported:
@@ -32,6 +43,10 @@ The following arguments are supported:
3243
* `ip_address` - (Optional) The IP address to assign to the NIC. Changing this
3344
forces a new resource to be created.
3445

46+
* `mac_address` - (Optional) The MAC address to assign to the NIC. If not specified,
47+
a MAC address will be automatically generated. Changing this forces a new resource
48+
to be created.
49+
3550
* `virtual_machine_id` - (Required) The ID of the virtual machine to which to
3651
attach the NIC. Changing this forces a new resource to be created.
3752

@@ -41,3 +56,4 @@ The following attributes are exported:
4156

4257
* `id` - The ID of the NIC.
4358
* `ip_address` - The assigned IP address.
59+
* `mac_address` - The assigned MAC address.

0 commit comments

Comments
 (0)