Skip to content

Commit

Permalink
r/aws_appmesh_route: Initial support for retry policy.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Aug 4, 2019
1 parent f5baa08 commit 14a78aa
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
113 changes: 113 additions & 0 deletions aws/resource_aws_appmesh_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,65 @@ func resourceAwsAppmeshRoute() *schema.Resource {
},
},
},

"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,
MinItems: 0,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"max_retries": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
},

// TODO The API default is 15000ms, but that cannot currently be expressed via 'Default:'.
// TODO The API always returns results as ms. Should the attribute be 'per_retry_timeout_millis'?
// TODO See https://github.com/aws/aws-app-mesh-roadmap/issues/7#issuecomment-518041427.
"per_retry_timeout": {
Type: schema.TypeList,
Optional: true,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"unit": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
appmesh.DurationUnitMs,
appmesh.DurationUnitS,
}, 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 @@ -537,6 +596,41 @@ func expandAppmeshRouteSpec(vSpec []interface{}) *appmesh.RouteSpec {

spec.HttpRoute.Match = httpRouteMatch
}

if vHttpRetryPolicy, ok := mHttpRoute["retry_policy"].([]interface{}); ok && len(vHttpRetryPolicy) > 0 && vHttpRetryPolicy[0] != nil {
httpRetryPolicy := &appmesh.HttpRetryPolicy{}

mHttpRetryPolicy := vHttpRetryPolicy[0].(map[string]interface{})

if vMaxRetries, ok := mHttpRetryPolicy["max_retries"].(int); ok && vMaxRetries > 0 {
httpRetryPolicy.MaxRetries = aws.Int64(int64(vMaxRetries))
}

if vHttpRetryEvents, ok := mHttpRetryPolicy["http_retry_events"].(*schema.Set); ok && vHttpRetryEvents.Len() > 0 {
httpRetryPolicy.HttpRetryEvents = expandStringSet(vHttpRetryEvents)
}

if vPerRetryTimeout, ok := mHttpRetryPolicy["per_retry_timeout"].([]interface{}); ok && len(vPerRetryTimeout) > 0 && vPerRetryTimeout[0] != nil {
perRetryTimeout := &appmesh.Duration{}

mPerRetryTimeout := vPerRetryTimeout[0].(map[string]interface{})

if vUnit, ok := mPerRetryTimeout["unit"].(string); ok && vUnit != "" {
perRetryTimeout.Unit = aws.String(vUnit)
}
if vValue, ok := mPerRetryTimeout["value"].(int); ok && vValue > 0 {
perRetryTimeout.Value = aws.Int64(int64(vValue))
}

httpRetryPolicy.PerRetryTimeout = perRetryTimeout
}

if vTcpRetryEvents, ok := mHttpRetryPolicy["tcp_retry_events"].(*schema.Set); ok && vTcpRetryEvents.Len() > 0 {
httpRetryPolicy.TcpRetryEvents = expandStringSet(vTcpRetryEvents)
}

spec.HttpRoute.RetryPolicy = httpRetryPolicy
}
}

if vTcpRoute, ok := mSpec["tcp_route"].([]interface{}); ok && len(vTcpRoute) > 0 && vTcpRoute[0] != nil {
Expand Down Expand Up @@ -648,6 +742,25 @@ func flattenAppmeshRouteSpec(spec *appmesh.RouteSpec) []interface{} {
}
}

if httpRetryPolicy := httpRoute.RetryPolicy; httpRetryPolicy != nil {
mHttpRetryPolicy := map[string]interface{}{
"http_retry_events": flattenStringSet(httpRetryPolicy.HttpRetryEvents),
"max_retries": int(aws.Int64Value(httpRetryPolicy.MaxRetries)),
"tcp_retry_events": flattenStringSet(httpRetryPolicy.TcpRetryEvents),
}

if perRetryTimeout := httpRetryPolicy.PerRetryTimeout; perRetryTimeout != nil {
mPerRetryTimeout := map[string]interface{}{
"unit": aws.StringValue(perRetryTimeout.Unit),
"value": int(aws.Int64Value(perRetryTimeout.Value)),
}

mHttpRetryPolicy["per_retry_timeout"] = []interface{}{mPerRetryTimeout}
}

mHttpRoute["retry_policy"] = []interface{}{mHttpRetryPolicy}
}

mSpec["http_route"] = []interface{}{mHttpRoute}
}

Expand Down
4 changes: 4 additions & 0 deletions aws/resource_aws_appmesh_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func testAccAwsAppmeshRoute_httpRoute(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.method", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.prefix", "/"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.scheme", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.retry_policy.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.tcp_route.#", "0"),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
Expand All @@ -147,6 +148,7 @@ func testAccAwsAppmeshRoute_httpRoute(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.method", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.prefix", "/path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.scheme", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.retry_policy.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.tcp_route.#", "0"),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
Expand Down Expand Up @@ -192,6 +194,7 @@ func testAccAwsAppmeshRoute_httpHeader(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.method", "POST"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.prefix", "/"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.scheme", "http"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.retry_policy.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.tcp_route.#", "0"),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
Expand Down Expand Up @@ -227,6 +230,7 @@ func testAccAwsAppmeshRoute_httpHeader(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.method", "PUT"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.prefix", "/path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.scheme", "https"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.retry_policy.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.tcp_route.#", "0"),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
Expand Down
15 changes: 15 additions & 0 deletions website/docs/r/appmesh_route.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The `http_route` object supports the following:

* `action` - (Required) The action to take if a match is determined.
* `match` - (Required) The criteria for determining an HTTP request match.
* `retry_policy` - (Optional) The retry policy.

The `tcp_route` object supports the following:

Expand All @@ -100,6 +101,20 @@ This parameter must always start with /, which by itself matches all requests to
* `method` - (Optional) The HTTP method with which to match requests. Valid values: `GET`, `HEAD`, `POST`, `PUT`, `DELETE`, `CONNECT`, `OPTIONS`, `TRACE`, `PATCH`.
* `scheme` - (Optional) The URL scheme with which to match requests. Valid values: `HTTP`, `HTTPS`.

The `retry_policy` object supports the following:

* `http_retry_events` - (Optional) List of HTTP retry events.
* `max_retries` - (Optional) The maximum number of retries. Default is `1`.
* `per_retry_timeout` - (Optional) The per-retry timeout. Default is `15s`.
* `tcp_retry_events` - (Optional) List of TCP retry events. The only valid value is `connection-error`.

You must specify at least one value for `http_retry_events`, or at least one value for `tcp_retry_events`.

The `per_retry_timeout` object supports the following:

* `unit` - (Required) Retry unit. Valid values: `ms`, `s`.
* `value` - (Required) Retry value.

The `weighted_target` object supports the following:

* `virtual_node` - (Required) The virtual node to associate with the weighted target.
Expand Down

0 comments on commit 14a78aa

Please sign in to comment.