Skip to content

Commit

Permalink
Treat _Default & _Required Sinks same as buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
zachberger committed Oct 31, 2023
1 parent 2b59ef2 commit 2f9cf7c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"log"

"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
Expand All @@ -17,7 +18,7 @@ const nonUniqueWriterAccount = "serviceAccount:[email protected]

func ResourceLoggingProjectSink() *schema.Resource {
schm := &schema.Resource{
Create: resourceLoggingProjectSinkCreate,
Create: resourceLoggingProjectSinkAcquireOrCreate,
Read: resourceLoggingProjectSinkRead,
Delete: resourceLoggingProjectSinkDelete,
Update: resourceLoggingProjectSinkUpdate,
Expand Down Expand Up @@ -49,7 +50,7 @@ func ResourceLoggingProjectSink() *schema.Resource {
return schm
}

func resourceLoggingProjectSinkCreate(d *schema.ResourceData, meta interface{}) error {
func resourceLoggingProjectSinkAcquireOrCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
Expand All @@ -65,25 +66,32 @@ func resourceLoggingProjectSinkCreate(d *schema.ResourceData, meta interface{})
uniqueWriterIdentity := d.Get("unique_writer_identity").(bool)
customWriterIdentity := d.Get("custom_writer_identity").(string)

projectSinkCreateRequest := config.NewLoggingClient(userAgent).Projects.Sinks.Create(id.parent(), sink)
log.Printf("[DEBUG] Fetching logging bucket config: %#v", id)

// if custom-sa is specified, use it to write log and it requires uniqueWriterIdentity to be set as well
// otherwise set the uniqueWriter identity
if customWriterIdentity != "" {
projectSinkCreateRequest = projectSinkCreateRequest.UniqueWriterIdentity(uniqueWriterIdentity).CustomWriterIdentity(customWriterIdentity)
} else {
projectSinkCreateRequest = projectSinkCreateRequest.UniqueWriterIdentity(uniqueWriterIdentity)
}
res, _ := config.NewLoggingClient(userAgent).Projects.Sinks.Get(id.canonicalId()).Do()
if res == nil {
projectSinkCreateRequest := config.NewLoggingClient(userAgent).Projects.Sinks.Create(id.parent(), sink)

_, err = projectSinkCreateRequest.Do()
// if custom-sa is specified, use it to write log and it requires uniqueWriterIdentity to be set as well
// otherwise set the uniqueWriter identity
if customWriterIdentity != "" {
projectSinkCreateRequest = projectSinkCreateRequest.UniqueWriterIdentity(uniqueWriterIdentity).CustomWriterIdentity(customWriterIdentity)
} else {
projectSinkCreateRequest = projectSinkCreateRequest.UniqueWriterIdentity(uniqueWriterIdentity)
}

if err != nil {
return err
}
_, err = projectSinkCreateRequest.Do()

if err != nil {
return err
}

d.SetId(id.canonicalId())
return resourceLoggingProjectSinkRead(d, meta)
}
d.SetId(id.canonicalId())

return resourceLoggingProjectSinkRead(d, meta)
return resourceLoggingProjectSinkUpdate(d, meta)
}

// if bigquery_options is set unique_writer_identity must be true
Expand Down Expand Up @@ -175,6 +183,14 @@ func resourceLoggingProjectSinkUpdate(d *schema.ResourceData, meta interface{})
}

func resourceLoggingProjectSinkDelete(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name")
for _, restrictedName := range []string{"_Required", "_Default"} {
if name == restrictedName {
log.Printf("[WARN] Default logging sinks cannot be deleted. Removing logging sinks config from state: %#v", d.Id())
return nil
}
}

config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ func TestAccLoggingProjectSink_basic(t *testing.T) {
})
}

func TestAccLoggingProjectSink_default(t *testing.T) {
t.Parallel()

sinkName := "_Default"
bucketName := "tf-test-sink-bucket-" + acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckLoggingProjectSinkDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccLoggingProjectSink_basic(sinkName, envvar.GetTestProjectFromEnv(), bucketName),
},
{
ResourceName: "google_logging_project_sink.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccLoggingProjectSink_described(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 2f9cf7c

Please sign in to comment.