Skip to content

Commit 2321b41

Browse files
Add customized diff for unique_writer_identity on resource google_logging_project_sink (#4301) (#2767)
* Add customized diff and documentation for bigquery options scenario * resolve comments Signed-off-by: Modular Magician <[email protected]>
1 parent 0869efe commit 2321b41

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

.changelog/4301.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
logging: added plan time validation for `unique_writer_identity` on `google_logging_project_sink`
3+
```

google-beta/resource_logging_project_sink.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package google
22

33
import (
4+
"context"
5+
"errors"
46
"fmt"
57

68
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -10,11 +12,12 @@ const nonUniqueWriterAccount = "serviceAccount:[email protected]
1012

1113
func resourceLoggingProjectSink() *schema.Resource {
1214
schm := &schema.Resource{
13-
Create: resourceLoggingProjectSinkCreate,
14-
Read: resourceLoggingProjectSinkRead,
15-
Delete: resourceLoggingProjectSinkDelete,
16-
Update: resourceLoggingProjectSinkUpdate,
17-
Schema: resourceLoggingSinkSchema(),
15+
Create: resourceLoggingProjectSinkCreate,
16+
Read: resourceLoggingProjectSinkRead,
17+
Delete: resourceLoggingProjectSinkDelete,
18+
Update: resourceLoggingProjectSinkUpdate,
19+
Schema: resourceLoggingSinkSchema(),
20+
CustomizeDiff: resourceLoggingProjectSinkCustomizeDiff,
1821
Importer: &schema.ResourceImporter{
1922
State: resourceLoggingSinkImportState("project"),
2023
},
@@ -61,6 +64,27 @@ func resourceLoggingProjectSinkCreate(d *schema.ResourceData, meta interface{})
6164
return resourceLoggingProjectSinkRead(d, meta)
6265
}
6366

67+
// if bigquery_options is set unique_writer_identity must be true
68+
func resourceLoggingProjectSinkCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
69+
// separate func to allow unit testing
70+
return resourceLoggingProjectSinkCustomizeDiffFunc(d)
71+
}
72+
73+
func resourceLoggingProjectSinkCustomizeDiffFunc(diff TerraformResourceDiff) error {
74+
if !diff.HasChange("bigquery_options.#") {
75+
return nil
76+
}
77+
78+
bigqueryOptions := diff.Get("bigquery_options.#").(int)
79+
if bigqueryOptions > 0 {
80+
uwi := diff.Get("unique_writer_identity")
81+
if !uwi.(bool) {
82+
return errors.New("unique_writer_identity must be true when bigquery_options is supplied")
83+
}
84+
}
85+
return nil
86+
}
87+
6488
func resourceLoggingProjectSinkRead(d *schema.ResourceData, meta interface{}) error {
6589
config := meta.(*Config)
6690
userAgent, err := generateUserAgentString(d, config.userAgent)

google-beta/resource_logging_project_sink_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,62 @@ func TestAccLoggingProjectSink_loggingbucket(t *testing.T) {
185185
})
186186
}
187187

188+
func TestLoggingProjectSink_bigqueryOptionCustomizedDiff(t *testing.T) {
189+
t.Parallel()
190+
191+
type LoggingProjectSink struct {
192+
BigqueryOptions int
193+
UniqueWriterIdentity bool
194+
}
195+
cases := map[string]struct {
196+
ExpectedError bool
197+
After LoggingProjectSink
198+
}{
199+
"no biquery options with false unique writer identity": {
200+
ExpectedError: false,
201+
After: LoggingProjectSink{
202+
BigqueryOptions: 0,
203+
UniqueWriterIdentity: false,
204+
},
205+
},
206+
"no biquery options with true unique writer identity": {
207+
ExpectedError: false,
208+
After: LoggingProjectSink{
209+
BigqueryOptions: 0,
210+
UniqueWriterIdentity: true,
211+
},
212+
},
213+
"biquery options with false unique writer identity": {
214+
ExpectedError: true,
215+
After: LoggingProjectSink{
216+
BigqueryOptions: 1,
217+
UniqueWriterIdentity: false,
218+
},
219+
},
220+
"biquery options with true unique writer identity": {
221+
ExpectedError: false,
222+
After: LoggingProjectSink{
223+
BigqueryOptions: 1,
224+
UniqueWriterIdentity: true,
225+
},
226+
},
227+
}
228+
229+
for tn, tc := range cases {
230+
d := &ResourceDiffMock{
231+
After: map[string]interface{}{
232+
"bigquery_options.#": tc.After.BigqueryOptions,
233+
"unique_writer_identity": tc.After.UniqueWriterIdentity,
234+
},
235+
}
236+
err := resourceLoggingProjectSinkCustomizeDiffFunc(d)
237+
hasError := err != nil
238+
if tc.ExpectedError != hasError {
239+
t.Errorf("%v: expected has error %v, but was %v", tn, tc.ExpectedError, hasError)
240+
}
241+
}
242+
}
243+
188244
func testAccCheckLoggingProjectSinkDestroyProducer(t *testing.T) func(s *terraform.State) error {
189245
return func(s *terraform.State) error {
190246
config := googleProviderConfig(t)

website/docs/r/logging_project_sink.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ The following arguments are supported:
140140

141141
* `unique_writer_identity` - (Optional) Whether or not to create a unique identity associated with this sink. If `false`
142142
(the default), then the `writer_identity` used is `serviceAccount:[email protected]`. If `true`,
143-
then a unique service account is created and used for this sink. If you wish to publish logs across projects, you
144-
must set `unique_writer_identity` to true.
143+
then a unique service account is created and used for this sink. If you wish to publish logs across projects or utilize
144+
`bigquery_options`, you must set `unique_writer_identity` to true.
145145

146146
* `bigquery_options` - (Optional) Options that affect sinks exporting data to BigQuery. Structure documented below.
147147

0 commit comments

Comments
 (0)