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_container_app_environment - add support for Azure Monitor as a log destination #26047

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

const (
LogsDestinationLogAnalytics string = "log-analytics"
LogsDestinationAzureMonitor string = "azure-monitor"
)

type ContainerAppEnvironmentResource struct{}

type ContainerAppEnvironmentModel struct {
Expand All @@ -32,6 +37,7 @@ type ContainerAppEnvironmentModel struct {
Location string `tfschema:"location"`
DaprApplicationInsightsConnectionString string `tfschema:"dapr_application_insights_connection_string"`
LogAnalyticsWorkspaceId string `tfschema:"log_analytics_workspace_id"`
LogsDestination string `tfschema:"logs_destination"`
InfrastructureSubnetId string `tfschema:"infrastructure_subnet_id"`
InternalLoadBalancerEnabled bool `tfschema:"internal_load_balancer_enabled"`
ZoneRedundant bool `tfschema:"zone_redundancy_enabled"`
Expand Down Expand Up @@ -95,6 +101,17 @@ func (r ContainerAppEnvironmentResource) Arguments() map[string]*pluginsdk.Schem
Description: "The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to.",
},

"logs_destination": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
LogsDestinationLogAnalytics,
LogsDestinationAzureMonitor,
}, false),
Description: "The destination for the application logs. Possible values are `log-analytics` or `azure-monitor`.",
},

"infrastructure_resource_group_name": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -248,6 +265,12 @@ func (r ContainerAppEnvironmentResource) Create() sdk.ResourceFunc {
managedEnvironment.Properties.InfrastructureResourceGroup = pointer.To(containerAppEnvironment.InfrastructureResourceGroup)
}

if containerAppEnvironment.LogsDestination != "" && containerAppEnvironment.LogsDestination != "log-analytics" {
managedEnvironment.Properties.AppLogsConfiguration = &managedenvironments.AppLogsConfiguration{
Destination: pointer.To(containerAppEnvironment.LogsDestination),
}
}

if containerAppEnvironment.LogAnalyticsWorkspaceId != "" {
logAnalyticsId, err := workspaces.ParseWorkspaceID(containerAppEnvironment.LogAnalyticsWorkspaceId)
if err != nil {
Expand Down Expand Up @@ -337,6 +360,10 @@ func (r ContainerAppEnvironmentResource) Read() sdk.ResourceFunc {
state.PlatformReservedDnsIP = pointer.From(vnet.PlatformReservedDnsIP)
}

if appLogsConfig := props.AppLogsConfiguration; appLogsConfig != nil {
state.LogsDestination = pointer.From(appLogsConfig.Destination)
}

state.CustomDomainVerificationId = pointer.From(props.CustomDomainConfiguration.CustomDomainVerificationId)
state.ZoneRedundant = pointer.From(props.ZoneRedundant)
state.StaticIP = pointer.From(props.StaticIP)
Expand Down Expand Up @@ -511,6 +538,16 @@ func (r ContainerAppEnvironmentResource) CustomizeDiff() sdk.ResourceFunc {
}
}

switch env.LogsDestination {
case LogsDestinationLogAnalytics:
if env.LogAnalyticsWorkspaceId == "" {
return fmt.Errorf("`log_analytics_workspace_id` must be set when `logs_destination` is set to `log-analytics`")
}
default:
if env.LogAnalyticsWorkspaceId != "" {
return fmt.Errorf("`log_analytics_workspace_id` can only be set when `logs_destination` is set to `log-analytics`")
}
}
return nil
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package containerapps_test
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
Expand Down Expand Up @@ -79,6 +80,30 @@ func TestAccContainerAppEnvironment_complete(t *testing.T) {
})
}

func TestAccContainerAppEnvironment_logsDestinationWithoutWorkspaceShouldFail(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_environment", "test")
r := ContainerAppEnvironmentResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.logsDestinationWithoutWorkspaceShouldFail(data),
ExpectError: regexp.MustCompile("`log_analytics_workspace_id` must be set when `logs_destination` is set to `log-analytics`"),
},
})
}

func TestAccContainerAppEnvironment_logsAzureMonitorWithWorkspaceShouldFail(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_environment", "test")
r := ContainerAppEnvironmentResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.logsAzureMonitorWithWorkspaceShouldFail(data),
ExpectError: regexp.MustCompile("`log_analytics_workspace_id` can only be set when `logs_destination` is set to `log-analytics`"),
},
})
}

