Skip to content

Commit a2d8ba1

Browse files
Add Logging Settings datasources. (#9526) (#16658)
[upstream:1ea0673f5cc562fca1d3517ffc998af062d68442] Signed-off-by: Modular Magician <[email protected]>
1 parent 1447331 commit a2d8ba1

11 files changed

+646
-0
lines changed

.changelog/9526.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```release-note:new-datasource
2+
`google_logging_organization_settings`
3+
```
4+
```release-note:new-datasource
5+
`google_logging_folder_settings`
6+
```
7+
```release-note:new-datasource
8+
`google_logging_project_settings`
9+
```

google/provider/provider_mmv1_resources.go

+3
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ var handwrittenDatasources = map[string]*schema.Resource{
209209
"google_folder": resourcemanager.DataSourceGoogleFolder(),
210210
"google_folders": resourcemanager.DataSourceGoogleFolders(),
211211
"google_folder_organization_policy": resourcemanager.DataSourceGoogleFolderOrganizationPolicy(),
212+
"google_logging_folder_settings": logging.DataSourceGoogleLoggingFolderSettings(),
213+
"google_logging_organization_settings": logging.DataSourceGoogleLoggingOrganizationSettings(),
212214
"google_logging_project_cmek_settings": logging.DataSourceGoogleLoggingProjectCmekSettings(),
215+
"google_logging_project_settings": logging.DataSourceGoogleLoggingProjectSettings(),
213216
"google_logging_sink": logging.DataSourceGoogleLoggingSink(),
214217
"google_monitoring_notification_channel": monitoring.DataSourceMonitoringNotificationChannel(),
215218
"google_monitoring_cluster_istio_service": monitoring.DataSourceMonitoringServiceClusterIstio(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package logging
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
10+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
11+
)
12+
13+
func DataSourceGoogleLoggingFolderSettings() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceGoogleLoggingFolderSettingsRead,
16+
Schema: map[string]*schema.Schema{
17+
"folder": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
Description: `The folder for which to retrieve settings.`,
21+
},
22+
"disable_default_sink": {
23+
Type: schema.TypeBool,
24+
Computed: true,
25+
Description: `If set to true, the _Default sink in newly created projects and folders will created in a disabled state. This can be used to automatically disable log storage if there is already an aggregated sink configured in the hierarchy. The _Default sink can be re-enabled manually if needed.`,
26+
},
27+
"kms_key_name": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
Description: `The resource name for the configured Cloud KMS key.
31+
KMS key name format:
32+
"projects/[PROJECT_ID]/locations/[LOCATION]/keyRings/[KEYRING]/cryptoKeys/[KEY]"
33+
To enable CMEK for the bucket, set this field to a valid kmsKeyName for which the associated service account has the required cloudkms.cryptoKeyEncrypterDecrypter roles assigned for the key.
34+
The Cloud KMS key used by the bucket can be updated by changing the kmsKeyName to a new valid key name. Encryption operations that are in progress will be completed with the key that was in use when they started. Decryption operations will be completed using the key that was used at the time of encryption unless access to that key has been revoked.
35+
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`,
36+
},
37+
"storage_location": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
Description: `The storage location that Cloud Logging will use to create new resources when a location is needed but not explicitly provided.`,
41+
},
42+
"kms_service_account_id": {
43+
Type: schema.TypeString,
44+
Computed: true,
45+
Description: `The service account associated with a project for which CMEK will apply.
46+
Before enabling CMEK for a logging bucket, you must first assign the cloudkms.cryptoKeyEncrypterDecrypter role to the service account associated with the project for which CMEK will apply. Use [v2.getCmekSettings](https://cloud.google.com/logging/docs/reference/v2/rest/v2/TopLevel/getCmekSettings#google.logging.v2.ConfigServiceV2.GetCmekSettings) to obtain the service account ID.
47+
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`,
48+
},
49+
"logging_service_account_id": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: `The service account for the given container. Sinks use this service account as their writerIdentity if no custom service account is provided.`,
53+
},
54+
"name": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: `The resource name of the CMEK settings.`,
58+
},
59+
},
60+
}
61+
}
62+
63+
func dataSourceGoogleLoggingFolderSettingsRead(d *schema.ResourceData, meta interface{}) error {
64+
config := meta.(*transport_tpg.Config)
65+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
66+
if err != nil {
67+
return err
68+
}
69+
70+
folder := d.Get("folder").(string)
71+
res, err := config.NewLoggingClient(userAgent).Folders.GetSettings(fmt.Sprintf("folders/%s", folder)).Do()
72+
if err != nil {
73+
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("LoggingFolderSettings %q", d.Id()), d.Id())
74+
}
75+
76+
d.SetId(fmt.Sprintf("folders/%s/settings", folder))
77+
78+
if err := d.Set("folder", folder); err != nil {
79+
return fmt.Errorf("Error reading FolderSettings: %s", err)
80+
}
81+
82+
if err := d.Set("name", res.Name); err != nil {
83+
return fmt.Errorf("Error reading FolderSettings: %s", err)
84+
}
85+
if err := d.Set("disable_default_sink", res.DisableDefaultSink); err != nil {
86+
return fmt.Errorf("Error reading FolderSettings: %s", err)
87+
}
88+
if err := d.Set("kms_key_name", res.KmsKeyName); err != nil {
89+
return fmt.Errorf("Error reading FolderSettings: %s", err)
90+
}
91+
if err := d.Set("storage_location", res.StorageLocation); err != nil {
92+
return fmt.Errorf("Error reading FolderSettings: %s", err)
93+
}
94+
if err := d.Set("kms_service_account_id", res.KmsServiceAccountId); err != nil {
95+
return fmt.Errorf("Error reading FolderSettings: %s", err)
96+
}
97+
if err := d.Set("logging_service_account_id", res.LoggingServiceAccountId); err != nil {
98+
return fmt.Errorf("Error reading FolderSettings: %s", err)
99+
}
100+
101+
return nil
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package logging_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-provider-google/google/acctest"
10+
"github.com/hashicorp/terraform-provider-google/google/envvar"
11+
)
12+
13+
func TestAccLoggingFolderSettings_datasource(t *testing.T) {
14+
t.Parallel()
15+
16+
context := map[string]interface{}{
17+
"folder_name": "tf-test-" + acctest.RandString(t, 10),
18+
"org_id": envvar.GetTestOrgFromEnv(t),
19+
}
20+
resourceName := "data.google_logging_folder_settings.settings"
21+
22+
acctest.VcrTest(t, resource.TestCase{
23+
PreCheck: func() { acctest.AccTestPreCheck(t) },
24+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccLoggingFolderSettings_datasource(context),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttrSet(resourceName, "kms_service_account_id"),
30+
resource.TestCheckResourceAttrSet(resourceName, "logging_service_account_id"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccLoggingFolderSettings_datasource(context map[string]interface{}) string {
38+
return acctest.Nprintf(`
39+
resource "google_folder" "default" {
40+
display_name = "%{folder_name}"
41+
parent = "organizations/%{org_id}"
42+
}
43+
44+
data "google_logging_folder_settings" "settings" {
45+
folder = google_folder.default.folder_id
46+
}
47+
`, context)
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package logging
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
10+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
11+
)
12+
13+
func DataSourceGoogleLoggingOrganizationSettings() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceGoogleLoggingOrganizationSettingsRead,
16+
Schema: map[string]*schema.Schema{
17+
"organization": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
Description: `The organization for which to retrieve settings.`,
21+
},
22+
"disable_default_sink": {
23+
Type: schema.TypeBool,
24+
Computed: true,
25+
Description: `If set to true, the _Default sink in newly created projects and folders will created in a disabled state. This can be used to automatically disable log storage if there is already an aggregated sink configured in the hierarchy. The _Default sink can be re-enabled manually if needed.`,
26+
},
27+
"kms_key_name": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
Description: `The resource name for the configured Cloud KMS key.
31+
KMS key name format:
32+
"projects/[PROJECT_ID]/locations/[LOCATION]/keyRings/[KEYRING]/cryptoKeys/[KEY]"
33+
To enable CMEK for the bucket, set this field to a valid kmsKeyName for which the associated service account has the required cloudkms.cryptoKeyEncrypterDecrypter roles assigned for the key.
34+
The Cloud KMS key used by the bucket can be updated by changing the kmsKeyName to a new valid key name. Encryption operations that are in progress will be completed with the key that was in use when they started. Decryption operations will be completed using the key that was used at the time of encryption unless access to that key has been revoked.
35+
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`,
36+
},
37+
"storage_location": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
Description: `The storage location that Cloud Logging will use to create new resources when a location is needed but not explicitly provided.`,
41+
},
42+
"kms_service_account_id": {
43+
Type: schema.TypeString,
44+
Computed: true,
45+
Description: `The service account associated with a project for which CMEK will apply.
46+
Before enabling CMEK for a logging bucket, you must first assign the cloudkms.cryptoKeyEncrypterDecrypter role to the service account associated with the project for which CMEK will apply. Use [v2.getCmekSettings](https://cloud.google.com/logging/docs/reference/v2/rest/v2/TopLevel/getCmekSettings#google.logging.v2.ConfigServiceV2.GetCmekSettings) to obtain the service account ID.
47+
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`,
48+
},
49+
"logging_service_account_id": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: `The service account for the given container. Sinks use this service account as their writerIdentity if no custom service account is provided.`,
53+
},
54+
"name": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: `The resource name of the CMEK settings.`,
58+
},
59+
},
60+
}
61+
}
62+
63+
func dataSourceGoogleLoggingOrganizationSettingsRead(d *schema.ResourceData, meta interface{}) error {
64+
config := meta.(*transport_tpg.Config)
65+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
66+
if err != nil {
67+
return err
68+
}
69+
70+
organization := d.Get("organization").(string)
71+
res, err := config.NewLoggingClient(userAgent).Organizations.GetSettings(fmt.Sprintf("organizations/%s", organization)).Do()
72+
if err != nil {
73+
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("LoggingOrganizationSettings %q", d.Id()), d.Id())
74+
}
75+
76+
d.SetId(fmt.Sprintf("organizations/%s/settings", organization))
77+
78+
if err := d.Set("organization", organization); err != nil {
79+
return fmt.Errorf("Error reading OrganizationSettings: %s", err)
80+
}
81+
82+
if err := d.Set("name", res.Name); err != nil {
83+
return fmt.Errorf("Error reading OrganizationSettings: %s", err)
84+
}
85+
if err := d.Set("disable_default_sink", res.DisableDefaultSink); err != nil {
86+
return fmt.Errorf("Error reading OrganizationSettings: %s", err)
87+
}
88+
if err := d.Set("kms_key_name", res.KmsKeyName); err != nil {
89+
return fmt.Errorf("Error reading OrganizationSettings: %s", err)
90+
}
91+
if err := d.Set("storage_location", res.StorageLocation); err != nil {
92+
return fmt.Errorf("Error reading OrganizationSettings: %s", err)
93+
}
94+
if err := d.Set("kms_service_account_id", res.KmsServiceAccountId); err != nil {
95+
return fmt.Errorf("Error reading OrganizationSettings: %s", err)
96+
}
97+
if err := d.Set("logging_service_account_id", res.LoggingServiceAccountId); err != nil {
98+
return fmt.Errorf("Error reading OrganizationSettings: %s", err)
99+
}
100+
101+
return nil
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package logging_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-provider-google/google/acctest"
10+
"github.com/hashicorp/terraform-provider-google/google/envvar"
11+
)
12+
13+
func TestAccLoggingOrganizationSettings_datasource(t *testing.T) {
14+
t.Parallel()
15+
16+
context := map[string]interface{}{
17+
"org_id": envvar.GetTestOrgFromEnv(t),
18+
}
19+
resourceName := "data.google_logging_organization_settings.settings"
20+
21+
acctest.VcrTest(t, resource.TestCase{
22+
PreCheck: func() { acctest.AccTestPreCheck(t) },
23+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccLoggingOrganizationSettings_datasource(context),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttrSet(resourceName, "kms_service_account_id"),
29+
resource.TestCheckResourceAttrSet(resourceName, "logging_service_account_id"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccLoggingOrganizationSettings_datasource(context map[string]interface{}) string {
37+
return acctest.Nprintf(`
38+
data "google_logging_organization_settings" "settings" {
39+
organization = "%{org_id}"
40+
}
41+
`, context)
42+
}

0 commit comments

Comments
 (0)