Skip to content

Commit ae77d5b

Browse files
committed
test
1 parent 1952662 commit ae77d5b

3 files changed

Lines changed: 189 additions & 2 deletions

File tree

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,13 @@ API (for read pools, effective_availability_type may differ from availability_ty
13811381
Required: true,
13821382
Description: `The name of the instance from which the point in time should be restored.`,
13831383
},
1384+
{{- if ne $.TargetVersionName "ga" }}
1385+
"source_project": {
1386+
Type: schema.TypeString,
1387+
Optional: true,
1388+
Description: `The project ID of the source project`,
1389+
},
1390+
{{- end }}
13841391
"point_in_time": {
13851392
Type: schema.TypeString,
13861393
Optional: true,
@@ -1561,7 +1568,11 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
15611568
ReplicaConfiguration: expandReplicaConfiguration(d.Get("replica_configuration").([]interface{})),
15621569
}
15631570

1564-
cloneContext, cloneSource := expandCloneContext(d.Get("clone").([]interface{}))
1571+
cloneContext, cloneSourceInstance := expandCloneContext(d.Get("clone").([]interface{}))
1572+
1573+
{{- if ne $.TargetVersionName "ga" }}
1574+
cloneSourceProject := expandCloneSourceProject(d.Get("clone").([]interface{}))
1575+
{{- end }}
15651576
pointInTimeRestoreContext := expandPointInTimeRestoreContext(d.Get("point_in_time_restore_context").([]interface{}))
15661577

