-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treat _Default & _Required Sinks same as buckets
- Loading branch information
1 parent
2b59ef2
commit 2f9cf7c
Showing
2 changed files
with
54 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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, | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters