Skip to content

Commit 15bcffc

Browse files
authored
Added support for databricks_mws_log_delivery update (#1439)
1 parent 73d5b17 commit 15bcffc

File tree

3 files changed

+199
-25
lines changed

3 files changed

+199
-25
lines changed

docs/resources/mws_log_delivery.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ subcategory: "Log Delivery"
55

66
-> **Note** This resource has an evolving API, which will change in the upcoming versions of the provider in order to simplify user experience.
77

8-
Make sure you have authenticated with [username and password for Accounts Console](../guides/aws-workspace.md). This resource configures the delivery of the two supported log types from Databricks workspaces: [billable usage logs](https://docs.databricks.com/administration-guide/account-settings/billable-usage-delivery.html) and [audit logs](https://docs.databricks.com/administration-guide/account-settings/audit-logs.html). You cannot delete a log delivery configuration, but you can disable it when you no longer need it. This fact is important because there is a limit to the number of enabled log delivery configurations that you can create for an account. There is a limit on the number of log delivery configurations that you can create for an account. You can create a maximum of two enabled configurations that use the account level (no workspace filter) and two enabled configurations for every specific workspace (a workspaceId can occur in the workspace filter for two configurations). You cannot delete a log delivery configuration, but you can disable it. You can re-enable a disabled configuration, but the request fails if it violates the limits previously described.
8+
Make sure you have authenticated with [username and password for Accounts Console](../guides/aws-workspace.md). This resource configures the delivery of the two supported log types from Databricks workspaces: [billable usage logs](https://docs.databricks.com/administration-guide/account-settings/billable-usage-delivery.html) and [audit logs](https://docs.databricks.com/administration-guide/account-settings/audit-logs.html).
9+
10+
You cannot delete a log delivery configuration, but you can disable it when you no longer need it. This fact is important because there is a limit to the number of enabled log delivery configurations that you can create for an account. You can create a maximum of two enabled configurations that use the account level (no workspace filter) and two enabled configurations for every specific workspace (a workspaceId can occur in the workspace filter for two configurations). You can re-enable a disabled configuration, but the request fails if it violates the limits previously described.
911

1012
## Example Usage
1113

@@ -130,6 +132,7 @@ resource "databricks_mws_log_delivery" "audit_logs" {
130132
* `output_format` - The file type of log delivery. Currently `CSV` (for `BILLABLE_USAGE`) and `JSON` (for `AUDIT_LOGS`) are supported.
131133
* `credentials_id` - The ID for a Databricks [credential configuration](mws_credentials.md) that represents the AWS IAM role [with policy](../data-sources/aws_assume_role_policy.md) and [trust relationship](../data-sources/aws_assume_role_policy.md) as described in the main billable usage documentation page.
132134
* `storage_configuration_id` - The ID for a Databricks [storage configuration](mws_storage_configurations.md) that represents the S3 bucket with [bucket policy](../data-sources/aws_bucket_policy.md) as described in the main billable usage documentation page.
135+
* `status` - Status of log delivery configuration. Set to ENABLED or DISABLED. Defaults to ENABLED. This is the only field you can update.
133136
* `workspace_ids_filter` - (Optional) By default, this log configuration applies to all workspaces associated with your account ID. If your account is on the E2 version of the platform or on a select custom plan that allows multiple workspaces per account, you may have multiple workspaces associated with your account ID. You can optionally set the field as mentioned earlier to an array of workspace IDs. If you plan to use different log delivery configurations for several workspaces, set this explicitly rather than leaving it blank. If you leave this blank and your account ID gets additional workspaces in the future, this configuration will also apply to the new workspaces.
134137
* `delivery_path_prefix` - (Optional) Defaults to empty, which means that logs are delivered to the root of the bucket. The value must be a valid S3 object key. It must not start or end with a slash character.
135138
* `delivery_start_time` - (Optional) The optional start month and year for delivery, specified in YYYY-MM format. Defaults to current year and month. Usage is not available before 2019-03.

mws/resource_mws_log_delivery.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package mws
33
import (
44
"context"
55
"fmt"
6-
"log"
76

87
"github.com/databricks/terraform-provider-databricks/common"
98

@@ -18,16 +17,16 @@ type LogDelivery struct {
1817

1918
// LogDeliveryConfiguration describes log delivery
2019
type LogDeliveryConfiguration struct {
21-
AccountID string `json:"account_id"`
22-
ConfigID string `json:"config_id,omitempty" tf:"computed"`
23-
CredentialsID string `json:"credentials_id"`
24-
StorageConfigurationID string `json:"storage_configuration_id"`
25-
WorkspaceIdsFilter []int64 `json:"workspace_ids_filter,omitempty"`
26-
ConfigName string `json:"config_name,omitempty"`
20+
AccountID string `json:"account_id" tf:"force_new"`
21+
ConfigID string `json:"config_id,omitempty" tf:"computed,force_new"`
22+
CredentialsID string `json:"credentials_id" tf:"force_new"`
23+
StorageConfigurationID string `json:"storage_configuration_id" tf:"force_new"`
24+
WorkspaceIdsFilter []int64 `json:"workspace_ids_filter,omitempty" tf:"force_new"`
25+
ConfigName string `json:"config_name,omitempty" tf:"force_new"`
2726
Status string `json:"status,omitempty" tf:"computed"`
28-
LogType string `json:"log_type"`
29-
OutputFormat string `json:"output_format"`
30-
DeliveryPathPrefix string `json:"delivery_path_prefix,omitempty"`
27+
LogType string `json:"log_type" tf:"force_new"`
28+
OutputFormat string `json:"output_format" tf:"force_new"`
29+
DeliveryPathPrefix string `json:"delivery_path_prefix,omitempty" tf:"force_new"`
3130
DeliveryStartTime string `json:"delivery_start_time,omitempty" tf:"computed,force_new"`
3231
}
3332

@@ -59,10 +58,10 @@ func (a LogDeliveryAPI) Create(ldc LogDeliveryConfiguration) (string, error) {
5958
return ld.LogDeliveryConfiguration.ConfigID, err
6059
}
6160

62-
// Disable log delivery configuration - e.g. delete it
63-
func (a LogDeliveryAPI) Disable(accountID, configID string) error {
61+
// patch log delivery configuration - i.e. can only enable or disable it
62+
func (a LogDeliveryAPI) Patch(accountID, configID string, status string) error {
6463
return a.client.Patch(a.context, fmt.Sprintf("/accounts/%s/log-delivery/%s", accountID, configID), map[string]string{
65-
"status": "DISABLED",
64+
"status": status,
6665
})
6766
}
6867

@@ -93,6 +92,15 @@ func ResourceMwsLogDelivery() *schema.Resource {
9392
p.Pack(d)
9493
return nil
9594
},
95+
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
96+
var ldc LogDeliveryConfiguration
97+
common.DataToStructPointer(d, s, &ldc)
98+
err := NewLogDeliveryAPI(ctx, c).Patch(ldc.AccountID, ldc.ConfigID, ldc.Status)
99+
if err != nil {
100+
return err
101+
}
102+
return common.StructToData(ldc, s, d)
103+
},
96104
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
97105
accountID, configID, err := p.Unpack(d)
98106
if err != nil {
@@ -102,19 +110,14 @@ func ResourceMwsLogDelivery() *schema.Resource {
102110
if err != nil {
103111
return err
104112
}
105-
if ldc.Status == "DISABLED" {
106-
log.Printf("[DEBUG] Log delivery configuration %s was disabled. Removing from state.", configID)
107-
d.SetId("")
108-
return nil
109-
}
110113
return common.StructToData(ldc, s, d)
111114
},
112115
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
113116
accountID, configID, err := p.Unpack(d)
114117
if err != nil {
115118
return err
116119
}
117-
return NewLogDeliveryAPI(ctx, c).Disable(accountID, configID)
120+
return NewLogDeliveryAPI(ctx, c).Patch(accountID, configID, "DISABLED")
118121
},
119122
}.ToResource()
120123
}

mws/resource_mws_log_delivery_test.go

Lines changed: 173 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestMwsAccLogDelivery(t *testing.T) {
4747
})
4848
require.NoError(t, err)
4949
defer func() {
50-
assert.NoError(t, logDeliveryAPI.Disable(acctID, configID))
50+
assert.NoError(t, logDeliveryAPI.Patch(acctID, configID, "DISABLED"))
5151
}()
5252

5353
ldc, err := logDeliveryAPI.Read(acctID, configID)
@@ -117,6 +117,71 @@ func TestResourceLogDeliveryCreate(t *testing.T) {
117117
assert.Equal(t, "nid", d.Get("config_id"))
118118
}
119119

120+
func TestResourceLogDeliveryCreateDisabled(t *testing.T) {
121+
d, err := qa.ResourceFixture{
122+
Fixtures: []qa.HTTPFixture{
123+
{
124+
Method: "POST",
125+
Resource: "/api/2.0/accounts/abc/log-delivery",
126+
ExpectedRequest: LogDelivery{
127+
LogDeliveryConfiguration: LogDeliveryConfiguration{
128+
AccountID: "abc",
129+
ConfigName: "Audit logs",
130+
CredentialsID: "bcd",
131+
DeliveryPathPrefix: "/a/b",
132+
LogType: "AUDIT_LOGS",
133+
OutputFormat: "JSON",
134+
StorageConfigurationID: "def",
135+
DeliveryStartTime: "2020-10",
136+
Status: "DISABLED",
137+
WorkspaceIdsFilter: []int64{1111111111111111, 222222222222222},
138+
},
139+
},
140+
Response: LogDelivery{
141+
LogDeliveryConfiguration: LogDeliveryConfiguration{
142+
ConfigID: "nid",
143+
},
144+
},
145+
},
146+
{
147+
Method: "GET",
148+
Resource: "/api/2.0/accounts/abc/log-delivery/nid",
149+
Response: LogDelivery{
150+
LogDeliveryConfiguration: LogDeliveryConfiguration{
151+
ConfigID: "nid",
152+
AccountID: "abc",
153+
ConfigName: "Audit logs",
154+
CredentialsID: "bcd",
155+
DeliveryPathPrefix: "/a/b",
156+
LogType: "AUDIT_LOGS",
157+
OutputFormat: "JSON",
158+
StorageConfigurationID: "def",
159+
DeliveryStartTime: "2020-10",
160+
Status: "DISABLED",
161+
WorkspaceIdsFilter: []int64{1111111111111111, 222222222222222},
162+
},
163+
},
164+
},
165+
},
166+
Resource: ResourceMwsLogDelivery(),
167+
HCL: `
168+
account_id = "abc"
169+
credentials_id = "bcd"
170+
storage_configuration_id = "def"
171+
config_name = "Audit logs"
172+
log_type = "AUDIT_LOGS"
173+
output_format = "JSON"
174+
delivery_path_prefix = "/a/b"
175+
workspace_ids_filter = [1111111111111111, 222222222222222]
176+
delivery_start_time = "2020-10"
177+
status = "DISABLED"`,
178+
Create: true,
179+
}.Apply(t)
180+
assert.NoError(t, err, err)
181+
assert.Equal(t, "abc|nid", d.Id())
182+
assert.Equal(t, "nid", d.Get("config_id"))
183+
}
184+
120185
func TestResourceLogDeliveryCreate_Error(t *testing.T) {
121186
d, err := qa.ResourceFixture{
122187
Fixtures: []qa.HTTPFixture{
@@ -222,8 +287,113 @@ func TestResourceLogDeliveryRead_Error(t *testing.T) {
222287
assert.Equal(t, "abc|nid", d.Id(), "Id should not be empty for error reads")
223288
}
224289

290+
func TestUpdateLogDelivery(t *testing.T) {
291+
qa.ResourceFixture{
292+
Fixtures: []qa.HTTPFixture{
293+
{
294+
Method: "PATCH",
295+
Resource: "/api/2.0/accounts/abc/log-delivery/nid",
296+
ExpectedRequest: map[string]interface{}{
297+
"status": "ENABLED",
298+
},
299+
},
300+
{
301+
Method: "GET",
302+
Resource: "/api/2.0/accounts/abc/log-delivery/nid",
303+
Response: LogDelivery{
304+
LogDeliveryConfiguration: LogDeliveryConfiguration{
305+
ConfigID: "nid",
306+
Status: "ENABLED",
307+
AccountID: "abc",
308+
ConfigName: "Audit logs",
309+
CredentialsID: "bcd",
310+
DeliveryPathPrefix: "/a/b",
311+
LogType: "AUDIT_LOGS",
312+
OutputFormat: "JSON",
313+
StorageConfigurationID: "def",
314+
DeliveryStartTime: "2020-10",
315+
},
316+
},
317+
},
318+
},
319+
Resource: ResourceMwsLogDelivery(),
320+
ID: "abc|nid",
321+
Update: true,
322+
RequiresNew: true,
323+
InstanceState: map[string]string{
324+
"account_id": "abc",
325+
"config_id": "nid",
326+
"config_name": "Audit logs",
327+
"credentials_id": "bcd",
328+
"delivery_path_prefix": "/a/b",
329+
"delivery_start_time": "2020-10",
330+
"id": "abc|nid",
331+
"log_type": "AUDIT_LOGS",
332+
"output_format": "JSON",
333+
"status": "DISABLED",
334+
"storage_configuration_id": "def",
335+
},
336+
HCL: `
337+
account_id = "abc"
338+
credentials_id = "bcd"
339+
storage_configuration_id = "def"
340+
config_name = "Audit logs"
341+
log_type = "AUDIT_LOGS"
342+
output_format = "JSON"
343+
delivery_path_prefix = "/a/b"
344+
delivery_start_time = "2020-10"
345+
status = "ENABLED"
346+
`,
347+
}.ApplyNoError(t)
348+
}
349+
350+
func TestUpdateLogDeliveryError(t *testing.T) {
351+
_, err := qa.ResourceFixture{
352+
Fixtures: []qa.HTTPFixture{
353+
{
354+
Method: "PATCH",
355+
Resource: "/api/2.0/accounts/abc/log-delivery/nid",
356+
Response: common.APIErrorBody{
357+
ErrorCode: "INVALID_REQUEST",
358+
Message: "Internal error happened",
359+
},
360+
Status: 400,
361+
},
362+
},
363+
Resource: ResourceMwsLogDelivery(),
364+
ID: "abc|nid",
365+
Update: true,
366+
RequiresNew: true,
367+
InstanceState: map[string]string{
368+
"account_id": "abc",
369+
"config_id": "nid",
370+
"config_name": "Audit logs",
371+
"credentials_id": "bcd",
372+
"delivery_path_prefix": "/a/b",
373+
"delivery_start_time": "2020-10",
374+
"id": "abc|nid",
375+
"log_type": "AUDIT_LOGS",
376+
"output_format": "JSON",
377+
"status": "DISABLED",
378+
"storage_configuration_id": "def",
379+
},
380+
HCL: `
381+
account_id = "abc"
382+
credentials_id = "bcd"
383+
storage_configuration_id = "def"
384+
config_name = "Audit logs"
385+
log_type = "AUDIT_LOGS"
386+
output_format = "JSON"
387+
delivery_path_prefix = "/a/b"
388+
delivery_start_time = "2020-10"
389+
status = "ENABLED"
390+
`,
391+
}.Apply(t)
392+
qa.AssertErrorStartsWith(t, err, "Internal error happened")
393+
}
394+
225395
func TestResourceLogDeliveryDelete(t *testing.T) {
226-
d, err := qa.ResourceFixture{
396+
qa.ResourceFixture{
227397
Fixtures: []qa.HTTPFixture{
228398
{
229399
Method: "PATCH",
@@ -236,9 +406,7 @@ func TestResourceLogDeliveryDelete(t *testing.T) {
236406
Resource: ResourceMwsLogDelivery(),
237407
Delete: true,
238408
ID: "abc|nid",
239-
}.Apply(t)
240-
assert.NoError(t, err, err)
241-
assert.Equal(t, "abc|nid", d.Id())
409+
}.ApplyNoError(t)
242410
}
243411

244412
func TestResourceLogDeliveryDelete_Error(t *testing.T) {

0 commit comments

Comments
 (0)