Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set 'project' attribute when importing 'google_logging_project_sink' #1019

Merged
merged 3 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/import_logging_project_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAccLoggingProjectSink_importBasic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccLoggingProjectSink_basic(sinkName, bucketName),
Config: testAccLoggingProjectSink_basic(sinkName, getTestProjectFromEnv(), bucketName),
},

resource.TestStep{
Expand Down
24 changes: 23 additions & 1 deletion google/resource_logging_project_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ func resourceLoggingProjectSink() *schema.Resource {
Update: resourceLoggingProjectSinkUpdate,
Schema: resourceLoggingSinkSchema(),
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceLoggingProjectSinkImportState,
},
}
schm.Schema["project"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
}
schm.Schema["unique_writer_identity"] = &schema.Schema{
Expand Down Expand Up @@ -57,11 +58,17 @@ func resourceLoggingProjectSinkCreate(d *schema.ResourceData, meta interface{})
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)
Expand Down Expand Up @@ -96,3 +103,18 @@ func resourceLoggingProjectSinkDelete(d *schema.ResourceData, meta interface{})
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
}
12 changes: 7 additions & 5 deletions google/resource_logging_project_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package google

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/logging/v2"
"strconv"
"testing"
)

func TestAccLoggingProjectSink_basic(t *testing.T) {
Expand All @@ -24,7 +25,7 @@ func TestAccLoggingProjectSink_basic(t *testing.T) {
CheckDestroy: testAccCheckLoggingProjectSinkDestroy,
Steps: []resource.TestStep{
{
Config: testAccLoggingProjectSink_basic(sinkName, bucketName),
Config: testAccLoggingProjectSink_basic(sinkName, getTestProjectFromEnv(), bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLoggingProjectSinkExists("google_logging_project_sink.basic", &sink),
testAccCheckLoggingProjectSink(&sink, "google_logging_project_sink.basic"),
Expand Down Expand Up @@ -165,18 +166,19 @@ func testAccCheckLoggingProjectSink(sink *logging.LogSink, n string) resource.Te
}
}

func testAccLoggingProjectSink_basic(name, bucketName string) string {
func testAccLoggingProjectSink_basic(name, project, bucketName string) string {
return fmt.Sprintf(`
resource "google_logging_project_sink" "basic" {
name = "%s"
project = "%s"
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
filter = "logName=\"projects/%s/logs/compute.googleapis.com%%2Factivity_log\" AND severity>=ERROR"
unique_writer_identity = false
}

resource "google_storage_bucket" "log-bucket" {
name = "%s"
}`, name, getTestProjectFromEnv(), bucketName)
}`, name, project, project, bucketName)
}

func testAccLoggingProjectSink_uniqueWriter(name, bucketName string) string {
Expand Down