forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Logging Sink data source. (GoogleCloudPlatform#7250)
- Loading branch information
1 parent
4d253c0
commit 65071e7
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
mmv1/third_party/terraform/data_sources/data_source_google_logging_sink.go
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,44 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceGoogleLoggingSink() *schema.Resource { | ||
dsSchema := datasourceSchemaFromResourceSchema(resourceLoggingSinkSchema()) | ||
dsSchema["id"] = &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Required. An identifier for the resource in format: "projects/[PROJECT_ID]/sinks/[SINK_NAME]", "organizations/[ORGANIZATION_ID]/sinks/[SINK_NAME]", "billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_NAME]", "folders/[FOLDER_ID]/sinks/[SINK_NAME]"`, | ||
} | ||
|
||
return &schema.Resource{ | ||
Read: dataSourceGoogleLoggingSinkRead, | ||
Schema: dsSchema, | ||
} | ||
} | ||
|
||
func dataSourceGoogleLoggingSinkRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
userAgent, err := generateUserAgentString(d, config.userAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sinkId := d.Get("id").(string) | ||
|
||
sink, err := config.NewLoggingClient(userAgent).Sinks.Get(sinkId).Do() | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Logging Sink %s", d.Id())) | ||
} | ||
|
||
if err := flattenResourceLoggingSink(d, sink); err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(sinkId) | ||
|
||
return nil | ||
} |
59 changes: 59 additions & 0 deletions
59
mmv1/third_party/terraform/tests/data_source_google_logging_sink_test.go
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,59 @@ | ||
package google | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceGoogleLoggingSink_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"project_name": getTestProjectFromEnv(), | ||
"sink_name": "tf-test-sink-ds-" + randString(t, 10), | ||
"bucket_name": "tf-test-sink-ds-bucket-" + randString(t, 10), | ||
} | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceGoogleLoggingSink_basic(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
checkDataSourceStateMatchesResourceStateWithIgnores( | ||
"data.google_logging_sink.basic", | ||
"google_logging_project_sink.basic", | ||
map[string]struct{}{ | ||
"project": {}, | ||
"unique_writer_identity": {}, | ||
}, | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleLoggingSink_basic(context map[string]interface{}) string { | ||
return Nprintf(` | ||
resource "google_logging_project_sink" "basic" { | ||
name = "%{sink_name}" | ||
project = "%{project_name}" | ||
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}" | ||
filter = "logName=\"projects/%{project_name}/logs/compute.googleapis.com%%2Factivity_log\" AND severity>=ERROR" | ||
unique_writer_identity = false | ||
} | ||
resource "google_storage_bucket" "log-bucket" { | ||
name = "%{bucket_name}" | ||
location = "US" | ||
} | ||
data "google_logging_sink" "basic" { | ||
id = google_logging_project_sink.basic.id | ||
} | ||
`, context) | ||
} |
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
70 changes: 70 additions & 0 deletions
70
mmv1/third_party/terraform/website/docs/d/logging_sink.html.markdown
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,70 @@ | ||
--- | ||
subcategory: "Cloud (Stackdriver) Logging" | ||
description: |- | ||
Get information about a Google Cloud Logging Sink. | ||
--- | ||
|
||
# google\_logging\_sink | ||
|
||
Use this data source to get a project, folder, organization or billing account logging sink details. | ||
To get more information about Service, see: | ||
|
||
[API documentation](https://cloud.google.com/logging/docs/reference/v2/rest/v2/sinks) | ||
|
||
## Example Usage - Retrieve Project Logging Sink Basic | ||
|
||
|
||
```hcl | ||
data google_logging_sink "project-sink" { | ||
id = "projects/0123456789/sinks/my-sink-name" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
|
||
|
||
- - - | ||
|
||
* `id` - (Required) The identifier for the resource. | ||
Examples: | ||
|
||
- `projects/[PROJECT_ID]/sinks/[SINK_NAME]` | ||
- `organizations/[ORGANIZATION_ID]/sinks/[SINK_NAME]` | ||
- `billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_NAME]` | ||
- `folders/[FOLDER_ID]/sinks/[SINK_NAME]` | ||
|
||
|
||
## Attributes Reference | ||
|
||
In addition to the arguments listed above, the following computed attributes are exported: | ||
|
||
|
||
* `name` - The name of the logging sink. | ||
|
||
* `destination` - The destination of the sink (or, in other words, where logs are written to). | ||
|
||
* `filter` - The filter which is applied when exporting logs. Only log entries that match the filter are exported. | ||
|
||
* `description` - The description of this sink. | ||
|
||
* `disabled` - Whether this sink is disabled and it does not export any log entries. | ||
|
||
* `writer_identity` - The identity associated with this sink. This identity must be granted write access to the configured `destination`. | ||
|
||
* `bigquery_options` - Options that affect sinks exporting data to BigQuery. Structure is [documented below](#nested_bigquery_options). | ||
|
||
* `exclusions` - Log entries that match any of the exclusion filters are not exported. Structure is [documented below](#nested_exclusions). | ||
|
||
<a name="nested_bigquery_options"></a>The `bigquery_options` block supports: | ||
|
||
* `use_partitioned_tables` - Whether [BigQuery's partition tables](https://cloud.google.com/bigquery/docs/partitioned-tables) are used. | ||
|
||
<a name="nested_exclusions"></a>The `exclusions` block supports: | ||
|
||
* `name` - A client-assigned identifier, such as `load-balancer-exclusion`. | ||
* `description` - A description of this exclusion. | ||
* `filter` - An advanced logs filter that matches the log entries to be excluded. | ||
* `disabled` - Whether this exclusion is disabled and it does not exclude any log entries. |