Skip to content

Commit 2003ec4

Browse files
committed
updates
1 parent 3e877e2 commit 2003ec4

File tree

2 files changed

+111
-67
lines changed

2 files changed

+111
-67
lines changed

cloudstack/resource_cloudstack_physical_network_test.go

Lines changed: 103 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,127 @@
2020
package cloudstack
2121

2222
import (
23+
"fmt"
2324
"testing"
2425

26+
"github.com/apache/cloudstack-go/v2/cloudstack"
2527
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
28+
"github.com/hashicorp/terraform-plugin-testing/terraform"
2629
)
2730

2831
func TestAccCloudStackPhysicalNetwork_basic(t *testing.T) {
32+
var physicalNetwork cloudstack.PhysicalNetwork
33+
34+
resource.Test(t, resource.TestCase{
35+
PreCheck: func() { testAccPreCheck(t) },
36+
Providers: testAccProviders,
37+
CheckDestroy: testAccCheckCloudStackPhysicalNetworkDestroy,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: testAccCloudStackPhysicalNetwork_basic,
41+
Check: resource.ComposeTestCheckFunc(
42+
testAccCheckCloudStackPhysicalNetworkExists(
43+
"cloudstack_physicalnetwork.foo", &physicalNetwork),
44+
testAccCheckCloudStackPhysicalNetworkBasicAttributes(&physicalNetwork),
45+
),
46+
},
47+
},
48+
})
49+
}
50+
51+
func TestAccCloudStackPhysicalNetwork_import(t *testing.T) {
2952
resource.Test(t, resource.TestCase{
30-
PreCheck: func() { testAccPreCheck(t) },
31-
Providers: testAccProviders,
53+
PreCheck: func() { testAccPreCheck(t) },
54+
Providers: testAccProviders,
55+
CheckDestroy: testAccCheckCloudStackPhysicalNetworkDestroy,
3256
Steps: []resource.TestStep{
3357
{
3458
Config: testAccCloudStackPhysicalNetwork_basic,
3559
},
3660
{
37-
Config: testAccCloudStackPhysicalNetwork_update,
61+
ResourceName: "cloudstack_physicalnetwork.foo",
62+
ImportState: true,
63+
ImportStateVerify: true,
3864
},
3965
},
4066
})
4167
}
4268

