Skip to content

Commit

Permalink
add tsts for customizeDiff
Browse files Browse the repository at this point in the history
fix crash in read
fixup customizeDiff logic
add consts to remove magic strings
  • Loading branch information
jackofallops committed Dec 11, 2024
1 parent 6a93346 commit 6a9693c
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 11 deletions.
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 Down Expand Up @@ -97,11 +102,14 @@ func (r ContainerAppEnvironmentResource) Arguments() map[string]*pluginsdk.Schem
},

"logs_destination": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"log-analytics", "azure-monitor"}, false),
Description: "The destination for the application logs. Possible values are `log-analytics` or `azure-monitor`.",
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": {
Expand Down Expand Up @@ -353,7 +361,7 @@ func (r ContainerAppEnvironmentResource) Read() sdk.ResourceFunc {
}

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

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

if env.LogsDestination == "log-analytics" && env.LogAnalyticsWorkspaceId == "" {
return fmt.Errorf("`log_analytics_workspace_id` must be set when `logs_destination` is set to `log-analytics`")
}
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`")
}

if env.LogAnalyticsWorkspaceId != "" && env.LogsDestination != "" && env.LogsDestination != "log-analytics" {
return fmt.Errorf("`log_analytics_workspace_id` can only be set when `logs_destination` is set to `log-analytics`")
}

Check failure on line 551 in internal/services/containerapps/container_app_environment_resource.go

View workflow job for this annotation

GitHub Actions / golint

unnecessary trailing newline (whitespace)

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 @@ -283,6 +308,105 @@ resource "azurerm_monitor_diagnostic_setting" "test" {
`, 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
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) completeWithoutWorkloadProfile(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down

0 comments on commit 6a9693c

Please sign in to comment.