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

added application insights web test resource #5

Merged
merged 2 commits into from
Mar 14, 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
31 changes: 21 additions & 10 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,27 @@ usage: |-
```hcl
# Basic
module "app-insights" {
source = "terraform/app-insights/azure"
version = "1.0.0"
name = "app"
environment = "test"
label_order = ["name", "environment"]
enabled = true
location = module.resource_group.resource_group_location
resource_group_name = module.resource_group.resource_group_name
# workspace_id = module.log-analytics.workspace_id
application_insights_config = "web"
source = "terraform/app-insights/azure"
version = "1.0.0"
name = "app"
environment = "test"
label_order = ["name", "environment", ]
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
# workspace_id = module.log-analytics.workspace_id
application_type = "web"
daily_data_cap_in_gb = 30

##web test
web_test_enable = false
kind = "ping"
frequency = 300
timeout = 60
monitored_enabled = true
retry_enabled = true
geo_locations = ["us-ca-sjc-azr", "us-tx-sn1-azr", "us-il-ch1-azr", "us-va-ash-azr", "us-fl-mia-edge"]
list_of_test_urls = ["https://www.google.com", ]
web_test_name = ["google", ]
}
```

Expand Down
23 changes: 17 additions & 6 deletions _example/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@ module "resource_group" {
module "application-insights" {
source = "../"

name = "test-app"
name = "app"
environment = "test"
label_order = ["name", "environment"]
enabled = true
location = module.resource_group.resource_group_location
label_order = ["name", "environment", ]
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
# workspace_id = module.log-analytics.workspace_id
application_insights_config = "web"
}
application_type = "web"
daily_data_cap_in_gb = 30

##web test
web_test_enable = true
kind = "ping"
frequency = 300
timeout = 60
monitored_enabled = true
retry_enabled = true
geo_locations = ["us-ca-sjc-azr", "us-tx-sn1-azr", "us-il-ch1-azr", "us-va-ash-azr", "us-fl-mia-edge"]
list_of_test_urls = ["https://www.google.com", ]
web_test_name = ["google", ]
}
82 changes: 64 additions & 18 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
## Managed By : CloudDrove
## Copyright @ CloudDrove. All Right Reserved.

module "labels" {

source = "clouddrove/labels/azure"
version = "1.0.0"

source = "clouddrove/labels/azure"
version = "1.0.0"
name = var.name
environment = var.environment
managedby = var.managedby
label_order = var.label_order
repository = var.repository
}

#Module : APPLICATION INSIGHTS
#Description : Terraform resource to create a application insights for Azure Environment.
locals {
header = var.header
footer = var.footer

test_header = format(local.header, random_uuid.parent.result, var.description)
replace_body = replace(var.test_body, "PARSEDEPS", var.parse_deps)
}

resource "random_uuid" "parent" {}

resource "random_uuid" "test_guids" {
count = var.enabled && var.web_test_enable ? length(var.list_of_test_urls) : 0
keepers = {
url = var.list_of_test_urls[count.index]
}
}

resource "azurerm_application_insights" "application_insights" {
count = var.enabled == true && var.application_insights_config != null ? 1 : 0

name = lower(format("appi-%s", var.name))
resource_group_name = var.resource_group_name
location = var.location
workspace_id = var.workspace_id
application_type = var.application_insights_config
tags = module.labels.tags
}
count = var.enabled ? 1 : 0
name = format("%s-app-insights", module.labels.id)
location = var.location
resource_group_name = var.resource_group_name
application_type = var.application_type
retention_in_days = var.retention_in_days
sampling_percentage = var.sampling_percentage
daily_data_cap_in_gb = var.daily_data_cap_in_gb
daily_data_cap_notifications_disabled = var.daily_data_cap_notifications_disabled
disable_ip_masking = var.disable_ip_masking
workspace_id = var.workspace_id
local_authentication_disabled = var.local_authentication_disabled
internet_ingestion_enabled = var.internet_ingestion_enabled
internet_query_enabled = var.internet_query_enable
force_customer_storage_for_profiler = var.force_customer_storage_for_profiler
tags = module.labels.tags
}


