Skip to content

Commit

Permalink
chore(example): Add a working example (FlexibleEngineCloud#874)
Browse files Browse the repository at this point in the history
* docs: Fix typo

* chore(example): Add a working example
  • Loading branch information
gaetanars authored Dec 22, 2022
1 parent cb653d3 commit 1d6ac42
Show file tree
Hide file tree
Showing 25 changed files with 221 additions and 196 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ vendor/
# Test exclusions
!command/test-fixtures/**/*.tfstate
!command/test-fixtures/**/.terraform/

examples/*.tfvars
2 changes: 1 addition & 1 deletion docs/resources/csbs_backup_policy_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ page_title: "flexibleengine_csbs_backup_policy_v1"

# flexibleengine_csbs_backup_policy_v1

Provides an FlexibleEngine Backup Policy of Resources.
Provides a FlexibleEngine Backup Policy of Resources.

## Example Usage

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/csbs_backup_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ page_title: "flexibleengine_csbs_backup_v1"

# flexibleengine_csbs_backup_v1

Provides an FlexibleEngine Backup of Resources.
Provides a FlexibleEngine Backup of Resources.

## Example Usage

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/rts_stack_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ page_title: "flexibleengine_rts_stack_v1"

# flexibleengine_rts_stack_v1

Provides an FlexibleEngine Stack.
Provides a FlexibleEngine Stack.

## Example Usage

Expand Down
67 changes: 47 additions & 20 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
# FULL Terraform flexibleengine Example

This script will create the following resources (if enabled):
* Volumes
* Floating IPs
* Neutron Ports
* Instances

* VPC
* VPC Subnet
* Keypair
* Network
* Subnet
* Router
* Router Interface
* Loadbalancer
* Templates
* Instances
* Security Group (Allow ICMP, 80/tcp, 22/tcp)
* Volumes
* EIP
* Loadbalancer
* Backup Policies and Backups

## Usage

## Resource Creation
This example will, by default not create Volumes. This is to show how to enable resources via parameters.
To enable Volume creation, set the **disk__size_gb** variable to a value > 10.

This example will, by default not create Volumes. This is to show how to enable resources via parameters. To enable Volume creation, set the **disk__size_gb** variable to a value > 10.
Add your variables to a file called **terraform.tfvars** or set them as environment variables.

```shell
export TF_VAR_access_key=your_access_key
export TF_VAR_secret_key=your_secret_key
export TF_VAR_domain_name=your_domain_name
```

Then run the following commands:

```shell
terraform init
terraform plan
terraform apply
```

Observe the output of the apply command, it will show you the external IP address of the loadbalancer.

##  Destroy

```shell
terraform destroy
```

You can encounter an error when destroying the bucket, this is because the bucket is not empty. You can delet
e the objects manually in the console and run the destroy command again.

## Available Variables

### Required

* **username** (your flexibleengine username)
* **password** (your flexibleengine password)
* **access_key** (your flexibleengine access_key)
* **secret_key** (your flexibleengine secret_key)
* **domain_name** (your flexibleengine domain name)
* You must have a **ssh_pub_key** file defined, or terraform will complain, see default path below.

### Optional
* **project** (this will prefix all your resources, _default=terraform_)
* **ssh_pub_key** (the path to the ssh public key you want to deploy, _default=~/.ssh/id_rsa.pub_)
* **instance_count** (affects the number of Floating IPs, Instances, Volumes and Ports, _default=1_)
* **flavor_name** (flavor of the created instances, _default=s1.medium_)
* **image_name** (image used for creating instances, _default=Standard_CentOS_7_latest_)
* **disk_size_gb** (size of the volumes in gigabytes, _default=None_)

* **project** (this will prefix all your resources, *default=terraform*)
* **ssh_pub_key** (the path to the ssh public key you want to deploy, *default=~/.ssh/id_rsa.pub*)
* **instance_count** (affects the number of Floating IPs, Instances, Volumes and Ports, *default=1*)
* **flavor_name** (flavor of the created instances, *default=s6.medium.2*)
* **image_name** (image used for creating instances, *default=OBS Ubuntu 22.04*)
* **disk_size_gb** (size of the volumes in gigabytes, *default=None*)
26 changes: 16 additions & 10 deletions examples/antiddos.tf
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
resource "flexibleengine_vpc_eip_v1" "eip_1" {
resource "flexibleengine_vpc_eip" "eip_1" {
publicip {
type = "5_bgp"
}
bandwidth {
name = "test"
size = 8
share_type = "PER"
name = "test"
size = 8
share_type = "PER"
charge_mode = "traffic"
}
}

resource "flexibleengine_vpc_eip_associate" "associated" {
public_ip = flexibleengine_vpc_eip.eip_1.address
fixed_ip = flexibleengine_lb_loadbalancer_v2.loadbalancer.vip_address
network_id = flexibleengine_vpc_subnet_v1.subnet.id
}

resource "flexibleengine_antiddos_v1" "antiddos_1" {
floating_ip_id = "${flexibleengine_vpc_eip_v1.eip_1.id}"
enable_l7 = true
traffic_pos_id = 1
http_request_pos_id = 2
floating_ip_id = flexibleengine_vpc_eip.eip_1.id
enable_l7 = true
traffic_pos_id = 1
http_request_pos_id = 2
cleaning_access_pos_id = 1
app_type_id = 0
}
app_type_id = 0
}
11 changes: 6 additions & 5 deletions examples/csbs_backup.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resource "flexibleengine_csbs_backup_v1" "backup_v1" {
backup_name = "${var.project}-backup"
resource_id = "${flexibleengine_compute_instance_v2.webserver.id}"
resource_type = "OS::Nova::Server"
}
# resource "flexibleengine_csbs_backup_v1" "backup_v1" {
# count = var.instance_count
# backup_name = "${var.project}-${flexibleengine_compute_instance_v2.webserver.*.name[count.index]}-backup"
# resource_id = flexibleengine_compute_instance_v2.webserver.*.id[count.index]
# resource_type = "OS::Nova::Server"
# }
26 changes: 15 additions & 11 deletions examples/csbs_backup_policy.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
resource "flexibleengine_csbs_backup_policy_v1" "backup_policy_v1" {
name = "${var.project}-policy"
resource {
id = "${flexibleengine_compute_instance_v2.webserver.id}"
type = "OS::Nova::Server"
name = "resource1"
name = "${var.project}-csbs-policy"
dynamic "resource" {
for_each = flexibleengine_compute_instance_v2.webserver.*.id
content {
id = resource.value
type = "OS::Nova::Server"
name = flexibleengine_compute_instance_v2.webserver.*.name[resource.key]
}
}

scheduled_operation {
name = "mybackup"
enabled = true
description = "My backup policy"
operation_type = "backup"
max_backups = 2
trigger_pattern = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nRRULE:FREQ=WEEKLY;BYDAY=TH;BYHOUR=12;BYMINUTE=27\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
name = "mybackup"
enabled = true
description = "My backup policy"
operation_type = "backup"
max_backups = 2
trigger_pattern = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nRRULE:FREQ=WEEKLY;BYDAY=TH;BYHOUR=12;BYMINUTE=27\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
}
}
26 changes: 14 additions & 12 deletions examples/cts_tracker.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
resource "random_string" "bucket" {
length = 16
upper = false
override_special = "-"
}

resource "flexibleengine_s3_bucket" "bucket" {
bucket = "tf-test-bucket"
acl = "public-read"
bucket = "${var.project}-${random_string.bucket.result}-bucket"
acl = "public-read"
}

resource "flexibleengine_smn_topic_v2" "topic_1" {
name = "topic_check"
display_name = "The display name of topic_1"
name = "topic_check"
display_name = "The display name of topic_1"
}

resource "flexibleengine_cts_tracker_v1" "tracker_v1" {
bucket_name = "${flexibleengine_s3_bucket.bucket.bucket}"
file_prefix_name = "yO8Q"
is_support_smn = true
topic_id = "${flexibleengine_smn_topic_v2.topic_1.id}"
is_send_all_key_operation = false
operations = ["delete","create","login"]
need_notify_user_list = ["user1"]
}
bucket_name = flexibleengine_s3_bucket.bucket.bucket
file_prefix_name = "yO8Q"
}
21 changes: 11 additions & 10 deletions examples/instances.tf
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
resource "flexibleengine_compute_instance_v2" "webserver" {
count = "${var.instance_count}"
name = "${var.project}-webserver${format("%02d", count.index+1)}"
image_name = "${var.image_name}"
flavor_name = "${var.flavor_name}"
key_pair = "${flexibleengine_compute_keypair_v2.keypair.name}"
count = var.instance_count
name = "${var.project}-webserver${format("%02d", count.index + 1)}"
image_name = var.image_name
flavor_name = var.flavor_name
key_pair = flexibleengine_compute_keypair_v2.keypair.name
user_data = "#cloud-config\npackage_update: true\npackages: ['nginx-light']\n"
security_groups = [
"${flexibleengine_compute_secgroup_v2.secgrp_web.name}"
"${flexibleengine_networking_secgroup_v2.secgrp_web.name}"
]

network {
uuid = "${flexibleengine_networking_network_v2.network.id}"
uuid = flexibleengine_vpc_subnet_v1.subnet.id
}
}

resource "flexibleengine_compute_volume_attach_v2" "volume_attach" {
count = "${var.disk_size_gb > 0 ? var.instance_count : 0}"
instance_id = "${element(flexibleengine_compute_instance_v2.webserver.*.id, count.index)}"
volume_id = "${element(flexibleengine_blockstorage_volume_v2.volume.*.id, count.index)}"
count = var.disk_size_gb > 0 ? var.instance_count : 0
instance_id = element(flexibleengine_compute_instance_v2.webserver.*.id, count.index)
volume_id = element(flexibleengine_blockstorage_volume_v2.volume.*.id, count.index)
}
3 changes: 1 addition & 2 deletions examples/keypairs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
resource "flexibleengine_compute_keypair_v2" "keypair" {
count = "${var.instance_count}"
name = "${var.project}-terraform_key"
public_key = "${file("${var.ssh_pub_key}")}"
public_key = file("${var.ssh_pub_key}")
}
30 changes: 12 additions & 18 deletions examples/loadbalancer.tf
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
resource "flexibleengine_lb_loadbalancer_v2" "loadbalancer" {
count = "${var.instance_count}"
name = "${var.project}-loadbalancer"
vip_subnet_id = "${flexibleengine_networking_subnet_v2.subnet.id}"
vip_subnet_id = flexibleengine_vpc_subnet_v1.subnet.ipv4_subnet_id
admin_state_up = "true"
depends_on = ["flexibleengine_networking_router_interface_v2.interface"]
depends_on = [flexibleengine_vpc_subnet_v1.subnet]
}

resource "flexibleengine_lb_listener_v2" "listener" {
name = "${var.project}-listener"
count = "${var.instance_count}"
protocol = "HTTP"
protocol_port = 80
loadbalancer_id = "${flexibleengine_lb_loadbalancer_v2.loadbalancer.id}"
admin_state_up = "true"
#connection_limit = "-1"
name = "${var.project}-listener"
protocol = "HTTP"
protocol_port = 80
loadbalancer_id = flexibleengine_lb_loadbalancer_v2.loadbalancer.id
}

resource "flexibleengine_lb_pool_v2" "pool" {
protocol = "HTTP"
count = "${var.instance_count}"
lb_method = "ROUND_ROBIN"
listener_id = "${flexibleengine_lb_listener_v2.listener.id}"
listener_id = flexibleengine_lb_listener_v2.listener.id
}

resource "flexibleengine_lb_member_v2" "member" {
count = "${var.instance_count}"
address = "${element(flexibleengine_compute_instance_v2.webserver.*.access_ip_v4, count.index)}"
pool_id = "${flexibleengine_lb_pool_v2.pool.id}"
subnet_id = "${flexibleengine_networking_subnet_v2.subnet.id}"
count = var.instance_count
address = element(flexibleengine_compute_instance_v2.webserver.*.access_ip_v4, count.index)
pool_id = flexibleengine_lb_pool_v2.pool.id
subnet_id = flexibleengine_vpc_subnet_v1.subnet.ipv4_subnet_id
protocol_port = 80
}

resource "flexibleengine_lb_monitor_v2" "monitor" {
pool_id = "${flexibleengine_lb_pool_v2.pool.id}"
count = "${var.instance_count}"
pool_id = flexibleengine_lb_pool_v2.pool.id
type = "HTTP"
url_path = "/"
expected_codes = "200"
Expand Down
21 changes: 10 additions & 11 deletions examples/network.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
resource "flexibleengine_networking_network_v2" "network" {
count = "${var.instance_count}"
name = "${var.project}-network"
admin_state_up = "true"
resource "flexibleengine_vpc_v1" "vpc" {
name = "${var.project}-vpc"
cidr = var.vpc_cidr
}

resource "flexibleengine_networking_subnet_v2" "subnet" {
name = "${var.project}-subnet"
count = "${var.instance_count}"
network_id = "${flexibleengine_networking_network_v2.network.id}"
cidr = "${var.subnet_cidr}"
ip_version = 4
dns_nameservers = ["8.8.8.8", "8.8.4.4"]
resource "flexibleengine_vpc_subnet_v1" "subnet" {
name = "${var.project}-subnet"
cidr = var.subnet_cidr
gateway_ip = var.gateway_ip
vpc_id = flexibleengine_vpc_v1.vpc.id
dns_list = ["1.1.1.1", "9.9.9.9"]

}
3 changes: 3 additions & 0 deletions examples/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "webserver_url" {
value = "http://${flexibleengine_vpc_eip.eip_1.address}/"
}
25 changes: 20 additions & 5 deletions examples/provider.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
terraform {
required_providers {
flexibleengine = {
source = "FlexibleEngineCloud/flexibleengine"
version = "1.35.1"
}
random = {
source = "hashicorp/random"
}
}
}


provider "random" {}

provider "flexibleengine" {
user_name = "${var.username}"
password = "${var.password}"
tenant_name = "${var.tenant_name}"
domain_name = "${var.domain_name}"
auth_url = "${var.endpoint}"
access_key = var.access_key
secret_key = var.secret_key
tenant_name = var.tenant_name
domain_name = var.domain_name
region = var.region
}
13 changes: 0 additions & 13 deletions examples/router.tf

This file was deleted.

Loading

0 comments on commit 1d6ac42

Please sign in to comment.