mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-01 16:21:06 +00:00
035a581b9f
Fixes #1494. * Add import support for `google_logging_organization_sink`, `google_logging_folder_sink`, `google_logging_billing_account_sink`. Using `StateFunc` over `DiffSuppressFunc` should only affect tests; for some reason `TestAccLoggingFolderSink_folderAcceptsFullFolderPath` expected a `folder` value of `folders/{{id}}` vs expecting `{{id}}` when only `DiffSuppressFunc` was used, when in real use `DiffSuppressFunc` should be sufficient.
98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
func resourceLoggingFolderSink() *schema.Resource {
|
|
schm := &schema.Resource{
|
|
Create: resourceLoggingFolderSinkCreate,
|
|
Read: resourceLoggingFolderSinkRead,
|
|
Delete: resourceLoggingFolderSinkDelete,
|
|
Update: resourceLoggingFolderSinkUpdate,
|
|
Schema: resourceLoggingSinkSchema(),
|
|
Importer: &schema.ResourceImporter{
|
|
State: resourceLoggingSinkImportState("folder"),
|
|
},
|
|
}
|
|
schm.Schema["folder"] = &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
StateFunc: func(v interface{}) string {
|
|
return strings.Replace(v.(string), "folders/", "", 1)
|
|
},
|
|
}
|
|
schm.Schema["include_children"] = &schema.Schema{
|
|
Type: schema.TypeBool,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
Default: false,
|
|
}
|
|
|
|
return schm
|
|
}
|
|
|
|
func resourceLoggingFolderSinkCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
folder := parseFolderId(d.Get("folder"))
|
|
id, sink := expandResourceLoggingSink(d, "folders", folder)
|
|
sink.IncludeChildren = d.Get("include_children").(bool)
|
|
|
|
// The API will reject any requests that don't explicitly set 'uniqueWriterIdentity' to true.
|
|
_, err := config.clientLogging.Folders.Sinks.Create(id.parent(), sink).UniqueWriterIdentity(true).Do()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId(id.canonicalId())
|
|
return resourceLoggingFolderSinkRead(d, meta)
|
|
}
|
|
|
|
func resourceLoggingFolderSinkRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
sink, err := config.clientLogging.Folders.Sinks.Get(d.Id()).Do()
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Folder Logging Sink %s", d.Get("name").(string)))
|
|
}
|
|
|
|
flattenResourceLoggingSink(d, sink)
|
|
d.Set("include_children", sink.IncludeChildren)
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceLoggingFolderSinkUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
sink := expandResourceLoggingSinkForUpdate(d)
|
|
// It seems the API might actually accept an update for include_children; this is not in the list of updatable
|
|
// properties though and might break in the future. Always include the value to prevent it changing.
|
|
sink.IncludeChildren = d.Get("include_children").(bool)
|
|
sink.ForceSendFields = append(sink.ForceSendFields, "IncludeChildren")
|
|
|
|
// The API will reject any requests that don't explicitly set 'uniqueWriterIdentity' to true.
|
|
_, err := config.clientLogging.Folders.Sinks.Patch(d.Id(), sink).UniqueWriterIdentity(true).Do()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceLoggingFolderSinkRead(d, meta)
|
|
}
|
|
|
|
func resourceLoggingFolderSinkDelete(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
_, err := config.clientLogging.Projects.Sinks.Delete(d.Id()).Do()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|