Skip to content

Commit

Permalink
Add support for logging.googleapis.com/LogBucket to TGC (#10980)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfdossantos authored Jun 19, 2024
1 parent 89adfa7 commit f63d714
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 4 deletions.
6 changes: 5 additions & 1 deletion mmv1/provider/terraform_tgc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ def copy_common_files(output_folder, generate_code, _generate_docs)
['converters/google/resources/appengine_application.go',
'third_party/tgc/appengine_application.go'],
['converters/google/resources/apikeys_key.go',
'third_party/tgc/apikeys_key.go']
'third_party/tgc/apikeys_key.go'],
['converters/google/resources/logging_folder_bucket_config.go',
'third_party/tgc/logging_folder_bucket_config.go'],
['converters/google/resources/logging_organization_bucket_config.go',
'third_party/tgc/logging_organization_bucket_config.go']
])
end

Expand Down
8 changes: 5 additions & 3 deletions mmv1/templates/tgc/resource_converters.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func ResourceConverters() map[string][]cai.ResourceConverter {
"google_spanner_instance_iam_binding": {spanner.ResourceConverterSpannerInstanceIamBinding()},
"google_spanner_instance_iam_member": {spanner.ResourceConverterSpannerInstanceIamMember()},
"google_project_service": {resourceConverterServiceUsage()},
"google_secret_manager_secret_version": {secretmanager.ResourceConverterSecretManagerSecretVersion()},
"google_secret_manager_secret_version": {secretmanager.ResourceConverterSecretManagerSecretVersion()},
"google_pubsub_lite_reservation": {pubsublite.ResourceConverterPubsubLiteReservation()},
"google_pubsub_lite_subscription": {pubsublite.ResourceConverterPubsubLiteSubscription()},
"google_pubsub_lite_subscription": {pubsublite.ResourceConverterPubsubLiteSubscription()},
"google_pubsub_lite_topic": {pubsublite.ResourceConverterPubsubLiteTopic()},
"google_pubsub_schema": {pubsub.ResourceConverterPubsubSchema()},
"google_pubsub_subscription": {pubsub.ResourceConverterPubsubSubscription()},
Expand All @@ -114,7 +114,9 @@ func ResourceConverters() map[string][]cai.ResourceConverter {
"google_storage_bucket_iam_binding": {resourceConverterStorageBucketIamBinding()},
"google_storage_bucket_iam_member": {resourceConverterStorageBucketIamMember()},
"google_compute_node_group": {compute.ResourceConverterComputeNodeGroup()},
"google_cloud_tasks_queue": {cloudtasks.ResourceConverterCloudTasksQueue()},
"google_logging_folder_bucket_config": {resourceConverterLogFolderBucket()},
"google_logging_organization_bucket_config": {resourceConverterLogOrganizationBucket()},
"google_cloud_tasks_queue": {cloudtasks.ResourceConverterCloudTasksQueue()},
"google_pubsub_topic": {pubsub.ResourceConverterPubsubTopic()},
"google_kms_crypto_key": {kms.ResourceConverterKMSCryptoKey()},
"google_kms_key_ring": {kms.ResourceConverterKMSKeyRing()},
Expand Down
179 changes: 179 additions & 0 deletions mmv1/third_party/tgc/logging_folder_bucket_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package google

import (
"reflect"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/cai"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)

const logFolderBucketAssetType string = "logging.googleapis.com/LogBucket"

func resourceConverterLogFolderBucket() cai.ResourceConverter {
return cai.ResourceConverter{
AssetType: logFolderBucketAssetType,
Convert: GetLogFolderBucketCaiObject,
}
}

func GetLogFolderBucketCaiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) ([]cai.Asset, error) {
name, err := cai.AssetName(d, config, "//logging.googleapis.com/projects/{{project}}/locations/{{location}}/buckets/{{bucket_id}}")
if err != nil {
return []cai.Asset{}, err
}
if obj, err := GetLogFolderBucketApiObject(d, config); err == nil {
return []cai.Asset{{
Name: name,
Type: logFolderBucketAssetType,
Resource: &cai.AssetResource{
Version: "v2",
DiscoveryDocumentURI: "https://logging.googleapis.com/$discovery/rest?version=v2",
DiscoveryName: "LogBucket",
Data: obj,
},
}}, nil
} else {
return []cai.Asset{}, err
}
}

func GetLogFolderBucketApiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})

