Skip to content

Commit 84d68ca

Browse files
committed
Adding resource pod
1 parent c776bd8 commit 84d68ca

File tree

2 files changed

+239
-0
lines changed

2 files changed

+239
-0
lines changed

cloudstack/resource_cloudstack_pod.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
"errors"
24+
"fmt"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
"github.com/hashicorp/terraform/helper/schema"
28+
)
29+
30+
func resourceCloudStackPod() *schema.Resource {
31+
return &schema.Resource{
32+
Create: resourceCloudStackPodCreate,
33+
Read: resourceCloudStackPodRead,
34+
Update: resourceCloudStackPodUpdate,
35+
Delete: resourceCloudStackPodDelete,
36+
Importer: &schema.ResourceImporter{
37+
State: schema.ImportStatePassthrough,
38+
},
39+
Schema: map[string]*schema.Schema{
40+
"allocation_state": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
},
44+
"end_ip": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
},
48+
"gateway": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
},
52+
"name": {
53+
Type: schema.TypeString,
54+
Required: true,
55+
},
56+
"netmask": {
57+
Type: schema.TypeString,
58+
Required: true,
59+
},
60+
"start_ip": {
61+
Type: schema.TypeString,
62+
Required: true,
63+
},
64+
"zone_id": {
65+
Type: schema.TypeString,
66+
Required: true,
67+
},
68+
},
69+
}
70+
}
71+
72+
func resourceCloudStackPodCreate(d *schema.ResourceData, meta interface{}) error {
73+
cs := meta.(*cloudstack.CloudStackClient)
74+
75+
p := cs.Pod.NewCreatePodParams(d.Get("gateway").(string), d.Get("name").(string), d.Get("netmask").(string), d.Get("start_ip").(string), d.Get("zone_id").(string))
76+
if v, ok := d.GetOk("allocation_state"); ok {
77+
p.SetAllocationstate(v.(string))
78+
}
79+
if v, ok := d.GetOk("end_ip"); ok {
80+
p.SetEndip(v.(string))
81+
}
82+
83+
r, err := cs.Pod.CreatePod(p)
84+
if err != nil {
85+
return err
86+
}
87+
88+
d.SetId(r.Id)
89+
90+
return resourceCloudStackPodRead(d, meta)
91+
}
92+
93+
func resourceCloudStackPodRead(d *schema.ResourceData, meta interface{}) error {
94+
cs := meta.(*cloudstack.CloudStackClient)
95+
96+
r, count, err := cs.Pod.GetPodByID(d.Id())
97+
if err != nil {
98+
return err
99+
}
100+
101+
if count != 1 {
102+
return errors.New(fmt.Sprintf("Multiple pods. Invalid zone id: %s", d.Id()))
103+
}
104+
105+
d.Set("allocation_state", r.Allocationstate)
106+
d.Set("end_ip", r.Endip)
107+
d.Set("gateway", r.Gateway)
108+
d.Set("name", r.Name)
109+
d.Set("netmask", r.Netmask)
110+
d.Set("start_ip", r.Startip)
111+
d.Set("zone_id", r.Zoneid)
112+
113+
return nil
114+
}
115+
116+
func resourceCloudStackPodUpdate(d *schema.ResourceData, meta interface{}) error {
117+
cs := meta.(*cloudstack.CloudStackClient)
118+
119+
p := cs.Pod.NewUpdatePodParams(d.Id())
120+
if v, ok := d.GetOk("allocation_state"); ok {
121+
p.SetAllocationstate(v.(string))
122+
}
123+
if v, ok := d.GetOk("end_ip"); ok {
124+
p.SetEndip(v.(string))
125+
}
126+
if v, ok := d.GetOk("gateway"); ok {
127+
p.SetGateway(v.(string))
128+
}
129+
if v, ok := d.GetOk("name"); ok {
130+
p.SetName(v.(string))
131+
}
132+
if v, ok := d.GetOk("netmask"); ok {
133+
p.SetNetmask(v.(string))
134+
}
135+
if v, ok := d.GetOk("start_ip"); ok {
136+
p.SetStartip(v.(string))
137+
}
138+
139+
_, err := cs.Pod.UpdatePod(p)
140+
if err != nil {
141+
return err
142+
}
143+
144+
return resourceCloudStackPodRead(d, meta)
145+
}
146+
147+
func resourceCloudStackPodDelete(d *schema.ResourceData, meta interface{}) error {
148+
cs := meta.(*cloudstack.CloudStackClient)
149+
150+
_, err := cs.Pod.DeletePod(cs.Pod.NewDeletePodParams(d.Id()))
151+
if err != nil {
152+
return err
153+
}
154+
155+
return nil
156+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 TestAccCloudStackPod_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: testAccCloudStackPod_basic,
35+
},
36+
{
37+
Config: testAccCloudStackPod_update,
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttr("cloudstack_pod.test", "name", "accpod2"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
const testAccCloudStackPod_basic = `
47+
resource "cloudstack_zone" "test" {
48+
name = "acctest"
49+
dns1 = "8.8.8.8"
50+
dns2 = "8.8.8.8"
51+
internal_dns1 = "8.8.4.4"
52+
internal_dns2 = "8.8.4.4"
53+
network_type = "Advanced"
54+
domain = "cloudstack.apache.org"
55+
}
56+
resource "cloudstack_pod" "test" {
57+
allocation_state = "Disabled"
58+
gateway = "172.29.0.1"
59+
name = "accpod"
60+
netmask = "255.255.240.0"
61+
start_ip = "172.29.0.2"
62+
zone_id = cloudstack_zone.test.id
63+
}
64+
`
65+
66+
const testAccCloudStackPod_update = `
67+
resource "cloudstack_zone" "test" {
68+
name = "acctest"
69+
dns1 = "8.8.8.8"
70+
dns2 = "8.8.8.8"
71+
internal_dns1 = "8.8.4.4"
72+
internal_dns2 = "8.8.4.4"
73+
network_type = "Advanced"
74+
domain = "cloudstack.apache.org"
75+
}
76+
resource "cloudstack_pod" "test" {
77+
allocation_state = "Disabled"
78+
gateway = "172.29.0.1"
79+
name = "accpod2"
80+
netmask = "255.255.240.0"
81+
start_ip = "172.29.0.2"
82+
zone_id = cloudstack_zone.test.id
83+
`

0 commit comments

Comments
 (0)