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

new resource: azurerm_spring_cloud_new_relic_application_performance_monitoring #24699

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ import (
"context"
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/appplatform/2023-11-01-preview/appplatform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type SpringCloudNewRelicApplicationPerformanceMonitoringModel struct {
Name string `tfschema:"name"`
SpringCloudServiceId string `tfschema:"spring_cloud_service_id"`
GloballyEnabled bool `tfschema:"globally_enabled"`
AppName string `tfschema:"app_name"`
AgentEnabled bool `tfschema:"agent_enabled"`
AppServerPort int `tfschema:"app_server_port"`
AuditModeEnabled bool `tfschema:"audit_mode_enabled"`
AutoAppNamingEnabled bool `tfschema:"auto_app_naming_enabled"`
AutoTransactionNamingEnabled bool `tfschema:"auto_transaction_naming_enabled"`
CustomTracingEnabled bool `tfschema:"custom_tracing_enabled"`
Labels string `tfschema:"labels"`
LicenseKey string `tfschema:"license_key"`
Name string `tfschema:"name"`
SpringCloudServiceId string `tfschema:"spring_cloud_service_id"`
GloballyEnabled bool `tfschema:"globally_enabled"`
AppName string `tfschema:"app_name"`
AgentEnabled bool `tfschema:"agent_enabled"`
AppServerPort int `tfschema:"app_server_port"`
AuditModeEnabled bool `tfschema:"audit_mode_enabled"`
AutoAppNamingEnabled bool `tfschema:"auto_app_naming_enabled"`
AutoTransactionNamingEnabled bool `tfschema:"auto_transaction_naming_enabled"`
CustomTracingEnabled bool `tfschema:"custom_tracing_enabled"`
Labels map[string]string `tfschema:"labels"`
LicenseKey string `tfschema:"license_key"`
}

type SpringCloudNewRelicApplicationPerformanceMonitoringResource struct{}
Expand Down Expand Up @@ -106,15 +108,16 @@ func (s SpringCloudNewRelicApplicationPerformanceMonitoringResource) Arguments()
},

"labels": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"globally_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
}
}
Expand All @@ -135,7 +138,7 @@ func (s SpringCloudNewRelicApplicationPerformanceMonitoringResource) Create() sd
client := metadata.Client.AppPlatform.AppPlatformClient
springId, err := commonids.ParseSpringCloudServiceID(model.SpringCloudServiceId)
if err != nil {
return fmt.Errorf("parsing spring service ID: %+v", err)
return err
}
id := appplatform.NewApmID(springId.SubscriptionId, springId.ResourceGroupName, springId.ServiceName, model.Name)

Expand All @@ -158,7 +161,7 @@ func (s SpringCloudNewRelicApplicationPerformanceMonitoringResource) Create() sd
"enable_auto_app_naming": fmt.Sprintf("%t", model.AutoAppNamingEnabled),
"enable_auto_transaction_naming": fmt.Sprintf("%t", model.AutoTransactionNamingEnabled),
"enable_custom_tracing": fmt.Sprintf("%t", model.CustomTracingEnabled),
"labels": model.Labels,
"labels": expandNewRelicLabels(model.Labels),
}),
Secrets: pointer.To(map[string]string{
"license_key": model.LicenseKey,
Expand Down Expand Up @@ -248,7 +251,7 @@ func (s SpringCloudNewRelicApplicationPerformanceMonitoringResource) Update() sd
}

if metadata.ResourceData.HasChange("labels") {
(*properties.Properties)["labels"] = model.Labels
(*properties.Properties)["labels"] = expandNewRelicLabels(model.Labels)
}

if metadata.ResourceData.HasChange("license_key") {
Expand Down Expand Up @@ -364,7 +367,7 @@ func (s SpringCloudNewRelicApplicationPerformanceMonitoringResource) Read() sdk.
state.CustomTracingEnabled = value == "true"
}
if value, ok := (*props.Properties)["labels"]; ok {
state.Labels = value
state.Labels = flattenNewRelicLabels(value)
}
}
}
Expand Down Expand Up @@ -394,3 +397,28 @@ func (s SpringCloudNewRelicApplicationPerformanceMonitoringResource) Delete() sd
},
}
}

func expandNewRelicLabels(input map[string]string) string {
if len(input) == 0 {
return ""
}
labels := make([]string, 0)
for k, v := range input {
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
}
return strings.Join(labels, ";")
}

func flattenNewRelicLabels(input string) map[string]string {
if input == "" {
return nil
}
labels := make(map[string]string)
for _, label := range strings.Split(input, ";") {
parts := strings.Split(label, ":")
if len(parts) == 2 {
labels[parts[0]] = parts[1]
}
}
return labels
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,19 @@ func (r SpringCloudNewRelicApplicationPerformanceMonitoringResource) complete(da
resource "azurerm_spring_cloud_new_relic_application_performance_monitoring" "test" {
name = "acctest-apm-%[2]d"
spring_cloud_service_id = azurerm_spring_cloud_service.test.id
app_name = "test-app-name"
license_key = "test-license-key"
agent_enabled = true
app_name = "updated-app-name"
license_key = "updated-license-key"
agent_enabled = false
app_server_port = 8080
audit_mode_enabled = true
auto_app_naming_enabled = true
auto_transaction_naming_enabled = true
custom_tracing_enabled = true
labels = "tagName1:tagValue1;tagName2:tagValue2;tagName3:tagValue3"
globally_enabled = true
auto_transaction_naming_enabled = false
custom_tracing_enabled = false
labels = {
"tagName1" = "tagValue1"
"tagName2" = "tagValue2"
magodo marked this conversation as resolved.
Show resolved Hide resolved
}
globally_enabled = true
}
`, template, data.RandomInteger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ resource "azurerm_spring_cloud_service" "example" {
}

resource "azurerm_spring_cloud_new_relic_application_performance_monitoring" "example" {
name = "example"
spring_cloud_service_id = azurerm_spring_cloud_service.example.id
app_name = "example-app-name"
license_key = "example-license-key"
agent_enabled = true
app_server_port = 8080
audit_mode_enabled = true
auto_app_naming_enabled = true
auto_transaction_naming_enabled = true
custom_tracing_enabled = true
labels = "tagName1:tagValue1;tagName2:tagValue2;tagName3:tagValue3"
globally_enabled = true
name = "example"
spring_cloud_service_id = azurerm_spring_cloud_service.example.id
app_name = "example-app-name"
license_key = "example-license-key"
app_server_port = 8080
labels = {
"tagName1" = "tagValue1"
"tagName2" = "tagValue2"
magodo marked this conversation as resolved.
Show resolved Hide resolved
}
globally_enabled = true
}
```

Expand Down Expand Up @@ -73,7 +71,7 @@ The following arguments are supported:

* `custom_tracing_enabled` - (Optional) Specifies whether enable all instrumentation using an `@Trace` annotation. Disabling this causes `@Trace` annotations to be ignored. Defaults to `true`.

* `labels` - (Optional) Specifies the labels to be added to the New Relic application. The format is `tagName1:tagValue1;tagName2:tagValue2;tagName3:tagValue3`.
* `labels` - (Optional) Specifies a mapping of labels to be added to the New Relic application.

* `globally_enabled` - (Optional) Specifies whether the Spring Cloud Application Performance Monitoring resource for Application Insights is enabled globally. Defaults to `false`.

Expand Down
Loading