-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add google_logging_folder_sink resource #470
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"strings" | ||
) | ||
|
||
func resourceLoggingFolderSink() *schema.Resource { | ||
schm := &schema.Resource{ | ||
Create: resourceLoggingFolderSinkCreate, | ||
Read: resourceLoggingFolderSinkRead, | ||
Delete: resourceLoggingFolderSinkDelete, | ||
Update: resourceLoggingFolderSinkUpdate, | ||
Schema: resourceLoggingSinkSchema(), | ||
} | ||
schm.Schema["folder"] = &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: optionalPrefixSuppress("folders/"), | ||
} | ||
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 := d.Get("folder").(string) | ||
if strings.HasPrefix(folder, "folders/") { | ||
folder = folder[len("folders/"):] | ||
} | ||
|
||
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 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"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" | ||
) | ||
|
||
func TestAccLoggingFolderSink_basic(t *testing.T) { | ||
skipIfEnvNotSet(t, "GOOGLE_ORG") | ||
|
||
sinkName := "tf-test-sink-" + acctest.RandString(10) | ||
bucketName := "tf-test-sink-bucket-" + acctest.RandString(10) | ||
folderName := "tf-test-folder-" + acctest.RandString(10) | ||
org := os.Getenv("GOOGLE_ORG") | ||
|
||
var sink logging.LogSink | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckLoggingFolderSinkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLoggingFolderSink_basic(sinkName, bucketName, folderName, "organizations/"+org), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.basic", &sink), | ||
testAccCheckLoggingFolderSink(&sink, "google_logging_folder_sink.basic"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccLoggingFolderSink_folderAcceptsFullFolderPath(t *testing.T) { | ||
skipIfEnvNotSet(t, "GOOGLE_ORG") | ||
|
||
sinkName := "tf-test-sink-" + acctest.RandString(10) | ||
bucketName := "tf-test-sink-bucket-" + acctest.RandString(10) | ||
folderName := "tf-test-folder-" + acctest.RandString(10) | ||
org := os.Getenv("GOOGLE_ORG") | ||
|
||
var sink logging.LogSink | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckLoggingFolderSinkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLoggingFolderSink_withFullFolderPath(sinkName, bucketName, folderName, "organizations/"+org), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.basic", &sink), | ||
testAccCheckLoggingFolderSink(&sink, "google_logging_folder_sink.basic"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccLoggingFolderSink_update(t *testing.T) { | ||
skipIfEnvNotSet(t, "GOOGLE_ORG") | ||
|
||
sinkName := "tf-test-sink-" + acctest.RandString(10) | ||
bucketName := "tf-test-sink-bucket-" + acctest.RandString(10) | ||
updatedBucketName := "tf-test-sink-bucket-" + acctest.RandString(10) | ||
folderName := "tf-test-folder-" + acctest.RandString(10) | ||
parent := "organizations/" + os.Getenv("GOOGLE_ORG") | ||
|
||
var sinkBefore, sinkAfter logging.LogSink | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckLoggingFolderSinkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLoggingFolderSink_basic(sinkName, bucketName, folderName, parent), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.basic", &sinkBefore), | ||
testAccCheckLoggingFolderSink(&sinkBefore, "google_logging_folder_sink.basic"), | ||
), | ||
}, { | ||
Config: testAccLoggingFolderSink_basic(sinkName, updatedBucketName, folderName, parent), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.basic", &sinkAfter), | ||
testAccCheckLoggingFolderSink(&sinkAfter, "google_logging_folder_sink.basic"), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
// Destination should have changed, but WriterIdentity should be the same | ||
if sinkBefore.Destination == sinkAfter.Destination { | ||
t.Errorf("Expected Destination to change, but it didn't: Destination = %#v", sinkBefore.Destination) | ||
} | ||
if sinkBefore.WriterIdentity != sinkAfter.WriterIdentity { | ||
t.Errorf("Expected WriterIdentity to be the same, but it differs: before = %#v, after = %#v", | ||
sinkBefore.WriterIdentity, sinkAfter.WriterIdentity) | ||
} | ||
} | ||
|
||
func testAccCheckLoggingFolderSinkDestroy(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "google_logging_folder_sink" { | ||
continue | ||
} | ||
|
||
attributes := rs.Primary.Attributes | ||
|
||
_, err := config.clientLogging.Folders.Sinks.Get(attributes["id"]).Do() | ||
if err == nil { | ||
return fmt.Errorf("folder sink still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckLoggingFolderSinkExists(n string, sink *logging.LogSink) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
attributes, err := getResourceAttributes(n, s) | ||
if err != nil { | ||
return err | ||
} | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
si, err := config.clientLogging.Folders.Sinks.Get(attributes["id"]).Do() | ||
if err != nil { | ||
return err | ||
} | ||
*sink = *si | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckLoggingFolderSink(sink *logging.LogSink, n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
attributes, err := getResourceAttributes(n, s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if sink.Destination != attributes["destination"] { | ||
return fmt.Errorf("mismatch on destination: api has %s but client has %s", sink.Destination, attributes["destination"]) | ||
} | ||
|
||
if sink.Filter != attributes["filter"] { | ||
return fmt.Errorf("mismatch on filter: api has %s but client has %s", sink.Filter, attributes["filter"]) | ||
} | ||
|
||
if sink.WriterIdentity != attributes["writer_identity"] { | ||
return fmt.Errorf("mismatch on writer_identity: api has %s but client has %s", sink.WriterIdentity, attributes["writer_identity"]) | ||
} | ||
|
||
includeChildren := false | ||
if attributes["include_children"] != "" { | ||
includeChildren, err = strconv.ParseBool(attributes["include_children"]) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if sink.IncludeChildren != includeChildren { | ||
return fmt.Errorf("mismatch on include_children: api has %s but client has %s", sink.IncludeChildren, attributes["include_children"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, went with %v (and the correct variable this time) |
||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccLoggingFolderSink_basic(sinkName, bucketName, folderName, folderParent string) string { | ||
return fmt.Sprintf(` | ||
resource "google_logging_folder_sink" "basic" { | ||
name = "%s" | ||
folder = "${element(split("/", google_folder.my-folder.name), 1)}" | ||
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}" | ||
filter = "logName=\"projects/%s/logs/compute.googleapis.com%%2Factivity_log\" AND severity>=ERROR" | ||
include_children = true | ||
} | ||
|
||
resource "google_storage_bucket" "log-bucket" { | ||
name = "%s" | ||
} | ||
|
||
resource "google_folder" "my-folder" { | ||
display_name = "%s" | ||
parent = "%s" | ||
}`, sinkName, getTestProjectFromEnv(), bucketName, folderName, folderParent) | ||
} | ||
|
||
func testAccLoggingFolderSink_withFullFolderPath(sinkName, bucketName, folderName, folderParent string) string { | ||
return fmt.Sprintf(` | ||
resource "google_logging_folder_sink" "basic" { | ||
name = "%s" | ||
folder = "${google_folder.my-folder.name}" | ||
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}" | ||
filter = "logName=\"projects/%s/logs/compute.googleapis.com%%2Factivity_log\" AND severity>=ERROR" | ||
include_children = false | ||
} | ||
|
||
resource "google_storage_bucket" "log-bucket" { | ||
name = "%s" | ||
} | ||
|
||
resource "google_folder" "my-folder" { | ||
display_name = "%s" | ||
parent = "%s" | ||
}`, sinkName, getTestProjectFromEnv(), bucketName, folderName, folderParent) | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
parseFolderId
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done