Skip to content

Commit

Permalink
azurerm_spring_cloud_gateway - support for the `application_perform…
Browse files Browse the repository at this point in the history
…ance_monitoring_ids` property (hashicorp#24919)

* `azurerm_spring_cloud_gateway` - support for the `application_performance_monitoring_ids` property

* make terrafmt

* update tests

* update test

* make terrafmt
  • Loading branch information
ms-henglu authored and rizkybiz committed Feb 29, 2024
1 parent 830325c commit 74146e7
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/services/springcloud/spring_cloud_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type SpringCloudGatewayModel struct {
Name string `tfschema:"name"`
SpringCloudServiceId string `tfschema:"spring_cloud_service_id"`
ApiMetadata []ApiMetadataModel `tfschema:"api_metadata"`
ApplicationPerformanceMonitoringIds []string `tfschema:"application_performance_monitoring_ids"`
ApplicationPerformanceMonitoringTypes []string `tfschema:"application_performance_monitoring_types"`
ClientAuthorization []ClientAuthorizationModel `tfschema:"client_authorization"`
Cors []CorsModel `tfschema:"cors"`
Expand Down Expand Up @@ -151,6 +152,16 @@ func (s SpringCloudGatewayResource) Arguments() map[string]*pluginsdk.Schema {
},
},

"application_performance_monitoring_ids": {
Type: pluginsdk.TypeList,
Optional: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: appplatform.ValidateApmID,
},
},

