Skip to content

Commit

Permalink
[Fix] Force send fields for settings resources (#3978)
Browse files Browse the repository at this point in the history
## Changes
<!-- Summary of your changes that are easy to understand -->

There was a problem that `databricks_automatic_cluster_update_setting`
wasn't applied because `minutes` attribute was set to 0, and it wasn't
sent in the JSON payload because it's marked as `omitempty` in the API.
After analysis, it was found that there are other places where we need
to send data but the corresponding fields were marked as `omitempty`.

This PR includes following changes:

- in 3 resources force sending of boolean and numeric fields with 0
values
- add customize schema for
`databricksenhanced_security_monitoring_setting`,
`databricks_automatic_cluster_update_setting` and
`databricks_enhanced_security_monitoring_setting` as fields are declared
as required in documentation/API, but not enforced in Terraform because
they are marked as `omitempty`
- expand documentation to include necessary blocks

Resolves #3976

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->

- [x] `make test` run locally
- [x] relevant change in `docs/` folder
- [ ] covered with integration tests in `internal/acceptance`
- [ ] relevant acceptance tests are passing
- [x] using Go SDK
  • Loading branch information
alexott authored Sep 3, 2024
1 parent a4b0225 commit 00d8fa8
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 33 deletions.
21 changes: 10 additions & 11 deletions docs/resources/automatic_cluster_update_setting.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ resource "databricks_automatic_cluster_update_workspace_setting" "this" {

The resource supports the following arguments:

* `enabled` - (Required) The configuration details.
* `restart_even_if_no_updates_available` - (Optional) To force clusters and other compute resources to restart during the maintenance window regardless of the availability of a new update.

A `maintenance_window` block that defines the maintenance frequency with the following arguments

* A `week_day_based_schedule` block with the following arguments
* `day_of_week` - the day of the week in uppercase, e.g. `MONDAY` or `SUNDAY`
* `frequency` - one of the `FIRST_OF_MONTH`, `SECOND_OF_MONTH`, `THIRD_OF_MONTH`, `FOURTH_OF_MONTH`, `FIRST_AND_THIRD_OF_MONTH`, `SECOND_AND_FOURTH_OF_MONTH`, `EVERY_WEEK`.
* A `window_start_time` block that defines the time of your maintenance window. The default timezone is UTC and cannot be changed.
* `hours`
* `minutes`
- `automatic_cluster_update_workspace` (Required) block with following attributes
- `enabled` - (Required) The configuration details.
- `restart_even_if_no_updates_available` - (Optional) To force clusters and other compute resources to restart during the maintenance window regardless of the availability of a new update.
- `maintenance_window` block that defines the maintenance frequency with the following arguments
- `week_day_based_schedule` block with the following arguments
- `day_of_week` - the day of the week in uppercase, e.g. `MONDAY` or `SUNDAY`
- `frequency` - one of the `FIRST_OF_MONTH`, `SECOND_OF_MONTH`, `THIRD_OF_MONTH`, `FOURTH_OF_MONTH`, `FIRST_AND_THIRD_OF_MONTH`, `SECOND_AND_FOURTH_OF_MONTH`, `EVERY_WEEK`.
- `window_start_time` block that defines the time of your maintenance window. The default timezone is UTC and cannot be changed.
- `hours` - hour to perform update: 0-23
- `minutes` - minute to perform update: 0-59

## Import

Expand Down
5 changes: 3 additions & 2 deletions docs/resources/compliance_security_profile_setting.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ resource "databricks_compliance_security_profile_workspace_setting" "this" {

The resource supports the following arguments:

* `is_enabled` - (Required) Enable the Compliance Security Profile on the workspace
* `compliance_standards` - (Required) Enable one or more compliance standards on the workspace, e.g. `HIPAA`, `PCI_DSS`, `FEDRAMP_MODERATE`
- `compliance_security_profile_workspace` block with following attributes:
- `is_enabled` - (Required) Enable the Compliance Security Profile on the workspace
- `compliance_standards` - (Required) Enable one or more compliance standards on the workspace, e.g. `HIPAA`, `PCI_DSS`, `FEDRAMP_MODERATE`

## Import

Expand Down
3 changes: 2 additions & 1 deletion docs/resources/enhanced_security_monitoring_setting.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ resource "databricks_enhanced_security_monitoring_workspace_setting" "this" {

The resource supports the following arguments:

* `is_enabled` - (Required) Enable the Enhanced Security Monitoring on the workspace
- `enhanced_security_monitoring_workspace` block with following attributes:
- `is_enabled` - (Required) Enable the Enhanced Security Monitoring on the workspace

## Import

Expand Down
21 changes: 19 additions & 2 deletions settings/resource_automatic_cluster_update_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ var automaticClusterUpdateFieldMask = strings.Join([]string{
"automatic_cluster_update_workspace.maintenance_window.week_day_based_schedule.window_start_time.hours",
"automatic_cluster_update_workspace.maintenance_window.week_day_based_schedule.window_start_time.minutes",
}, ",")

var automaticClusterUpdateSetting = workspaceSetting[settings.AutomaticClusterUpdateSetting]{
settingStruct: settings.AutomaticClusterUpdateSetting{},
customizeSchemaFunc: func(s map[string]*schema.Schema) map[string]*schema.Schema {
common.MustSchemaPath(s, "automatic_cluster_update_workspace", "enablement_details").Computed = true
common.CustomizeSchemaPath(s, "automatic_cluster_update_workspace", "enablement_details").SetReadOnly()
common.CustomizeSchemaPath(s, "automatic_cluster_update_workspace", "enabled").SetRequired()
common.CustomizeSchemaPath(s, "automatic_cluster_update_workspace", "maintenance_window",
"week_day_based_schedule", "window_start_time", "hours").SetRequired()
common.CustomizeSchemaPath(s, "automatic_cluster_update_workspace", "maintenance_window",
"week_day_based_schedule", "day_of_week").SetRequired()
common.CustomizeSchemaPath(s, "automatic_cluster_update_workspace", "maintenance_window",
"week_day_based_schedule", "frequency").SetRequired()
common.CustomizeSchemaPath(s, "automatic_cluster_update_workspace", "maintenance_window",
"week_day_based_schedule", "window_start_time", "minutes").SetRequired()
return s
},
readFunc: func(ctx context.Context, w *databricks.WorkspaceClient, etag string) (*settings.AutomaticClusterUpdateSetting, error) {
Expand All @@ -32,6 +42,12 @@ var automaticClusterUpdateSetting = workspaceSetting[settings.AutomaticClusterUp
},
updateFunc: func(ctx context.Context, w *databricks.WorkspaceClient, t settings.AutomaticClusterUpdateSetting) (string, error) {
t.SettingName = "default"
t.AutomaticClusterUpdateWorkspace.ForceSendFields = []string{"Enabled", "RestartEvenIfNoUpdatesAvailable"}
if t.AutomaticClusterUpdateWorkspace.MaintenanceWindow != nil &&
t.AutomaticClusterUpdateWorkspace.MaintenanceWindow.WeekDayBasedSchedule != nil &&
t.AutomaticClusterUpdateWorkspace.MaintenanceWindow.WeekDayBasedSchedule.WindowStartTime != nil {
t.AutomaticClusterUpdateWorkspace.MaintenanceWindow.WeekDayBasedSchedule.WindowStartTime.ForceSendFields = []string{"Hours", "Minutes"}
}
res, err := w.Settings.AutomaticClusterUpdate().Update(ctx, settings.UpdateAutomaticClusterUpdateSettingRequest{
AllowMissing: true,
Setting: t,
Expand All @@ -49,7 +65,8 @@ var automaticClusterUpdateSetting = workspaceSetting[settings.AutomaticClusterUp
Etag: etag,
SettingName: "default",
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: false,
Enabled: false,
ForceSendFields: []string{"Enabled"},
},
},
FieldMask: automaticClusterUpdateFieldMask,
Expand Down
23 changes: 16 additions & 7 deletions settings/resource_automatic_cluster_update_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestQueryCreateAutomaticClusterUpdateSetting(t *testing.T) {
Setting: settings.AutomaticClusterUpdateSetting{
Etag: "",
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: true,
Enabled: true,
ForceSendFields: []string{"Enabled", "RestartEvenIfNoUpdatesAvailable"},
},
SettingName: "default",
},
Expand All @@ -44,7 +45,8 @@ func TestQueryCreateAutomaticClusterUpdateSetting(t *testing.T) {
Setting: settings.AutomaticClusterUpdateSetting{
Etag: "etag1",
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: true,
Enabled: true,
ForceSendFields: []string{"Enabled", "RestartEvenIfNoUpdatesAvailable"},
},
SettingName: "default",
},
Expand Down Expand Up @@ -150,14 +152,16 @@ func TestQueryUpdateAutomaticClusterUpdateSetting(t *testing.T) {
Etag: "etag1",
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: true,
ForceSendFields: []string{"Enabled", "RestartEvenIfNoUpdatesAvailable"},
RestartEvenIfNoUpdatesAvailable: true,
MaintenanceWindow: &settings.ClusterAutoRestartMessageMaintenanceWindow{
WeekDayBasedSchedule: &settings.ClusterAutoRestartMessageMaintenanceWindowWeekDayBasedSchedule{
DayOfWeek: "MONDAY",
Frequency: "EVERY_WEEK",
WindowStartTime: &settings.ClusterAutoRestartMessageMaintenanceWindowWindowStartTime{
Hours: 1,
Minutes: 0,
Hours: 1,
Minutes: 0,
ForceSendFields: []string{"Hours", "Minutes"},
},
},
},
Expand Down Expand Up @@ -238,6 +242,7 @@ func TestQueryUpdateAutomaticClusterUpdateSettingWithConflict(t *testing.T) {
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: true,
RestartEvenIfNoUpdatesAvailable: true,
ForceSendFields: []string{"Enabled", "RestartEvenIfNoUpdatesAvailable"},
},
SettingName: "default",
},
Expand All @@ -260,6 +265,7 @@ func TestQueryUpdateAutomaticClusterUpdateSettingWithConflict(t *testing.T) {
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: true,
RestartEvenIfNoUpdatesAvailable: true,
ForceSendFields: []string{"Enabled", "RestartEvenIfNoUpdatesAvailable"},
},
SettingName: "default",
},
Expand Down Expand Up @@ -312,7 +318,8 @@ func TestQueryDeleteAutomaticClusterUpdateSetting(t *testing.T) {
Etag: "etag1",
SettingName: "default",
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: false,
Enabled: false,
ForceSendFields: []string{"Enabled"},
},
},
}).Return(&settings.AutomaticClusterUpdateSetting{
Expand Down Expand Up @@ -347,7 +354,8 @@ func TestQueryDeleteAutomaticClusterUpdateSettingWithConflict(t *testing.T) {
Etag: "etag1",
SettingName: "default",
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: false,
Enabled: false,
ForceSendFields: []string{"Enabled"},
},
},
}).Return(nil, &apierr.APIError{
Expand All @@ -368,7 +376,8 @@ func TestQueryDeleteAutomaticClusterUpdateSettingWithConflict(t *testing.T) {
Etag: "etag2",
SettingName: "default",
AutomaticClusterUpdateWorkspace: settings.ClusterAutoRestartMessage{
Enabled: false,
Enabled: false,
ForceSendFields: []string{"Enabled"},
},
},
}).Return(&settings.AutomaticClusterUpdateSetting{
Expand Down
8 changes: 8 additions & 0 deletions settings/resource_compliance_security_profile_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/settings"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Enhanced Security Monitoring setting
Expand All @@ -16,13 +18,19 @@ var complianceSecurityProfileFieldMask = strings.Join([]string{
}, ",")
var complianceSecurityProfileSetting = workspaceSetting[settings.ComplianceSecurityProfileSetting]{
settingStruct: settings.ComplianceSecurityProfileSetting{},
customizeSchemaFunc: func(s map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(s, "compliance_security_profile_workspace", "compliance_standards").SetRequired()
common.CustomizeSchemaPath(s, "compliance_security_profile_workspace", "is_enabled").SetRequired()
return s
},
readFunc: func(ctx context.Context, w *databricks.WorkspaceClient, etag string) (*settings.ComplianceSecurityProfileSetting, error) {
return w.Settings.ComplianceSecurityProfile().Get(ctx, settings.GetComplianceSecurityProfileSettingRequest{
Etag: etag,
})
},
updateFunc: func(ctx context.Context, w *databricks.WorkspaceClient, t settings.ComplianceSecurityProfileSetting) (string, error) {
t.SettingName = "default"
t.ComplianceSecurityProfileWorkspace.ForceSendFields = []string{"IsEnabled"}
res, err := w.Settings.ComplianceSecurityProfile().Update(ctx, settings.UpdateComplianceSecurityProfileSettingRequest{
AllowMissing: true,
Setting: t,
Expand Down
5 changes: 5 additions & 0 deletions settings/resource_compliance_security_profile_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestQueryCreateComplianceSecurityProfileSettingWithNoneStandard(t *testing.
ComplianceSecurityProfileWorkspace: settings.ComplianceSecurityProfile{
IsEnabled: true,
ComplianceStandards: []settings.ComplianceStandard{"NONE"},
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand All @@ -47,6 +48,7 @@ func TestQueryCreateComplianceSecurityProfileSettingWithNoneStandard(t *testing.
ComplianceSecurityProfileWorkspace: settings.ComplianceSecurityProfile{
IsEnabled: true,
ComplianceStandards: []settings.ComplianceStandard{"NONE"},
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand Down Expand Up @@ -136,6 +138,7 @@ func TestQueryUpdateComplianceSecurityProfileSetting(t *testing.T) {
ComplianceSecurityProfileWorkspace: settings.ComplianceSecurityProfile{
IsEnabled: true,
ComplianceStandards: []settings.ComplianceStandard{"HIPAA", "PCI_DSS"},
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand Down Expand Up @@ -192,6 +195,7 @@ func TestQueryUpdateComplianceSecurityProfileSettingWithConflict(t *testing.T) {
ComplianceSecurityProfileWorkspace: settings.ComplianceSecurityProfile{
IsEnabled: true,
ComplianceStandards: []settings.ComplianceStandard{"HIPAA"},
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand All @@ -214,6 +218,7 @@ func TestQueryUpdateComplianceSecurityProfileSettingWithConflict(t *testing.T) {
ComplianceSecurityProfileWorkspace: settings.ComplianceSecurityProfile{
IsEnabled: true,
ComplianceStandards: []settings.ComplianceStandard{"HIPAA"},
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand Down
10 changes: 9 additions & 1 deletion settings/resource_enhanced_security_monitoring_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/settings"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Enhanced Security Monitoring setting
Expand All @@ -14,13 +16,18 @@ var enhancedSecurityMonitoringFieldMask = strings.Join([]string{
}, ",")
var enhancedSecurityMonitoringSetting = workspaceSetting[settings.EnhancedSecurityMonitoringSetting]{
settingStruct: settings.EnhancedSecurityMonitoringSetting{},
customizeSchemaFunc: func(s map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(s, "enhanced_security_monitoring_workspace", "is_enabled").SetRequired()
return s
},
readFunc: func(ctx context.Context, w *databricks.WorkspaceClient, etag string) (*settings.EnhancedSecurityMonitoringSetting, error) {
return w.Settings.EnhancedSecurityMonitoring().Get(ctx, settings.GetEnhancedSecurityMonitoringSettingRequest{
Etag: etag,
})
},
updateFunc: func(ctx context.Context, w *databricks.WorkspaceClient, t settings.EnhancedSecurityMonitoringSetting) (string, error) {
t.SettingName = "default"
t.EnhancedSecurityMonitoringWorkspace.ForceSendFields = []string{"IsEnabled"}
res, err := w.Settings.EnhancedSecurityMonitoring().Update(ctx, settings.UpdateEnhancedSecurityMonitoringSettingRequest{
AllowMissing: true,
Setting: t,
Expand All @@ -38,7 +45,8 @@ var enhancedSecurityMonitoringSetting = workspaceSetting[settings.EnhancedSecuri
Etag: etag,
SettingName: "default",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: false,
IsEnabled: false,
ForceSendFields: []string{"IsEnabled"},
},
},
FieldMask: enhancedSecurityMonitoringFieldMask,
Expand Down
27 changes: 18 additions & 9 deletions settings/resource_enhanced_security_monitoring_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestQueryCreateEnhancedSecurityMonitoringSetting(t *testing.T) {
Setting: settings.EnhancedSecurityMonitoringSetting{
Etag: "",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: true,
IsEnabled: true,
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand All @@ -44,7 +45,8 @@ func TestQueryCreateEnhancedSecurityMonitoringSetting(t *testing.T) {
Setting: settings.EnhancedSecurityMonitoringSetting{
Etag: "etag1",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: true,
IsEnabled: true,
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand Down Expand Up @@ -123,14 +125,16 @@ func TestQueryUpdateEnhancedSecurityMonitoringSetting(t *testing.T) {
Setting: settings.EnhancedSecurityMonitoringSetting{
Etag: "etag1",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: true,
IsEnabled: true,
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
}).Return(&settings.EnhancedSecurityMonitoringSetting{
Etag: "etag2",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: true,
IsEnabled: true,
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
}, nil)
Expand Down Expand Up @@ -173,7 +177,8 @@ func TestQueryUpdateEnhancedSecurityMonitoringSettingWithConflict(t *testing.T)
Setting: settings.EnhancedSecurityMonitoringSetting{
Etag: "etag1",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: true,
IsEnabled: true,
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand All @@ -194,7 +199,8 @@ func TestQueryUpdateEnhancedSecurityMonitoringSettingWithConflict(t *testing.T)
Setting: settings.EnhancedSecurityMonitoringSetting{
Etag: "etag2",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: true,
IsEnabled: true,
ForceSendFields: []string{"IsEnabled"},
},
SettingName: "default",
},
Expand Down Expand Up @@ -244,7 +250,8 @@ func TestQueryDeleteEnhancedSecurityMonitoringSetting(t *testing.T) {
Etag: "etag1",
SettingName: "default",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: false,
IsEnabled: false,
ForceSendFields: []string{"IsEnabled"},
},
},
}).Return(&settings.EnhancedSecurityMonitoringSetting{
Expand Down Expand Up @@ -279,7 +286,8 @@ func TestQueryDeleteEnhancedSecurityMonitoringSettingWithConflict(t *testing.T)
Etag: "etag1",
SettingName: "default",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: false,
IsEnabled: false,
ForceSendFields: []string{"IsEnabled"},
},
},
}).Return(nil, &apierr.APIError{
Expand All @@ -300,7 +308,8 @@ func TestQueryDeleteEnhancedSecurityMonitoringSettingWithConflict(t *testing.T)
Etag: "etag2",
SettingName: "default",
EnhancedSecurityMonitoringWorkspace: settings.EnhancedSecurityMonitoring{
IsEnabled: false,
IsEnabled: false,
ForceSendFields: []string{"IsEnabled"},
},
},
}).Return(&settings.EnhancedSecurityMonitoringSetting{
Expand Down

0 comments on commit 00d8fa8

Please sign in to comment.