Skip to content

Commit 105af30

Browse files
PITR using GCBDR datasource (#15015) (#10786)
[upstream:1880447eff22371ca3ad8b63064c11659b6d453d] Signed-off-by: Modular Magician <[email protected]>
1 parent 24ed7f4 commit 105af30

File tree

6 files changed

+438
-4
lines changed

6 files changed

+438
-4
lines changed

.changelog/15015.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sql: added `point_in_time_restore_context` field to `google_sql_database_instance`
3+
```

google-beta/services/sql/resource_sql_database_instance.go

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
231231
Type: schema.TypeList,
232232
Optional: true,
233233
Computed: true,
234-
AtLeastOneOf: []string{"settings", "clone"},
234+
AtLeastOneOf: []string{"settings", "clone", "point_in_time_restore_context"},
235235
MaxItems: 1,
236236
Elem: &schema.Resource{
237237
Schema: map[string]*schema.Schema{
@@ -1228,7 +1228,7 @@ API (for read pools, effective_availability_type may differ from availability_ty
12281228
Type: schema.TypeList,
12291229
Optional: true,
12301230
Computed: false,
1231-
AtLeastOneOf: []string{"settings", "clone"},
1231+
AtLeastOneOf: []string{"settings", "clone", "point_in_time_restore_context"},
12321232
Description: `Configuration for creating a new instance as a clone of another instance.`,
12331233
MaxItems: 1,
12341234
Elem: &schema.Resource{
@@ -1265,6 +1265,44 @@ API (for read pools, effective_availability_type may differ from availability_ty
12651265
},
12661266
},
12671267
},
1268+
"point_in_time_restore_context": {
1269+
Type: schema.TypeList,
1270+
Optional: true,
1271+
Computed: false,
1272+
AtLeastOneOf: []string{"settings", "clone", "point_in_time_restore_context"},
1273+
Description: `Configuration for creating a new instance using point-in-time-restore from backupdr backup.`,
1274+
MaxItems: 1,
1275+
Elem: &schema.Resource{
1276+
Schema: map[string]*schema.Schema{
1277+
"datasource": {
1278+
Type: schema.TypeString,
1279+
Required: true,
1280+
Description: `The Google Cloud Backup and Disaster Recovery Datasource URI. For example: "projects/my-project/locations/us-central1/datasources/my-datasource".`,
1281+
},
1282+
"point_in_time": {
1283+
Type: schema.TypeString,
1284+
Optional: true,
1285+
DiffSuppressFunc: tpgresource.TimestampDiffSuppress(time.RFC3339Nano),
1286+
Description: `The date and time to which you want to restore the instance.`,
1287+
},
1288+
"preferred_zone": {
1289+
Type: schema.TypeString,
1290+
Optional: true,
1291+
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.`,
1292+
},
1293+
"allocated_ip_range": {
1294+
Type: schema.TypeString,
1295+
Optional: true,
1296+
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])?.`,
1297+
},
1298+
"target_instance": {
1299+
Type: schema.TypeString,
1300+
Optional: true,
1301+
Description: `The name of the target instance to restore to.`,
1302+
},
1303+
},
1304+
},
1305+
},
12681306
},
12691307
UseJSONNumber: true,
12701308
}
@@ -1357,6 +1395,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
13571395
}
13581396

13591397
cloneContext, cloneSource := expandCloneContext(d.Get("clone").([]interface{}))
1398+
pointInTimeRestoreContext := expandPointInTimeRestoreContext(d.Get("point_in_time_restore_context").([]interface{}))
13601399

13611400
s, ok := d.GetOk("settings")
13621401
desiredSettings := expandSqlDatabaseInstanceSettings(s.([]interface{}), databaseVersion)
@@ -1410,6 +1449,9 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
14101449
cloneContext.DestinationInstanceName = name
14111450
clodeReq := sqladmin.InstancesCloneRequest{CloneContext: cloneContext}
14121451
op, operr = config.NewSqlAdminClient(userAgent).Instances.Clone(project, cloneSource, &clodeReq).Do()
1452+
} else if pointInTimeRestoreContext != nil {
1453+
parent := fmt.Sprintf("projects/%s", project)
1454+
op, operr = config.NewSqlAdminClient(userAgent).Instances.PointInTimeRestore(parent, pointInTimeRestoreContext).Do()
14131455
} else {
14141456
op, operr = config.NewSqlAdminClient(userAgent).Instances.Insert(project, instance).Do()
14151457
}
@@ -1496,8 +1538,8 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
14961538

14971539
// Refresh settings from read as they may have defaulted from the API
14981540
s = d.Get("settings")
1499-
// If we've created an instance as a clone, we need to update it to set any user defined settings
1500-
if len(s.([]interface{})) != 0 && cloneContext != nil && desiredSettings != nil {
1541+
// If we've created an instance as a clone or pitr, we need to update it to set any user defined settings
1542+
if len(s.([]interface{})) != 0 && (cloneContext != nil || pointInTimeRestoreContext != nil) && desiredSettings != nil {
15011543
instanceUpdate := &sqladmin.DatabaseInstance{
15021544
Settings: desiredSettings,
15031545
}
@@ -3092,6 +3134,47 @@ func sqlDatabaseInstanceRestoreFromBackup(d *schema.ResourceData, config *transp
30923134
return nil
30933135
}
30943136

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

google-beta/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)