func TestAccContainerAppEnvironment_updateWorkloadProfile(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_app_environment", "test")
r := ContainerAppEnvironmentResource{}
Expand Down Expand Up @@ -242,10 +267,109 @@ provider "azurerm" {

%[1]s

resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
logs_destination = "azure-monitor"
infrastructure_subnet_id = azurerm_subnet.control.id

internal_load_balancer_enabled = true
zone_redundancy_enabled = true
mutual_tls_enabled = true

workload_profile {
maximum_count = 3
minimum_count = 0
name = "D4-01"
workload_profile_type = "D4"
}

tags = {
Foo = "Bar"
secret = "sauce"
}
}

resource "azurerm_monitor_diagnostic_setting" "test" {
name = "diagnostics"
target_resource_id = azurerm_container_app_environment.test.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id

enabled_log {
category_group = "allLogs"
}

metric {
category = "AllMetrics"
enabled = true
}
}
`, r.templateVNet(data), data.RandomInteger)
}

func (r ContainerAppEnvironmentResource) logsDestinationWithoutWorkspaceShouldFail(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%[1]s

resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
logs_destination = "log-analytics"
infrastructure_subnet_id = azurerm_subnet.control.id

internal_load_balancer_enabled = true
zone_redundancy_enabled = true
mutual_tls_enabled = true

workload_profile {
maximum_count = 3
minimum_count = 0
name = "D4-01"
workload_profile_type = "D4"
}

tags = {
Foo = "Bar"
secret = "sauce"
}
}

resource "azurerm_monitor_diagnostic_setting" "test" {
name = "diagnostics"
target_resource_id = azurerm_container_app_environment.test.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id

enabled_log {
category_group = "allLogs"
}

metric {
category = "AllMetrics"
enabled = true
}
}
`, r.templateVNet(data), data.RandomInteger)
}

func (r ContainerAppEnvironmentResource) logsAzureMonitorWithWorkspaceShouldFail(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%[1]s

resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
logs_destination = "azure-monitor"
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
infrastructure_subnet_id = azurerm_subnet.control.id

Expand All @@ -265,6 +389,21 @@ resource "azurerm_container_app_environment" "test" {
secret = "sauce"
}
}

resource "azurerm_monitor_diagnostic_setting" "test" {
name = "diagnostics"
target_resource_id = azurerm_container_app_environment.test.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id

enabled_log {
category_group = "allLogs"
}

metric {
category = "AllMetrics"
enabled = true
}
}
`, r.templateVNet(data), data.RandomInteger)
}

Expand Down Expand Up @@ -303,10 +442,9 @@ provider "azurerm" {
%[1]s

resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location

workload_profile {
name = "Consumption"
Expand Down Expand Up @@ -336,7 +474,8 @@ resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
log_analytics_workspace_id = azurerm_log_analytics_workspace.second.id
logs_destination = "log-analytics"
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
infrastructure_subnet_id = azurerm_subnet.control.id

internal_load_balancer_enabled = true
Expand Down Expand Up @@ -366,11 +505,10 @@ provider "azurerm" {
%[1]s

resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
infrastructure_subnet_id = azurerm_subnet.control.id
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
infrastructure_subnet_id = azurerm_subnet.control.id

internal_load_balancer_enabled = true
zone_redundancy_enabled = true
Expand Down Expand Up @@ -416,7 +554,6 @@ resource "azurerm_container_app_environment" "test" {
name = "acctest-CAEnv%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
infrastructure_subnet_id = azurerm_subnet.control.id
zone_redundancy_enabled = true
internal_load_balancer_enabled = true
Expand Down
7 changes: 6 additions & 1 deletion website/docs/r/container_app_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ resource "azurerm_container_app_environment" "example" {
name = "my-environment"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
logs_destination = "log-analytics"
log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id
}
```
Expand Down Expand Up @@ -64,7 +65,11 @@ The following arguments are supported:

~> **Note:** can only be set to `true` if `infrastructure_subnet_id` is specified.

* `log_analytics_workspace_id` - (Optional) The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to.
* `log_analytics_workspace_id` - (Optional) The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to.

~> **Note:** required if `logs_destination` is set to `log-analytics`.

* `logs_destination` - (Optional) Where the application logs will be saved for this Container Apps Managed Environment. Options are `log-analytics` or `azure-monitor`.

* `workload_profile` - (Optional) The profile of the workload to scope the container app execution. A `workload_profile` block as defined below.

Expand Down
Loading