Skip to content

Commit

Permalink
get log analytics workspace name
Browse files Browse the repository at this point in the history
Signed-off-by: ziyeqf <[email protected]>
  • Loading branch information
ziyeqf committed Sep 19, 2023
1 parent a8aef80 commit bd4eb39
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/containerapps/2023-05-01/managedenvironments"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -155,7 +157,12 @@ func (r ContainerAppEnvironmentDataSource) Read() sdk.ResourceFunc {
}

if appsLogs := props.AppLogsConfiguration; appsLogs != nil && appsLogs.LogAnalyticsConfiguration != nil {
environment.LogAnalyticsWorkspaceName = pointer.From(appsLogs.LogAnalyticsConfiguration.CustomerId)
lawClient := metadata.Client.LogAnalytics.SharedKeyWorkspacesClient
lawName, err := findLogAnalyticsWorkspaceName(ctx, lawClient, subscriptionId, pointer.From(appsLogs.LogAnalyticsConfiguration.CustomerId))
if err != nil {
return fmt.Errorf("retrieving Log Analytics Workspace: %+v", err)
}
environment.LogAnalyticsWorkspaceName = lawName
}

environment.StaticIP = pointer.From(props.StaticIP)
Expand All @@ -169,3 +176,28 @@ func (r ContainerAppEnvironmentDataSource) Read() sdk.ResourceFunc {
},
}
}

func findLogAnalyticsWorkspaceName(ctx context.Context, client *workspaces.WorkspacesClient, subscriptionId, targetCustomerId string) (string, error) {
parsedSubscriptionId := commonids.NewSubscriptionID(subscriptionId)

resp, err := client.List(ctx, parsedSubscriptionId)
if err != nil {
return "", err
}

if resp.Model == nil {
return "", fmt.Errorf("model was nil")
}

if resp.Model.Value == nil {
return "", fmt.Errorf("value was nil")
}

for _, law := range *resp.Model.Value {
if law.Properties != nil && law.Properties.CustomerId != nil && *law.Properties.CustomerId == targetCustomerId && law.Name != nil {
return *law.Name, nil
}
}

return "", fmt.Errorf("no matching workspace found")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccContainerAppEnvironmentDataSource_basic(t *testing.T) {
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("log_analytics_workspace_name").IsSet(),
check.That(data.ResourceName).Key("log_analytics_workspace_name").HasValue(fmt.Sprintf("acctestLAW-%d", data.RandomInteger)),
check.That(data.ResourceName).Key("location").IsSet(),
check.That(data.ResourceName).Key("internal_load_balancer_enabled").HasValue("true"),
),
Expand Down

0 comments on commit bd4eb39

Please sign in to comment.