Skip to content

Commit 4ecd903

Browse files
committed
added resource_cloudstack_loadbalancer and corresponding doc page
1 parent 16915b6 commit 4ecd903

File tree

3 files changed

+245
-0
lines changed

3 files changed

+245
-0
lines changed

cloudstack/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func Provider() *schema.Provider {
126126
"cloudstack_ipaddress": resourceCloudStackIPAddress(),
127127
"cloudstack_kubernetes_cluster": resourceCloudStackKubernetesCluster(),
128128
"cloudstack_kubernetes_version": resourceCloudStackKubernetesVersion(),
129+
"cloudstack_loadbalancer": resourceCloudStackLoadBalancer(),
129130
"cloudstack_loadbalancer_rule": resourceCloudStackLoadBalancerRule(),
130131
"cloudstack_network": resourceCloudStackNetwork(),
131132
"cloudstack_network_acl": resourceCloudStackNetworkACL(),
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
"github.com/apache/cloudstack-go/v2/cloudstack"
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25+
)
26+
27+
func resourceCloudStackLoadBalancer() *schema.Resource {
28+
return &schema.Resource{
29+
Create: resourceCloudStackLoadBalancerCreate,
30+
Read: resourceCloudStackLoadBalancerRead,
31+
Delete: resourceCloudStackLoadBalancerDelete,
32+
33+
Schema: map[string]*schema.Schema{
34+
"algorithm": {
35+
Description: "load balancer algorithm (source, roundrobin, leastconn)",
36+
Type: schema.TypeString,
37+
Required: true,
38+
ForceNew: true,
39+
},
40+
41+
"instanceport": {
42+
Description: "the TCP port of the virtual machine where the network traffic will be load balanced to",
43+
Type: schema.TypeInt,
44+
Required: true,
45+
ForceNew: true,
46+
},
47+
48+
"name": {
49+
Description: "name of the load balancer",
50+
Type: schema.TypeString,
51+
Required: true,
52+
ForceNew: true,
53+
},
54+
55+
"networkid": {
56+
Description: "The guest network the load balancer will be created for",
57+
Type: schema.TypeString,
58+
Required: true,
59+
ForceNew: true,
60+
},
61+
62+
"scheme": {
63+
Description: "the load balancer scheme. Supported value in this release is Internal",
64+
Type: schema.TypeString,
65+
Required: true,
66+
ForceNew: true,
67+
},
68+
69+
"sourceipaddressnetworkid": {
70+
Description: "the network id of the source ip address",
71+
Type: schema.TypeString,
72+
Required: true,
73+
ForceNew: true,
74+
},
75+
76+
"sourceport": {
77+
Description: "the source port the network traffic will be load balanced from",
78+
Type: schema.TypeInt,
79+
Required: true,
80+
ForceNew: true,
81+
},
82+
83+
"description": {
84+
Description: "the description of the load balancer",
85+
Type: schema.TypeString,
86+
Optional: true,
87+
ForceNew: true,
88+
},
89+
90+
"sourceipaddress": {
91+
Description: "the source IP address the network traffic will be load balanced from",
92+
Type: schema.TypeString,
93+
Optional: true,
94+
ForceNew: true,
95+
},
96+
97+
"virtualmachineids": {
98+
Description: "the list of IDs of the virtual machine that are being assigned to the load balancer rule(i.e. virtualMachineIds=1,2,3)",
99+
Type: schema.TypeList,
100+
Optional: true,
101+
ForceNew: true,
102+
Elem: &schema.Schema{
103+
Type: schema.TypeString,
104+
},
105+
},
106+
},
107+
}
108+
}
109+
110+
func resourceCloudStackLoadBalancerCreate(d *schema.ResourceData, meta interface{}) error {
111+
cs := meta.(*cloudstack.CloudStackClient)
112+
113+
p := cs.LoadBalancer.NewCreateLoadBalancerParams(
114+
d.Get("algorithm").(string),
115+
d.Get("instanceport").(int),
116+
d.Get("name").(string),
117+
d.Get("networkid").(string),
118+
d.Get("scheme").(string),
119+
d.Get("sourceipaddressnetworkid").(string),
120+
d.Get("sourceport").(int),
121+
)
122+
if v, ok := d.GetOk("description"); ok {
123+
p.SetDescription(v.(string))
124+
}
125+
if v, ok := d.GetOk("sourceipaddress"); ok {
126+
p.SetSourceipaddress(v.(string))
127+
}
128+
129+
r, err := cs.LoadBalancer.CreateLoadBalancer(p)
130+
if err != nil {
131+
return err
132+
}
133+
134+
if v, ok := d.GetOk("virtualmachineids"); ok {
135+
vmIds := v.([]interface{})
136+
for _, vmId := range vmIds {
137+
assignParams := cs.LoadBalancer.NewAssignToLoadBalancerRuleParams(r.Id)
138+
assignParams.SetVirtualmachineids([]string{vmId.(string)})
139+
_, err := cs.LoadBalancer.AssignToLoadBalancerRule(assignParams)
140+
if err != nil {
141+
return err
142+
}
143+
}
144+
}
145+
146+
d.SetId(r.Id)
147+
148+
return resourceCloudStackLoadBalancerRead(d, meta)
149+
}
150+
151+
func resourceCloudStackLoadBalancerRead(d *schema.ResourceData, meta interface{}) error {
152+
cs := meta.(*cloudstack.CloudStackClient)
153+
154+
r, _, err := cs.LoadBalancer.GetLoadBalancerByID(d.Id())
155+
if err != nil {
156+
return err
157+
}
158+
159+
d.Set("algorithm", r.Algorithm)
160+
d.Set("name", r.Name)
161+
d.Set("network_id", r.Networkid)
162+
d.Set("sourceipaddressnetworkid", r.Sourceipaddressnetworkid)
163+
164+
var vmIds []string
165+
for _, vm := range r.Loadbalancerinstance {
166+
vmIds = append(vmIds, vm.Id)
167+
}
168+
d.Set("virtualmachineids", vmIds)
169+
170+
return nil
171+
}
172+
173+
func resourceCloudStackLoadBalancerDelete(d *schema.ResourceData, meta interface{}) error {
174+
cs := meta.(*cloudstack.CloudStackClient)
175+
176+
_, err := cs.LoadBalancer.DeleteLoadBalancer(cs.LoadBalancer.NewDeleteLoadBalancerParams(d.Id()))
177+
if err != nil {
178+
return err
179+
}
180+
181+
return nil
182+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
layout: "cloudstack"
3+
page_title: "CloudStack: cloudstack_loadbalancer"
4+
sidebar_current: "docs-cloudstack-resource-loadbalancer"
5+
description: |-
6+
Creates an internal load balancer.
7+
---
8+
9+
# cloudstack_pod
10+
11+
Creates an internal load balancer.
12+
13+
## Example Usage
14+
15+
Basic usage:
16+
17+
```hcl
18+
resource "cloudstack_loadbalancer" "example" {
19+
algorithm = "Source"
20+
description = "Example Load Balancer"
21+
instanceport = "8081"
22+
name = "internal-lb-example"
23+
networkid = "0ae8fa84-c78e-441a-8628-917d276c7d5c"
24+
scheme = "Internal"
25+
sourceipaddressnetworkid = "0ae8fa84-c78e-441a-8628-917d276c7d5c"
26+
sourceport = "8081"
27+
virtualmachineids = [ "48600f99-d890-472c-bc1a-7379af22727c", "749485a1-4081-49cd-9668-1d160ac94488", "9bbf3c6d-c8b8-42e8-ab37-e4f60a71f61d" ]
28+
}
29+
```
30+
31+
## Argument Reference
32+
33+
The following arguments are supported:
34+
35+
* `algorithm` - (Required) load balancer algorithm (source, roundrobin, leastconn).
36+
* `description` - (Optional) the description of the load balancer.
37+
* `instanceport` - (Required) the TCP port of the virtual machine where the network traffic will be load balanced to.
38+
* `name` - (Required) name of the load balancer.
39+
* `networkid` - (Required) The guest network the load balancer will be created for.
40+
* `scheme` - (Required) the load balancer scheme. Supported value in this release is Internal.
41+
* `sourceipaddressnetworkid` - (Required) the network id of the source ip address.
42+
* `sourceport` - (Required) the source port the network traffic will be load balanced from.
43+
* `sourceipaddress` - (Optional) the source IP address the network traffic will be load balanced from.
44+
* `virtualmachineids` - (Optional) the list of IDs of the virtual machine that are being assigned to the load balancer rule(i.e. virtualMachineIds=1,2,3).
45+
46+
47+
## Attributes Reference
48+
49+
The following attributes are exported:
50+
51+
* `id` - the Load Balancer ID.
52+
53+
54+
55+
## Import
56+
57+
A pod can be imported; use `<LOADBALANCER ID>` as the import ID. For
58+
example:
59+
60+
```shell
61+
terraform import cloudstack_loadbalancer.example eefce154-e759-45a4-989a-9a432792801b
62+
```

0 commit comments

Comments
 (0)