Skip to content

Commit ec3d3d6

Browse files
adding source_instance_deletion_time (#15239) (#10827)
[upstream:bd6209a2bc78e3496d8cbd5b8a708c7241a9d912] Signed-off-by: Modular Magician <[email protected]>
1 parent 554da8a commit ec3d3d6

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

.changelog/15239.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
sql: added `source_instance_deletion_time` field to `google_sql_database_instance` resource
3+
```
4+
```release-note:enhancement
5+
sql: added `source_instance_deletion_time` field to `google_sql_database_instance_latest_recovery_time` data source
6+
```

google-beta/services/sql/data_source_sql_database_instance_latest_recovery_time.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
func DataSourceSqlDatabaseInstanceLatestRecoveryTime() *schema.Resource {
2828
return &schema.Resource{
2929
Read: dataSourceSqlDatabaseInstanceLatestRecoveryTimeRead,
30-
3130
Schema: map[string]*schema.Schema{
3231
"instance": {
3332
Type: schema.TypeString,
@@ -45,6 +44,11 @@ func DataSourceSqlDatabaseInstanceLatestRecoveryTime() *schema.Resource {
4544
Computed: true,
4645
Description: `Timestamp, identifies the latest recovery time of the source instance.`,
4746
},
47+
"source_instance_deletion_time": {
48+
Type: schema.TypeString,
49+
Optional: true,
50+
Description: `Timestamp, identifies when the source instance was deleted. If this instance is deleted, then you must set the timestamp.`,
51+
},
4852
},
4953
}
5054
}
@@ -55,23 +59,28 @@ func dataSourceSqlDatabaseInstanceLatestRecoveryTimeRead(d *schema.ResourceData,
5559
if err != nil {
5660
return err
5761
}
58-
5962
fv, err := tpgresource.ParseProjectFieldValue("instances", d.Get("instance").(string), "project", d, config, false)
6063
if err != nil {
6164
return err
6265
}
6366
project := fv.Project
6467
instance := fv.Name
6568

66-
latestRecoveryTime, err := config.NewSqlAdminClient(userAgent).Projects.Instances.GetLatestRecoveryTime(project, instance).Do()
69+
deletionTime := d.Get("source_instance_deletion_time").(string)
70+
71+
latestRecoveryTimeCall := config.NewSqlAdminClient(userAgent).Projects.Instances.GetLatestRecoveryTime(project, instance)
72+
73+
if deletionTime != "" {
74+
latestRecoveryTimeCall = latestRecoveryTimeCall.SourceInstanceDeletionTime(deletionTime)
75+
}
76+
77+
latestRecoveryTime, err := latestRecoveryTimeCall.Do()
6778
if err != nil {
6879
return err
6980
}
70-
7181
if err := d.Set("project", project); err != nil {
7282
return fmt.Errorf("Error setting project: %s", err)
7383
}
74-
7584
if err := d.Set("latest_recovery_time", latestRecoveryTime.LatestRecoveryTime); err != nil {
7685
return fmt.Errorf("Error setting latest_recovery_time: %s", err)
7786
}

google-beta/services/sql/data_source_sql_database_instance_latest_recovery_time_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func TestAccDataSourceSqlDatabaseInstanceLatestRecoveryTime_basic(t *testing.T)
3535
PreCheck: func() { acctest.AccTestPreCheck(t) },
3636
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3737
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
38+
ExternalProviders: map[string]resource.ExternalProvider{
39+
"time": {},
40+
},
3841
Steps: []resource.TestStep{
3942
{
4043
Config: testAccDataSourceSqlDatabaseInstanceLatestRecoveryTime_basic(context),
@@ -68,8 +71,15 @@ resource "google_sql_database_instance" "main" {
6871
deletion_protection = false
6972
}
7073
74+
resource "time_sleep" "wait_for_instance" {
75+
// Wait 30 seconds after the instance is created
76+
depends_on = [google_sql_database_instance.main]
77+
create_duration = "330s"
78+
}
79+
7180
data "google_sql_database_instance_latest_recovery_time" "default" {
72-
instance = resource.google_sql_database_instance.main.name
81+
instance = google_sql_database_instance.main.name
82+
depends_on = [time_sleep.wait_for_instance]
7383
}
7484
`, context)
7585
}