"application_performance_monitoring_types": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down Expand Up @@ -462,6 +473,7 @@ func (s SpringCloudGatewayResource) Create() sdk.ResourceFunc {
Properties: &appplatform.GatewayProperties{
ClientAuth: expandGatewayClientAuth(model.ClientAuthorization),
ApiMetadataProperties: expandGatewayGatewayAPIMetadataProperties(model.ApiMetadata),
Apms: expandGatewayApms(model.ApplicationPerformanceMonitoringIds),
ApmTypes: expandGatewayGatewayApmTypes(model.ApplicationPerformanceMonitoringTypes),
CorsProperties: expandGatewayGatewayCorsProperties(model.Cors),
EnvironmentVariables: expandGatewayGatewayEnvironmentVariables(model.EnvironmentVariables, model.SensitiveEnvironmentVariables),
Expand Down Expand Up @@ -530,6 +542,10 @@ func (s SpringCloudGatewayResource) Update() sdk.ResourceFunc {
properties.ApiMetadataProperties = expandGatewayGatewayAPIMetadataProperties(model.ApiMetadata)
}

if metadata.ResourceData.HasChange("application_performance_monitoring_ids") {
properties.Apms = expandGatewayApms(model.ApplicationPerformanceMonitoringIds)
}

if metadata.ResourceData.HasChange("application_performance_monitoring_types") {
properties.ApmTypes = expandGatewayGatewayApmTypes(model.ApplicationPerformanceMonitoringTypes)
}
Expand Down Expand Up @@ -614,6 +630,11 @@ func (s SpringCloudGatewayResource) Read() sdk.ResourceFunc {
if resp.Model != nil {
if props := resp.Model.Properties; props != nil {
state.ApiMetadata = flattenGatewayGatewayAPIMetadataProperties(props.ApiMetadataProperties)
apms, err := flattenGatewayApms(props.Apms)
if err != nil {
return err
}
state.ApplicationPerformanceMonitoringIds = apms
state.ApplicationPerformanceMonitoringTypes = flattenGatewayGatewayApmTypes(props.ApmTypes)
state.ClientAuthorization = flattenGatewayClientAuth(props.ClientAuth)
state.Cors = flattenGatewayGatewayCorsProperties(props.CorsProperties)
Expand Down Expand Up @@ -766,6 +787,19 @@ func expandGatewayResponseCacheProperties(input SpringCloudGatewayModel) appplat
return appplatform.RawGatewayResponseCachePropertiesImpl{}
}

func expandGatewayApms(input []string) *[]appplatform.ApmReference {
if len(input) == 0 {
return nil
}
out := make([]appplatform.ApmReference, 0)
for _, v := range input {
out = append(out, appplatform.ApmReference{
ResourceId: v,
})
}
return pointer.To(out)
}

func flattenGatewayGatewayAPIMetadataProperties(input *appplatform.GatewayApiMetadataProperties) []ApiMetadataModel {
if input == nil {
return make([]ApiMetadataModel, 0)
Expand Down Expand Up @@ -907,3 +941,18 @@ func flattenGatewayLocalResponseCachePerInstanceProperties(input appplatform.Gat
}
return make([]ResponseCacheModel, 0)
}

func flattenGatewayApms(input *[]appplatform.ApmReference) ([]string, error) {
out := make([]string, 0)
if input == nil {
return out, nil
}
for _, v := range *input {
id, err := appplatform.ParseApmIDInsensitively(v.ResourceId)
if err != nil {
return nil, err
}
out = append(out, id.ID())
}
return out, nil
}
130 changes: 130 additions & 0 deletions internal/services/springcloud/spring_cloud_gateway_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,62 @@ func TestAccSpringCloudGateway_responseCache(t *testing.T) {
})
}

func TestAccSpringCloudGateway_apms(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_spring_cloud_gateway", "test")
r := SpringCloudGatewayResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.elasticApm(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccSpringCloudGateway_apmsUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_spring_cloud_gateway", "test")
r := SpringCloudGatewayResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.elasticApm(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.dynatraceApm(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.multipleApms(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.noApms(data),
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 @@ -250,6 +306,80 @@ resource "azurerm_spring_cloud_gateway" "test" {
`, template, clientId, clientSecret)
}

func (r SpringCloudGatewayResource) apmsTemplate(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%[1]s
resource "azurerm_spring_cloud_dynatrace_application_performance_monitoring" "test" {
name = "acctest-dapm-%[2]d"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id
tenant = "test-tenant"
tenant_token = "dt0s01.AAAAAAAAAAAAAAAAAAAAAAAA.BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
connection_point = "https://example.live.dynatrace.com:443"
}
resource "azurerm_spring_cloud_elastic_application_performance_monitoring" "test" {
name = "acctest-eapm-%[2]d"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id
application_packages = ["org.example", "org.another.example"]
service_name = "test-service-name"
server_url = "http://127.0.0.1:8200"
}
`, template, data.RandomInteger)
}

func (r SpringCloudGatewayResource) dynatraceApm(data acceptance.TestData) string {
template := r.apmsTemplate(data)
return fmt.Sprintf(`
%[1]s
resource "azurerm_spring_cloud_gateway" "test" {
name = "default"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id
application_performance_monitoring_ids = [azurerm_spring_cloud_dynatrace_application_performance_monitoring.test.id]
}
`, template)
}

func (r SpringCloudGatewayResource) elasticApm(data acceptance.TestData) string {
template := r.apmsTemplate(data)
return fmt.Sprintf(`
%[1]s
resource "azurerm_spring_cloud_gateway" "test" {
name = "default"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id
application_performance_monitoring_ids = [azurerm_spring_cloud_elastic_application_performance_monitoring.test.id]
}
`, template)
}

func (r SpringCloudGatewayResource) multipleApms(data acceptance.TestData) string {
template := r.apmsTemplate(data)
return fmt.Sprintf(`
%[1]s
resource "azurerm_spring_cloud_gateway" "test" {
name = "default"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id
application_performance_monitoring_ids = [azurerm_spring_cloud_dynatrace_application_performance_monitoring.test.id, azurerm_spring_cloud_elastic_application_performance_monitoring.test.id]
}
`, template)
}

func (r SpringCloudGatewayResource) noApms(data acceptance.TestData) string {
template := r.apmsTemplate(data)
return fmt.Sprintf(`
%[1]s
resource "azurerm_spring_cloud_gateway" "test" {
name = "default"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id
}
`, template)
}

func (r SpringCloudGatewayResource) clientAuth(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/spring_cloud_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ The following arguments are supported:

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

* `application_performance_monitoring_ids` - (Optional) Specifies a list of Spring Cloud Application Performance Monitoring IDs.

* `application_performance_monitoring_types` - (Optional) Specifies a list of application performance monitoring types used in the Spring Cloud Gateway. The allowed values are `AppDynamics`, `ApplicationInsights`, `Dynatrace`, `ElasticAPM` and `NewRelic`.

* `client_authorization` - (Optional) A `client_authorization` block as defined below.
Expand Down

0 comments on commit 74146e7

Please sign in to comment.