Skip to content

Commit 61c5207

Browse files
Support allocated_ip_range in google_sql_database_instance (#5500) (#10687)
* support allocated_ip_range in sql_database_instance * support allocated_ip_range * clean up * remove unused variable * separate tests * increase size of allogcated ip range Signed-off-by: Modular Magician <[email protected]>
1 parent 3dc7a34 commit 61c5207

5 files changed

+183
-43
lines changed

.changelog/5500.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sql: added field `allocated_ip_range` to resource `google_sql_database_instance`
3+
```

google/resource_sql_database_instance.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
"settings.0.ip_configuration.0.ipv4_enabled",
5353
"settings.0.ip_configuration.0.require_ssl",
5454
"settings.0.ip_configuration.0.private_network",
55+
"settings.0.ip_configuration.0.allocated_ip_range",
5556
}
5657

5758
maintenanceWindowKeys = []string{
@@ -306,6 +307,13 @@ settings.backup_configuration.binary_log_enabled are both set to true.`,
306307
AtLeastOneOf: ipConfigurationKeys,
307308
Description: `The VPC network from which the Cloud SQL instance is accessible for private IP. For example, projects/myProject/global/networks/default. Specifying a network enables private IP. At least ipv4_enabled must be enabled or a private_network must be configured. This setting can be updated, but it cannot be removed after it is set.`,
308309
},
310+
"allocated_ip_range": {
311+
Type: schema.TypeString,
312+
Optional: true,
313+
ForceNew: true,
314+
AtLeastOneOf: ipConfigurationKeys,
315+
Description: `The name of the allocated ip range for the private ip CloudSQL instance. For example: "google-managed-services-default". If set, the instance ip will be created in the allocated range. The range name must comply with RFC 1035. Specifically, the name must be 1-63 characters long and match the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.`,
316+
},
309317
},
310318
},
311319
},
@@ -1021,6 +1029,7 @@ func expandIpConfiguration(configured []interface{}) *sqladmin.IpConfiguration {
10211029
Ipv4Enabled: _ipConfiguration["ipv4_enabled"].(bool),
10221030
RequireSsl: _ipConfiguration["require_ssl"].(bool),
10231031
PrivateNetwork: _ipConfiguration["private_network"].(string),
1032+
AllocatedIpRange: _ipConfiguration["allocated_ip_range"].(string),
10241033
AuthorizedNetworks: expandAuthorizedNetworks(_ipConfiguration["authorized_networks"].(*schema.Set).List()),
10251034
ForceSendFields: []string{"Ipv4Enabled", "RequireSsl"},
10261035
}
@@ -1395,9 +1404,10 @@ func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]
13951404

13961405
func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface{} {
13971406
data := map[string]interface{}{
1398-
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
1399-
"private_network": ipConfiguration.PrivateNetwork,
1400-
"require_ssl": ipConfiguration.RequireSsl,
1407+
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
1408+
"private_network": ipConfiguration.PrivateNetwork,
1409+
"allocated_ip_range": ipConfiguration.AllocatedIpRange,
1410+
"require_ssl": ipConfiguration.RequireSsl,
14011411
}
14021412

14031413
if ipConfiguration.AuthorizedNetworks != nil {

google/resource_sql_database_instance_test.go

+125
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,56 @@ func TestAccSqlDatabaseInstance_basic_with_user_labels(t *testing.T) {
665665
})
666666
}
667667