resource "azurerm_application_insights_web_test" "main" {
count = var.enabled && var.web_test_enable ? length(var.list_of_test_urls) : 0
name = element(var.web_test_name, count.index)
location = join("", azurerm_application_insights.application_insights.*.location)
resource_group_name = var.resource_group_name
application_insights_id = join("", azurerm_application_insights.application_insights.*.id)
kind = var.kind
frequency = var.frequency
timeout = var.timeout
enabled = var.monitored_enabled
retry_enabled = var.retry_enabled
geo_locations = var.geo_locations
description = var.description

configuration = format("%s%s%s",
local.test_header,
join("", formatlist(local.replace_body, random_uuid.test_guids.*.result[count.index], random_uuid.test_guids.*.keepers.url[count.index])),
local.footer)

lifecycle {
ignore_changes = [
tags,
]
}

}
181 changes: 153 additions & 28 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ variable "name" {
description = "Name (e.g. `app` or `cluster`)."
}

variable "application" {
variable "environment" {
type = string
default = ""
description = "Application (e.g. `cd` or `clouddrove`)."
description = "Environment (e.g. `prod`, `dev`, `staging`)."
}

variable "environment" {
variable "repository" {
type = string
default = ""
description = "Environment (e.g. `prod`, `dev`, `staging`)."
description = "Terraform current module repo"
}

variable "tags" {
type = map(any)
default = {}
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)."
variable "business_unit" {
type = string
default = "Corp"
description = "Top-level division of your company that owns the subscription or workload that the resource belongs to. In smaller organizations, this tag might represent a single corporate or shared top-level organizational element."
}

variable "label_order" {
type = list(any)
default = []
description = "Label order, e.g. sequence of application name and environment `name`,`environment`,'attribute' [`webserver`,`qa`,`devops`,`public`,] ."
}

variable "managedby" {
Expand All @@ -30,47 +36,166 @@ variable "managedby" {
description = "ManagedBy, eg 'CloudDrove' or 'AnmolNagpal'."
}

variable "label_order" {
type = list(any)
variable "attributes" {
type = list(string)
default = []
description = "Label order, e.g. `name`,`application`."
description = "Additional attributes (e.g. `1`)."
}
variable "repository" {

variable "extra_tags" {
type = map(string)
default = {}
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)."
}

variable "resource_group_name" {
type = string
default = ""
description = "Terraform current module repo"
description = "The name of the resource group in which to create the network security group."
}

## Common Variables
variable "tags" {
type = map(string)
default = {}
description = "A mapping of tags to assign to the resource."
}

variable "enabled" {
type = bool
description = "Set to false to prevent the module from creating any resources."
default = true
description = "Flag to control the module creation."
}

variable "resource_group_name" {

variable "application_type" {
type = string
default = ""
description = "The name of the resource group in which to create the virtual network."
description = "Required) Specifies the type of Application Insights to create. Valid values are ios for iOS, java for Java web, MobileCenter for App Center, Node.JS for Node.js, other for General, phone for Windows Phone, store for Windows Store and web for ASP.NET. Please note these values are case sensitive; unmatched values are treated as ASP.NET by Azure. Changing this forces a new resource to be created."
}

variable "location" {
type = string
default = ""
description = "Location where resource should be created."

variable "retention_in_days" {
type = number
default = 90
description = "Specifies the retention period in days. Possible values are 30, 60, 90, 120, 180, 270, 365, 550 or 730. Defaults to 90."
}

# Application Insights
variable "sampling_percentage" {
type = number
default = 100
description = " Specifies the percentage of the data produced by the monitored application that is sampled for Application Insights telemetry."
}

variable "application_insights_config" {
variable "daily_data_cap_in_gb" {
type = number
default = null
description = "Specifies the Application Insights component daily data volume cap in GB."
}

variable "web_test_enable" {
type = bool
default = false
}
variable "kind" {
type = string
default = ""
}
variable "frequency" {
default = 300
description = "Interval in seconds between test runs for this WebTest. Default is 300."
}
variable "timeout" {
default = 30
description = "Seconds until this WebTest will timeout and fail. Default is 30."
}
variable "monitored_enabled" {
type = bool
default = false
}
variable "list_of_test_locations" {
type = list(string)
default = ["us-ca-sjc-azr", "us-tx-sn1-azr", "us-il-ch1-azr", "us-va-ash-azr", "us-fl-mia-edge"]
description = "List of Azure locations that will perform the specified web tests. Default is set to 5 US locations. Microsoft recommendation is a minimum of 5 test locations with an alert threshold of N-2. Ref: https://docs.microsoft.com/en-us/azure/azure-monitor/app/monitor-web-app-availability"
}
variable "retry_enabled" {
type = bool
default = true
description = "Allow for retries should this WebTest fail."
}
variable "description" {
type = string
default = "web"
description = "Specifies the type of Application Insights to create. Valid values are ios for iOS, java for Java web, MobileCenter for App Center, Node.JS for Node.js, other for General, phone for Windows Phone, store for Windows Store and web for ASP.NET. Please note these values are case sensitive; unmatched values are treated as ASP.NET by Azure. Changing this forces a new resource to be created."
default = ""
description = "Purpose/user defined descriptive test for this WebTest."
}
variable "test_body" {
default = "<Request Method=\"GET\" Guid=\"%s\" Version=\"1.1\" Url=\"%s\" ThinkTime=\"0\" Timeout=\"300\" ParseDependentRequests=\"PARSEDEPS\" FollowRedirects=\"True\" RecordResult=\"True\" Cache=\"True\" ResponseTimeGoal=\"0\" Encoding=\"utf-8\" ExpectedHttpStatusCode=\"200\" ExpectedResponseUrl=\"\" ReportingName=\"\" IgnoreHttpStatusCode=\"False\" />"
description = "WebTest XML Request body. If overridden, make sure to retain all the string format() parameters needed by the local variable calculations."
}
variable "parse_deps" {
default = "false"
description = "Retrieve resources that are linked to by the test URL as part of the web test. Valid values are \"True\" or \"False\". Default value is \"False\"."
}

variable "location" {
type = string
default = ""
description = "(Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created."
}
variable "daily_data_cap_notifications_disabled" {
type = bool
default = false
description = "Optional) Specifies if a notification email will be send when the daily data volume cap is met."
}
variable "disable_ip_masking" {
type = bool
default = false
description = "By default the real client IP is masked as 0.0.0.0 in the logs. Use this argument to disable masking and log the real client IP. Defaults to false."
}
variable "workspace_id" {
type = string
default = null
description = "Specifies the id of a log analytics workspace resource. Changing this forces a new resource to be created."
}
description = "(Optional) Specifies the id of a log analytics workspace resource. Changing this forces a new resource to be created."
}
variable "local_authentication_disabled" {
type = bool
default = false
description = "(Optional) Disable Non-Azure AD based Auth. Defaults to false."
}
variable "internet_ingestion_enabled" {
type = bool
default = true
description = " (Optional) Should the Application Insights component support ingestion over the Public Internet? Defaults to true."
}
variable "internet_query_enable" {
type = bool
default = true
description = "(Optional) Should the Application Insights component support querying over the Public Internet? Defaults to true."
}
variable "force_customer_storage_for_profiler" {
type = bool
default = false
description = "Should the Application Insights component force users to create their own storage account for profiling? Defaults to false."
}
variable "web_test_name" {
type = list(string)
default = []
}
variable "geo_locations" {
type = list(string)
default = []
description = "Specifies a list of where to physically run the tests from to give global coverage for accessibility of your application."
}

variable "list_of_test_urls" {
type = list(string)
default = []
description = "List of URLs to put in the availability tests. Example: [\"https://test1.example.com\", \"https://test2.example.com/app\"]"
}

variable "header" {
type = string
default = "<WebTest Name=\"WebTest1\" Id=\"%s\" Enabled=\"True\" CssProjectStructure=\"\" CssIteration=\"\" Timeout=\"0\" WorkItemIds=\"\" xmlns=\"http://microsoft.com/schemas/VisualStudio/TeamTest/2010\" Description=\"%s\" CredentialUserName=\"\" CredentialPassword=\"\" PreAuthenticate=\"True\" Proxy=\"default\" StopOnError=\"False\" RecordedResultFile=\"\" ResultsLocale=\"\"><Items>"
}
variable "footer" {
type = string
default = "</Items></WebTest>"
}