Skip to content

Commit 497e9d8

Browse files
Add Logging Sink data source. (#7250) (#5207)
Signed-off-by: Modular Magician <[email protected]>
1 parent 5bacdfa commit 497e9d8

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

.changelog/7250.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_logging_sink`
3+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func dataSourceGoogleLoggingSink() *schema.Resource {
10+
dsSchema := datasourceSchemaFromResourceSchema(resourceLoggingSinkSchema())
11+
dsSchema["id"] = &schema.Schema{
12+
Type: schema.TypeString,
13+
Required: true,
14+
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]"`,
15+
}
16+
17+
return &schema.Resource{
18+
Read: dataSourceGoogleLoggingSinkRead,
19+
Schema: dsSchema,
20+
}
21+
}
22+
23+
func dataSourceGoogleLoggingSinkRead(d *schema.ResourceData, meta interface{}) error {
24+
config := meta.(*Config)
25+
userAgent, err := generateUserAgentString(d, config.userAgent)
26+
if err != nil {
27+
return err
28+
}
29+
30+
sinkId := d.Get("id").(string)
31+
32+
sink, err := config.NewLoggingClient(userAgent).Sinks.Get(sinkId).Do()
33+
if err != nil {
34+
return handleNotFoundError(err, d, fmt.Sprintf("Logging Sink %s", d.Id()))
35+
}
36+
37+
if err := flattenResourceLoggingSink(d, sink); err != nil {
38+
return err
39+
}
40+
41+
d.SetId(sinkId)
42+
43+
return nil
44+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceGoogleLoggingSink_basic(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"project_name": getTestProjectFromEnv(),
14+
"sink_name": "tf-test-sink-ds-" + randString(t, 10),
15+
"bucket_name": "tf-test-sink-ds-bucket-" + randString(t, 10),
16+
}
17+
18+
vcrTest(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccDataSourceGoogleLoggingSink_basic(context),
24+
Check: resource.ComposeTestCheckFunc(
25+
checkDataSourceStateMatchesResourceStateWithIgnores(
26+
"data.google_logging_sink.basic",
27+
"google_logging_project_sink.basic",
28+
map[string]struct{}{
29+
"project": {},
30+
"unique_writer_identity": {},
31+
},
32+
),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccDataSourceGoogleLoggingSink_basic(context map[string]interface{}) string {
40+
return Nprintf(`
41+
resource "google_logging_project_sink" "basic" {
42+
name = "%{sink_name}"
43+
project = "%{project_name}"
44+
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
45+
filter = "logName=\"projects/%{project_name}/logs/compute.googleapis.com%%2Factivity_log\" AND severity>=ERROR"
46+
47+
unique_writer_identity = false
48+
}
49+
50+
resource "google_storage_bucket" "log-bucket" {
51+
name = "%{bucket_name}"
52+
location = "US"
53+
}
54+
55+
data "google_logging_sink" "basic" {
56+
id = google_logging_project_sink.basic.id
57+
}
58+
`, context)
59+
}

google-beta/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ func Provider() *schema.Provider {
722722
"google_folders": dataSourceGoogleFolders(),
723723
"google_folder_organization_policy": dataSourceGoogleFolderOrganizationPolicy(),
724724
"google_logging_project_cmek_settings": dataSourceGoogleLoggingProjectCmekSettings(),
725+
"google_logging_sink": dataSourceGoogleLoggingSink(),
725726
"google_monitoring_notification_channel": dataSourceMonitoringNotificationChannel(),
726727
"google_monitoring_cluster_istio_service": dataSourceMonitoringServiceClusterIstio(),
727728
"google_monitoring_istio_canonical_service": dataSourceMonitoringIstioCanonicalService(),
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
subcategory: "Cloud (Stackdriver) Logging"
3+
description: |-
4+
Get information about a Google Cloud Logging Sink.
5+
---
6+
7+
# google\_logging\_sink
8+
9+
Use this data source to get a project, folder, organization or billing account logging sink details.
10+
To get more information about Service, see:
11+
12+
[API documentation](https://cloud.google.com/logging/docs/reference/v2/rest/v2/sinks)
13+
14+
## Example Usage - Retrieve Project Logging Sink Basic
15+
16+
17+
```hcl
18+
data google_logging_sink "project-sink" {
19+
id = "projects/0123456789/sinks/my-sink-name"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
28+
29+
- - -
30+
31+
* `id` - (Required) The identifier for the resource.
32+
Examples:
33+
34+
- `projects/[PROJECT_ID]/sinks/[SINK_NAME]`
35+
- `organizations/[ORGANIZATION_ID]/sinks/[SINK_NAME]`
36+
- `billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_NAME]`
37+
- `folders/[FOLDER_ID]/sinks/[SINK_NAME]`
38+
39+
40+
## Attributes Reference
41+
42+
In addition to the arguments listed above, the following computed attributes are exported:
43+
44+
45+
* `name` - The name of the logging sink.
46+
47+
* `destination` - The destination of the sink (or, in other words, where logs are written to).
48+
49+
* `filter` - The filter which is applied when exporting logs. Only log entries that match the filter are exported.
50+
51+
* `description` - The description of this sink.
52+
53+
* `disabled` - Whether this sink is disabled and it does not export any log entries.
54+
55+
* `writer_identity` - The identity associated with this sink. This identity must be granted write access to the configured `destination`.
56+
57+
* `bigquery_options` - Options that affect sinks exporting data to BigQuery. Structure is [documented below](#nested_bigquery_options).
58+
59+
* `exclusions` - Log entries that match any of the exclusion filters are not exported. Structure is [documented below](#nested_exclusions).
60+
61+
<a name="nested_bigquery_options"></a>The `bigquery_options` block supports:
62+
63+
* `use_partitioned_tables` - Whether [BigQuery's partition tables](https://cloud.google.com/bigquery/docs/partitioned-tables) are used.
64+
65+
<a name="nested_exclusions"></a>The `exclusions` block supports:
66+
67+
* `name` - A client-assigned identifier, such as `load-balancer-exclusion`.
68+
* `description` - A description of this exclusion.
69+
* `filter` - An advanced logs filter that matches the log entries to be excluded.
70+
* `disabled` - Whether this exclusion is disabled and it does not exclude any log entries.

0 commit comments

Comments
 (0)