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

azurerm_spring_cloud_gateway - support for the local_response_cache_per_route, local_response_cache_per_instance properties #24697

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
122 changes: 113 additions & 9 deletions internal/services/springcloud/spring_cloud_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type SpringCloudGatewayModel struct {
ClientAuthorization []ClientAuthorizationModel `tfschema:"client_authorization"`
Cors []CorsModel `tfschema:"cors"`
EnvironmentVariables map[string]string `tfschema:"environment_variables"`
LocalResponseCachePerRoute []ResponseCacheModel `tfschema:"local_response_cache_per_route"`
LocalResponseCachePerInstance []ResponseCacheModel `tfschema:"local_response_cache_per_instance"`
SensitiveEnvironmentVariables map[string]string `tfschema:"sensitive_environment_variables"`
HttpsOnly bool `tfschema:"https_only"`
InstanceCount int `tfschema:"instance_count"`
Expand Down Expand Up @@ -70,6 +72,11 @@ type QuotaModel struct {
Memory string `tfschema:"memory"`
}

type ResponseCacheModel struct {
Size string `tfschema:"size"`
TimeToLive string `tfschema:"time_to_live"`
}

type SpringCloudGatewayResource struct{}

var _ sdk.ResourceWithUpdate = SpringCloudGatewayResource{}
Expand Down Expand Up @@ -263,6 +270,50 @@ func (s SpringCloudGatewayResource) Arguments() map[string]*pluginsdk.Schema {
},
},