668+
func TestAccSqlDatabaseInstance_withPrivateNetwork_withoutAllocatedIpRange(t *testing.T) {
669+
t.Parallel()
670+
671+
databaseName := "tf-test-" + randString(t, 10)
672+
addressName := "tf-test-" + randString(t, 10)
673+
networkName := BootstrapSharedTestNetwork(t, "sql-instance-private")
674+
675+
vcrTest(t, resource.TestCase{
676+
PreCheck: func() { testAccPreCheck(t) },
677+
Providers: testAccProviders,
678+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
679+
Steps: []resource.TestStep{
680+
{
681+
Config: testAccSqlDatabaseInstance_withPrivateNetwork_withoutAllocatedIpRange(databaseName, networkName, addressName),
682+
},
683+
{
684+
ResourceName: "google_sql_database_instance.instance",
685+
ImportState: true,
686+
ImportStateVerify: true,
687+
ImportStateVerifyIgnore: []string{"deletion_protection"},
688+
},
689+
},
690+
})
691+
}
692+
693+
func TestAccSqlDatabaseInstance_withPrivateNetwork_withAllocatedIpRange(t *testing.T) {
694+
t.Parallel()
695+
696+
databaseName := "tf-test-" + randString(t, 10)
697+
addressName := "tf-test-" + randString(t, 10)
698+
networkName := BootstrapSharedTestNetwork(t, "sql-instance-private-allocated-ip-range")
699+
700+
vcrTest(t, resource.TestCase{
701+
PreCheck: func() { testAccPreCheck(t) },
702+
Providers: testAccProviders,
703+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
704+
Steps: []resource.TestStep{
705+
{
706+
Config: testAccSqlDatabaseInstance_withPrivateNetwork_withAllocatedIpRange(databaseName, networkName, addressName),
707+
},
708+
{
709+
ResourceName: "google_sql_database_instance.instance",
710+
ImportState: true,
711+
ImportStateVerify: true,
712+
ImportStateVerifyIgnore: []string{"deletion_protection"},
713+
},
714+
},
715+
})
716+
}
717+
668718
func TestAccSqlDatabaseInstance_createFromBackup(t *testing.T) {
669719
// Sqladmin client
670720
skipIfVcr(t)
@@ -998,6 +1048,81 @@ resource "google_sql_database_instance" "instance-failover" {
9981048
`, instanceName, failoverName)
9991049
}
10001050

1051+
func testAccSqlDatabaseInstance_withPrivateNetwork_withoutAllocatedIpRange(databaseName, networkName, addressRangeName string) string {
1052+
return fmt.Sprintf(`
1053+
data "google_compute_network" "servicenet" {
1054+
name = "%s"
1055+
}
1056+
1057+
resource "google_compute_global_address" "foobar" {
1058+
name = "%s"
1059+
purpose = "VPC_PEERING"
1060+
address_type = "INTERNAL"
1061+
prefix_length = 16
1062+
network = data.google_compute_network.servicenet.self_link
1063+
}
1064+
1065+
resource "google_service_networking_connection" "foobar" {
1066+
network = data.google_compute_network.servicenet.self_link
1067+
service = "servicenetworking.googleapis.com"
1068+
reserved_peering_ranges = [google_compute_global_address.foobar.name]
1069+
}
1070+
1071+
resource "google_sql_database_instance" "instance" {
1072+
depends_on = [google_service_networking_connection.foobar]
1073+
name = "%s"
1074+
region = "us-central1"
1075+
database_version = "MYSQL_5_7"
1076+
deletion_protection = false
1077+
settings {
1078+
tier = "db-f1-micro"
1079+
ip_configuration {
1080+
ipv4_enabled = "false"
1081+
private_network = data.google_compute_network.servicenet.self_link
1082+
}
1083+
}
1084+
}
1085+
`, networkName, addressRangeName, databaseName)
1086+
}
1087+
1088+
func testAccSqlDatabaseInstance_withPrivateNetwork_withAllocatedIpRange(databaseName, networkName, addressRangeName string) string {
1089+
return fmt.Sprintf(`
1090+
data "google_compute_network" "servicenet" {
1091+
name = "%s"
1092+
}
1093+
1094+
resource "google_compute_global_address" "foobar" {
1095+
name = "%s"
1096+
purpose = "VPC_PEERING"
1097+
address_type = "INTERNAL"
1098+
prefix_length = 24
1099+
network = data.google_compute_network.servicenet.self_link
1100+
}
1101+
1102+
resource "google_service_networking_connection" "foobar" {
1103+
network = data.google_compute_network.servicenet.self_link
1104+
service = "servicenetworking.googleapis.com"
1105+
reserved_peering_ranges = [google_compute_global_address.foobar.name]
1106+
}
1107+
1108+
resource "google_sql_database_instance" "instance" {
1109+
depends_on = [google_service_networking_connection.foobar]
1110+
name = "%s"
1111+
region = "us-central1"
1112+
database_version = "MYSQL_5_7"
1113+
deletion_protection = false
1114+
settings {
1115+
tier = "db-f1-micro"
1116+
ip_configuration {
1117+
ipv4_enabled = "false"
1118+
private_network = data.google_compute_network.servicenet.self_link
1119+
allocated_ip_range = google_compute_global_address.foobar.name
1120+
}
1121+
}
1122+
}
1123+
`, networkName, addressRangeName, databaseName)
1124+
}
1125+
10011126
var testGoogleSqlDatabaseInstance_settings = `
10021127
resource "google_sql_database_instance" "instance" {
10031128
name = "%s"

website/docs/r/os_config_os_policy_assignment.html.markdown

+40-40
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,46 @@ The `disruption_budget` block supports:
703703
(Optional)
704704
Specifies the relative value defined as a percentage, which will be multiplied by a reference value.
705705

706+
The `source` block supports:
707+
708+
* `allow_insecure` -
709+
(Optional)
710+
Defaults to false. When false, files are subject to validations based on the file type: Remote: A checksum must be specified. Cloud Storage: An object generation number must be specified.
711+
712+
* `gcs` -
713+
(Optional)
714+
A Cloud Storage object.
715+
716+
* `local_path` -
717+
(Optional)
718+
A local path within the VM to use.
719+
720+
* `remote` -
721+
(Optional)
722+
A generic remote file.
723+
724+
The `validate` block supports:
725+
726+
* `interpreter` -
727+
(Required)
728+
Required. The script interpreter to use. Possible values: INTERPRETER_UNSPECIFIED, NONE, SHELL, POWERSHELL
729+
730+
* `args` -
731+
(Optional)
732+
Optional arguments to pass to the source during execution.
733+
734+
* `file` -
735+
(Optional)
736+
Required. A deb package.
737+
738+
* `output_file_path` -
739+
(Optional)
740+
Only recorded for enforce Exec. Path to an output file (that is created by this Exec) whose content will be recorded in OSPolicyResourceCompliance after a successful run. Absence or failure to read this file will result in this ExecResource being non-compliant. Output file size is limited to 100K bytes.
741+
742+
* `script` -
743+
(Optional)
744+
An inline script. The size of the script is limited to 1024 characters.
745+
706746
- - -
707747

708748
* `description` -
@@ -952,24 +992,6 @@ The `zypper` block supports:
952992
(Required)
953993
Required. A one word, unique name for this repository. This is the `repo id` in the zypper config file and also the `display_name` if `display_name` is omitted. This id is also used as the unique identifier when checking for GuestPolicy conflicts.
954994

955-
The `file` block supports:
956-
957-
* `allow_insecure` -
958-
(Optional)
959-
Defaults to false. When false, files are subject to validations based on the file type: Remote: A checksum must be specified. Cloud Storage: An object generation number must be specified.
960-
961-
* `gcs` -
962-
(Optional)
963-
A Cloud Storage object.
964-
965-
* `local_path` -
966-
(Optional)
967-
A local path within the VM to use.
968-
969-
* `remote` -
970-
(Optional)
971-
A generic remote file.
972-
973995
The `gcs` block supports:
974996

975997
* `bucket` -
@@ -994,28 +1016,6 @@ The `remote` block supports:
9941016
(Optional)
9951017
SHA256 checksum of the remote file.
9961018

997-
The `enforce` block supports:
998-
999-
* `interpreter` -
1000-
(Required)
1001-
Required. The script interpreter to use. Possible values: INTERPRETER_UNSPECIFIED, NONE, SHELL, POWERSHELL
1002-
1003-
* `args` -
1004-
(Optional)
1005-
Optional arguments to pass to the source during execution.
1006-
1007-
* `file` -
1008-
(Optional)
1009-
Required. A deb package.
1010-
1011-
* `output_file_path` -
1012-
(Optional)
1013-
Only recorded for enforce Exec. Path to an output file (that is created by this Exec) whose content will be recorded in OSPolicyResourceCompliance after a successful run. Absence or failure to read this file will result in this ExecResource being non-compliant. Output file size is limited to 100K bytes.
1014-
1015-
* `script` -
1016-
(Optional)
1017-
An inline script. The size of the script is limited to 1024 characters.
1018-
10191019
## Attributes Reference
10201020

10211021
In addition to the arguments listed above, the following computed attributes are exported:

website/docs/r/sql_database_instance.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ This setting can be updated, but it cannot be removed after it is set.
295295

296296
* `require_ssl` - (Optional) Whether SSL connections over IP are enforced or not.
297297

298+
* `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 instance ip will be created in the allocated range. The range name must comply with [RFC 1035](https://datatracker.ietf.org/doc/html/rfc1035). Specifically, the name must be 1-63 characters long and match the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.
299+
298300
The optional `settings.ip_configuration.authorized_networks[]` sublist supports:
299301

300302
* `expiration_time` - (Optional) The [RFC 3339](https://tools.ietf.org/html/rfc3339)

0 commit comments

Comments
 (0)