in cloudstack/resource_cloudstack_loadbalancer_rule.go [109:195]
func resourceCloudStackLoadBalancerRuleCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Make sure all required parameters are there
if err := verifyLoadBalancerRule(d); err != nil {
return err
}
d.Partial(true)
// Create a new parameter struct
p := cs.LoadBalancer.NewCreateLoadBalancerRuleParams(
d.Get("algorithm").(string),
d.Get("name").(string),
d.Get("private_port").(int),
d.Get("public_port").(int),
)
// Don't autocreate a firewall rule, use a resource if needed
p.SetOpenfirewall(false)
// Set the description
if description, ok := d.GetOk("description"); ok {
p.SetDescription(description.(string))
} else {
p.SetDescription(d.Get("name").(string))
}
if networkid, ok := d.GetOk("network_id"); ok {
// Set the network id
p.SetNetworkid(networkid.(string))
}
// Set the protocol
if protocol, ok := d.GetOk("protocol"); ok {
p.SetProtocol(protocol.(string))
}
// Set the ipaddress id
p.SetPublicipid(d.Get("ip_address_id").(string))
// Create the load balancer rule
r, err := cs.LoadBalancer.CreateLoadBalancerRule(p)
if err != nil {
return err
}
// Set the load balancer rule ID and set partials
d.SetId(r.Id)
d.SetPartial("name")
d.SetPartial("description")
d.SetPartial("ip_address_id")
d.SetPartial("network_id")
d.SetPartial("algorithm")
d.SetPartial("private_port")
d.SetPartial("public_port")
d.SetPartial("protocol")
if certificateID, ok := d.GetOk("certificate_id"); ok {
// Create a new parameter struct
cp := cs.LoadBalancer.NewAssignCertToLoadBalancerParams(certificateID.(string), r.Id)
if _, err := cs.LoadBalancer.AssignCertToLoadBalancer(cp); err != nil {
return err
}
}
d.SetPartial("certificate_id")
// Create a new parameter struct
mp := cs.LoadBalancer.NewAssignToLoadBalancerRuleParams(r.Id)
var mbs []string
for _, id := range d.Get("member_ids").(*schema.Set).List() {
mbs = append(mbs, id.(string))
}
mp.SetVirtualmachineids(mbs)
_, err = cs.LoadBalancer.AssignToLoadBalancerRule(mp)
if err != nil {
return err
}
d.SetPartial("member_ids")
d.Partial(false)
return resourceCloudStackLoadBalancerRuleRead(d, meta)
}