Skip to content

Commit 60b060b

Browse files
authored
tests: Added backup restore from backup id example (#158)
1 parent b65ea46 commit 60b060b

File tree

9 files changed

+129
-1
lines changed

9 files changed

+129
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This module implements an instance of the IBM Cloud Messages for RabbitMQ servic
2020
* [Basic example](./examples/basic)
2121
* [Complete example with BYOK encryption, CBR rules, autoscaling, and service credentials creation](./examples/complete)
2222
* [Financial Services Cloud profile example with autoscaling enabled](./examples/fscloud)
23+
* [Restore from backup example](./examples/backup-restore)
2324
* [Contributing](#contributing)
2425
<!-- END OVERVIEW HOOK -->
2526

examples/backup-restore/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Restore from backup example
2+
3+
This example provides an end-to-end executable flow of how a RabbitMQ DB can be created from a backup instance. This example uses the IBM Cloud terraform provider to:
4+
5+
- Create a new resource group if one is not passed in.
6+
- Create a restored ICD RabbitMQ database instance pointing to the lastest backup of the existing RabbitMQ database instance crn passed.

examples/backup-restore/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "terraform-ibm-modules/resource-group/ibm"
7+
version = "1.1.5"
8+
# if an existing resource group is not set (null) create a new one using prefix
9+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
10+
existing_resource_group_name = var.resource_group
11+
}
12+
13+
data "ibm_database_backups" "backup_database" {
14+
deployment_id = var.rabbitmq_db_crn
15+
}
16+
17+
# New RabbitMQ db instance pointing to the backup instance
18+
module "restored_rabbitmq_db" {
19+
source = "../.."
20+
resource_group_id = module.resource_group.resource_group_id
21+
instance_name = "${var.prefix}-rabbitmq-restored"
22+
region = var.region
23+
rabbitmq_version = var.rabbitmq_version
24+
access_tags = var.access_tags
25+
tags = var.resource_tags
26+
backup_crn = data.ibm_database_backups.backup_database.backups[0].backup_id
27+
}

examples/backup-restore/outputs.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "restored_rabbitmq_db_id" {
6+
description = "Restored RabbitMQ db instance id"
7+
value = module.restored_rabbitmq_db.id
8+
}
9+
10+
output "restored_rabbitmq_db_version" {
11+
description = "Restored RabbitMQ instance version"
12+
value = module.restored_rabbitmq_db.version
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
variable "ibmcloud_api_key" {
2+
type = string
3+
description = "The IBM Cloud API Key"
4+
sensitive = true
5+
}
6+
7+
variable "region" {
8+
type = string
9+
description = "Region to provision all resources created by this example"
10+
default = "us-south"
11+
}
12+
13+
variable "prefix" {
14+
type = string
15+
description = "Prefix to append to all resources created by this example"
16+
default = "rabbitmq-restore"
17+
}
18+
19+
variable "resource_group" {
20+
type = string
21+
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
22+
default = null
23+
}
24+
25+
variable "rabbitmq_version" {
26+
type = string
27+
description = "Version of rabbitmq to deploy"
28+
default = null
29+
}
30+
31+
variable "resource_tags" {
32+
type = list(string)
33+
description = "Optional list of tags to be added to created resources"
34+
default = []
35+
}
36+
37+
variable "access_tags" {
38+
type = list(string)
39+
description = "A list of access tags to apply to the rabbitmq instance created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details"
40+
default = []
41+
}
42+
43+
variable "rabbitmq_db_crn" {
44+
type = string
45+
description = "The existing CRN of a rabbitMQ instance to fetch the latest backup crn."
46+
}

examples/backup-restore/version.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.3.0, <1.7.0"
3+
required_providers {
4+
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
5+
ibm = {
6+
source = "IBM-Cloud/ibm"
7+
version = ">=1.62.0, <2.0.0"
8+
}
9+
}
10+
}

tests/other_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package test
33

44
import (
5+
"fmt"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -50,3 +51,23 @@ func TestPlanICDVersions(t *testing.T) {
5051
t.Run(version, func(t *testing.T) { testPlanICDVersions(t, version) })
5152
}
5253
}
54+
55+
func TestRunRestoredDBExample(t *testing.T) {
56+
t.Parallel()
57+
58+
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
59+
Testing: t,
60+
TerraformDir: "examples/backup-restore",
61+
Prefix: "rabbitmq-restored",
62+
ResourceGroup: resourceGroup,
63+
Region: fmt.Sprint(permanentResources["rabbitmqRegion"]),
64+
TerraformVars: map[string]interface{}{
65+
"rabbitmq_db_crn": permanentResources["rabbitmqCrn"],
66+
},
67+
CloudInfoService: sharedInfoSvc,
68+
})
69+
70+
output, err := options.RunTestConsistency()
71+
assert.Nil(t, err, "This should not have errored")
72+
assert.NotNil(t, output, "Expected some output")
73+
}

0 commit comments

Comments
 (0)