Skip to content

Commit

Permalink
Merge pull request #23080 from myc2h6o/app_dapr
Browse files Browse the repository at this point in the history
`azurerm_container_app_environment` - support `dapr_application_insights_connection_string`
  • Loading branch information
tombuildsstuff authored Aug 25, 2023
2 parents fd5ab7d + bf132a5 commit 5ca4398
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containerapps/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type ContainerAppEnvironmentResource struct{}

type ContainerAppEnvironmentModel struct {
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
LogAnalyticsWorkspaceId string `tfschema:"log_analytics_workspace_id"`
InfrastructureSubnetId string `tfschema:"infrastructure_subnet_id"`
InternalLoadBalancerEnabled bool `tfschema:"internal_load_balancer_enabled"`
Tags map[string]interface{} `tfschema:"tags"`
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
DaprApplicationInsightsConnectionString string `tfschema:"dapr_application_insights_connection_string"`
LogAnalyticsWorkspaceId string `tfschema:"log_analytics_workspace_id"`
InfrastructureSubnetId string `tfschema:"infrastructure_subnet_id"`
InternalLoadBalancerEnabled bool `tfschema:"internal_load_balancer_enabled"`
Tags map[string]interface{} `tfschema:"tags"`

DefaultDomain string `tfschema:"default_domain"`
DockerBridgeCidr string `tfschema:"docker_bridge_cidr"`
Expand Down Expand Up @@ -67,6 +69,15 @@ func (r ContainerAppEnvironmentResource) Arguments() map[string]*pluginsdk.Schem

"resource_group_name": commonschema.ResourceGroupName(),

"dapr_application_insights_connection_string": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "Application Insights connection string used by Dapr to export Service to Service communication telemetry.",
},

"log_analytics_workspace_id": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -165,6 +176,10 @@ func (r ContainerAppEnvironmentResource) Create() sdk.ResourceFunc {
Tags: tags.Expand(containerAppEnvironment.Tags),
}

if containerAppEnvironment.DaprApplicationInsightsConnectionString != "" {
managedEnvironment.Properties.DaprAIConnectionString = pointer.To(containerAppEnvironment.DaprApplicationInsightsConnectionString)
}

if containerAppEnvironment.LogAnalyticsWorkspaceId != "" {
logAnalyticsId, err := workspaces.ParseWorkspaceID(containerAppEnvironment.LogAnalyticsWorkspaceId)
if err != nil {
Expand Down Expand Up @@ -255,6 +270,11 @@ func (r ContainerAppEnvironmentResource) Read() sdk.ResourceFunc {
}
}

// `dapr_application_insights_connection_string` is sensitive and not returned by API
if v := metadata.ResourceData.Get("dapr_application_insights_connection_string").(string); v != "" {
state.DaprApplicationInsightsConnectionString = v
}

// Reading in log_analytics_workspace_id is not possible, so reading from config. Import will need to ignore_changes unfortunately
if v := metadata.ResourceData.Get("log_analytics_workspace_id").(string); v != "" {
state.LogAnalyticsWorkspaceId = v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ func TestAccContainerAppEnvironment_completeUpdate(t *testing.T) {
})
}

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

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

func (r ContainerAppEnvironmentResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := managedenvironments.ParseManagedEnvironmentID(state.ID)
if err != nil {
Expand Down Expand Up @@ -184,6 +199,31 @@ resource "azurerm_container_app_environment" "test" {
`, r.templateVNet(data), data.RandomInteger)
}

func (r ContainerAppEnvironmentResource) daprApplicationInsightsConnectionString(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
%[1]s
resource "azurerm_application_insights" "test" {
name = "acctestappinsights-%[2]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
application_type = "web"
}
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
dapr_application_insights_connection_string = azurerm_application_insights.test.connection_string
}
`, r.template(data), data.RandomInteger)
}

func (r ContainerAppEnvironmentResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/container_app_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The following arguments are supported:

---

* `dapr_application_insights_connection_string` - (Optional) Application Insights connection string used by Dapr to export Service to Service communication telemetry.

* `infrastructure_subnet_id` - (Optional) The existing Subnet to use for the Container Apps Control Plane. Changing this forces a new resource to be created.

~> **NOTE:** The Subnet must have a `/21` or larger address space.
Expand Down

0 comments on commit 5ca4398

Please sign in to comment.