Skip to content

Commit

Permalink
Add maintenance_window support.
Browse files Browse the repository at this point in the history
  • Loading branch information
gh-mlfowler committed Feb 8, 2017
1 parent abb2e3d commit e41f4a1
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ func resourceSqlDatabaseInstance() *schema.Resource {
},
},
},
"maintenance_window": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"day": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateDay,
},
"hour": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validateHour,
},
},
},
},
"pricing_plan": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -425,6 +443,26 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
}
}

if v, ok := _settings["maintenance_window"]; ok {
_maintenanceWindowList := v.([]interface{})
if len(_maintenanceWindowList) > 1 {
return fmt.Errorf("At most one maintenance_window block is allowed")
}

if len(_maintenanceWindowList) == 1 && _maintenanceWindowList[0] != nil {
settings.MaintenanceWindow = &sqladmin.MaintenanceWindow{}
_maintenanceWindow := _maintenanceWindowList[0].(map[string]interface{})

if vp, okp := _maintenanceWindow["day"]; okp {
settings.MaintenanceWindow.Day = int64(vp.(int))
}

if vp, okp := _maintenanceWindow["hour"]; okp {
settings.MaintenanceWindow.Hour = int64(vp.(int))
}
}
}

if v, ok := _settings["pricing_plan"]; ok {
settings.PricingPlan = v.(string)
}
Expand Down Expand Up @@ -730,6 +768,28 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
}
}

if v, ok := _settings["maintenance_window"]; ok {
_maintenanceWindowList := v.([]interface{})
if len(_maintenanceWindowList) > 1 {
return fmt.Errorf("At most one maintenance_window block is allowed")
}

if len(_maintenanceWindowList) == 1 && _maintenanceWindowList[0] != nil {
_maintenanceWindow := _maintenanceWindowList[0].(map[string]interface{})

if vp, okp := _maintenanceWindow["day"]; okp && vp != nil {
_maintenanceWindow["day"] = settings.MaintenanceWindow.Day
}

if vp, okp := _maintenanceWindow["enabled"]; okp && vp != nil {
_maintenanceWindow["hour"] = settings.MaintenanceWindow.Hour
}

_maintenanceWindowList[0] = _maintenanceWindow
_settings["maintenance_window"] = _maintenanceWindowList
}
}

if v, ok := _settings["pricing_plan"]; ok && len(v.(string)) > 0 {
_settings["pricing_plan"] = settings.PricingPlan
}
Expand Down Expand Up @@ -1037,6 +1097,26 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
}
}

if v, ok := _settings["maintenance_window"]; ok {
_maintenanceWindowList := v.([]interface{})
if len(_maintenanceWindowList) > 1 {
return fmt.Errorf("At most one maintenance_window block is allowed")
}

if len(_maintenanceWindowList) == 1 && _maintenanceWindowList[0] != nil {
settings.MaintenanceWindow = &sqladmin.MaintenanceWindow{}
_maintenanceWindow := _maintenanceWindowList[0].(map[string]interface{})

if vp, okp := _maintenanceWindow["day"]; okp {
settings.MaintenanceWindow.Day = int64(vp.(int))
}

if vp, okp := _maintenanceWindow["hour"]; okp {
settings.MaintenanceWindow.Hour = int64(vp.(int))
}
}
}

if v, ok := _settings["pricing_plan"]; ok {
settings.PricingPlan = v.(string)
}
Expand Down Expand Up @@ -1085,3 +1165,20 @@ func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{})

return nil
}

func validateDay(v interface{}, k string) (ws []string, errors []error) {
return validateNumericRange(v, k, 1, 7)
}

func validateHour(v interface{}, k string) (ws []string, errors []error) {
return validateNumericRange(v, k, 0, 23)
}

func validateNumericRange(v interface{}, k string, min int, max int) (ws []string, errors []error) {
value := v.(int)
if min > value || value > max {
errors = append(errors, fmt.Errorf(
"%q outside range %d-%d.", k, min, max))
}
return
}

0 comments on commit e41f4a1

Please sign in to comment.