Skip to content

Commit a066e60

Browse files
Terraform Team Automationravinitp
authored andcommitted
Added - Support for Terraform integration for MHS: Manual Cross region backup copy
1 parent 32a612c commit a066e60

12 files changed

+788
-6
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// It needs to be run against the destination region, for example,
2+
// if mysql/main.tf created the backup in IAD, and the copy should go to PHX,
3+
// then this needs to be run against the PHX endpoint.
4+
5+
// Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
6+
// Licensed under the Mozilla Public License v2.0
7+
8+
variable "tenancy_ocid" {
9+
}
10+
11+
variable "user_ocid" {
12+
}
13+
14+
variable "fingerprint" {
15+
}
16+
17+
variable "private_key_path" {
18+
}
19+
20+
variable "region" {
21+
// Define the region where destination backup will be created.
22+
// Example: region = "us-phoenix-1"
23+
}
24+
25+
variable "source_region" {
26+
// Define the region where the source backup is created.
27+
// Example: source_region = "us-ashburn-1"
28+
}
29+
30+
variable "source_backup_id" {
31+
// Define the source backup ID to be copied when using resource
32+
// oci_mysql_mysql_backup.test_mysql_backup_cross_region_backup_copy
33+
}
34+
35+
variable "compartment_ocid" {
36+
}
37+
38+
provider "oci" {
39+
tenancy_ocid = var.tenancy_ocid
40+
user_ocid = var.user_ocid
41+
fingerprint = var.fingerprint
42+
private_key_path = var.private_key_path
43+
region = var.region
44+
}
45+
46+
resource "oci_mysql_mysql_backup" "test_mysql_backup_cross_region_backup_copy" {
47+
# Required
48+
source_details {
49+
region = var.source_region
50+
backup_id = var.source_backup_id
51+
compartment_id = var.compartment_ocid
52+
}
53+
54+
# Optional
55+
display_name = "CrossRegionBackupCopy"
56+
description = "test backup copy created by terraform"
57+
}

examples/mysql/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ variable "private_key_path" {
1313
}
1414

1515
variable "region" {
16-
}
16+
// Define the region where destination backup will be created.
17+
}
1718

