Skip to content

Commit

Permalink
chore(sdk): migrate Azure Application Insights from classic mode to w…
Browse files Browse the repository at this point in the history
…orkspace-based mode (#4891)

Closes #4866

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
garysassano authored Nov 12, 2023
1 parent e4684e4 commit 8eb7e2a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
33 changes: 33 additions & 0 deletions libs/wingsdk/src/target-tf-azure/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Function } from "./function";
import { APP_AZURE_TF_SYMBOL } from "./internal";
import { TestRunner } from "./test-runner";
import { ApplicationInsights } from "../.gen/providers/azurerm/application-insights";
import { LogAnalyticsWorkspace } from "../.gen/providers/azurerm/log-analytics-workspace";
import { AzurermProvider } from "../.gen/providers/azurerm/provider";
import { ResourceGroup } from "../.gen/providers/azurerm/resource-group";
import { ServicePlan } from "../.gen/providers/azurerm/service-plan";
Expand Down Expand Up @@ -35,6 +36,18 @@ const RESOURCEGROUP_NAME_OPTS: NameOptions = {
disallowedRegex: /([^a-zA-Z0-9\-\_\(\)\.]+)/g,
};

/**
* Configuration options for generating a name for Azure Log Analytics Workspace.
*
* - The workspace name must be between 4 and 63 characters.
* - The workspace name can contain only letters, numbers, and hyphens (`"-"`).
* - The hyphen (`"-"`) should not be the first or the last character in the name.
*/
const LOG_ANALYTICS_WORKSPACE_NAME_OPTS: NameOptions = {
maxLen: 63,
disallowedRegex: /([^a-zA-Z0-9\-]+)/g,
};

