mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
const nonUniqueWriterAccount = "serviceAccount:cloud-logs@system.gserviceaccount.com"
|
|
|
|
func resourceLoggingProjectSink() *schema.Resource {
|
|
schm := &schema.Resource{
|
|
Create: resourceLoggingProjectSinkCreate,
|
|
Read: resourceLoggingProjectSinkRead,
|
|
Delete: resourceLoggingProjectSinkDelete,
|
|
Update: resourceLoggingProjectSinkUpdate,
|
|
Schema: resourceLoggingSinkSchema(),
|
|
Importer: &schema.ResourceImporter{
|
|
State: resourceLoggingProjectSinkImportState,
|
|
},
|
|
}
|
|
schm.Schema["project"] = &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
}
|
|
schm.Schema["unique_writer_identity"] = &schema.Schema{
|
|
Type: schema.TypeBool,
|
|
Optional: true,
|
|
Default: false,
|
|
ForceNew: true,
|
|
}
|
|
return schm
|
|
}
|
|
|
|
func resourceLoggingProjectSinkCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
id, sink := expandResourceLoggingSink(d, "projects", project)
|
|
uniqueWriterIdentity := d.Get("unique_writer_identity").(bool)
|
|
|
|
_, err = config.clientLogging.Projects.Sinks.Create(id.parent(), sink).UniqueWriterIdentity(uniqueWriterIdentity).Do()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId(id.canonicalId())
|
|
|
|
return resourceLoggingProjectSinkRead(d, meta)
|
|
}
|
|
|
|
func resourceLoggingProjectSinkRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sink, err := config.clientLogging.Projects.Sinks.Get(d.Id()).Do()
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Project Logging Sink %s", d.Get("name").(string)))
|
|
}
|
|
|
|
d.Set("project", project)
|
|
flattenResourceLoggingSink(d, sink)
|
|
if sink.WriterIdentity != nonUniqueWriterAccount {
|
|
d.Set("unique_writer_identity", true)
|
|
} else {
|
|
d.Set("unique_writer_identity", false)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func resourceLoggingProjectSinkUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
sink := expandResourceLoggingSinkForUpdate(d)
|
|
uniqueWriterIdentity := d.Get("unique_writer_identity").(bool)
|
|
|
|
_, err := config.clientLogging.Projects.Sinks.Patch(d.Id(), sink).UniqueWriterIdentity(uniqueWriterIdentity).Do()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceLoggingProjectSinkRead(d, meta)
|
|
}
|
|
|
|
func resourceLoggingProjectSinkDelete(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
_, err := config.clientLogging.Projects.Sinks.Delete(d.Id()).Do()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId("")
|
|
return nil
|
|
}
|
|
|
|
func resourceLoggingProjectSinkImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
config := meta.(*Config)
|
|
|
|
loggingSinkId, err := parseLoggingSinkId(d.Id())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if config.Project != loggingSinkId.resourceId {
|
|
d.Set("project", loggingSinkId.resourceId)
|
|
}
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
}
|