google-beta/services/sql/resource_sql_database_instance.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,12 @@ API (for read pools, effective_availability_type may differ from availability_ty
12621262
Optional: true,
12631263
Description: `The name of the allocated ip range for the private ip CloudSQL instance. For example: "google-managed-services-default". If set, the cloned instance ip will be created in the allocated range. The range name must comply with [RFC 1035](https://tools.ietf.org/html/rfc1035). Specifically, the name must be 1-63 characters long and match the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.`,
12641264
},
1265+
"source_instance_deletion_time": {
1266+
Type: schema.TypeString,
1267+
Optional: true,
1268+
DiffSuppressFunc: tpgresource.TimestampDiffSuppress(time.RFC3339Nano),
1269+
Description: `The timestamp of when the source instance was deleted for a clone from a deleted instance.`,
1270+
},
12651271
},
12661272
},
12671273
},
@@ -1680,10 +1686,11 @@ func expandCloneContext(configured []interface{}) (*sqladmin.CloneContext, strin
16801686
}
16811687

16821688
return &sqladmin.CloneContext{
1683-
PointInTime: _cloneConfiguration["point_in_time"].(string),
1684-
PreferredZone: _cloneConfiguration["preferred_zone"].(string),
1685-
DatabaseNames: databaseNames,
1686-
AllocatedIpRange: _cloneConfiguration["allocated_ip_range"].(string),
1689+
PointInTime: _cloneConfiguration["point_in_time"].(string),
1690+
PreferredZone: _cloneConfiguration["preferred_zone"].(string),
1691+
DatabaseNames: databaseNames,
1692+
AllocatedIpRange: _cloneConfiguration["allocated_ip_range"].(string),
1693+
SourceInstanceDeletionTime: _cloneConfiguration["source_instance_deletion_time"].(string),
16871694
}, _cloneConfiguration["source_instance_name"].(string)
16881695
}
16891696

website/docs/r/sql_database_instance.html.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ The optional `point_in_time_restore_context` block supports:
639639

640640
* `allocated_ip_range` - (Optional) The name of the allocated ip range for the private ip CloudSQL instance. For example: "google-managed-services-default". If set, the cloned instance ip will be created in the allocated range. The range name must comply with [RFC 1035](https://tools.ietf.org/html/rfc1035). Specifically, the name must be 1-63 characters long and match the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.
641641

642+
* `source_instance_deletion_time` - (Optional) The timestamp of when the source instance was deleted for a clone from a deleted instance.
643+
644+
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
645+
642646
* `database_names` - (Optional) (SQL Server only, use with `point_in_time`) Clone only the specified databases from the source instance. Clone all databases if empty.
643647

644648
The optional `clone` block supports:
@@ -655,6 +659,10 @@ The optional `clone` block supports:
655659

656660
* `allocated_ip_range` - (Optional) The name of the allocated ip range for the private ip CloudSQL instance. For example: "google-managed-services-default". If set, the cloned instance ip will be created in the allocated range. The range name must comply with [RFC 1035](https://tools.ietf.org/html/rfc1035). Specifically, the name must be 1-63 characters long and match the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.
657661

662+
* `source_instance_deletion_time` - (Optional) The timestamp of when the source instance was deleted for a clone from a deleted instance.
663+
664+
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
665+
658666
The optional `restore_backup_context` block supports:
659667
**NOTE:** Restoring from a backup is an imperative action and not recommended via Terraform. Adding or modifying this
660668
block during resource creation/update will trigger the restore action after the resource is created/updated.

0 commit comments

Comments
 (0)