Skip to content

Commit 1880447

Browse files
PITR using GCBDR datasource (#15015)
1 parent 210b0bf commit 1880447

File tree

5 files changed

+435
-4
lines changed

5 files changed

+435
-4
lines changed

mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
216216
Type: schema.TypeList,
217217
Optional: true,
218218
Computed: true,
219-
AtLeastOneOf: []string{"settings", "clone"},
219+
AtLeastOneOf: []string{"settings", "clone", "point_in_time_restore_context"},
220220
MaxItems: 1,
221221
Elem: &schema.Resource{
222222
Schema: map[string]*schema.Schema{
@@ -1217,7 +1217,7 @@ API (for read pools, effective_availability_type may differ from availability_ty
12171217
Type: schema.TypeList,
12181218
Optional: true,
12191219
Computed: false,
1220-
AtLeastOneOf: []string{"settings", "clone"},
1220+
AtLeastOneOf: []string{"settings", "clone", "point_in_time_restore_context"},
12211221
Description: `Configuration for creating a new instance as a clone of another instance.`,
12221222
MaxItems: 1,
12231223
Elem: &schema.Resource{
@@ -1254,6 +1254,44 @@ API (for read pools, effective_availability_type may differ from availability_ty
12541254
},
12551255
},
12561256
},
1257+
"point_in_time_restore_context": {
1258+
Type: schema.TypeList,
1259+
Optional: true,
1260+
Computed: false,
1261+
AtLeastOneOf: []string{"settings", "clone", "point_in_time_restore_context"},
1262+
Description: `Configuration for creating a new instance using point-in-time-restore from backupdr backup.`,
1263+
MaxItems: 1,
1264+
Elem: &schema.Resource{
1265+
Schema: map[string]*schema.Schema{
1266+
"datasource": {
1267+
Type: schema.TypeString,
1268+
Required: true,
1269+
Description: `The Google Cloud Backup and Disaster Recovery Datasource URI. For example: "projects/my-project/locations/us-central1/datasources/my-datasource".`,
1270+
},
1271+
"point_in_time": {
1272+
Type: schema.TypeString,
1273+
Optional: true,
1274+
DiffSuppressFunc: tpgresource.TimestampDiffSuppress(time.RFC3339Nano),
1275+
Description: `The date and time to which you want to restore the instance.`,
1276+
},
1277+
"preferred_zone": {
1278+
Type: schema.TypeString,
1279+
Optional: true,
1280+
Description: `Point-in-time recovery of an instance to the specified zone. If no zone is specified, then clone to the same primary zone as the source instance.`,
1281+
},
1282+
"allocated_ip_range": {
1283+
Type: schema.TypeString,
1284+
Optional: true,
1285+
Description: `The name of the allocated IP range for the internal IP Cloud SQL instance. For example: "google-managed-services-default". If you set this, then Cloud SQL creates the IP address for the cloned instance in the allocated range. This range must comply with [RFC 1035](https://tools.ietf.org/html/rfc1035) standards. Specifically, the name must be 1-63 characters long and match the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.`,
1286+
},
1287+
"target_instance": {
1288+
Type: schema.TypeString,
1289+
Optional: true,
1290+
Description: `The name of the target instance to restore to.`,
1291+
},
1292+
},
1293+
},
1294+
},
12571295
},
12581296
UseJSONNumber: true,
12591297
}
@@ -1346,6 +1384,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
13461384
}
13471385

13481386
cloneContext, cloneSource := expandCloneContext(d.Get("clone").([]interface{}))
1387+
pointInTimeRestoreContext := expandPointInTimeRestoreContext(d.Get("point_in_time_restore_context").([]interface{}))
13491388

13501389
s, ok := d.GetOk("settings")
13511390
desiredSettings := expandSqlDatabaseInstanceSettings(s.([]interface{}), databaseVersion)
@@ -1399,6 +1438,9 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
13991438
cloneContext.DestinationInstanceName = name
14001439
clodeReq := sqladmin.InstancesCloneRequest{CloneContext: cloneContext}
14011440
op, operr = config.NewSqlAdminClient(userAgent).Instances.Clone(project, cloneSource, &clodeReq).Do()
1441+
} else if pointInTimeRestoreContext != nil {
1442+
parent := fmt.Sprintf("projects/%s", project)
1443+
op, operr = config.NewSqlAdminClient(userAgent).Instances.PointInTimeRestore(parent, pointInTimeRestoreContext).Do()
14021444
} else {
14031445
op, operr = config.NewSqlAdminClient(userAgent).Instances.Insert(project, instance).Do()
14041446
}
@@ -1485,8 +1527,8 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
14851527

