Skip to content

Commit

Permalink
Move expr to GA (#3028) (#5532)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 29, 2020
1 parent a4a451f commit 12a8ddb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/3028.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: `google_compute_security_policy` `rule.match.expr` field is now GA
```
3 changes: 0 additions & 3 deletions .changelog/3034.txt

This file was deleted.

60 changes: 60 additions & 0 deletions google/resource_compute_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ func resourceComputeSecurityPolicy() *schema.Resource {
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"SRC_IPS_V1"}, false),
},

"expr": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"expression": {
Type: schema.TypeString,
Required: true,
},
// These fields are not yet supported (Issue terraform-providers/terraform-provider-google#4497: mbang)
// "title": {
// Type: schema.TypeString,
// Optional: true,
// },
// "description": {
// Type: schema.TypeString,
// Optional: true,
// },
// "location": {
// Type: schema.TypeString,
// Optional: true,
// },
},
},
},
},
},
},
Expand Down Expand Up @@ -330,6 +357,7 @@ func expandSecurityPolicyMatch(configured []interface{}) *compute.SecurityPolicy
return &compute.SecurityPolicyRuleMatcher{
VersionedExpr: data["versioned_expr"].(string),
Config: expandSecurityPolicyMatchConfig(data["config"].([]interface{})),
Expr: expandSecurityPolicyMatchExpr(data["expr"].([]interface{})),
}
}

Expand All @@ -344,6 +372,21 @@ func expandSecurityPolicyMatchConfig(configured []interface{}) *compute.Security
}
}

func expandSecurityPolicyMatchExpr(expr []interface{}) *compute.Expr {
if len(expr) == 0 || expr[0] == nil {
return nil
}

data := expr[0].(map[string]interface{})
return &compute.Expr{
Expression: data["expression"].(string),
// These fields are not yet supported (Issue terraform-providers/terraform-provider-google#4497: mbang)
// Title: data["title"].(string),
// Description: data["description"].(string),
// Location: data["location"].(string),
}
}

func flattenSecurityPolicyRules(rules []*compute.SecurityPolicyRule) []map[string]interface{} {
rulesSchema := make([]map[string]interface{}, 0, len(rules))
for _, rule := range rules {
Expand All @@ -368,6 +411,7 @@ func flattenMatch(match *compute.SecurityPolicyRuleMatcher) []map[string]interfa
data := map[string]interface{}{
"versioned_expr": match.VersionedExpr,
"config": flattenMatchConfig(match.Config),
"expr": flattenMatchExpr(match),
}

return []map[string]interface{}{data}
Expand All @@ -385,6 +429,22 @@ func flattenMatchConfig(conf *compute.SecurityPolicyRuleMatcherConfig) []map[str
return []map[string]interface{}{data}
}

func flattenMatchExpr(match *compute.SecurityPolicyRuleMatcher) []map[string]interface{} {
if match.Expr == nil {
return nil
}

data := map[string]interface{}{
"expression": match.Expr.Expression,
// These fields are not yet supported (Issue terraform-providers/terraform-provider-google#4497: mbang)
// "title": match.Expr.Title,
// "description": match.Expr.Description,
// "location": match.Expr.Location,
}

return []map[string]interface{}{data}
}

func resourceSecurityPolicyStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{"projects/(?P<project>[^/]+)/global/securityPolicies/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil {
Expand Down

0 comments on commit 12a8ddb

Please sign in to comment.