|
| 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 | +} |
0 commit comments