15671578
if valueI, ok := d.GetOk("settings.0.auto_upgrade_enabled"); ok && !(valueI.(bool)) {
@@ -1623,8 +1634,16 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
16231634
RetryFunc: func() (operr error) {
16241635
if cloneContext != nil {
16251636
cloneContext.DestinationInstanceName = name
1637+
{{- if ne $.TargetVersionName "ga" }}
1638+
cloneContext.DestinationProject = project
1639+
cloneContext.DestinationNetwork = network
1640+
{{- end }}
16261641
clodeReq := sqladmin.InstancesCloneRequest{CloneContext: cloneContext}
1627-
op, operr = config.NewSqlAdminClient(userAgent).Instances.Clone(project, cloneSource, &clodeReq).Do()
1642+
{{- if ne $.TargetVersionName "ga" }}
1643+
op, operr = config.NewSqlAdminClient(userAgent).Instances.Clone(cloneSourceProject, cloneSourceInstance, &clodeReq).Do()
1644+
{{- else }}
1645+
op, operr = config.NewSqlAdminClient(userAgent).Instances.Clone(project, cloneSourceInstance, &clodeReq).Do()
1646+
{{- end }}
16281647
} else if pointInTimeRestoreContext != nil {
16291648
parent := fmt.Sprintf("projects/%s", project)
16301649
op, operr = config.NewSqlAdminClient(userAgent).Instances.PointInTimeRestore(parent, pointInTimeRestoreContext).Do()
@@ -1872,6 +1891,16 @@ func expandCloneContext(configured []interface{}) (*sqladmin.CloneContext, strin
18721891
}, _cloneConfiguration["source_instance_name"].(string)
18731892
}
18741893

1894+
{{- if ne $.TargetVersionName "ga" }}
1895+
func expandCloneSourceProject(configured []interface{}) (string) {
1896+
if len(configured) == 0 || configured[0] == nil {
1897+
return ""
1898+
}
1899+
_cloneConfiguration := configured[0].(map[string]interface{})
1900+
return _cloneConfiguration["source_project"].(string)
1901+
}
1902+
{{- end }}
1903+
18751904
func expandMaintenanceWindow(configured []interface{}) *sqladmin.MaintenanceWindow {
18761905
if len(configured) == 0 || configured[0] == nil {
18771906
return nil

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ fields:
1313
- field: 'clone.preferred_zone'
1414
- field: 'clone.source_instance_deletion_time'
1515
- field: 'clone.source_instance_name'
16+
{{- if ne $.TargetVersionName "ga" }}
17+
- field: 'clone.source_project'
18+
{{- end }}
1619
- api_field: 'connectionName'
1720
- api_field: 'databaseVersion'
1821
- field: 'deletion_protection'

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

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,49 @@ func TestAccSqlDatabaseInstance_basicClone(t *testing.T) {
17181718
})
17191719
}
17201720

1721+
{{- if ne $.TargetVersionName "ga" }}
1722+
func TestAccSqlDatabaseInstance_crossProjectClone(t *testing.T) {
1723+
t.Parallel()
1724+
1725+
context := map[string]interface{}{
1726+
"random_suffix": acctest.RandString(t, 10),
1727+
"orgId": envvar.GetTestOrgFromEnv(t),
1728+
"billingAccount": envvar.GetTestBillingAccountFromEnv(t),
1729+
"cloneSourceProject": envvar.GetTestProjectFromEnv(),
1730+
}
1731+
1732+
acctest.VcrTest(t, resource.TestCase{
1733+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1734+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
1735+
ExternalProviders: map[string]resource.ExternalProvider{
1736+
"time": {},
1737+
},
1738+
Steps: []resource.TestStep{
1739+
{
1740+
Config: testAccSqlDatabaseInstance_crossProjectClone(context),
1741+
},
1742+
{
1743+
ResourceName: "google_sql_database_instance.instance",
1744+
ImportState: true,
1745+
ImportStateVerify: true,
1746+
ImportStateIdFunc: testAccSqlDatabaseInstanceImportStateIdFunc("google_sql_database_instance.instance"),
1747+
ImportStateVerifyIgnore: []string{"deletion_protection", "clone"},
1748+
},
1749+
},
1750+
})
1751+
}
1752+
{{- end }}
1753+
1754+
func testAccSqlDatabaseInstanceImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
1755+
return func(s *terraform.State) (string, error) {
1756+
rs, ok := s.RootModule().Resources[resourceName]
1757+
if !ok {
1758+
return "", fmt.Errorf("Not found: %s", resourceName)
1759+
}
1760+
return fmt.Sprintf("%s/%s", rs.Primary.Attributes["project"], rs.Primary.Attributes["name"]), nil
1761+
}
1762+
}
1763+
17211764
func TestAccSqlDatabaseInstance_cloneWithSettings(t *testing.T) {
17221765
// Sqladmin client
17231766
acctest.SkipIfVcr(t)
@@ -8252,6 +8295,118 @@ data "google_sql_backup_run" "backup" {
82528295
`, context)
82538296
}
82548297

8298+
func testAccSqlDatabaseInstance_crossProjectClone(context map[string]interface{}) string {
8299+
return acctest.Nprintf(`
8300+
resource "google_project" "project" {
8301+
provider = google-beta
8302+
name = "tf-test-cpc-%{random_suffix}"
8303+
project_id = "tf-test-cpc-%{random_suffix}"
8304+
org_id = "%{orgId}"
8305+
billing_account = "%{billingAccount}"
8306+
deletion_policy = "DELETE"
8307+
}
8308+
8309+
resource "time_sleep" "wait_60_seconds" {
8310+
create_duration = "60s"
8311+
depends_on = [google_project.project]
8312+
}
8313+
8314+
resource "google_project_service" "apigee" {
8315+
provider = google-beta
8316+
project = google_project.project.project_id
8317+
service = "apigee.googleapis.com"
8318+
depends_on = [time_sleep.wait_60_seconds]
8319+
}
8320+
8321+
resource "google_project_service" "compute" {
8322+
provider = google-beta
8323+
project = google_project.project.project_id
8324+
service = "compute.googleapis.com"
8325+
depends_on = [google_project_service.apigee]
8326+
}
8327+
8328+
resource "google_project_service" "servicenetworking" {
8329+
provider = google-beta
8330+
project = google_project.project.project_id
8331+
service = "servicenetworking.googleapis.com"
8332+
depends_on = [google_project_service.compute]
8333+
}
8334+
8335+
resource "time_sleep" "wait_300_seconds" {
8336+
create_duration = "300s"
8337+
depends_on = [google_project_service.servicenetworking]
8338+
}
8339+
8340+
resource "google_compute_network" "apigee_network" {
8341+
provider = google-beta
8342+
name = "apigee-network"
8343+
project = google_project.project.project_id
8344+
depends_on = [time_sleep.wait_300_seconds]
8345+
}
8346+
8347+
resource "google_compute_global_address" "apigee_range" {
8348+
provider = google-beta
8349+
name = "apigee-range"
8350+
purpose = "VPC_PEERING"
8351+
address_type = "INTERNAL"
8352+
prefix_length = 16
8353+
network = google_compute_network.apigee_network.id
8354+
project = google_project.project.project_id
8355+
}
8356+
8357+
resource "google_service_networking_connection" "apigee_vpc_connection" {
8358+
provider = google-beta
8359+
network = google_compute_network.apigee_network.id
8360+
service = "servicenetworking.googleapis.com"
8361+
reserved_peering_ranges = [google_compute_global_address.apigee_range.name]
8362+
depends_on = [google_project_service.servicenetworking]
8363+
}
8364+
8365+
resource "google_sql_database_instance" "instance" {
8366+
provider = google-beta
8367+
name = "tf-test-cpc-%{random_suffix}"
8368+
database_version = "POSTGRES_11"
8369+
region = "us-central1"
8370+
project = google_project.destination_project.project_id
8371+
8372+
settings {
8373+
tier = "db-custom-2-3840"
8374+
edition = "ENTERPRISE"
8375+
ip_configuration {
8376+
private_network = google_compute_network.apigee_network.self_link
8377+
}
8378+
backup_configuration {
8379+
enabled = true
8380+
point_in_time_recovery_enabled = true
8381+
}
8382+
}
8383+
8384+
clone {
8385+
source_project = "%{cloneSourceProject}"
8386+
source_instance_name = google_sql_database_instance.source_instance.name
8387+
}
8388+
8389+
deletion_protection = false
8390+
8391+
// Ignore changes, since the most recent backup may change during the test
8392+
lifecycle{
8393+
ignore_changes = [clone[0].point_in_time]
8394+
}
8395+
}
8396+
8397+
resource "google_sql_database_instance" "source_instance" {
8398+
provider = google-beta
8399+
name = "tf-test-cpc-%{random_suffix}"
8400+
database_version = "POSTGRES_11"
8401+
region = "us-central1"
8402+
deletion_protection = false
8403+
settings {
8404+
tier = "db-g1-small"
8405+
}
8406+
}
8407+
`, context)
8408+
}
8409+
82558410
func testAccSqlDatabaseInstance_cloneWithSettings(context map[string]interface{}) string {
82568411
return acctest.Nprintf(`
82578412
resource "google_sql_database_instance" "instance" {

0 commit comments

Comments
 (0)