Skip to content

Commit

Permalink
azurerm_signalr_service - Add support for live_trace (#17629)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Jul 14, 2022
1 parent 7abe308 commit 7847294
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 6 deletions.
151 changes: 145 additions & 6 deletions internal/services/signalr/signalr_service_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/signalr/2022-02-01/signalr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -106,9 +107,10 @@ func resourceArmSignalRServiceCreate(d *pluginsdk.ResourceData, meta interface{}
resourceType := signalr.SignalRResource{
Location: utils.String(location),
Properties: &signalr.SignalRProperties{
Cors: expandSignalRCors(cors),
Features: &expandedFeatures,
Upstream: expandUpstreamSettings(upstreamSettings),
Cors: expandSignalRCors(cors),
Features: &expandedFeatures,
Upstream: expandUpstreamSettings(upstreamSettings),
LiveTraceConfiguration: expandSignalRLiveTraceConfig(d.Get("live_trace").([]interface{})),
},
Sku: expandSignalRServiceSku(sku),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Expand Down Expand Up @@ -195,6 +197,10 @@ func resourceArmSignalRServiceRead(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("setting `upstream_endpoint`: %+v", err)
}

if err := d.Set("live_trace", flattenSignalRLiveTraceConfig(props.LiveTraceConfiguration)); err != nil {
return fmt.Errorf("setting `live_trace`:%+v", err)
}

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
Expand Down Expand Up @@ -223,14 +229,18 @@ func resourceArmSignalRServiceUpdate(d *pluginsdk.ResourceData, meta interface{}

resourceType := signalr.SignalRResource{}

if d.HasChanges("cors", "features", "upstream_endpoint", "connectivity_logs_enabled", "messaging_logs_enabled", "service_mode", "live_trace_enabled") {
if d.HasChanges("cors", "features", "upstream_endpoint", "connectivity_logs_enabled", "messaging_logs_enabled", "service_mode", "live_trace_enabled", "live_trace") {
resourceType.Properties = &signalr.SignalRProperties{}

if d.HasChange("cors") {
corsRaw := d.Get("cors").([]interface{})
resourceType.Properties.Cors = expandSignalRCors(corsRaw)
}

if d.HasChange("live_trace") {
resourceType.Properties.LiveTraceConfiguration = expandSignalRLiveTraceConfig(d.Get("live_trace").([]interface{}))
}

if d.HasChanges("connectivity_logs_enabled", "messaging_logs_enabled", "service_mode", "live_trace_enabled") {
features := make([]signalr.SignalRFeature, 0)
if d.HasChange("connectivity_logs_enabled") {
Expand Down Expand Up @@ -457,6 +467,101 @@ func flattenSignalRServiceSku(input *signalr.ResourceSku) []interface{} {
}
}

func expandSignalRLiveTraceConfig(input []interface{}) *signalr.LiveTraceConfiguration {
resourceCategories := make([]signalr.LiveTraceCategory, 0)
if len(input) == 0 || input[0] == nil {
return nil
}

v := input[0].(map[string]interface{})

enabled := "false"
if v["enabled"].(bool) {
enabled = "true"
}

messageLogEnabled := "false"
if v["messaging_logs_enabled"].(bool) {
messageLogEnabled = "true"
}
resourceCategories = append(resourceCategories, signalr.LiveTraceCategory{
Name: utils.String("MessagingLogs"),
Enabled: utils.String(messageLogEnabled),
})

connectivityLogEnabled := "false"
if v["connectivity_logs_enabled"].(bool) {
connectivityLogEnabled = "true"
}
resourceCategories = append(resourceCategories, signalr.LiveTraceCategory{
Name: utils.String("ConnectivityLogs"),
Enabled: utils.String(connectivityLogEnabled),
})

httpLogEnabled := "false"
if v["http_request_logs_enabled"].(bool) {
httpLogEnabled = "true"
}
resourceCategories = append(resourceCategories, signalr.LiveTraceCategory{
Name: utils.String("HttpRequestLogs"),
Enabled: utils.String(httpLogEnabled),
})

return &signalr.LiveTraceConfiguration{
Enabled: &enabled,
Categories: &resourceCategories,
}
}

func flattenSignalRLiveTraceConfig(input *signalr.LiveTraceConfiguration) []interface{} {
result := make([]interface{}, 0)
if input == nil {
return result
}

var enabled bool
if input.Enabled != nil {
enabled = strings.EqualFold(*input.Enabled, "true")
}

var (
messagingLogEnabled bool
connectivityLogEnabled bool
httpLogsEnabled bool
)

if input.Categories != nil {
for _, item := range *input.Categories {
name := ""
if item.Name != nil {
name = *item.Name
}

var cateEnabled string
if item.Enabled != nil {
cateEnabled = *item.Enabled
}

switch name {
case "MessagingLogs":
messagingLogEnabled = strings.EqualFold(cateEnabled, "true")
case "ConnectivityLogs":
connectivityLogEnabled = strings.EqualFold(cateEnabled, "true")
case "HttpRequestLogs":
httpLogsEnabled = strings.EqualFold(cateEnabled, "true")
default:
continue
}
}
}
return []interface{}{map[string]interface{}{
"enabled": enabled,
"messaging_logs_enabled": messagingLogEnabled,
"connectivity_logs_enabled": connectivityLogEnabled,
"http_request_logs_enabled": httpLogsEnabled,
}}
}

func resourceArmSignalRServiceSchema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Expand Down Expand Up @@ -508,9 +613,43 @@ func resourceArmSignalRServiceSchema() map[string]*pluginsdk.Schema {
},

"live_trace_enabled": {
Type: pluginsdk.TypeBool,
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
Deprecated: "`live_trace_enabled` has been deprecated in favor of `live_trace` and will be removed in 4.0.",
},

"live_trace": {
Type: pluginsdk.TypeList,
Optional: true,
Default: false,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"connectivity_logs_enabled": {
Type: pluginsdk.TypeBool,
Default: true,
Optional: true,
},

"messaging_logs_enabled": {
Type: pluginsdk.TypeBool,
Default: true,
Optional: true,
},

"http_request_logs_enabled": {
Type: pluginsdk.TypeBool,
Default: true,
Optional: true,
},
},
},
},

"service_mode": {
Expand Down
83 changes: 83 additions & 0 deletions internal/services/signalr/signalr_service_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,28 @@ func TestAccSignalRService_withTags(t *testing.T) {
})
}

func TestAccSignalRService_liveTrace(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_signalr_service", "test")
r := SignalRServiceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.liveTrace(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.liveTraceUpdated(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r SignalRServiceResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := signalr.ParseSignalRID(state.ID)
if err != nil {
Expand Down Expand Up @@ -738,3 +760,64 @@ resource "azurerm_signalr_service" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (r SignalRServiceResource) liveTrace(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_signalr_service" "test" {
name = "acctestSignalR-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
live_trace {
enabled = true
messaging_logs_enabled = false
connectivity_logs_enabled = true
}
sku {
name = "Free_F1"
capacity = 1
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (r SignalRServiceResource) liveTraceUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_signalr_service" "test" {
name = "acctestSignalR-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
live_trace {
enabled = false
messaging_logs_enabled = true
connectivity_logs_enabled = false
http_request_logs_enabled = false
}
sku {
name = "Free_F1"
capacity = 1
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
14 changes: 14 additions & 0 deletions website/docs/r/signalr_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ The following arguments are supported:

* `upstream_endpoint` - (Optional) An `upstream_endpoint` block as documented below. Using this block requires the SignalR service to be Serverless. When creating multiple blocks they will be processed in the order they are defined in.

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

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand All @@ -91,6 +93,18 @@ An `upstream_endpoint` block supports the following:

---

A `live_trace` block supports the following:

* `enabled` - (Optional) Whether the live trace is enabled? Defaults to `true`.

* `messaging_logs_enabled` - (Optional) Whether the log category `MessagingLogs` is enabled? Defaults to `true`

* `connectivity_logs_enabled` - (Optional) Whether the log category `ConnectivityLogs` is enabled? Defaults to `true`

* `http_request_logs_enabled` - (Optional) Whether the log category `HttpRequestLogs` is enabled? Defaults to `true`

---

A `sku` block supports the following:

* `name` - (Required) Specifies which tier to use. Valid values are `Free_F1`, `Standard_S1` and `Premium_P1`.
Expand Down

0 comments on commit 7847294

Please sign in to comment.