1819
variable "compartment_ocid" {
1920
}
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
package integrationtest
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/oracle/terraform-provider-oci/internal/acctest"
10+
tf_client "github.com/oracle/terraform-provider-oci/internal/client"
11+
"github.com/oracle/terraform-provider-oci/internal/tfresource"
12+
"github.com/oracle/terraform-provider-oci/internal/utils"
13+
14+
oci_common "github.com/oracle/oci-go-sdk/v65/common"
15+
oci_core "github.com/oracle/oci-go-sdk/v65/core"
16+
oci_identity "github.com/oracle/oci-go-sdk/v65/identity"
17+
oci_mysql "github.com/oracle/oci-go-sdk/v65/mysql"
18+
)
19+
20+
func createDbSystemInRegion(clients *tf_client.OracleClients, region string) (string, error) {
21+
compartment := utils.GetEnvSettingWithBlankDefault("compartment_ocid")
22+
23+
dbSystemClient, err := oci_mysql.NewDbSystemClientWithConfigurationProvider(*clients.DbSystemClient().ConfigurationProvider())
24+
if err != nil {
25+
return "", fmt.Errorf("cannot Create client for the region %s: %v", region, err)
26+
}
27+
28+
err = tf_client.ConfigureClientVar(&dbSystemClient.BaseClient)
29+
if err != nil {
30+
return "", fmt.Errorf("cannot configure client for the region: %v", err)
31+
}
32+
33+
dbSystemClient.SetRegion(region)
34+
35+
identityClient, err := oci_identity.NewIdentityClientWithConfigurationProvider(*clients.IdentityClient().ConfigurationProvider())
36+
if err != nil {
37+
return "", fmt.Errorf("cannot Create client for the region %s: %v", region, err)
38+
}
39+
40+
err = tf_client.ConfigureClientVar(&identityClient.BaseClient)
41+
if err != nil {
42+
return "", fmt.Errorf("cannot configure client for the region: %v", err)
43+
}
44+
45+
identityClient.SetRegion(region)
46+
47+
listAvailabilityDomainsResponse, err := identityClient.ListAvailabilityDomains(context.Background(),
48+
oci_identity.ListAvailabilityDomainsRequest{
49+
CompartmentId: &compartment,
50+
})
51+
if err != nil {
52+
return "", fmt.Errorf("[WARN] failed to get the domain name with the error %v", err)
53+
}
54+
domain := listAvailabilityDomainsResponse.Items[0].Name
55+
56+
// Create subnet
57+
networkClient, err := oci_core.NewVirtualNetworkClientWithConfigurationProvider(*clients.VirtualNetworkClient().ConfigurationProvider())
58+
if err != nil {
59+
return "", fmt.Errorf("[WARN] cannot configure client for the region %v", err)
60+
}
61+
62+
cidrBlockVcn := cidrBlockVcn
63+
networkClient.SetRegion(region)
64+
createVcnResponse, err := networkClient.CreateVcn(context.Background(), oci_core.CreateVcnRequest{
65+
CreateVcnDetails: oci_core.CreateVcnDetails{
66+
CidrBlock: &cidrBlockVcn,
67+
CompartmentId: &compartment,
68+
}})
69+
70+
if err != nil {
71+
return "", fmt.Errorf("[WARN] failed to Create source VCN with the error %v", err)
72+
}
73+
74+
cidrBlockSubnet := cidrBlockSubnet
75+
76+
createSubnetResponse, err := networkClient.CreateSubnet(context.Background(), oci_core.CreateSubnetRequest{
77+
CreateSubnetDetails: oci_core.CreateSubnetDetails{
78+
CompartmentId: &compartment,
79+
CidrBlock: &cidrBlockSubnet,
80+
VcnId: createVcnResponse.Id,
81+
},
82+
})
83+
84+
// Required DB System configurations
85+
shape := "MySQL.VM.Standard.E3.1.8GB"
86+
adminPassword := "BEstrO0ng_#11"
87+
adminUsername := "adminUser"
88+
89+
createDbSystemResponse, err := dbSystemClient.CreateDbSystem(context.Background(), oci_mysql.CreateDbSystemRequest{
90+
CreateDbSystemDetails: oci_mysql.CreateDbSystemDetails{
91+
SubnetId: createSubnetResponse.Id,
92+
ShapeName: &shape,
93+
AvailabilityDomain: domain,
94+
CompartmentId: &compartment,
95+
AdminUsername: &adminUsername,
96+
AdminPassword: &adminPassword,
97+
},
98+
RequestMetadata: oci_common.RequestMetadata{
99+
RetryPolicy: tfresource.GetRetryPolicy(false, "mysql"),
100+
},
101+
})
102+
if err != nil {
103+
return "", fmt.Errorf("[WARN] failed to Create source DB System with the error %v", err)
104+
}
105+
106+
retryPolicy := tfresource.GetRetryPolicy(false, "mysql")
107+
retryPolicy.ShouldRetryOperation = acctest.ConditionShouldRetry(15*time.Minute, dbSystemActiveWaitCondition, "mysql", false)
108+
109+
_, err = dbSystemClient.GetDbSystem(context.Background(), oci_mysql.GetDbSystemRequest{
110+
DbSystemId: createDbSystemResponse.Id,
111+
RequestMetadata: oci_common.RequestMetadata{
112+
RetryPolicy: retryPolicy,
113+
},
114+
})
115+
if err != nil {
116+
return "", fmt.Errorf("[WARN] wait for dbSystemActiveWaitCondition failed for %s resource with error %v", *createDbSystemResponse.Id, err)
117+
} else {
118+
log.Printf("[INFO] end of WaitTillCondition for resource %s ", *createDbSystemResponse.Id)
119+
}
120+
121+
return *createDbSystemResponse.Id, nil
122+
}
123+
124+
func createBackupInRegion(clients *tf_client.OracleClients, region string, dbSystemId *string) (string, error) {
125+
dbBackupsClient, err := initializeDbBackupsClientWithConfigurationProvider(clients, region, "DbSystemClient")
126+
127+
createMysqlBackupResponse, err := dbBackupsClient.CreateBackup(context.Background(), oci_mysql.CreateBackupRequest{
128+
CreateBackupDetails: oci_mysql.CreateBackupDetails{
129+
DbSystemId: dbSystemId,
130+
},
131+
})
132+
if err != nil {
133+
return "", fmt.Errorf("[WARN] failed to Create source DB System Backup with the error %v", err)
134+
135+
}
136+
137+
retryPolicy := tfresource.GetRetryPolicy(false, "core")
138+
retryPolicy.ShouldRetryOperation = acctest.ConditionShouldRetry(10*time.Minute, mysqlBackupActiveWaitCondition, "mysql", false)
139+
_, err = dbBackupsClient.GetBackup(context.Background(), oci_mysql.GetBackupRequest{
140+
BackupId: createMysqlBackupResponse.Id,
141+
RequestMetadata: oci_common.RequestMetadata{
142+
RetryPolicy: retryPolicy,
143+
},
144+
})
145+
if err != nil {
146+
return "", fmt.Errorf("[WARN] wait for mysqlBackupActiveWaitCondition failed for %s resource with error %v", *createMysqlBackupResponse.Id, err)
147+
} else {
148+
log.Printf("[INFO] end of WaitTillCondition for resource %s ", *createMysqlBackupResponse.Id)
149+
}
150+
151+
return *createMysqlBackupResponse.Id, nil
152+
}
153+
154+
func deleteBackupInRegion(clients *tf_client.OracleClients, region string, mysqlBackupId string) error {
155+
dbBackupsClient, _ := initializeDbBackupsClientWithConfigurationProvider(clients, region, "DbBackupsClient")
156+
157+
if mysqlBackupId != "" {
158+
deleteMysqlBackupRequest := oci_mysql.DeleteBackupRequest{}
159+
deleteMysqlBackupRequest.BackupId = &mysqlBackupId
160+
161+
_, err := dbBackupsClient.DeleteBackup(context.Background(), deleteMysqlBackupRequest)
162+
if err != nil {
163+
return fmt.Errorf("failed to delete source DB System Backup %s resource with error %v", *deleteMysqlBackupRequest.BackupId, err)
164+
}
165+
}
166+
167+
return nil
168+
}
169+
170+
func deleteDbSystemInRegion(clients *tf_client.OracleClients, region string, mysqlDbSystemId string) error {
171+
dbSystemClient, err := oci_mysql.NewDbSystemClientWithConfigurationProvider(*clients.DbSystemClient().ConfigurationProvider())
172+
if err != nil {
173+
return fmt.Errorf("cannot Create client for the region %s: %v", region, err)
174+
}
175+
176+
err = tf_client.ConfigureClientVar(&dbSystemClient.BaseClient)
177+
if err != nil {
178+
return fmt.Errorf("cannot configure client for the region: %v", err)
179+
}
180+
181+
dbSystemClient.SetRegion(region)
182+
183+
if mysqlDbSystemId != "" {
184+
deleteMysqlDbSystemRequest := oci_mysql.DeleteDbSystemRequest{}
185+
deleteMysqlDbSystemRequest.DbSystemId = &mysqlDbSystemId
186+
187+
_, err := dbSystemClient.DeleteDbSystem(context.Background(), deleteMysqlDbSystemRequest)
188+
if err != nil {
189+
return fmt.Errorf("failed to delete source DB System %s resource with error %v", *deleteMysqlDbSystemRequest.DbSystemId, err)
190+
}
191+
}
192+
193+
return nil
194+
}
195+
196+
func dbSystemActiveWaitCondition(response oci_common.OCIOperationResponse) bool {
197+
if dbSystemResponse, ok := response.Response.(oci_mysql.GetDbSystemResponse); ok {
198+
return dbSystemResponse.LifecycleState != oci_mysql.DbSystemLifecycleStateActive
199+
}
200+
201+
return false
202+
}
203+
204+
func mysqlBackupActiveWaitCondition(response oci_common.OCIOperationResponse) bool {
205+
if dbBackupResponse, ok := response.Response.(oci_mysql.GetBackupResponse); ok {
206+
return dbBackupResponse.LifecycleState != oci_mysql.BackupLifecycleStateActive
207+
}
208+
209+
return false
210+
}
211+
212+
func initializeDbBackupsClientWithConfigurationProvider(clients *tf_client.OracleClients, region string, clientType string) (*oci_mysql.DbBackupsClient, error) {
213+
var dbBackupsClient oci_mysql.DbBackupsClient
214+
var err error
215+
216+
switch clientType {
217+
case "DbSystemClient":
218+
dbBackupsClient, err = oci_mysql.NewDbBackupsClientWithConfigurationProvider(*clients.DbSystemClient().ConfigurationProvider())
219+
case "DbBackupsClient":
220+
dbBackupsClient, err = oci_mysql.NewDbBackupsClientWithConfigurationProvider(*clients.DbBackupsClient().ConfigurationProvider())
221+
default:
222+
return nil, fmt.Errorf("unsupported client type: %s", clientType)
223+
}
224+
225+
if err != nil {
226+
return nil, fmt.Errorf("cannot create client for the region %s: %v", region, err)
227+
}
228+
229+
err = tf_client.ConfigureClientVar(&dbBackupsClient.BaseClient)
230+
if err != nil {
231+
return nil, fmt.Errorf("cannot configure client for the region: %v", err)
232+
}
233+
234+
dbBackupsClient.SetRegion(region)
235+
236+
return &dbBackupsClient, err
237+
}

0 commit comments

Comments
 (0)