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_monitor_action_group - fix event_hub_receiver when dependent event_hub is not in the same resource group #17335

Merged
merged 5 commits into from
Sep 9, 2022
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
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/2017-04-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 @@ -11,6 +11,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 @@ -22,7 +23,7 @@ import (
)

func resourceMonitorActionGroup() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Create: resourceMonitorActionGroupCreateUpdate,
Read: resourceMonitorActionGroupRead,
Update: resourceMonitorActionGroupCreateUpdate,
Expand Down Expand Up @@ -372,37 +373,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 @@ -442,7 +507,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 @@ -729,28 +794,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 @@ -1022,11 +1099,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