#!/bin/bash
# Load balancer ID passed as the first argument
LOAD_BALANCER_ID=$1
if [ -z "$LOAD_BALANCER_ID" ]; then
echo "Usage: $0 <load_balancer_id>"
exit 1
fi
# Get the port ID and private IP of the load balancer
PORT_ID=$(openstack loadbalancer show $LOAD_BALANCER_ID -c vip_port_id -f value)
PRIVATE_IP=$(openstack loadbalancer show $LOAD_BALANCER_ID -c vip_address -f value)
if [ -z "$PORT_ID" ] || [ -z "$PRIVATE_IP" ]; then
echo "Failed to get port ID or private IP for load balancer $LOAD_BALANCER_ID"
exit 1
fi
# Allocate a floating IP from the default public network
FLOATING_IP=$(openstack floating ip create public -c floating_ip_address -f value)
if [ -z "$FLOATING_IP" ]; then
echo "Failed to allocate floating IP"
exit 1
fi
# Associate the floating IP with the port
openstack floating ip set --port $PORT_ID $FLOATING_IP
if [ $? -eq 0 ]; then
echo "Floating IP $FLOATING_IP successfully associated with load balancer $LOAD_BALANCER_ID (private IP: $PRIVATE_IP)"
else
echo "Failed to associate floating IP with the port"
fi