Skip to content

Commit

Permalink
azurerm_monitor_action_group - fix event_hub_receiver when depend…
Browse files Browse the repository at this point in the history
…ent event_hub is not in the same resource group (#17335)
  • Loading branch information
teowa authored Sep 9, 2022
1 parent 8caaba9 commit 65fc056
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 77 deletions.
119 changes: 92 additions & 27 deletions internal/services/monitor/monitor_action_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/monitor/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand All @@ -15,7 +16,7 @@ import (
)

func dataSourceMonitorActionGroup() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Read: dataSourceMonitorActionGroupRead,

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down Expand Up @@ -303,36 +304,100 @@ func dataSourceMonitorActionGroup() *pluginsdk.Resource {
},
},
},
"event_hub_receiver": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: eventhubs.ValidateEventhubID,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
"use_common_alert_schema": {
Type: pluginsdk.TypeBool,
Optional: true,
},
},
}

if !features.FourPointOhBeta() {
resource.Schema["event_hub_receiver"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: eventhubs.ValidateEventhubID,
Deprecated: "This property is deprecated and will be removed in version 4.0 of the provider, please use 'event_hub_name' and 'event_hub_namespace' instead.",
},
"event_hub_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_namespace": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
"use_common_alert_schema": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"subscription_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
},
},
},
}
} else {
resource.Schema["event_hub_receiver"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_namespace": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
"use_common_alert_schema": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"subscription_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
},
},
}
}
return resource
}

func dataSourceMonitorActionGroupRead(d *pluginsdk.ResourceData, meta interface{}) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,10 @@ resource "azurerm_monitor_action_group" "test" {
}
event_hub_receiver {
name = "eventhub-test-action"
event_hub_id = azurerm_eventhub.test.id
name = "eventhub-test-action"
event_hub_name = azurerm_eventhub.test.name
event_hub_namespace = azurerm_eventhub.test.namespace_name
}
}
Expand Down
161 changes: 120 additions & 41 deletions internal/services/monitor/monitor_action_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/monitor/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/monitor/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/monitor/validate"
Expand All @@ -23,7 +24,7 @@ import (
)

func resourceMonitorActionGroup() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Create: resourceMonitorActionGroupCreateUpdate,
Read: resourceMonitorActionGroupRead,
Update: resourceMonitorActionGroupCreateUpdate,
Expand Down Expand Up @@ -373,37 +374,101 @@ func resourceMonitorActionGroup() *pluginsdk.Resource {
},
},

"event_hub_receiver": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: eventhubs.ValidateEventhubID,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
"use_common_alert_schema": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"tags": tags.Schema(),
},
}

if !features.FourPointOhBeta() {
resource.Schema["event_hub_receiver"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: eventhubs.ValidateEventhubID,
Deprecated: "This property is deprecated and will be removed in version 4.0 of the provider, please use 'event_hub_name' and 'event_hub_namespace' instead.",
},
"event_hub_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_namespace": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
"use_common_alert_schema": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"subscription_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
},
},
"tags": tags.Schema(),
},
}
} else {
resource.Schema["event_hub_receiver"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"event_hub_namespace": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
"use_common_alert_schema": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"subscription_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},
},
},
}
}
return resource
}

func resourceMonitorActionGroupCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -443,7 +508,7 @@ func resourceMonitorActionGroupCreateUpdate(d *pluginsdk.ResourceData, meta inte
armRoleReceiversRaw := d.Get("arm_role_receiver").([]interface{})
eventHubReceiversRaw := d.Get("event_hub_receiver").([]interface{})

expandedEventHubReceiver, err := expandMonitorActionGroupEventHubReceiver(tenantId, eventHubReceiversRaw)
expandedEventHubReceiver, err := expandMonitorActionGroupEventHubReceiver(tenantId, subscriptionId, eventHubReceiversRaw)
if err != nil {
return err
}
Expand Down Expand Up @@ -751,28 +816,40 @@ func expandMonitorActionGroupRoleReceiver(v []interface{}) *[]insights.ArmRoleRe
return &receivers
}

func expandMonitorActionGroupEventHubReceiver(tenantId string, v []interface{}) (*[]insights.EventHubReceiver, error) {
func expandMonitorActionGroupEventHubReceiver(tenantId string, subscriptionId string, v []interface{}) (*[]insights.EventHubReceiver, error) {
receivers := make([]insights.EventHubReceiver, 0)
for _, receiverValue := range v {
val := receiverValue.(map[string]interface{})

eventHubId, err := eventhubs.ParseEventhubID(*utils.String(val["event_hub_id"].(string)))
if err != nil {
return nil, err
eventHubNameSpace, eventHubName, subId := val["event_hub_namespace"].(string), val["event_hub_name"].(string), val["subscription_id"].(string)
if !features.FourPointOhBeta() {
if eventHubNameSpace == "" && eventHubName == "" && subId == "" && val["event_hub_id"].(string) != "" {
eventHubId, err := eventhubs.ParseEventhubID(*utils.String(val["event_hub_id"].(string)))
if err != nil {
return nil, err
}
eventHubNameSpace, eventHubName, subId = eventHubId.NamespaceName, eventHubId.EventHubName, eventHubId.SubscriptionId
} else if val["event_hub_id"].(string) != "" || eventHubNameSpace == "" || eventHubName == "" {
return nil, fmt.Errorf("in event_hub_receiver, exactly one of event_hub_id or (event_hub_namespace, event_hub_name) must be set")
}
}

receiver := insights.EventHubReceiver{
EventHubNameSpace: utils.String(eventHubNameSpace),
EventHubName: utils.String(eventHubName),
Name: utils.String(val["name"].(string)),
EventHubNameSpace: &eventHubId.NamespaceName,
EventHubName: &eventHubId.EventHubName,
UseCommonAlertSchema: utils.Bool(val["use_common_alert_schema"].(bool)),
}
if v := val["tenant_id"].(string); v != "" {
receiver.TenantID = utils.String(v)
} else {
receiver.TenantID = utils.String(tenantId)
}
receiver.SubscriptionID = &eventHubId.SubscriptionId
if subId != "" {
receiver.SubscriptionID = utils.String(subId)
} else {
receiver.SubscriptionID = utils.String(subscriptionId)
}
receivers = append(receivers, receiver)
}
return &receivers, nil
Expand Down Expand Up @@ -1044,11 +1121,13 @@ func flattenMonitorActionGroupEventHubReceiver(resourceGroup string, receivers *
val["name"] = *receiver.Name
}
if receiver.EventHubNameSpace != nil && receiver.EventHubName != nil && receiver.SubscriptionID != nil {
event_hub_namespace := *receiver.EventHubNameSpace
event_hub_name := *receiver.EventHubName
subscription_id := *receiver.SubscriptionID

val["event_hub_id"] = eventhubs.NewEventhubID(subscription_id, resourceGroup, event_hub_namespace, event_hub_name).ID()
eventHubNamespace := *receiver.EventHubNameSpace
eventHubName := *receiver.EventHubName
subscriptionId := *receiver.SubscriptionID
if !features.FourPointOhBeta() {
val["event_hub_id"] = eventhubs.NewEventhubID(subscriptionId, resourceGroup, eventHubNamespace, eventHubName).ID()
}
val["subscription_id"], val["event_hub_namespace"], val["event_hub_name"] = subscriptionId, eventHubNamespace, eventHubName
}
if receiver.UseCommonAlertSchema != nil {
val["use_common_alert_schema"] = *receiver.UseCommonAlertSchema
Expand Down
Loading

0 comments on commit 65fc056

Please sign in to comment.