forked from hashicorp/terraform-provider-google-beta
-
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 Settings datasources. (#9526) (hashicorp#6699)
[upstream:1ea0673f5cc562fca1d3517ffc998af062d68442] Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
807d501
commit 5a2b54c
Showing
11 changed files
with
646 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
```release-note:new-datasource | ||
`google_logging_organization_settings` | ||
``` | ||
```release-note:new-datasource | ||
`google_logging_folder_settings` | ||
``` | ||
```release-note:new-datasource | ||
`google_logging_project_settings` | ||
``` |
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
102 changes: 102 additions & 0 deletions
102
google-beta/services/logging/data_source_google_logging_folder_settings.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,102 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" | ||
) | ||
|
||
func DataSourceGoogleLoggingFolderSettings() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleLoggingFolderSettingsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"folder": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `The folder for which to retrieve settings.`, | ||
}, | ||
"disable_default_sink": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
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.`, | ||
}, | ||
"kms_key_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The resource name for the configured Cloud KMS key. | ||
KMS key name format: | ||
"projects/[PROJECT_ID]/locations/[LOCATION]/keyRings/[KEYRING]/cryptoKeys/[KEY]" | ||
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. | ||
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. | ||
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`, | ||
}, | ||
"storage_location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The storage location that Cloud Logging will use to create new resources when a location is needed but not explicitly provided.`, | ||
}, | ||
"kms_service_account_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The service account associated with a project for which CMEK will apply. | ||
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. | ||
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`, | ||
}, | ||
"logging_service_account_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The service account for the given container. Sinks use this service account as their writerIdentity if no custom service account is provided.`, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The resource name of the CMEK settings.`, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleLoggingFolderSettingsRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
folder := d.Get("folder").(string) | ||
res, err := config.NewLoggingClient(userAgent).Folders.GetSettings(fmt.Sprintf("folders/%s", folder)).Do() | ||
if err != nil { | ||
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("LoggingFolderSettings %q", d.Id()), d.Id()) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("folders/%s/settings", folder)) | ||
|
||
if err := d.Set("folder", folder); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
|
||
if err := d.Set("name", res.Name); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("disable_default_sink", res.DisableDefaultSink); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("kms_key_name", res.KmsKeyName); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("storage_location", res.StorageLocation); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("kms_service_account_id", res.KmsServiceAccountId); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("logging_service_account_id", res.LoggingServiceAccountId); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
|
||
return nil | ||
} |
48 changes: 48 additions & 0 deletions
48
google-beta/services/logging/data_source_google_logging_folder_settings_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,48 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package logging_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" | ||
) | ||
|
||
func TestAccLoggingFolderSettings_datasource(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"folder_name": "tf-test-" + acctest.RandString(t, 10), | ||
"org_id": envvar.GetTestOrgFromEnv(t), | ||
} | ||
resourceName := "data.google_logging_folder_settings.settings" | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLoggingFolderSettings_datasource(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet(resourceName, "kms_service_account_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "logging_service_account_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccLoggingFolderSettings_datasource(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
resource "google_folder" "default" { | ||
display_name = "%{folder_name}" | ||
parent = "organizations/%{org_id}" | ||
} | ||
data "google_logging_folder_settings" "settings" { | ||
folder = google_folder.default.folder_id | ||
} | ||
`, context) | ||
} |
102 changes: 102 additions & 0 deletions
102
google-beta/services/logging/data_source_google_logging_organization_settings.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,102 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" | ||
) | ||
|
||
func DataSourceGoogleLoggingOrganizationSettings() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleLoggingOrganizationSettingsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"organization": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `The organization for which to retrieve settings.`, | ||
}, | ||
"disable_default_sink": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
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.`, | ||
}, | ||
"kms_key_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The resource name for the configured Cloud KMS key. | ||
KMS key name format: | ||
"projects/[PROJECT_ID]/locations/[LOCATION]/keyRings/[KEYRING]/cryptoKeys/[KEY]" | ||
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. | ||
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. | ||
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`, | ||
}, | ||
"storage_location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The storage location that Cloud Logging will use to create new resources when a location is needed but not explicitly provided.`, | ||
}, | ||
"kms_service_account_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The service account associated with a project for which CMEK will apply. | ||
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. | ||
See [Enabling CMEK for Logging Buckets](https://cloud.google.com/logging/docs/routing/managed-encryption-storage) for more information.`, | ||
}, | ||
"logging_service_account_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The service account for the given container. Sinks use this service account as their writerIdentity if no custom service account is provided.`, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The resource name of the CMEK settings.`, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleLoggingOrganizationSettingsRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
organization := d.Get("organization").(string) | ||
res, err := config.NewLoggingClient(userAgent).Organizations.GetSettings(fmt.Sprintf("organizations/%s", organization)).Do() | ||
if err != nil { | ||
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("LoggingOrganizationSettings %q", d.Id()), d.Id()) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("organizations/%s/settings", organization)) | ||
|
||
if err := d.Set("organization", organization); err != nil { | ||
return fmt.Errorf("Error reading OrganizationSettings: %s", err) | ||
} | ||
|
||
if err := d.Set("name", res.Name); err != nil { | ||
return fmt.Errorf("Error reading OrganizationSettings: %s", err) | ||
} | ||
if err := d.Set("disable_default_sink", res.DisableDefaultSink); err != nil { | ||
return fmt.Errorf("Error reading OrganizationSettings: %s", err) | ||
} | ||
if err := d.Set("kms_key_name", res.KmsKeyName); err != nil { | ||
return fmt.Errorf("Error reading OrganizationSettings: %s", err) | ||
} | ||
if err := d.Set("storage_location", res.StorageLocation); err != nil { | ||
return fmt.Errorf("Error reading OrganizationSettings: %s", err) | ||
} | ||
if err := d.Set("kms_service_account_id", res.KmsServiceAccountId); err != nil { | ||
return fmt.Errorf("Error reading OrganizationSettings: %s", err) | ||
} | ||
if err := d.Set("logging_service_account_id", res.LoggingServiceAccountId); err != nil { | ||
return fmt.Errorf("Error reading OrganizationSettings: %s", err) | ||
} | ||
|
||
return nil | ||
} |
42 changes: 42 additions & 0 deletions
42
google-beta/services/logging/data_source_google_logging_organization_settings_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,42 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package logging_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" | ||
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" | ||
) | ||
|
||
func TestAccLoggingOrganizationSettings_datasource(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"org_id": envvar.GetTestOrgFromEnv(t), | ||
} | ||
resourceName := "data.google_logging_organization_settings.settings" | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLoggingOrganizationSettings_datasource(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet(resourceName, "kms_service_account_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "logging_service_account_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccLoggingOrganizationSettings_datasource(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
data "google_logging_organization_settings" "settings" { | ||
organization = "%{org_id}" | ||
} | ||
`, context) | ||
} |
Oops, something went wrong.