Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deny Maintenance Period Fields #13106

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6840.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
sql: added 'deny_maintenance_period' field for 'google_sql_database_instance' within which 'end_date', 'start_date' and 'time' fields are present.
```
64 changes: 64 additions & 0 deletions google/resource_sql_database_instance.go
Expand Up @@ -159,6 +159,30 @@ func resourceSqlDatabaseInstance() *schema.Resource {
},
},
},
"deny_maintenance_period": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"end_date": {
Type: schema.TypeString,
Required: true,
Description: `End date before which maintenance will not take place. The date is in format yyyy-mm-dd i.e., 2020-11-01, or mm-dd, i.e., 11-01`,
},
"start_date": {
Type: schema.TypeString,
Required: true,
Description: `Start date after which maintenance will not take place. The date is in format yyyy-mm-dd i.e., 2020-11-01, or mm-dd, i.e., 11-01`,
},
"time": {
Type: schema.TypeString,
Required: true,
Description: `Time in UTC when the "deny maintenance period" starts on start_date and ends on end_date. The time is in format: HH:mm:SS, i.e., 00:00:00`,
},
},
},
},
"sql_server_audit_config": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1066,6 +1090,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}) *sqladmin.Setti
ForceSendFields: []string{"StorageAutoResize"},
ActivationPolicy: _settings["activation_policy"].(string),
ActiveDirectoryConfig: expandActiveDirectoryConfig(_settings["active_directory_config"].([]interface{})),
DenyMaintenancePeriods: expandDenyMaintenancePeriod(_settings["deny_maintenance_period"].([]interface{})),
SqlServerAuditConfig: expandSqlServerAuditConfig(_settings["sql_server_audit_config"].([]interface{})),
TimeZone: _settings["time_zone"].(string),
AvailabilityType: _settings["availability_type"].(string),
Expand Down Expand Up @@ -1245,6 +1270,25 @@ func expandActiveDirectoryConfig(configured interface{}) *sqladmin.SqlActiveDire
}
}

func expandDenyMaintenancePeriod(configured []interface{}) []*sqladmin.DenyMaintenancePeriod {
denyMaintenancePeriod := make([]*sqladmin.DenyMaintenancePeriod, 0, len(configured))

for _, _flag := range configured {
if _flag == nil {
continue
}
_entry := _flag.(map[string]interface{})

denyMaintenancePeriod = append(denyMaintenancePeriod, &sqladmin.DenyMaintenancePeriod{
EndDate: _entry["end_date"].(string),
StartDate: _entry["start_date"].(string),
Time: _entry["time"].(string),
})
}
return denyMaintenancePeriod

}

func expandSqlServerAuditConfig(configured interface{}) *sqladmin.SqlServerAuditConfig {
l := configured.([]interface{})
if len(l) == 0 {
Expand Down Expand Up @@ -1597,6 +1641,10 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} {
data["active_directory_config"] = flattenActiveDirectoryConfig(settings.ActiveDirectoryConfig)
}

if settings.DenyMaintenancePeriods != nil {
data["deny_maintenance_period"] = flattenDenyMaintenancePeriod(settings.DenyMaintenancePeriods)
}

if settings.SqlServerAuditConfig != nil {
data["sql_server_audit_config"] = flattenSqlServerAuditConfig(settings.SqlServerAuditConfig)
}
Expand Down Expand Up @@ -1676,6 +1724,22 @@ func flattenActiveDirectoryConfig(sqlActiveDirectoryConfig *sqladmin.SqlActiveDi
}
}

func flattenDenyMaintenancePeriod(denyMaintenancePeriod []*sqladmin.DenyMaintenancePeriod) []map[string]interface{} {
flags := make([]map[string]interface{}, 0, len(denyMaintenancePeriod))

for _, flag := range denyMaintenancePeriod {
data := map[string]interface{}{
"end_date": flag.EndDate,
"start_date": flag.StartDate,
"time": flag.Time,
}

flags = append(flags, data)
}

return flags
}

func flattenSqlServerAuditConfig(sqlServerAuditConfig *sqladmin.SqlServerAuditConfig) []map[string]interface{} {
if sqlServerAuditConfig == nil {
return nil
Expand Down
43 changes: 43 additions & 0 deletions google/resource_sql_database_instance_test.go
Expand Up @@ -1239,6 +1239,30 @@ func TestAccSqlDatabaseInstance_ActiveDirectory(t *testing.T) {
})
}

func TestAccSQLDatabaseInstance_DenyMaintenancePeriod(t *testing.T) {
t.Parallel()
databaseName := "tf-test-" + randString(t, 10)
endDate := "2022-12-5"
startDate := "2022-10-5"
time := "00:00:00"
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_DenyMaintenancePeriodConfig(databaseName, endDate, startDate, time),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
},
})
}

func TestAccSqlDatabaseInstance_SqlServerAuditConfig(t *testing.T) {
// Service Networking
skipIfVcr(t)
Expand Down Expand Up @@ -1494,6 +1518,25 @@ resource "google_sql_database_instance" "instance-with-ad" {
}`, networkName, addressRangeName, databaseName, rootPassword, adDomainName)
}

func testGoogleSqlDatabaseInstance_DenyMaintenancePeriodConfig(databaseName, endDate, startDate, time string) string {
return fmt.Sprintf(`

resource "google_sql_database_instance" "instance" {
name = "%s"
region = "us-central1"
database_version = "MYSQL_5_7"
deletion_protection = false
settings {
tier = "db-custom-4-26624"
deny_maintenance_period {
end_date = "%s"
start_date = "%s"
time = "%s"
}
}
}`, databaseName, endDate, startDate, time)
}

func testGoogleSqlDatabaseInstance_SqlServerAuditConfig(networkName, addressName, databaseName, rootPassword, bucketName, uploadInterval, retentionInterval string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "gs-bucket" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/sql_database_instance.html.markdown
Expand Up @@ -263,6 +263,14 @@ The optional `settings.active_directory_config` subblock supports:
* `domain` - (Required) The domain name for the active directory (e.g., mydomain.com).
Can only be used with SQL Server.

The optional `settings.deny_maintenance_period` subblock supports:

* `end_date` - (Required) "deny maintenance period" end date. If the year of the end date is empty, the year of the start date also must be empty. In this case, it means the no maintenance interval recurs every year. The date is in format yyyy-mm-dd i.e., 2020-11-01, or mm-dd, i.e., 11-01

* `start_date` - (Required) "deny maintenance period" start date. If the year of the start date is empty, the year of the end date also must be empty. In this case, it means the deny maintenance period recurs every year. The date is in format yyyy-mm-dd i.e., 2020-11-01, or mm-dd, i.e., 11-01

* `time` - (Required) Time in UTC when the "deny maintenance period" starts on startDate and ends on endDate. The time is in format: HH:mm:SS, i.e., 00:00:00

The optional `settings.sql_server_audit_config` subblock supports:

* `bucket` - (Required) The name of the destination bucket (e.g., gs://mybucket).
Expand Down