-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add folder and organization setting resources * Try GetTestOrgTargetFromEnv instead of GetTestOrgFromEnv. * Use correct organization for testing. * Remove key rotation to fix VCR test. * Don't specify fields matching default values. * Specify true instead of yes. * Use BootstrapKMSKeyInLocation instead of creating new keys. * Add missing quotes * Add additional examples to generate additional tests. * Remove unneeded resources from examples. * Simplify tests to be one full resource creation and one update. * Fix typo in test * Document and cleanup example. [upstream:5e89b3b459087978b89d3405bd4821d9af1436f3] Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
311cf8b
commit af09665
Showing
9 changed files
with
1,187 additions
and
2 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,6 @@ | ||
```release-note:new-resource | ||
`google_logging_folder_settings` | ||
``` | ||
```release-note:new-resource | ||
`google_logging_organization_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
336 changes: 336 additions & 0 deletions
336
google/services/logging/resource_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,336 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** Type: MMv1 *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// This file is automatically generated by Magic Modules and manual | ||
// changes will be clobbered when the file is regenerated. | ||
// | ||
// Please read more about how to change this file in | ||
// .github/CONTRIBUTING.md. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/hashicorp/terraform-provider-google/google/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" | ||
) | ||
|
||
func ResourceLoggingFolderSettings() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceLoggingFolderSettingsCreate, | ||
Read: resourceLoggingFolderSettingsRead, | ||
Update: resourceLoggingFolderSettingsUpdate, | ||
Delete: resourceLoggingFolderSettingsDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: resourceLoggingFolderSettingsImport, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(20 * time.Minute), | ||
Update: schema.DefaultTimeout(20 * time.Minute), | ||
Delete: schema.DefaultTimeout(20 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"folder": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: `The folder for which to retrieve settings.`, | ||
}, | ||
"disable_default_sink": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Optional: 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, | ||
Optional: true, | ||
Description: `The resource name for the configured Cloud KMS key.`, | ||
}, | ||
"storage_location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: 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 that will be used by the Log Router to access your Cloud KMS key.`, | ||
}, | ||
"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 settings.`, | ||
}, | ||
}, | ||
UseJSONNumber: true, | ||
} | ||
} | ||
|
||
func resourceLoggingFolderSettingsCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
obj := make(map[string]interface{}) | ||
kmsKeyNameProp, err := expandLoggingFolderSettingsKmsKeyName(d.Get("kms_key_name"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("kms_key_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(kmsKeyNameProp)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) { | ||
obj["kmsKeyName"] = kmsKeyNameProp | ||
} | ||
storageLocationProp, err := expandLoggingFolderSettingsStorageLocation(d.Get("storage_location"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("storage_location"); !tpgresource.IsEmptyValue(reflect.ValueOf(storageLocationProp)) && (ok || !reflect.DeepEqual(v, storageLocationProp)) { | ||
obj["storageLocation"] = storageLocationProp | ||
} | ||
disableDefaultSinkProp, err := expandLoggingFolderSettingsDisableDefaultSink(d.Get("disable_default_sink"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("disable_default_sink"); !tpgresource.IsEmptyValue(reflect.ValueOf(disableDefaultSinkProp)) && (ok || !reflect.DeepEqual(v, disableDefaultSinkProp)) { | ||
obj["disableDefaultSink"] = disableDefaultSinkProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}folders/{{folder}}/settings?updateMask=disableDefaultSink,storageLocation,kmsKeyName") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Creating new FolderSettings: %#v", obj) | ||
billingProject := "" | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "PATCH", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutCreate), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error creating FolderSettings: %s", err) | ||
} | ||
if err := d.Set("name", flattenLoggingFolderSettingsName(res["name"], d, config)); err != nil { | ||
return fmt.Errorf(`Error setting computed identity field "name": %s`, err) | ||
} | ||
|
||
// Store the ID now | ||
id, err := tpgresource.ReplaceVars(d, config, "folders/{{folder}}/settings") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
log.Printf("[DEBUG] Finished creating FolderSettings %q: %#v", d.Id(), res) | ||
|
||
return resourceLoggingFolderSettingsRead(d, meta) | ||
} | ||
|
||
func resourceLoggingFolderSettingsRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}folders/{{folder}}/settings") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "GET", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
}) | ||
if err != nil { | ||
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("LoggingFolderSettings %q", d.Id())) | ||
} | ||
|
||
if err := d.Set("name", flattenLoggingFolderSettingsName(res["name"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("kms_key_name", flattenLoggingFolderSettingsKmsKeyName(res["kmsKeyName"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("kms_service_account_id", flattenLoggingFolderSettingsKmsServiceAccountId(res["kmsServiceAccountId"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("storage_location", flattenLoggingFolderSettingsStorageLocation(res["storageLocation"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("disable_default_sink", flattenLoggingFolderSettingsDisableDefaultSink(res["disableDefaultSink"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
if err := d.Set("logging_service_account_id", flattenLoggingFolderSettingsLoggingServiceAccountId(res["loggingServiceAccountId"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading FolderSettings: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceLoggingFolderSettingsUpdate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
obj := make(map[string]interface{}) | ||
kmsKeyNameProp, err := expandLoggingFolderSettingsKmsKeyName(d.Get("kms_key_name"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("kms_key_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) { | ||
obj["kmsKeyName"] = kmsKeyNameProp | ||
} | ||
storageLocationProp, err := expandLoggingFolderSettingsStorageLocation(d.Get("storage_location"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("storage_location"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, storageLocationProp)) { | ||
obj["storageLocation"] = storageLocationProp | ||
} | ||
disableDefaultSinkProp, err := expandLoggingFolderSettingsDisableDefaultSink(d.Get("disable_default_sink"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("disable_default_sink"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, disableDefaultSinkProp)) { | ||
obj["disableDefaultSink"] = disableDefaultSinkProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{LoggingBasePath}}folders/{{folder}}/settings?updateMask=disableDefaultSink,storageLocation,kmsKeyName") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Updating FolderSettings %q: %#v", d.Id(), obj) | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "PATCH", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error updating FolderSettings %q: %s", d.Id(), err) | ||
} else { | ||
log.Printf("[DEBUG] Finished updating FolderSettings %q: %#v", d.Id(), res) | ||
} | ||
|
||
return resourceLoggingFolderSettingsRead(d, meta) | ||
} | ||
|
||
func resourceLoggingFolderSettingsDelete(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[WARNING] Logging FolderSettings resources"+ | ||
" cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+ | ||
" state, but will still be present on Google Cloud.", d.Id()) | ||
d.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
func resourceLoggingFolderSettingsImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
config := meta.(*transport_tpg.Config) | ||
if err := tpgresource.ParseImportId([]string{ | ||
"^folders/(?P<folder>[^/]+)/settings$", | ||
"^(?P<folder>[^/]+)$", | ||
}, d, config); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Replace import id for the resource id | ||
id, err := tpgresource.ReplaceVars(d, config, "folders/{{folder}}/settings") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func flattenLoggingFolderSettingsName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenLoggingFolderSettingsKmsKeyName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenLoggingFolderSettingsKmsServiceAccountId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenLoggingFolderSettingsStorageLocation(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenLoggingFolderSettingsDisableDefaultSink(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenLoggingFolderSettingsLoggingServiceAccountId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func expandLoggingFolderSettingsKmsKeyName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandLoggingFolderSettingsStorageLocation(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandLoggingFolderSettingsDisableDefaultSink(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
return v, nil | ||
} |
Oops, something went wrong.