Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lb_loadbalancer_v2: Add availability_zone #1225

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 1 addition & 44 deletions openstack/lb_v2_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"

octavialisteners "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
octavialoadbalancers "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
octaviamonitors "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
octaviapools "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"

Expand All @@ -26,6 +25,7 @@ import (
const octaviaLBClientType = "load-balancer"

const (
octaviaLBAvailabilityZoneMicroversion = "2.14"
octaviaLBQuotaRuleAndPolicyMicroversion = "2.19"
)

Expand Down Expand Up @@ -1067,49 +1067,6 @@ func expandLBMembersV2(members *schema.Set) []octaviapools.BatchUpdateMemberOpts
return m
}

// chooseLBV2LoadBalancerCreateOpts will determine which load balancer Create options to use:
// either the Octavia/LBaaS or the Neutron/Networking v2.
func chooseLBV2LoadBalancerCreateOpts(d *schema.ResourceData, config *Config) neutronloadbalancers.CreateOptsBuilder {
var createOpts neutronloadbalancers.CreateOptsBuilder

var lbProvider string
if v, ok := d.GetOk("loadbalancer_provider"); ok {
lbProvider = v.(string)
}

adminStateUp := d.Get("admin_state_up").(bool)

if config.UseOctavia {
// Use Octavia.
createOpts = octavialoadbalancers.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
VipNetworkID: d.Get("vip_network_id").(string),
VipSubnetID: d.Get("vip_subnet_id").(string),
VipPortID: d.Get("vip_port_id").(string),
ProjectID: d.Get("tenant_id").(string),
VipAddress: d.Get("vip_address").(string),
AdminStateUp: &adminStateUp,
FlavorID: d.Get("flavor_id").(string),
Provider: lbProvider,
}
} else {
// Use Neutron.
createOpts = neutronloadbalancers.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
VipSubnetID: d.Get("vip_subnet_id").(string),
TenantID: d.Get("tenant_id").(string),
VipAddress: d.Get("vip_address").(string),
AdminStateUp: &adminStateUp,
FlavorID: d.Get("flavor_id").(string),
Provider: lbProvider,
}
}

return createOpts
}

func resourceLoadBalancerV2SetSecurityGroups(networkingClient *gophercloud.ServiceClient, vipPortID string, d *schema.ResourceData) error {
if vipPortID != "" {
if v, ok := d.GetOk("security_group_ids"); ok {
Expand Down
48 changes: 44 additions & 4 deletions openstack/resource_openstack_lb_loadbalancer_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ func resourceLoadBalancerV2() *schema.Resource {
ForceNew: true,
},

"availability_zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"security_group_ids": {
Type: schema.TypeSet,
Optional: true,
Expand All @@ -120,14 +126,36 @@ func resourceLoadBalancerV2Create(d *schema.ResourceData, meta interface{}) erro
}

var (
lbID string
vipPortID string
lbID string
vipPortID string
lbProvider string
)

// Choose either the Octavia or Neutron create options.
createOpts := chooseLBV2LoadBalancerCreateOpts(d, config)
if v, ok := d.GetOk("loadbalancer_provider"); ok {
lbProvider = v.(string)
}

adminStateUp := d.Get("admin_state_up").(bool)

if lbClient.Type == octaviaLBClientType {
createOpts := octavialoadbalancers.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
VipNetworkID: d.Get("vip_network_id").(string),
VipSubnetID: d.Get("vip_subnet_id").(string),
VipPortID: d.Get("vip_port_id").(string),
ProjectID: d.Get("tenant_id").(string),
VipAddress: d.Get("vip_address").(string),
AdminStateUp: &adminStateUp,
FlavorID: d.Get("flavor_id").(string),
Provider: lbProvider,
}
if v, ok := d.GetOk("availability_zone"); ok {
lbClient.Microversion = octaviaLBAvailabilityZoneMicroversion
aZ := v.(string)
createOpts.AvailabilityZone = aZ
}

log.Printf("[DEBUG][Octavia] openstack_lb_loadbalancer_v2 create options: %#v", createOpts)
lb, err := octavialoadbalancers.Create(lbClient, createOpts).Extract()
if err != nil {
Expand All @@ -136,6 +164,17 @@ func resourceLoadBalancerV2Create(d *schema.ResourceData, meta interface{}) erro
lbID = lb.ID
vipPortID = lb.VipPortID
} else {
createOpts := neutronloadbalancers.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
VipSubnetID: d.Get("vip_subnet_id").(string),
TenantID: d.Get("tenant_id").(string),
VipAddress: d.Get("vip_address").(string),
AdminStateUp: &adminStateUp,
FlavorID: d.Get("flavor_id").(string),
Provider: lbProvider,
}

log.Printf("[DEBUG][Neutron] openstack_lb_loadbalancer_v2 create options: %#v", createOpts)
lb, err := neutronloadbalancers.Create(lbClient, createOpts).Extract()
if err != nil {
Expand Down Expand Up @@ -194,6 +233,7 @@ func resourceLoadBalancerV2Read(d *schema.ResourceData, meta interface{}) error
d.Set("admin_state_up", lb.AdminStateUp)
d.Set("flavor_id", lb.FlavorID)
d.Set("loadbalancer_provider", lb.Provider)
d.Set("availability_zone", lb.AvailabilityZone)
d.Set("region", GetRegion(d, config))
vipPortID = lb.VipPortID
} else {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/lb_loadbalancer_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ The following arguments are supported:
* `loadbalancer_provider` - (Optional) The name of the provider. Changing this
creates a new loadbalancer.

* `availability_zone` - (Optional) The availability zone of the Loadbalancer.
Changing this creates a new loadbalancer. Available only for Octavia
microversion 2.14 or later.

* `security_group_ids` - (Optional) A list of security group IDs to apply to the
loadbalancer. The security groups must be specified by ID and not name (as
opposed to how they are configured with the Compute Instance).
Expand All @@ -81,6 +85,7 @@ The following attributes are exported:
* `admin_state_up` - See Argument Reference above.
* `flavor_id` - See Argument Reference above.
* `loadbalancer_provider` - See Argument Reference above.
* `availability_zone` - See Argument Reference above.
* `security_group_ids` - See Argument Reference above.
* `vip_port_id` - The Port ID of the Load Balancer IP.

Expand Down