folderProp, err := expandLogFolderBucketFolderId(d.Get("folder"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("folder"); !tpgresource.IsEmptyValue(reflect.ValueOf(folderProp)) && (ok || !reflect.DeepEqual(v, folderProp)) {
obj["id"] = folderProp
}

nameProp, err := expandLogFolderBucketName(d.Get("name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}

bucketIdProp, err := expandLogFolderBucketBucketId(d.Get("bucket_id"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("bucket_id"); !tpgresource.IsEmptyValue(reflect.ValueOf(bucketIdProp)) && (ok || !reflect.DeepEqual(v, bucketIdProp)) {
obj["bucketId"] = bucketIdProp
}

locationProp, err := expandLogFolderBucketLocation(d.Get("location"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("location"); !tpgresource.IsEmptyValue(reflect.ValueOf(locationProp)) && (ok || !reflect.DeepEqual(v, locationProp)) {
obj["location"] = locationProp
}

descriptionProp, err := expandLogFolderBucketDescription(d.Get("description"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}

retentionDaysProp, err := expandLogFolderBucketRetentionDays(d.Get("retention_days"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("retention_days"); !tpgresource.IsEmptyValue(reflect.ValueOf(retentionDaysProp)) && (ok || !reflect.DeepEqual(v, retentionDaysProp)) {
obj["retentionDays"] = retentionDaysProp
}

indexConfigsProp, err := expandLogFolderBucketIndexConfigs(d.Get("index_configs"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("index_configs"); !tpgresource.IsEmptyValue(reflect.ValueOf(indexConfigsProp)) && (ok || !reflect.DeepEqual(v, indexConfigsProp)) {
obj["indexConfigs"] = indexConfigsProp
}

lifecycleStateProp, err := expandLogFolderBucketLifecycleState(d.Get("lifecycle_state"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("lifecycle_state"); !tpgresource.IsEmptyValue(reflect.ValueOf(lifecycleStateProp)) && (ok || !reflect.DeepEqual(v, lifecycleStateProp)) {
obj["lifecycleState"] = lifecycleStateProp
}

return obj, nil
}

func expandLogFolderBucketFolderId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
v, err := tpgresource.ReplaceVars(d, config, "folders/{{folder}}/locations/{{location}}/buckets/{{bucket_id}}")
if err != nil {
return nil, err
}

return v, nil
}

func expandLogFolderBucketName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketLifecycleState(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketIndexConfigs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
v = v.(*schema.Set).List()
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedFieldPath, err := expandLogFolderBucketFieldPath(original["field_path"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedFieldPath); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["fieldPath"] = transformedFieldPath
}

transformedType, err := expandLogFolderBucketType(original["type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedType); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["type"] = transformedType
}

req = append(req, transformed)
}

return req, nil
}

func expandLogFolderBucketType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketFieldPath(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketRetentionDays(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketLocation(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketBucketId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogFolderBucketFolder(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
175 changes: 175 additions & 0 deletions mmv1/third_party/tgc/logging_organization_bucket_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package google

import (
"reflect"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/cai"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)

const logOrganizationBucketAssetType string = "logging.googleapis.com/LogBucket"

func resourceConverterLogOrganizationBucket() cai.ResourceConverter {
return cai.ResourceConverter{
AssetType: logOrganizationBucketAssetType,
Convert: GetLogOrganizationBucketCaiObject,
}
}

func GetLogOrganizationBucketCaiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) ([]cai.Asset, error) {
name, err := cai.AssetName(d, config, "//logging.googleapis.com/projects/{{project}}/locations/{{location}}/buckets/{{bucket_id}}")
if err != nil {
return []cai.Asset{}, err
}
if obj, err := GetLogOrganizationBucketApiObject(d, config); err == nil {
return []cai.Asset{{
Name: name,
Type: logOrganizationBucketAssetType,
Resource: &cai.AssetResource{
Version: "v2",
DiscoveryDocumentURI: "https://logging.googleapis.com/$discovery/rest?version=v2",
DiscoveryName: "LogBucket",
Data: obj,
},
}}, nil
} else {
return []cai.Asset{}, err
}
}

func GetLogOrganizationBucketApiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})

organizationProp, err := expandLogOrganizationBucketOrganizationId(d.Get("organization"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("organization"); !tpgresource.IsEmptyValue(reflect.ValueOf(organizationProp)) && (ok || !reflect.DeepEqual(v, organizationProp)) {
obj["id"] = organizationProp
}

nameProp, err := expandLogOrganizationBucketName(d.Get("name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}

bucketIdProp, err := expandLogOrganizationBucketBucketId(d.Get("bucket_id"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("bucket_id"); !tpgresource.IsEmptyValue(reflect.ValueOf(bucketIdProp)) && (ok || !reflect.DeepEqual(v, bucketIdProp)) {
obj["bucketId"] = bucketIdProp
}

locationProp, err := expandLogOrganizationBucketLocation(d.Get("location"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("location"); !tpgresource.IsEmptyValue(reflect.ValueOf(locationProp)) && (ok || !reflect.DeepEqual(v, locationProp)) {
obj["location"] = locationProp
}

descriptionProp, err := expandLogOrganizationBucketDescription(d.Get("description"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}

retentionDaysProp, err := expandLogOrganizationBucketRetentionDays(d.Get("retention_days"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("retention_days"); !tpgresource.IsEmptyValue(reflect.ValueOf(retentionDaysProp)) && (ok || !reflect.DeepEqual(v, retentionDaysProp)) {
obj["retentionDays"] = retentionDaysProp
}

indexConfigsProp, err := expandLogOrganizationBucketIndexConfigs(d.Get("index_configs"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("index_configs"); !tpgresource.IsEmptyValue(reflect.ValueOf(indexConfigsProp)) && (ok || !reflect.DeepEqual(v, indexConfigsProp)) {
obj["indexConfigs"] = indexConfigsProp
}

lifecycleStateProp, err := expandLogOrganizationBucketLifecycleState(d.Get("lifecycle_state"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("lifecycle_state"); !tpgresource.IsEmptyValue(reflect.ValueOf(lifecycleStateProp)) && (ok || !reflect.DeepEqual(v, lifecycleStateProp)) {
obj["lifecycleState"] = lifecycleStateProp
}

return obj, nil
}

func expandLogOrganizationBucketOrganizationId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
v, err := tpgresource.ReplaceVars(d, config, "organizations/{{organization}}/locations/{{location}}/buckets/{{bucket_id}}")
if err != nil {
return nil, err
}

return v, nil
}

func expandLogOrganizationBucketName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogOrganizationBucketLifecycleState(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogOrganizationBucketIndexConfigs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
v = v.(*schema.Set).List()
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedFieldPath, err := expandLogOrganizationBucketFieldPath(original["field_path"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedFieldPath); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["fieldPath"] = transformedFieldPath
}

transformedType, err := expandLogOrganizationBucketType(original["type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedType); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["type"] = transformedType
}

req = append(req, transformed)
}

return req, nil
}

func expandLogOrganizationBucketType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogOrganizationBucketFieldPath(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogOrganizationBucketRetentionDays(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogOrganizationBucketDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogOrganizationBucketLocation(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandLogOrganizationBucketBucketId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Loading

0 comments on commit f63d714

Please sign in to comment.