43-
const testAccCloudStackPhysicalNetwork_basic = `
44-
resource "cloudstack_zone" "test" {
45-
name = "acctest"
46-
dns1 = "8.8.8.8"
47-
dns2 = "8.8.8.8"
48-
internal_dns1 = "8.8.4.4"
49-
internal_dns2 = "8.8.4.4"
50-
network_type = "Advanced"
51-
domain = "cloudstack.apache.org"
69+
func testAccCheckCloudStackPhysicalNetworkExists(
70+
n string, physicalNetwork *cloudstack.PhysicalNetwork) resource.TestCheckFunc {
71+
return func(s *terraform.State) error {
72+
rs, ok := s.RootModule().Resources[n]
73+
if !ok {
74+
return fmt.Errorf("Not found: %s", n)
75+
}
76+
77+
if rs.Primary.ID == "" {
78+
return fmt.Errorf("No physical network ID is set")
79+
}
80+
81+
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
82+
p, _, err := cs.Network.GetPhysicalNetworkByID(rs.Primary.ID)
83+
if err != nil {
84+
return err
85+
}
86+
87+
if p.Id != rs.Primary.ID {
88+
return fmt.Errorf("Physical network not found")
89+
}
90+
91+
*physicalNetwork = *p
92+
93+
return nil
94+
}
5295
}
53-
resource "cloudstack_physical_network" "test" {
54-
broadcast_domain_range = "ZONE"
55-
isolation_methods = "VLAN"
56-
name = "test01"
57-
network_speed = "1G"
58-
tags = "vlan"
59-
zone_id = cloudstack_zone.test.id
96+
97+
func testAccCheckCloudStackPhysicalNetworkBasicAttributes(
98+
physicalNetwork *cloudstack.PhysicalNetwork) resource.TestCheckFunc {
99+
return func(s *terraform.State) error {
100+
if physicalNetwork.Name != "terraform-physical-network" {
101+
return fmt.Errorf("Bad name: %s", physicalNetwork.Name)
102+
}
103+
104+
if physicalNetwork.Broadcastdomainrange != "ZONE" {
105+
return fmt.Errorf("Bad broadcast domain range: %s", physicalNetwork.Broadcastdomainrange)
106+
}
107+
108+
return nil
109+
}
60110
}
61-
`
62-
63-
const testAccCloudStackPhysicalNetwork_update = `
64-
resource "cloudstack_zone" "test" {
65-
name = "acctest"
66-
dns1 = "8.8.8.8"
67-
dns2 = "8.8.8.8"
68-
internal_dns1 = "8.8.4.4"
69-
internal_dns2 = "8.8.4.4"
70-
network_type = "Advanced"
71-
domain = "cloudstack.apache.org"
111+
112+
func testAccCheckCloudStackPhysicalNetworkDestroy(s *terraform.State) error {
113+
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
114+
115+
for _, rs := range s.RootModule().Resources {
116+
if rs.Type != "cloudstack_physicalnetwork" {
117+
continue
118+
}
119+
120+
if rs.Primary.ID == "" {
121+
return fmt.Errorf("No physical network ID is set")
122+
}
123+
124+
_, _, err := cs.Network.GetPhysicalNetworkByID(rs.Primary.ID)
125+
if err == nil {
126+
return fmt.Errorf("Physical network %s still exists", rs.Primary.ID)
127+
}
128+
}
129+
130+
return nil
72131
}
73-
resource "cloudstack_physical_network" "test" {
74-
broadcast_domain_range = "ZONE"
75-
isolation_methods = "VLAN"
76-
name = "test01"
77-
network_speed = "10G"
78-
tags = "vxlan"
79-
zone_id = cloudstack_zone.test.id
132+
133+
const testAccCloudStackPhysicalNetwork_basic = `
134+
resource "cloudstack_zone" "foo" {
135+
name = "terraform-zone"
136+
dns1 = "8.8.8.8"
137+
internal_dns1 = "8.8.4.4"
138+
network_type = "Advanced"
80139
}
81-
`
140+
141+
resource "cloudstack_physicalnetwork" "foo" {
142+
name = "terraform-physical-network"
143+
zone = cloudstack_zone.foo.name
144+
broadcast_domain_range = "ZONE"
145+
isolation_methods = ["VLAN"]
146+
}`

