Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added conditions to Cloud Asset Feeds #7632

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changelog/4023.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```release-note:enhancement
cloud_asset: Added `condition` to `google_cloud_asset_organization_feed`
cloud_asset: Added `condition` to `google_cloud_asset_folder_feed`
cloud_asset: Added `condition` to `google_cloud_asset_project_feed`
```
147 changes: 147 additions & 0 deletions google/resource_cloud_asset_folder_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,43 @@ supported asset types.`,
Type: schema.TypeString,
},
},
"condition": {
Type: schema.TypeList,
Optional: true,
Description: `A condition which determines whether an asset update should be published. If specified, an asset
will be returned only when the expression evaluates to true. When set, expression field
must be a valid CEL expression on a TemporalAsset with name temporal_asset. Example: a Feed with
expression "temporal_asset.deleted == true" will only publish Asset deletions. Other fields of
condition are optional.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"expression": {
Type: schema.TypeString,
Required: true,
Description: `Textual representation of an expression in Common Expression Language syntax.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: `Description of the expression. This is a longer text which describes the expression,
e.g. when hovered over it in a UI.`,
},
"location": {
Type: schema.TypeString,
Optional: true,
Description: `String indicating the location of the expression for error reporting, e.g. a file
name and a position in the file.`,
},
"title": {
Type: schema.TypeString,
Optional: true,
Description: `Title for the expression, i.e. a short string describing its purpose.
This can be used e.g. in UIs which allow to enter the expression.`,
},
},
},
},
"content_type": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -165,6 +202,12 @@ func resourceCloudAssetFolderFeedCreate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("feed_output_config"); !isEmptyValue(reflect.ValueOf(feedOutputConfigProp)) && (ok || !reflect.DeepEqual(v, feedOutputConfigProp)) {
obj["feedOutputConfig"] = feedOutputConfigProp
}
conditionProp, err := expandCloudAssetFolderFeedCondition(d.Get("condition"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("condition"); !isEmptyValue(reflect.ValueOf(conditionProp)) && (ok || !reflect.DeepEqual(v, conditionProp)) {
obj["condition"] = conditionProp
}

obj, err = resourceCloudAssetFolderFeedEncoder(d, meta, obj)
if err != nil {
Expand Down Expand Up @@ -260,6 +303,9 @@ func resourceCloudAssetFolderFeedRead(d *schema.ResourceData, meta interface{})
if err := d.Set("feed_output_config", flattenCloudAssetFolderFeedFeedOutputConfig(res["feedOutputConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading FolderFeed: %s", err)
}
if err := d.Set("condition", flattenCloudAssetFolderFeedCondition(res["condition"], d, config)); err != nil {
return fmt.Errorf("Error reading FolderFeed: %s", err)
}

return nil
}
Expand Down Expand Up @@ -298,6 +344,12 @@ func resourceCloudAssetFolderFeedUpdate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("feed_output_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, feedOutputConfigProp)) {
obj["feedOutputConfig"] = feedOutputConfigProp
}
conditionProp, err := expandCloudAssetFolderFeedCondition(d.Get("condition"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("condition"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, conditionProp)) {
obj["condition"] = conditionProp
}

obj, err = resourceCloudAssetFolderFeedEncoder(d, meta, obj)
if err != nil {
Expand Down Expand Up @@ -327,6 +379,10 @@ func resourceCloudAssetFolderFeedUpdate(d *schema.ResourceData, meta interface{}
if d.HasChange("feed_output_config") {
updateMask = append(updateMask, "feedOutputConfig")
}

if d.HasChange("condition") {
updateMask = append(updateMask, "condition")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -446,6 +502,41 @@ func flattenCloudAssetFolderFeedFeedOutputConfigPubsubDestinationTopic(v interfa
return v
}

func flattenCloudAssetFolderFeedCondition(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["expression"] =
flattenCloudAssetFolderFeedConditionExpression(original["expression"], d, config)
transformed["title"] =
flattenCloudAssetFolderFeedConditionTitle(original["title"], d, config)
transformed["description"] =
flattenCloudAssetFolderFeedConditionDescription(original["description"], d, config)
transformed["location"] =
flattenCloudAssetFolderFeedConditionLocation(original["location"], d, config)
return []interface{}{transformed}
}
func flattenCloudAssetFolderFeedConditionExpression(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudAssetFolderFeedConditionTitle(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudAssetFolderFeedConditionDescription(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudAssetFolderFeedConditionLocation(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandCloudAssetFolderFeedAssetNames(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -500,6 +591,62 @@ func expandCloudAssetFolderFeedFeedOutputConfigPubsubDestinationTopic(v interfac
return v, nil
}

func expandCloudAssetFolderFeedCondition(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedExpression, err := expandCloudAssetFolderFeedConditionExpression(original["expression"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedExpression); val.IsValid() && !isEmptyValue(val) {
transformed["expression"] = transformedExpression
}

transformedTitle, err := expandCloudAssetFolderFeedConditionTitle(original["title"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedTitle); val.IsValid() && !isEmptyValue(val) {
transformed["title"] = transformedTitle
}

transformedDescription, err := expandCloudAssetFolderFeedConditionDescription(original["description"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedDescription); val.IsValid() && !isEmptyValue(val) {
transformed["description"] = transformedDescription
}

transformedLocation, err := expandCloudAssetFolderFeedConditionLocation(original["location"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedLocation); val.IsValid() && !isEmptyValue(val) {
transformed["location"] = transformedLocation
}

return transformed, nil
}

func expandCloudAssetFolderFeedConditionExpression(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudAssetFolderFeedConditionTitle(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudAssetFolderFeedConditionDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudAssetFolderFeedConditionLocation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func resourceCloudAssetFolderFeedEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
// Remove the "folders/" prefix from the folder ID
if folder, ok := d.GetOkExists("folder"); ok {
Expand Down
9 changes: 9 additions & 0 deletions google/resource_cloud_asset_folder_feed_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ resource "google_cloud_asset_folder_feed" "folder_feed" {
}
}

condition {
expression = <<-EOT
!temporal_asset.deleted &&
temporal_asset.prior_asset_state == google.cloud.asset.v1.TemporalAsset.PriorAssetState.DOES_NOT_EXIST
EOT
title = "created"
description = "Send notifications on creation events"
}

# Wait for the permission to be ready on the destination topic.
depends_on = [
google_pubsub_topic_iam_member.cloud_asset_writer,
Expand Down
Loading