Skip to content

Commit

Permalink
Add Deny Maintenance Period Fields (GoogleCloudPlatform#6840)
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketkumarj authored and googlerjk committed Nov 25, 2022
1 parent 2f9af90 commit ec9190d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ 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 @@ -1068,6 +1093,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 @@ -1247,6 +1273,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 @@ -1599,6 +1644,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 @@ -1678,6 +1727,23 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,31 @@ 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 @@ -1497,6 +1522,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 Expand Up @@ -2526,4 +2570,4 @@ data "google_sql_backup_run" "backup" {
most_recent = true
}
`, context)
}
}
Original file line number Diff line number Diff line change
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

0 comments on commit ec9190d

Please sign in to comment.