-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathresource_cloudstack_loadbalancer.go
More file actions
182 lines (155 loc) · 4.97 KB
/
resource_cloudstack_loadbalancer.go
File metadata and controls
182 lines (155 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package cloudstack
import (
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCloudStackLoadBalancer() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackLoadBalancerCreate,
Read: resourceCloudStackLoadBalancerRead,
Delete: resourceCloudStackLoadBalancerDelete,
Schema: map[string]*schema.Schema{
"algorithm": {
Description: "load balancer algorithm (source, roundrobin, leastconn)",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instanceport": {
Description: "the TCP port of the virtual machine where the network traffic will be load balanced to",
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"name": {
Description: "name of the load balancer",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"networkid": {
Description: "The guest network the load balancer will be created for",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"scheme": {
Description: "the load balancer scheme. Supported value in this release is Internal",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sourceipaddressnetworkid": {
Description: "the network id of the source ip address",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sourceport": {
Description: "the source port the network traffic will be load balanced from",
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"description": {
Description: "the description of the load balancer",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"sourceipaddress": {
Description: "the source IP address the network traffic will be load balanced from",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"virtualmachineids": {
Description: "the list of IDs of the virtual machine that are being assigned to the load balancer rule(i.e. virtualMachineIds=1,2,3)",
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceCloudStackLoadBalancerCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.LoadBalancer.NewCreateLoadBalancerParams(
d.Get("algorithm").(string),
d.Get("instanceport").(int),
d.Get("name").(string),
d.Get("networkid").(string),
d.Get("scheme").(string),
d.Get("sourceipaddressnetworkid").(string),
d.Get("sourceport").(int),
)
if v, ok := d.GetOk("description"); ok {
p.SetDescription(v.(string))
}
if v, ok := d.GetOk("sourceipaddress"); ok {
p.SetSourceipaddress(v.(string))
}
r, err := cs.LoadBalancer.CreateLoadBalancer(p)
if err != nil {
return err
}
if v, ok := d.GetOk("virtualmachineids"); ok {
vmIds := v.(*schema.Set).List()
for _, vmId := range vmIds {
p_update := cs.LoadBalancer.NewAssignToLoadBalancerRuleParams(r.Id)
p_update.SetVirtualmachineids([]string{vmId.(string)})
_, err := cs.LoadBalancer.AssignToLoadBalancerRule(p_update)
if err != nil {
return err
}
}
}
d.SetId(r.Id)
return resourceCloudStackLoadBalancerRead(d, meta)
}
func resourceCloudStackLoadBalancerRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
r, _, err := cs.LoadBalancer.GetLoadBalancerByID(d.Id())
if err != nil {
return err
}
d.Set("algorithm", r.Algorithm)
d.Set("name", r.Name)
d.Set("network_id", r.Networkid)
d.Set("sourceipaddressnetworkid", r.Sourceipaddressnetworkid)
var vmIds []string
for _, vm := range r.Loadbalancerinstance {
vmIds = append(vmIds, vm.Id)
}
d.Set("virtualmachineids", vmIds)
return nil
}
func resourceCloudStackLoadBalancerDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
_, err := cs.LoadBalancer.DeleteLoadBalancer(cs.LoadBalancer.NewDeleteLoadBalancerParams(d.Id()))
if err != nil {
return err
}
return nil
}