"local_response_cache_per_route": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"local_response_cache_per_instance"},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"size": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"time_to_live": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"local_response_cache_per_instance": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"local_response_cache_per_route"},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"size": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"time_to_live": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"sensitive_environment_variables": {
Type: pluginsdk.TypeMap,
Optional: true,
Expand Down Expand Up @@ -409,15 +460,16 @@ func (s SpringCloudGatewayResource) Create() sdk.ResourceFunc {

gatewayResource := appplatform.GatewayResource{
Properties: &appplatform.GatewayProperties{
ClientAuth: expandGatewayClientAuth(model.ClientAuthorization),
ApiMetadataProperties: expandGatewayGatewayAPIMetadataProperties(model.ApiMetadata),
ApmTypes: expandGatewayGatewayApmTypes(model.ApplicationPerformanceMonitoringTypes),
CorsProperties: expandGatewayGatewayCorsProperties(model.Cors),
EnvironmentVariables: expandGatewayGatewayEnvironmentVariables(model.EnvironmentVariables, model.SensitiveEnvironmentVariables),
HTTPSOnly: pointer.To(model.HttpsOnly),
Public: pointer.To(model.PublicNetworkAccessEnabled),
ResourceRequests: expandGatewayGatewayResourceRequests(model.Quota),
SsoProperties: expandGatewaySsoProperties(model.Sso),
ClientAuth: expandGatewayClientAuth(model.ClientAuthorization),
ApiMetadataProperties: expandGatewayGatewayAPIMetadataProperties(model.ApiMetadata),
ApmTypes: expandGatewayGatewayApmTypes(model.ApplicationPerformanceMonitoringTypes),
CorsProperties: expandGatewayGatewayCorsProperties(model.Cors),
EnvironmentVariables: expandGatewayGatewayEnvironmentVariables(model.EnvironmentVariables, model.SensitiveEnvironmentVariables),
HTTPSOnly: pointer.To(model.HttpsOnly),
Public: pointer.To(model.PublicNetworkAccessEnabled),
ResponseCacheProperties: expandGatewayResponseCacheProperties(model),
ResourceRequests: expandGatewayGatewayResourceRequests(model.Quota),
SsoProperties: expandGatewaySsoProperties(model.Sso),
},
Sku: &appplatform.Sku{
Name: service.Model.Sku.Name,
Expand Down Expand Up @@ -506,6 +558,10 @@ func (s SpringCloudGatewayResource) Update() sdk.ResourceFunc {
properties.SsoProperties = expandGatewaySsoProperties(model.Sso)
}

if metadata.ResourceData.HasChange("local_response_cache_per_instance") || metadata.ResourceData.HasChange("local_response_cache_per_route") {
properties.ResponseCacheProperties = expandGatewayResponseCacheProperties(model)
}

if metadata.ResourceData.HasChange("instance_count") {
sku.Capacity = pointer.To(int64(model.InstanceCount))
}
Expand Down Expand Up @@ -568,6 +624,8 @@ func (s SpringCloudGatewayResource) Read() sdk.ResourceFunc {
state.PublicNetworkAccessEnabled = pointer.From(props.Public)
state.Quota = flattenGatewayGatewayResourceRequests(props.ResourceRequests)
state.Sso = flattenGatewaySsoProperties(props.SsoProperties, model.Sso)
state.LocalResponseCachePerRoute = flattenGatewayLocalResponseCachePerRouteProperties(props.ResponseCacheProperties)
state.LocalResponseCachePerInstance = flattenGatewayLocalResponseCachePerInstanceProperties(props.ResponseCacheProperties)
}

if sku := resp.Model.Sku; sku != nil {
Expand Down Expand Up @@ -692,6 +750,22 @@ func expandGatewayClientAuth(input []ClientAuthorizationModel) *appplatform.Gate
}
}

func expandGatewayResponseCacheProperties(input SpringCloudGatewayModel) appplatform.GatewayResponseCacheProperties {
if len(input.LocalResponseCachePerRoute) != 0 {
return appplatform.GatewayLocalResponseCachePerRouteProperties{
Size: pointer.To(input.LocalResponseCachePerRoute[0].Size),
TimeToLive: pointer.To(input.LocalResponseCachePerRoute[0].TimeToLive),
}
}
if len(input.LocalResponseCachePerInstance) != 0 {
return appplatform.GatewayLocalResponseCachePerInstanceProperties{
Size: pointer.To(input.LocalResponseCachePerInstance[0].Size),
TimeToLive: pointer.To(input.LocalResponseCachePerInstance[0].TimeToLive),
}
}
return appplatform.RawGatewayResponseCachePropertiesImpl{}
}

func flattenGatewayGatewayAPIMetadataProperties(input *appplatform.GatewayApiMetadataProperties) []ApiMetadataModel {
if input == nil {
return make([]ApiMetadataModel, 0)
Expand Down Expand Up @@ -803,3 +877,33 @@ func flattenGatewayClientAuth(input *appplatform.GatewayPropertiesClientAuth) []
},
}
}

func flattenGatewayLocalResponseCachePerRouteProperties(input appplatform.GatewayResponseCacheProperties) []ResponseCacheModel {
if input == nil {
return make([]ResponseCacheModel, 0)
}
if v, ok := input.(appplatform.GatewayLocalResponseCachePerRouteProperties); ok {
return []ResponseCacheModel{
{
Size: pointer.From(v.Size),
TimeToLive: pointer.From(v.TimeToLive),
},
}
}
return make([]ResponseCacheModel, 0)
}

func flattenGatewayLocalResponseCachePerInstanceProperties(input appplatform.GatewayResponseCacheProperties) []ResponseCacheModel {
if input == nil {
return make([]ResponseCacheModel, 0)
}
if v, ok := input.(appplatform.GatewayLocalResponseCachePerInstanceProperties); ok {
return []ResponseCacheModel{
{
Size: pointer.From(v.Size),
TimeToLive: pointer.From(v.TimeToLive),
},
}
}
return make([]ResponseCacheModel, 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ func TestAccSpringCloudGateway_update(t *testing.T) {
})
}

func TestAccSpringCloudGateway_responseCache(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_spring_cloud_gateway", "test")
r := SpringCloudGatewayResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.responseCachePerInstance(data, "10MB", "30s"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.responseCachePerRoute(data, "900KB", "5m"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r SpringCloudGatewayResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := appplatform.ParseGatewayID(state.ID)
if err != nil {
Expand Down Expand Up @@ -345,3 +366,37 @@ resource "azurerm_spring_cloud_gateway" "test" {
}
`, template, data.RandomIntOfLength(10))
}

func (r SpringCloudGatewayResource) responseCachePerRoute(data acceptance.TestData, size, timeToLive string) string {
template := r.template(data)
return fmt.Sprintf(`
%[1]s

resource "azurerm_spring_cloud_gateway" "test" {
name = "default"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id

local_response_cache_per_route {
size = "%[2]s"
time_to_live = "%[3]s"
}
}
`, template, size, timeToLive)
}

func (r SpringCloudGatewayResource) responseCachePerInstance(data acceptance.TestData, size, timeToLive string) string {
template := r.template(data)
return fmt.Sprintf(`
%[1]s

resource "azurerm_spring_cloud_gateway" "test" {
name = "default"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id

local_response_cache_per_instance {
size = "%[2]s"
time_to_live = "%[3]s"
}
}
`, template, size, timeToLive)
}
25 changes: 25 additions & 0 deletions website/docs/r/spring_cloud_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ resource "azurerm_spring_cloud_gateway" "example" {
issuer_uri = "https://www.test.com/issueToken"
scope = ["read"]
}

local_response_cache_per_instance {
size = "100MB"
time_to_live = "30s"
}
}
```

Expand Down Expand Up @@ -98,6 +103,10 @@ The following arguments are supported:

* `quota` - (Optional) A `quota` block as defined below.

* `local_response_cache_per_instance` - (Optional) A `local_response_cache_per_instance` block as defined below. Only one of `local_response_cache_per_instance` or `local_response_cache_per_route` can be specified.

* `local_response_cache_per_route` - (Optional) A `local_response_cache_per_route` block as defined below. Only one of `local_response_cache_per_instance` or `local_response_cache_per_route` can be specified.

* `sensitive_environment_variables` - (Optional) Specifies the sensitive environment variables of the Spring Cloud Gateway as a map of key-value pairs. Changing this forces a new resource to be created.

* `sso` - (Optional) A `sso` block as defined below.
Expand Down Expand Up @@ -144,6 +153,22 @@ A `cors` block supports the following:

---

A `local_response_cache_per_route` block supports the following:

* `size` - (Optional) Specifies the maximum size of cache (10MB, 900KB, 1GB...) to determine if the cache needs to evict some entries.

* `time_to_live` - (Optional) Specifies the time before a cached entry is expired (300s, 5m, 1h...).

---

A `local_response_cache_per_instance` block supports the following:

* `size` - (Optional) Specifies the maximum size of cache (10MB, 900KB, 1GB...) to determine if the cache needs to evict some entries.

* `time_to_live` - (Optional) Specifies the time before a cached entry is expired (300s, 5m, 1h...).

---

The `quota` block supports the following:

* `cpu` - (Optional) Specifies the required cpu of the Spring Cloud Deployment. Possible Values are `500m`, `1`, `2`, `3` and `4`. Defaults to `1` if not specified.
Expand Down
Loading