Update loadbalancer example to use stackit sources instead of openstack (#601)

Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
This commit is contained in:
Alexander Dahmen 2024-12-02 13:52:12 +01:00 committed by GitHub
parent 0b1b13eb80
commit 58e99b4d67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 95 additions and 195 deletions

View file

@ -4,31 +4,7 @@ page_title: "stackit_loadbalancer Resource - stackit"
subcategory: "" subcategory: ""
description: |- description: |-
Setting up supporting infrastructure Setting up supporting infrastructure
Configuring an OpenStack provider The example below creates the supporting infrastructure using the STACKIT Terraform provider, including the network, network interface, a public IP address and server resources.
To automate the creation of load balancers, OpenStack can be used to setup the supporting infrastructure.
To set up the OpenStack provider, you can create a token through the STACKIT Portal, in your project's Infrastructure API page.
There, the OpenStack user domain name, username, and password are generated and can be obtained. The provider can then be configured as follows:
terraform {
required_providers {
(...)
openstack = {
source = "terraform-provider-openstack/openstack"
}
}
}
provider "openstack" {
user_domain_name = "{OpenStack user domain name}"
user_name = "{OpenStack username}"
password = "{OpenStack password}"
region = "RegionOne"
auth_url = "https://keystone.api.iaas.eu01.stackit.cloud/v3"
}
Configuring the supporting infrastructure
The example below uses OpenStack to create the network, router, a public IP address and a compute instance.
--- ---
# stackit_loadbalancer (Resource) # stackit_loadbalancer (Resource)
@ -36,99 +12,60 @@ description: |-
## Setting up supporting infrastructure ## Setting up supporting infrastructure
### Configuring an OpenStack provider The example below creates the supporting infrastructure using the STACKIT Terraform provider, including the network, network interface, a public IP address and server resources.
To automate the creation of load balancers, OpenStack can be used to setup the supporting infrastructure.
To set up the OpenStack provider, you can create a token through the STACKIT Portal, in your project's Infrastructure API page.
There, the OpenStack user domain name, username, and password are generated and can be obtained. The provider can then be configured as follows:
```terraform
terraform {
required_providers {
(...)
openstack = {
source = "terraform-provider-openstack/openstack"
}
}
}
provider "openstack" {
user_domain_name = "{OpenStack user domain name}"
user_name = "{OpenStack username}"
password = "{OpenStack password}"
region = "RegionOne"
auth_url = "https://keystone.api.iaas.eu01.stackit.cloud/v3"
}
```
### Configuring the supporting infrastructure
The example below uses OpenStack to create the network, router, a public IP address and a compute instance.
## Example Usage ## Example Usage
```terraform ```terraform
# Create a network # Create a network
resource "openstack_networking_network_v2" "example" { resource "stackit_network" "example_network" {
name = "example-network" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} name = "example-network"
ipv4_nameservers = ["8.8.8.8"]
# Create a subnet ipv4_prefix = "192.168.0.0/25"
resource "openstack_networking_subnet_v2" "example" { labels = {
name = "example-subnet" "key" = "value"
cidr = "192.168.0.0/25"
dns_nameservers = ["8.8.8.8"]
network_id = openstack_networking_network_v2.example.id
}
# Get public network
data "openstack_networking_network_v2" "public" {
name = "floating-net"
}
# Create a floating IP
resource "openstack_networking_floatingip_v2" "example" {
pool = data.openstack_networking_network_v2.public.name
}
# Get flavor for instance
data "openstack_compute_flavor_v2" "example" {
name = "g1.1"
}
# Create an instance
resource "openstack_compute_instance_v2" "example" {
depends_on = [openstack_networking_subnet_v2.example]
name = "example-instance"
flavor_id = data.openstack_compute_flavor_v2.example.id
admin_pass = "example"
security_groups = ["default"]
block_device {
uuid = "4364cdb2-dacd-429b-803e-f0f7cfde1c24" // Ubuntu 22.04
volume_size = 32
source_type = "image"
destination_type = "volume"
delete_on_termination = true
} }
routed = true
}
network { # Create a network interface
name = openstack_networking_network_v2.example.name resource "stackit_network_interface" "nic" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
network_id = stackit_network.example_network.network_id
}
# Create a public IP and assign it to the network interface
resource "stackit_public_ip" "public-ip" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
network_interface_id = stackit_network_interface.nic.network_interface_id
}
# Create a key pair for accessing the server instance
resource "stackit_key_pair" "keypair" {
name = "example-key-pair"
public_key = chomp(file("path/to/id_rsa.pub"))
}
# Create a server instance
resource "stackit_server" "boot-from-volume" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-server"
boot_volume = {
size = 64
source_type = "image"
source_id = "59838a89-51b1-4892-b57f-b3caf598ee2f" // Ubuntu 24.04
} }
availability_zone = "xxxx-x"
machine_type = "g1.1"
keypair_name = stackit_key_pair.keypair.name
} }
# Create a router and attach it to the public network # Attach the network interface to the server
resource "openstack_networking_router_v2" "example" { resource "stackit_server_network_interface_attach" "nic-attachment" {
name = "example-router" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
admin_state_up = "true" server_id = stackit_server.boot-from-volume.server_id
external_network_id = data.openstack_networking_network_v2.public.id network_interface_id = stackit_network_interface.nic.network_interface_id
}
# Attach the subnet to the router
resource "openstack_networking_router_interface_v2" "example_interface" {
router_id = openstack_networking_router_v2.example.id
subnet_id = openstack_networking_subnet_v2.example.id
} }
# Create a load balancer # Create a load balancer
@ -142,7 +79,7 @@ resource "stackit_loadbalancer" "example" {
targets = [ targets = [
{ {
display_name = "example-target" display_name = "example-target"
ip = openstack_compute_instance_v2.example.network.0.fixed_ip_v4 ip = stackit_network_interface.nic.ipv4
} }
] ]
active_health_check = { active_health_check = {
@ -164,11 +101,11 @@ resource "stackit_loadbalancer" "example" {
] ]
networks = [ networks = [
{ {
network_id = openstack_networking_network_v2.example.id network_id = stackit_network.example_network.network_id
role = "ROLE_LISTENERS_AND_TARGETS" role = "ROLE_LISTENERS_AND_TARGETS"
} }
] ]
external_address = openstack_networking_floatingip_v2.example.address external_address = stackit_public_ip.public-ip.ip
options = { options = {
private_network_only = false private_network_only = false
} }

View file

@ -1,63 +1,52 @@
# Create a network # Create a network
resource "openstack_networking_network_v2" "example" { resource "stackit_network" "example_network" {
name = "example-network" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} name = "example-network"
ipv4_nameservers = ["8.8.8.8"]
# Create a subnet ipv4_prefix = "192.168.0.0/25"
resource "openstack_networking_subnet_v2" "example" { labels = {
name = "example-subnet" "key" = "value"
cidr = "192.168.0.0/25"
dns_nameservers = ["8.8.8.8"]
network_id = openstack_networking_network_v2.example.id
}
# Get public network
data "openstack_networking_network_v2" "public" {
name = "floating-net"
}
# Create a floating IP
resource "openstack_networking_floatingip_v2" "example" {
pool = data.openstack_networking_network_v2.public.name
}
# Get flavor for instance
data "openstack_compute_flavor_v2" "example" {
name = "g1.1"
}
# Create an instance
resource "openstack_compute_instance_v2" "example" {
depends_on = [openstack_networking_subnet_v2.example]
name = "example-instance"
flavor_id = data.openstack_compute_flavor_v2.example.id
admin_pass = "example"
security_groups = ["default"]
block_device {
uuid = "4364cdb2-dacd-429b-803e-f0f7cfde1c24" // Ubuntu 22.04
volume_size = 32
source_type = "image"
destination_type = "volume"
delete_on_termination = true
} }
routed = true
}
network { # Create a network interface
name = openstack_networking_network_v2.example.name resource "stackit_network_interface" "nic" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
network_id = stackit_network.example_network.network_id
}
# Create a public IP and assign it to the network interface
resource "stackit_public_ip" "public-ip" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
network_interface_id = stackit_network_interface.nic.network_interface_id
}
# Create a key pair for accessing the server instance
resource "stackit_key_pair" "keypair" {
name = "example-key-pair"
public_key = chomp(file("path/to/id_rsa.pub"))
}
# Create a server instance
resource "stackit_server" "boot-from-volume" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-server"
boot_volume = {
size = 64
source_type = "image"
source_id = "59838a89-51b1-4892-b57f-b3caf598ee2f" // Ubuntu 24.04
} }
availability_zone = "xxxx-x"
machine_type = "g1.1"
keypair_name = stackit_key_pair.keypair.name
} }
# Create a router and attach it to the public network # Attach the network interface to the server
resource "openstack_networking_router_v2" "example" { resource "stackit_server_network_interface_attach" "nic-attachment" {
name = "example-router" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
admin_state_up = "true" server_id = stackit_server.boot-from-volume.server_id
external_network_id = data.openstack_networking_network_v2.public.id network_interface_id = stackit_network_interface.nic.network_interface_id
}
# Attach the subnet to the router
resource "openstack_networking_router_interface_v2" "example_interface" {
router_id = openstack_networking_router_v2.example.id
subnet_id = openstack_networking_subnet_v2.example.id
} }
# Create a load balancer # Create a load balancer
@ -71,7 +60,7 @@ resource "stackit_loadbalancer" "example" {
targets = [ targets = [
{ {
display_name = "example-target" display_name = "example-target"
ip = openstack_compute_instance_v2.example.network.0.fixed_ip_v4 ip = stackit_network_interface.nic.ipv4
} }
] ]
active_health_check = { active_health_check = {
@ -93,11 +82,11 @@ resource "stackit_loadbalancer" "example" {
] ]
networks = [ networks = [
{ {
network_id = openstack_networking_network_v2.example.id network_id = stackit_network.example_network.network_id
role = "ROLE_LISTENERS_AND_TARGETS" role = "ROLE_LISTENERS_AND_TARGETS"
} }
] ]
external_address = openstack_networking_floatingip_v2.example.address external_address = stackit_public_ip.public-ip.ip
options = { options = {
private_network_only = false private_network_only = false
} }

View file

@ -262,33 +262,7 @@ func (r *loadBalancerResource) Schema(_ context.Context, _ resource.SchemaReques
MarkdownDescription: ` MarkdownDescription: `
## Setting up supporting infrastructure` + "\n" + ` ## Setting up supporting infrastructure` + "\n" + `
### Configuring an OpenStack provider` + "\n" + ` The example below creates the supporting infrastructure using the STACKIT Terraform provider, including the network, network interface, a public IP address and server resources.
To automate the creation of load balancers, OpenStack can be used to setup the supporting infrastructure.
To set up the OpenStack provider, you can create a token through the STACKIT Portal, in your project's Infrastructure API page.
There, the OpenStack user domain name, username, and password are generated and can be obtained. The provider can then be configured as follows:` + "\n" +
"```terraform" + `
terraform {
required_providers {
(...)
openstack = {
source = "terraform-provider-openstack/openstack"
}
}
}
provider "openstack" {
user_domain_name = "{OpenStack user domain name}"
user_name = "{OpenStack username}"
password = "{OpenStack password}"
region = "RegionOne"
auth_url = "https://keystone.api.iaas.eu01.stackit.cloud/v3"
}
` + "\n```" + `
### Configuring the supporting infrastructure
The example below uses OpenStack to create the network, router, a public IP address and a compute instance.
`, `,
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{ "id": schema.StringAttribute{