Skip to content

Commit aabe35e

Browse files
committed
adding resource cloudstack_physical_network
1 parent 891703d commit aabe35e

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

cloudstack/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func Provider() terraform.ResourceProvider {
106106
"cloudstack_network_acl": resourceCloudStackNetworkACL(),
107107
"cloudstack_network_acl_rule": resourceCloudStackNetworkACLRule(),
108108
"cloudstack_nic": resourceCloudStackNIC(),
109+
"cloudstack_physical_network": resourceCloudStackPhysicalNetwork(),
109110
"cloudstack_port_forward": resourceCloudStackPortForward(),
110111
"cloudstack_private_gateway": resourceCloudStackPrivateGateway(),
111112
"cloudstack_secondary_ipaddress": resourceCloudStackSecondaryIPAddress(),
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"fmt"
24+
"strings"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
"github.com/hashicorp/terraform/helper/schema"
28+
)
29+
30+
func resourceCloudStackPhysicalNetwork() *schema.Resource {
31+
return &schema.Resource{
32+
Create: resourceCloudStackPhysicalNetworkCreate,
33+
Read: resourceCloudStackPhysicalNetworkRead,
34+
Update: resourceCloudStackPhysicalNetworkUpdate,
35+
Delete: resourceCloudStackPhysicalNetworkDelete,
36+
Importer: &schema.ResourceImporter{
37+
State: schema.ImportStatePassthrough,
38+
},
39+
Schema: map[string]*schema.Schema{
40+
"broadcast_domain_range": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
},
44+
"domainid": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
},
48+
"isolation_methods": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
},
52+
"name": {
53+
Type: schema.TypeString,
54+
Required: true,
55+
},
56+
"network_speed": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
},
60+
"tags": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
},
64+
"vlan": {
65+
Type: schema.TypeString,
66+
Optional: true,
67+
},
68+
"zoneid": {
69+
Type: schema.TypeString,
70+
Required: true,
71+
},
72+
},
73+
}
74+
}
75+
76+
func resourceCloudStackPhysicalNetworkCreate(d *schema.ResourceData, meta interface{}) error {
77+
cs := meta.(*cloudstack.CloudStackClient)
78+
79+
p := cs.Network.NewCreatePhysicalNetworkParams(d.Get("name").(string), d.Get("zoneid").(string))
80+
if v, ok := d.GetOk("broadcast_domain_range"); ok {
81+
p.SetBroadcastdomainrange(strings.ToUpper(v.(string)))
82+
}
83+
if v, ok := d.GetOk("domainid"); ok {
84+
p.SetDomainid(v.(string))
85+
}
86+
if v, ok := d.GetOk("isolation_methods"); ok {
87+
p.SetIsolationmethods([]string{v.(string)})
88+
}
89+
if v, ok := d.GetOk("network_speed"); ok {
90+
p.SetNetworkspeed(v.(string))
91+
}
92+
if v, ok := d.GetOk("tags"); ok {
93+
p.SetTags([]string{v.(string)})
94+
}
95+
if v, ok := d.GetOk("vlan"); ok {
96+
p.SetVlan(v.(string))
97+
}
98+
99+
r, err := cs.Network.CreatePhysicalNetwork(p)
100+
if err != nil {
101+
return err
102+
}
103+
104+
d.SetId(r.Id)
105+
106+
return resourceCloudStackPhysicalNetworkRead(d, meta)
107+
}
108+
109+
func resourceCloudStackPhysicalNetworkRead(d *schema.ResourceData, meta interface{}) error {
110+
cs := meta.(*cloudstack.CloudStackClient)
111+
112+
pn, _, err := cs.Network.GetPhysicalNetworkByID(d.Id())
113+
if err != nil {
114+
return err
115+
}
116+
117+
d.Set("broadcast_domain_range", pn.Broadcastdomainrange)
118+
d.Set("domainid", pn.Domainid)
119+
d.Set("isolation_methods", pn.Isolationmethods)
120+
d.Set("name", pn.Name)
121+
d.Set("network_speed", pn.Networkspeed)
122+
d.Set("tags", pn.Tags)
123+
d.Set("vlan", pn.Vlan)
124+
d.Set("zoneid", pn.Zoneid)
125+
126+
return nil
127+
}
128+
129+
func resourceCloudStackPhysicalNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
130+
cs := meta.(*cloudstack.CloudStackClient)
131+
132+
p := cs.Network.NewUpdatePhysicalNetworkParams(d.Id())
133+
if v, ok := d.GetOk("network_speed"); ok {
134+
p.SetNetworkspeed(v.(string))
135+
}
136+
if v, ok := d.GetOk("tags"); ok {
137+
p.SetTags([]string{v.(string)})
138+
}
139+
if v, ok := d.GetOk("vlan"); ok {
140+
p.SetVlan(v.(string))
141+
}
142+
143+
_, err := cs.Network.UpdatePhysicalNetwork(p)
144+
if err != nil {
145+
return fmt.Errorf("Error deleting physical network: %s", err)
146+
}
147+
148+
return resourceCloudStackPhysicalNetworkRead(d, meta)
149+
}
150+
151+
func resourceCloudStackPhysicalNetworkDelete(d *schema.ResourceData, meta interface{}) error {
152+
cs := meta.(*cloudstack.CloudStackClient)
153+
154+
p := cs.Network.NewDeletePhysicalNetworkParams(d.Id())
155+
_, err := cs.Network.DeletePhysicalNetwork(p)
156+
if err != nil {
157+
return fmt.Errorf("Error deleting phsyical network: %s", err)
158+
}
159+
160+
return nil
161+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"testing"
24+
25+
"github.com/hashicorp/terraform/helper/resource"
26+
)
27+
28+
func TestAccCloudStackPhysicalNetwork_basic(t *testing.T) {
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: testAccProviders,
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccCloudStackPhysicalNetwork_basic,
35+
},
36+
{
37+
Config: testAccCloudStackPhysicalNetwork_update,
38+
},
39+
},
40+
})
41+
}
42+
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"
52+
}
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+
zoneid = cloudstack_zone.test.id
60+
}
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"
72+
}
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+
zoneid = cloudstack_zone.test.id
80+
}
81+
`

0 commit comments

Comments
 (0)