14861528
// Refresh settings from read as they may have defaulted from the API
14871529
s = d.Get("settings")
1488-
// If we've created an instance as a clone, we need to update it to set any user defined settings
1489-
if len(s.([]interface{})) != 0 && cloneContext != nil && desiredSettings != nil {
1530+
// If we've created an instance as a clone or pitr, we need to update it to set any user defined settings
1531+
if len(s.([]interface{})) != 0 && (cloneContext != nil || pointInTimeRestoreContext != nil) && desiredSettings != nil {
14901532
instanceUpdate := &sqladmin.DatabaseInstance{
14911533
Settings: desiredSettings,
14921534
}
@@ -3086,6 +3128,47 @@ func sqlDatabaseInstanceRestoreFromBackup(d *schema.ResourceData, config *transp
30863128
return nil
30873129
}
30883130

3131+
func expandPointInTimeRestoreContext(configured []interface{}) *sqladmin.PointInTimeRestoreContext {
3132+
if len(configured) == 0 || configured[0] == nil {
3133+
return nil
3134+
}
3135+
3136+
_pitrc := configured[0].(map[string]interface{})
3137+
return &sqladmin.PointInTimeRestoreContext{
3138+
PointInTime: _pitrc["point_in_time"].(string),
3139+
PreferredZone: _pitrc["preferred_zone"].(string),
3140+
Datasource: _pitrc["datasource"].(string),
3141+
AllocatedIpRange: _pitrc["allocated_ip_range"].(string),
3142+
TargetInstance: _pitrc["target_instance"].(string),
3143+
}
3144+
}
3145+
3146+
func sqlDatabaseInstancePointInTimeRestore(d *schema.ResourceData, config *transport_tpg.Config, userAgent, project, instanceId string, r interface{}) error {
3147+
log.Printf("[DEBUG] Initiating GCBDR managed SQL database instance backup point in time restore")
3148+
pointInTimeRestoreContext := r.([]interface{})
3149+
parent := fmt.Sprintf("projects/%s", project)
3150+
3151+
var op *sqladmin.Operation
3152+
err := transport_tpg.Retry(transport_tpg.RetryOptions{
3153+
RetryFunc: func() (operr error) {
3154+
op, operr = config.NewSqlAdminClient(userAgent).Instances.PointInTimeRestore(parent, expandPointInTimeRestoreContext(pointInTimeRestoreContext)).Do()
3155+
return operr
3156+
},
3157+
Timeout: d.Timeout(schema.TimeoutUpdate),
3158+
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsSqlOperationInProgressError},
3159+
})
3160+
if err != nil {
3161+
return fmt.Errorf("Error, failed to point in restore an instance %s: %s", instanceId, err)
3162+
}
3163+
3164+
err = SqlAdminOperationWaitTime(config, op, project, "Point in Time Restore", userAgent, d.Timeout(schema.TimeoutUpdate))
3165+
if err != nil {
3166+
return err
3167+
}
3168+
3169+
return nil
3170+
}
3171+
30893172
func caseDiffDashSuppress(_, old, new string, _ *schema.ResourceData) bool {
30903173
postReplaceNew := strings.Replace(new, "-", "_", -1)
30913174
return strings.ToUpper(postReplaceNew) == strings.ToUpper(old)

mmv1/third_party/terraform/services/sql/resource_sql_database_instance_meta.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ fields:
2525
- field: 'maintenance_version'
2626
- field: 'master_instance_name'
2727
- field: 'name'
28+
- field: 'point_in_time_restore.allocated_ip_range'
29+
- field: 'point_in_time_restore.datasource'
30+
- field: 'point_in_time_restore.target_instance'
31+
- field: 'point_in_time_restore.point_in_time'
32+
- field: 'point_in_time_restore.preferred_zone'
2833
- field: 'private_ip_address'
2934
- field: 'project'
3035
- field: 'psc_service_attachment_link'

0 commit comments

Comments
 (0)