Skip to content

Commit

Permalink
azurerm_container_app_environment - Make `log_analytics_workspace_i…
Browse files Browse the repository at this point in the history
…d` an optional argument. (#22733)

* make log_analytics_workspace_id an optional argument so users could save cost

* update document

* fix terrafmt lint issue

* update test code as code review suggested, adjust document

* fix terrafmt issue

* removing duplicate basic test

---------

Co-authored-by: stephybun <[email protected]>
  • Loading branch information
lonegunmanb and stephybun authored Aug 9, 2023
1 parent a718559 commit d00e29a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
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

0 comments on commit d00e29a

Please sign in to comment.