cloudstack/resource_cloudstack_traffic_type.go

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func resourceCloudStackTrafficType() *schema.Resource {
4343
Description: "The network name label of the physical device dedicated to this traffic on a Hyperv host",
4444
Type: schema.TypeString,
4545
Optional: true,
46-
ForceNew: true,
4746
},
4847
"isolation_method": {
4948
Description: "Used if physical network has multiple isolation types and traffic type is public. Choose which isolation method. Valid options currently 'vlan' or 'vxlan', defaults to 'vlan'.",
@@ -55,13 +54,11 @@ func resourceCloudStackTrafficType() *schema.Resource {
5554
Description: "The network name label of the physical device dedicated to this traffic on a KVM host",
5655
Type: schema.TypeString,
5756
Optional: true,
58-
ForceNew: true,
5957
},
6058
"ovm3_network_label": {
6159
Description: "The network name of the physical device dedicated to this traffic on an OVM3 host",
6260
Type: schema.TypeString,
6361
Optional: true,
64-
ForceNew: true,
6562
},
6663
"physical_network_id": {
6764
Description: "the Physical Network ID",
@@ -80,19 +77,16 @@ func resourceCloudStackTrafficType() *schema.Resource {
8077
Description: "The VLAN id to be used for Management traffic by VMware host",
8178
Type: schema.TypeString,
8279
Optional: true,
83-
ForceNew: true,
8480
},
8581
"vmware_network_label": {
8682
Description: "The network name label of the physical device dedicated to this traffic on a VMware host",
8783
Type: schema.TypeString,
8884
Optional: true,
89-
ForceNew: true,
9085
},
9186
"xen_network_label": {
9287
Description: "The network name label of the physical device dedicated to this traffic on a XenServer host",
9388
Type: schema.TypeString,
9489
Optional: true,
95-
ForceNew: true,
9690
},
9791
},
9892
}
@@ -102,7 +96,7 @@ func resourceCloudStackTrafficTypeCreate(d *schema.ResourceData, meta interface{
10296
cs := meta.(*cloudstack.CloudStackClient)
10397

10498
physicalNetworkID := d.Get("physical_network_id").(string)
105-
trafficType := d.Get("type").(string)
99+
trafficType := d.Get("traffic_type").(string)
106100

107101
// Create a new parameter struct
108102
p := cs.Usage.NewAddTrafficTypeParams(physicalNetworkID, trafficType)
@@ -144,14 +138,6 @@ func resourceCloudStackTrafficTypeCreate(d *schema.ResourceData, meta interface{
144138

145139
d.SetId(r.Id)
146140

147-
d.Set("physical_network_id", d.Get("physical_network_id").(string))
148-
d.Set("hyperv_network_label", r.Hypervnetworklabel)
149-
d.Set("kvm_network_label", r.Kvmnetworklabel)
150-
d.Set("ovm3_network_label", r.Ovm3networklabel)
151-
d.Set("traffic_type", r.Traffictype)
152-
d.Set("vmware_network_label", r.Vmwarenetworklabel)
153-
d.Set("xen_network_label", r.Xennetworklabel)
154-
155141
return resourceCloudStackTrafficTypeRead(d, meta)
156142
}
157143

@@ -176,15 +162,15 @@ func resourceCloudStackTrafficTypeRead(d *schema.ResourceData, meta interface{})
176162
}
177163

178164
if trafficType == nil {
179-
log.Printf("[DEBUG] Traffic type %s does no longer exist", d.Get("type").(string))
165+
log.Printf("[DEBUG] Traffic type %s does no longer exist", d.Get("traffic_type").(string))
180166
d.SetId("")
181167
return nil
182168
}
183169

184170
// The TrafficType struct has a Name field which contains the traffic type
185171
// But in some cases it might be empty, so we'll keep the original value from the state
186172
if trafficType.Name != "" {
187-
d.Set("type", trafficType.Name)
173+
d.Set("traffic_type", trafficType.Name)
188174
}
189175

190176
// Note: The TrafficType struct doesn't have fields for network labels or VLAN
@@ -224,18 +210,11 @@ func resourceCloudStackTrafficTypeUpdate(d *schema.ResourceData, meta interface{
224210
// so we can't update the VLAN
225211

226212
// Update the traffic type
227-
r, err := cs.Usage.UpdateTrafficType(p)
213+
_, err := cs.Usage.UpdateTrafficType(p)
228214
if err != nil {
229-
return fmt.Errorf("Error updating traffic type %s: %s", d.Get("type").(string), err)
215+
return fmt.Errorf("Error updating traffic type %s: %s", d.Get("traffic_type").(string), err)
230216
}
231217

232-
d.Set("hyperv_network_label", r.Hypervnetworklabel)
233-
d.Set("kvm_network_label", r.Kvmnetworklabel)
234-
d.Set("ovm3_network_label", r.Ovm3networklabel)
235-
d.Set("traffic_type", r.Traffictype)
236-
d.Set("vmware_network_label", r.Vmwarenetworklabel)
237-
d.Set("xen_network_label", r.Xennetworklabel)
238-
239218
return resourceCloudStackTrafficTypeRead(d, meta)
240219
}
241220

@@ -255,7 +234,7 @@ func resourceCloudStackTrafficTypeDelete(d *schema.ResourceData, meta interface{
255234
return nil
256235
}
257236

258-
return fmt.Errorf("Error deleting traffic type %s: %s", d.Get("type").(string), err)
237+
return fmt.Errorf("Error deleting traffic type %s: %s", d.Get("traffic_type").(string), err)
259238
}
260239

261240
return nil
@@ -289,11 +268,11 @@ func resourceCloudStackTrafficTypeImport(d *schema.ResourceData, meta interface{
289268
// Set the type attribute - use the original value from the API call
290269
// If the Name field is empty, use a default value based on the traffic type ID
291270
if tt.Name != "" {
292-
d.Set("type", tt.Name)
271+
d.Set("traffic_type", tt.Name)
293272
} else {
294273
// Use a default value based on common traffic types
295274
// This is a fallback and might not be accurate
296-
d.Set("type", "Management")
275+
d.Set("traffic_type", "Management")
297276
}
298277

299278
// For import to work correctly, we need to set default values for network labels

0 commit comments

Comments
 (0)