Skip to content

Commit

Permalink
Merge pull request #11660 from ewbankkit/issue-10075
Browse files Browse the repository at this point in the history
r/aws_appmesh_route: Add support for retry policies
  • Loading branch information
breathingdust authored Sep 29, 2020
2 parents 98b5f7b + 2efca8a commit 562940f
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 109 deletions.
128 changes: 57 additions & 71 deletions aws/resource_aws_appmesh_route.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"bytes"
"fmt"
"log"
"regexp"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

Expand Down Expand Up @@ -98,7 +96,6 @@ func resourceAwsAppmeshRoute() *schema.Resource {
},
},
},
Set: appmeshWeightedTargetHash,
},
},
},
Expand Down Expand Up @@ -185,23 +182,12 @@ func resourceAwsAppmeshRoute() *schema.Resource {
},
},
},
Set: appmeshHttpRouteHeaderHash,
},

"method": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
appmesh.HttpMethodConnect,
appmesh.HttpMethodDelete,
appmesh.HttpMethodGet,
appmesh.HttpMethodHead,
appmesh.HttpMethodOptions,
appmesh.HttpMethodPatch,
appmesh.HttpMethodPost,
appmesh.HttpMethodPut,
appmesh.HttpMethodTrace,
}, false),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(appmesh.HttpMethod_Values(), false),
},

"prefix": {
Expand All @@ -211,12 +197,61 @@ func resourceAwsAppmeshRoute() *schema.Resource {
},

"scheme": {
Type: schema.TypeString,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(appmesh.HttpScheme_Values(), false),
},
},
},
},

"retry_policy": {
Type: schema.TypeList,
Optional: true,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"http_retry_events": {
Type: schema.TypeSet,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
appmesh.HttpSchemeHttp,
appmesh.HttpSchemeHttps,
}, false),
MinItems: 0,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"max_retries": {
Type: schema.TypeInt,
Required: true,
},

"per_retry_timeout": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"unit": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(appmesh.DurationUnit_Values(), false),
},

"value": {
Type: schema.TypeInt,
Required: true,
},
},
},
},

"tcp_retry_events": {
Type: schema.TypeSet,
Optional: true,
MinItems: 0,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
Expand Down Expand Up @@ -266,7 +301,6 @@ func resourceAwsAppmeshRoute() *schema.Resource {
},
},
},
Set: appmeshWeightedTargetHash,
},
},
},
Expand Down Expand Up @@ -465,51 +499,3 @@ func resourceAwsAppmeshRouteImport(d *schema.ResourceData, meta interface{}) ([]

return []*schema.ResourceData{d}, nil
}

func appmeshHttpRouteHeaderHash(vHttpRouteHeader interface{}) int {
var buf bytes.Buffer
mHttpRouteHeader := vHttpRouteHeader.(map[string]interface{})
if v, ok := mHttpRouteHeader["invert"].(bool); ok {
buf.WriteString(fmt.Sprintf("%t-", v))
}
if vMatch, ok := mHttpRouteHeader["match"].([]interface{}); ok && len(vMatch) > 0 && vMatch[0] != nil {
mMatch := vMatch[0].(map[string]interface{})
if v, ok := mMatch["exact"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
if v, ok := mMatch["prefix"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
if vRange, ok := mMatch["range"].([]interface{}); ok && len(vRange) > 0 && vRange[0] != nil {
mRange := vRange[0].(map[string]interface{})
if v, ok := mRange["end"].(int); ok {
buf.WriteString(fmt.Sprintf("%d-", v))
}
if v, ok := mRange["start"].(int); ok {
buf.WriteString(fmt.Sprintf("%d-", v))
}
}
if v, ok := mMatch["regex"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
if v, ok := mMatch["suffix"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
}
if v, ok := mHttpRouteHeader["name"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
return hashcode.String(buf.String())
}

func appmeshWeightedTargetHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
if v, ok := m["virtual_node"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
if v, ok := m["weight"].(int); ok {
buf.WriteString(fmt.Sprintf("%d-", v))
}
return hashcode.String(buf.String())
}
Loading

0 comments on commit 562940f

Please sign in to comment.