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 - Make log_analytics_workspace_id an optional argument. #22733

Merged
merged 6 commits into from
Aug 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (r ContainerAppEnvironmentResource) Arguments() map[string]*pluginsdk.Schem

"log_analytics_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: workspaces.ValidateWorkspaceID,
Description: "The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to.",
Expand Down Expand Up @@ -156,49 +156,50 @@ func (r ContainerAppEnvironmentResource) Create() sdk.ResourceFunc {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

logAnalyticsId, err := workspaces.ParseWorkspaceID(containerAppEnvironment.LogAnalyticsWorkspaceId)
if err != nil {
return err
}

workspace, err := logAnalyticsClient.Get(ctx, *logAnalyticsId)
if err != nil {
return fmt.Errorf("retrieving %s for %s: %+v", logAnalyticsId, id, err)
}

if workspace.Model == nil || workspace.Model.Properties == nil {
return fmt.Errorf("reading customer ID from %s", logAnalyticsId)
}

if workspace.Model.Properties.CustomerId == nil {
return fmt.Errorf("reading customer ID from %s, `customer_id` is nil", logAnalyticsId)
}

keys, err := logAnalyticsClient.SharedKeysGetSharedKeys(ctx, *logAnalyticsId)
if err != nil {
return fmt.Errorf("retrieving access keys to %s for %s: %+v", logAnalyticsId, id, err)
}

if keys.Model.PrimarySharedKey == nil {
return fmt.Errorf("reading shared key for %s in %s", logAnalyticsId, id)
}

managedEnvironment := managedenvironments.ManagedEnvironment{
Location: containerAppEnvironment.Location,
Name: pointer.To(containerAppEnvironment.Name),
Properties: &managedenvironments.ManagedEnvironmentProperties{
AppLogsConfiguration: &managedenvironments.AppLogsConfiguration{
Destination: pointer.To("log-analytics"),
LogAnalyticsConfiguration: &managedenvironments.LogAnalyticsConfiguration{
CustomerId: workspace.Model.Properties.CustomerId,
SharedKey: keys.Model.PrimarySharedKey,
},
},
VnetConfiguration: &managedenvironments.VnetConfiguration{},
},
Tags: tags.Expand(containerAppEnvironment.Tags),
}

if containerAppEnvironment.LogAnalyticsWorkspaceId != "" {
logAnalyticsId, err := workspaces.ParseWorkspaceID(containerAppEnvironment.LogAnalyticsWorkspaceId)
if err != nil {
return err
}

workspace, err := logAnalyticsClient.Get(ctx, *logAnalyticsId)
if err != nil {
return fmt.Errorf("retrieving %s for %s: %+v", logAnalyticsId, id, err)
}

if workspace.Model == nil || workspace.Model.Properties == nil {
return fmt.Errorf("reading customer ID from %s", logAnalyticsId)
}

if workspace.Model.Properties.CustomerId == nil {
return fmt.Errorf("reading customer ID from %s, `customer_id` is nil", logAnalyticsId)
}

keys, err := logAnalyticsClient.SharedKeysGetSharedKeys(ctx, *logAnalyticsId)
if err != nil {
return fmt.Errorf("retrieving access keys to %s for %s: %+v", logAnalyticsId, id, err)
}
if keys.Model.PrimarySharedKey == nil {
return fmt.Errorf("reading shared key for %s in %s", logAnalyticsId, id)
}
managedEnvironment.Properties.AppLogsConfiguration = &managedenvironments.AppLogsConfiguration{
Destination: pointer.To("log-analytics"),
LogAnalyticsConfiguration: &managedenvironments.LogAnalyticsConfiguration{
CustomerId: workspace.Model.Properties.CustomerId,
SharedKey: keys.Model.PrimarySharedKey,
},
}
}

if containerAppEnvironment.InfrastructureSubnetId != "" {
managedEnvironment.Properties.VnetConfiguration.InfrastructureSubnetId = pointer.To(containerAppEnvironment.InfrastructureSubnetId)
managedEnvironment.Properties.VnetConfiguration.Internal = pointer.To(containerAppEnvironment.InternalLoadBalancerEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAccContainerAppEnvironment_basic(t *testing.T) {
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("log_analytics_workspace_id"),
data.ImportStep(),
})
}

Expand Down Expand Up @@ -112,24 +112,23 @@ 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
}
`, r.template(data), data.RandomInteger)
}

func (r ContainerAppEnvironmentResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`


%[1]s

resource "azurerm_container_app_environment" "import" {
name = azurerm_container_app_environment.test.name
resource_group_name = azurerm_container_app_environment.test.resource_group_name
location = azurerm_container_app_environment.test.location
log_analytics_workspace_id = azurerm_container_app_environment.test.log_analytics_workspace_id
name = azurerm_container_app_environment.test.name
resource_group_name = azurerm_container_app_environment.test.resource_group_name
location = azurerm_container_app_environment.test.location
}
`, r.basic(data), data.RandomInteger)
}
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/container_app_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ The following arguments are supported:

* `location` - (Required) Specifies the supported Azure location where the Container App Environment is to exist. Changing this forces a new resource to be created.

* `log_analytics_workspace_id` - (Required) The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to. Changing this forces a new resource to be created.

---

* `infrastructure_subnet_id` - (Optional) The existing Subnet to use for the Container Apps Control Plane. Changing this forces a new resource to be created.
Expand All @@ -56,6 +54,8 @@ 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. Changing this forces a new resource to be created.

* `tags` - (Optional) A mapping of tags to assign to the resource.

## Attributes Reference
Expand Down