/**
* StorageAccount names are limited to 24 characters.
* You can only use alphanumeric characters.
Expand Down Expand Up @@ -66,6 +79,7 @@ export class App extends CdktfApp {
private _storageAccount?: StorageAccount;
private _servicePlan?: ServicePlan;
private _applicationInsights?: ApplicationInsights;
private _logAnalyticsWorkspace?: LogAnalyticsWorkspace;
protected readonly testRunner: TestRunner;

constructor(props: AzureAppProps) {
Expand Down Expand Up @@ -110,6 +124,24 @@ export class App extends CdktfApp {
}
}

public get logAnalyticsWorkspace() {
if (!this._logAnalyticsWorkspace) {
this._logAnalyticsWorkspace = new LogAnalyticsWorkspace(
this,
"LogAnalyticsWorkspace",
{
location: this.location,
resourceGroupName: this.resourceGroup.name,
name: ResourceNames.generateName(
this,
LOG_ANALYTICS_WORKSPACE_NAME_OPTS
),
}
);
}
return this._logAnalyticsWorkspace;
}

public get applicationInsights() {
if (!this._applicationInsights) {
this._applicationInsights = new ApplicationInsights(
Expand All @@ -120,6 +152,7 @@ export class App extends CdktfApp {
resourceGroupName: this.resourceGroup.name,
location: this.resourceGroup.location,
applicationType: "web",
workspaceId: this.logAnalyticsWorkspace.id,
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ exports[`function with a bucket binding requiring only read 2`] = `
"location": "\${azurerm_resource_group.ResourceGroup.location}",
"name": "application-insights",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
"workspace_id": "\${azurerm_log_analytics_workspace.LogAnalyticsWorkspace.id}",
},
},
"azurerm_linux_function_app": {
Expand Down Expand Up @@ -59,6 +60,13 @@ exports[`function with a bucket binding requiring only read 2`] = `
"storage_account_name": "\${azurerm_storage_account.StorageAccount.name}",
},
},
"azurerm_log_analytics_workspace": {
"LogAnalyticsWorkspace": {
"location": "East US",
"name": "Default-c82bf964",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
},
},
"azurerm_resource_group": {
"ResourceGroup": {
"location": "East US",
Expand Down Expand Up @@ -144,6 +152,7 @@ exports[`function with a bucket binding requiring read_write 2`] = `
"location": "\${azurerm_resource_group.ResourceGroup.location}",
"name": "application-insights",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
"workspace_id": "\${azurerm_log_analytics_workspace.LogAnalyticsWorkspace.id}",
},
},
"azurerm_linux_function_app": {
Expand Down Expand Up @@ -174,6 +183,13 @@ exports[`function with a bucket binding requiring read_write 2`] = `
"storage_account_name": "\${azurerm_storage_account.StorageAccount.name}",
},
},
"azurerm_log_analytics_workspace": {
"LogAnalyticsWorkspace": {
"location": "East US",
"name": "Default-c82bf964",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
},
},
"azurerm_resource_group": {
"ResourceGroup": {
"location": "East US",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports[`basic function 1`] = `
"location": "\${azurerm_resource_group.ResourceGroup.location}",
"name": "application-insights",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
"workspace_id": "\${azurerm_log_analytics_workspace.LogAnalyticsWorkspace.id}",
},
},
"azurerm_linux_function_app": {
Expand Down Expand Up @@ -41,6 +42,13 @@ exports[`basic function 1`] = `
"storage_account_name": "\${azurerm_storage_account.StorageAccount.name}",
},
},
"azurerm_log_analytics_workspace": {
"LogAnalyticsWorkspace": {
"location": "East US",
"name": "Default-c82bf964",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
},
},
"azurerm_resource_group": {
"ResourceGroup": {
"location": "East US",
Expand Down Expand Up @@ -189,6 +197,14 @@ exports[`basic function 2`] = `
"id": "Handler",
"path": "root/Default/Handler",
},
"LogAnalyticsWorkspace": {
"constructInfo": {
"fqn": "cdktf.TerraformResource",
"version": "0.17.0",
},
"id": "LogAnalyticsWorkspace",
"path": "root/Default/LogAnalyticsWorkspace",
},
"ResourceGroup": {
"constructInfo": {
"fqn": "cdktf.TerraformResource",
Expand Down Expand Up @@ -294,6 +310,7 @@ exports[`basic function with environment variables 1`] = `
"location": "\${azurerm_resource_group.ResourceGroup.location}",
"name": "application-insights",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
"workspace_id": "\${azurerm_log_analytics_workspace.LogAnalyticsWorkspace.id}",
},
},
"azurerm_linux_function_app": {
Expand Down Expand Up @@ -323,6 +340,13 @@ exports[`basic function with environment variables 1`] = `
"storage_account_name": "\${azurerm_storage_account.StorageAccount.name}",
},
},
"azurerm_log_analytics_workspace": {
"LogAnalyticsWorkspace": {
"location": "East US",
"name": "Default-c82bf964",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
},
},
"azurerm_resource_group": {
"ResourceGroup": {
"location": "East US",
Expand Down Expand Up @@ -471,6 +495,14 @@ exports[`basic function with environment variables 2`] = `
"id": "Handler",
"path": "root/Default/Handler",
},
"LogAnalyticsWorkspace": {
"constructInfo": {
"fqn": "cdktf.TerraformResource",
"version": "0.17.0",
},
"id": "LogAnalyticsWorkspace",
"path": "root/Default/LogAnalyticsWorkspace",
},
"ResourceGroup": {
"constructInfo": {
"fqn": "cdktf.TerraformResource",
Expand Down Expand Up @@ -576,6 +608,7 @@ exports[`replace invalid character from function name 1`] = `
"location": "\${azurerm_resource_group.ResourceGroup.location}",
"name": "application-insights",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
"workspace_id": "\${azurerm_log_analytics_workspace.LogAnalyticsWorkspace.id}",
},
},
"azurerm_linux_function_app": {
Expand Down Expand Up @@ -603,6 +636,13 @@ exports[`replace invalid character from function name 1`] = `
"storage_account_name": "\${azurerm_storage_account.StorageAccount.name}",
},
},
"azurerm_log_analytics_workspace": {
"LogAnalyticsWorkspace": {
"location": "East US",
"name": "Default-c82bf964",
"resource_group_name": "\${azurerm_resource_group.ResourceGroup.name}",
},
},
"azurerm_resource_group": {
"ResourceGroup": {
"location": "East US",
Expand Down Expand Up @@ -683,6 +723,14 @@ exports[`replace invalid character from function name 2`] = `
"id": "Handler",
"path": "root/Default/Handler",
},
"LogAnalyticsWorkspace": {
"constructInfo": {
"fqn": "cdktf.TerraformResource",
"version": "0.17.0",
},
"id": "LogAnalyticsWorkspace",
"path": "root/Default/LogAnalyticsWorkspace",
},
"ResourceGroup": {
"constructInfo": {
"fqn": "cdktf.TerraformResource",
Expand Down
3 changes: 2 additions & 1 deletion libs/wingsdk/test/target-tf-azure/captures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test("function with a bucket binding requiring read_write", () => {
expect(tfResourcesOf(output)).toEqual([
"azurerm_application_insights",
"azurerm_linux_function_app",
"azurerm_log_analytics_workspace",
"azurerm_resource_group",
"azurerm_role_assignment",
"azurerm_service_plan",
Expand Down Expand Up @@ -97,11 +98,11 @@ test("function with a bucket binding requiring only read", () => {
}
)
).toEqual(false);

expect(sanitizeCode(inflight._toInflight())).toMatchSnapshot();
expect(tfResourcesOf(output)).toEqual([
"azurerm_application_insights",
"azurerm_linux_function_app",
"azurerm_log_analytics_workspace",
"azurerm_resource_group",
"azurerm_role_assignment",
"azurerm_service_plan",
Expand Down
1 change: 1 addition & 0 deletions libs/wingsdk/test/target-tf-azure/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test("basic function", () => {
expect(tfResourcesOf(output)).toEqual([
"azurerm_application_insights",
"azurerm_linux_function_app", // function app
"azurerm_log_analytics_workspace",
"azurerm_resource_group", // resource group
"azurerm_role_assignment", // role assignment for function app
"azurerm_service_plan", // service plan for function app
Expand Down

0 comments on commit 8eb7e2a

Please sign in to comment.