Skip to content

Commit

Permalink
Refactor module structure (#12)
Browse files Browse the repository at this point in the history
* refactor: apply common module structure

* Refactor: Set the resource group name as input variable

* feat: Add module use examples
  • Loading branch information
regiluze authored Aug 10, 2021
1 parent fea1db9 commit e5cb1df
Show file tree
Hide file tree
Showing 33 changed files with 117 additions and 53 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ module "cloud_vision_azure" {

| Name | Source | Version |
|------|--------|---------|
| <a name="module_cloud_connector"></a> [cloud\_connector](#module\_cloud\_connector) | ./modules/cloud-connector | |
| <a name="module_cloud_connector"></a> [cloud\_connector](#module\_cloud\_connector) | ./modules/services/cloud-connector | |
| <a name="module_infrastructure_eventhub"></a> [infrastructure\_eventhub](#module\_infrastructure\_eventhub) | ./modules/infrastructure/eventhub | |

## Resources

Expand All @@ -47,9 +48,10 @@ module "cloud_vision_azure" {
| <a name="input_cloudconnector_deploy"></a> [cloudconnector\_deploy](#input\_cloudconnector\_deploy) | Whether to deploy or not CloudConnector | `bool` | `true` | no |
| <a name="input_location"></a> [location](#input\_location) | Zone where the stack will be deployed | `string` | `"centralus"` | no |
| <a name="input_naming_prefix"></a> [naming\_prefix](#input\_naming\_prefix) | Prefix for resource names. Use the default unless you need to install multiple instances, and modify the deployment at the main account accordingly | `string` | `"cloudconn"` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The resource group name to deploy cloud vision stack | `string` | `""` | no |
| <a name="input_sysdig_secure_api_token"></a> [sysdig\_secure\_api\_token](#input\_sysdig\_secure\_api\_token) | Sysdig's Secure API Token | `string` | n/a | yes |
| <a name="input_sysdig_secure_endpoint"></a> [sysdig\_secure\_endpoint](#input\_sysdig\_secure\_endpoint) | Sysdig Secure API endpoint | `string` | `"https://secure.sysdig.com"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to be added to the resources | `map(string)` | <pre>{<br> "Team": "Cloud Vision"<br>}</pre> | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to be added to the resources | `map(string)` | <pre>{<br> "Team": "CloudVision"<br>}</pre> | no |

## Outputs

Expand Down
13 changes: 13 additions & 0 deletions examples/creating_new_resource_group/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
provider "azurerm" {
features {}
subscription_id = "[SUBSCRIPTION_ID]"
}

module "cloudvision" {
source = "../../"

location = "[LOCATION]"
naming_prefix = "cloudvision"
sysdig_secure_api_token = "[SYSDIG_SECURE_API_TOKEN]"
sysdig_secure_endpoint = "[SYSDIG_SECURE_ENDPOINT]"
}
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions examples/creating_new_resource_group/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 0.15.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.64.0"
}
}
}
15 changes: 15 additions & 0 deletions examples/existing_resource_group/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

provider "azurerm" {
features {}
subscription_id = "[SUBSCRIPTION_ID]"
}

module "cloudvision" {
source = "../../"

location = "[LOCATION]"

sysdig_secure_api_token = "[SYSDIG_SECURE_API_TOKEN]"
sysdig_secure_endpoint = "[SYSDIG_SECURE_ENDPOINT]"
resource_group_name = "[RESOURCE_GROUP_NAME]"
}
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions examples/existing_resource_group/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 0.15.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.64.0"
}
}
}
28 changes: 20 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
locals {
deploy_cloudconnector = var.cloudconnector_deploy
verify_ssl = length(regexall("^https://.*?\\.sysdig.com/?", var.sysdig_secure_endpoint)) != 0
resource_group_name = var.resource_group_name != "" ? var.resource_group_name : module.infrastructure_eventhub.resource_group_name
}

data "azurerm_subscription" "current" {
}

module "infrastructure_eventhub" {
source = "./modules/infrastructure/eventhub"

subscription_id = data.azurerm_subscription.current.subscription_id
location = var.location
naming_prefix = "cloudconnector"
tags = var.tags
resource_group_name = var.resource_group_name
}

module "cloud_connector" {
count = local.deploy_cloudconnector ? 1 : 0
source = "./modules/cloud-connector"
source = "./modules/services/cloud-connector"

naming_prefix = var.naming_prefix
location = var.location
sysdig_secure_api_token = var.sysdig_secure_api_token
sysdig_secure_endpoint = var.sysdig_secure_endpoint
verify_ssl = local.verify_ssl
subscription_id = data.azurerm_subscription.current.subscription_id
tags = var.tags
resource_group_name = local.resource_group_name
eventhub_connection_string = module.infrastructure_eventhub.eventhub_connection_string
naming_prefix = var.naming_prefix
location = var.location
sysdig_secure_api_token = var.sysdig_secure_api_token
sysdig_secure_endpoint = var.sysdig_secure_endpoint
verify_ssl = local.verify_ssl
tags = var.tags
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
locals {
deploy_resource_group = var.resource_group_name == ""
resource_group_name = var.resource_group_name != "" ? var.resource_group_name : azurerm_resource_group.rg[0].name
}

resource "azurerm_resource_group" "rg" {
count = local.deploy_resource_group ? 1 : 0
name = "${lower(var.naming_prefix)}-resourcegroup"
location = var.location

Expand All @@ -11,8 +14,8 @@ resource "azurerm_resource_group" "rg" {

resource "azurerm_eventhub_namespace" "evn" {
name = "${lower(var.naming_prefix)}-eventhub-namespace"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
location = var.location
resource_group_name = local.resource_group_name
sku = var.sku
capacity = var.namespace_capacity

Expand All @@ -22,7 +25,7 @@ resource "azurerm_eventhub_namespace" "evn" {
resource "azurerm_eventhub_namespace_authorization_rule" "ns_auth_rule" {
name = "${lower(var.naming_prefix)}-namespace-auth-rule"
namespace_name = azurerm_eventhub_namespace.evn.name
resource_group_name = azurerm_resource_group.rg.name
resource_group_name = local.resource_group_name

listen = true
send = true
Expand All @@ -32,7 +35,7 @@ resource "azurerm_eventhub_namespace_authorization_rule" "ns_auth_rule" {
resource "azurerm_eventhub" "aev" {
name = "${lower(var.naming_prefix)}-eventhub"
namespace_name = azurerm_eventhub_namespace.evn.name
resource_group_name = azurerm_resource_group.rg.name
resource_group_name = local.resource_group_name
partition_count = var.eventhub_partition_count
message_retention = var.eventhub_retention_days
}
Expand All @@ -41,7 +44,7 @@ resource "azurerm_eventhub_authorization_rule" "eh_auth_rule" {
name = "${lower(var.naming_prefix)}-eventhub_auth_rule"
namespace_name = azurerm_eventhub_namespace.evn.name
eventhub_name = azurerm_eventhub.aev.name
resource_group_name = azurerm_resource_group.rg.name
resource_group_name = local.resource_group_name

listen = true
send = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
output "resource_group_name" {
value = azurerm_resource_group.rg.name
value = length(azurerm_resource_group.rg) > 0 ? azurerm_resource_group.rg[0].name : "n/a"
description = "Created resources group name"
}

output "resource_group_location" {
value = azurerm_resource_group.rg.location
description = "Created resources group location"
}

output "eventhub_connection_string" {
value = azurerm_eventhub_authorization_rule.eh_auth_rule.primary_connection_string
description = "EventHub SAS policy connection string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ variable "tags" {
}
}

variable "resource_group_name" {
type = string
description = "The resource group name to deploy cloud vision stack"
}

variable "subscription_id" {
type = string
description = "Subscription ID where apply the infrastructure"
Expand Down
File renamed without changes.
Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_eventhub_setup_infrastructure"></a> [eventhub\_setup\_infrastructure](#module\_eventhub\_setup\_infrastructure) | ../eventhub-setup-infrastructure | |
No modules.

## Resources

Expand All @@ -36,13 +34,14 @@
|------|-------------|------|---------|:--------:|
| <a name="input_config_content"></a> [config\_content](#input\_config\_content) | Configuration contents for the file stored in the bucket | `string` | `null` | no |
| <a name="input_config_source"></a> [config\_source](#input\_config\_source) | Path to a file that contains the contents of the configuration file to be saved in the bucket | `string` | `null` | no |
| <a name="input_eventhub_connection_string"></a> [eventhub\_connection\_string](#input\_eventhub\_connection\_string) | The eventhub connection string | `string` | n/a | yes |
| <a name="input_image"></a> [image](#input\_image) | Image of the cloud-connector to deploy | `string` | `"sysdiglabs/cloud-connector:latest"` | no |
| <a name="input_location"></a> [location](#input\_location) | Zone where the stack will be deployed | `string` | `"centralus"` | no |
| <a name="input_location"></a> [location](#input\_location) | Zone where the stack will be deployed | `string` | n/a | yes |
| <a name="input_naming_prefix"></a> [naming\_prefix](#input\_naming\_prefix) | Prefix for cloud connector resource names. Use the default unless you need to install multiple instances, and modify the deployment at the main account accordingly | `string` | `"cloudconnector"` | no |
| <a name="input_subscription_id"></a> [subscription\_id](#input\_subscription\_id) | Subscription ID where apply the infrastructure | `string` | n/a | yes |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The resource group name to deploy cloud vision stack | `string` | n/a | yes |
| <a name="input_sysdig_secure_api_token"></a> [sysdig\_secure\_api\_token](#input\_sysdig\_secure\_api\_token) | Sysdig's Secure API Token | `string` | n/a | yes |
| <a name="input_sysdig_secure_endpoint"></a> [sysdig\_secure\_endpoint](#input\_sysdig\_secure\_endpoint) | Sysdig's Secure API URL | `string` | `"https://secure-staging.sysdig.com/"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to be added to the resources | `map(string)` | <pre>{<br> "Team": "Sysdig"<br>}</pre> | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to be added to the resources | `map(string)` | <pre>{<br> "Team": "CloudVision"<br>}</pre> | no |
| <a name="input_verify_ssl"></a> [verify\_ssl](#input\_verify\_ssl) | Verify the SSL certificate of the Secure endpoint | `bool` | `true` | no |

## Outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ locals {
SECURE_API_TOKEN = var.sysdig_secure_api_token,
VERIFY_SSL = tostring(var.verify_ssl)
CONFIG_PATH = "az://${azurerm_storage_account.sa.name}.blob.core.windows.net/${azurerm_storage_container.sc.name}/${azurerm_storage_blob.sb.name}"
EVENT_HUB_CONNECTION_STRING = module.eventhub_setup_infrastructure.eventhub_connection_string
EVENT_HUB_CONNECTION_STRING = var.eventhub_connection_string
AZURE_STORAGE_ACCOUNT = azurerm_storage_account.sa.name
AZURE_STORAGE_ACCESS_KEY = azurerm_storage_account.sa.primary_access_key
}
Expand All @@ -18,25 +18,16 @@ locals {
config_content = var.config_content == null && var.config_source == null ? local.default_config : var.config_content
}

module "eventhub_setup_infrastructure" {
source = "../eventhub-setup-infrastructure"

subscription_id = var.subscription_id
location = var.location
naming_prefix = "cloudconnector"
tags = var.tags
}

resource "azurerm_virtual_network" "vn" {
name = "${var.naming_prefix}-vn"
address_space = ["10.0.0.0/16"]
location = module.eventhub_setup_infrastructure.resource_group_location
resource_group_name = module.eventhub_setup_infrastructure.resource_group_name
location = var.location
resource_group_name = var.resource_group_name
}

resource "azurerm_subnet" "sn" {
name = "${var.naming_prefix}-vn"
resource_group_name = module.eventhub_setup_infrastructure.resource_group_name
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.vn.name
address_prefixes = ["10.0.2.0/24"]
service_endpoints = ["Microsoft.ContainerRegistry"]
Expand All @@ -54,9 +45,9 @@ resource "azurerm_subnet" "sn" {

resource "azurerm_storage_account" "sa" {
name = "${var.naming_prefix}sa"
resource_group_name = module.eventhub_setup_infrastructure.resource_group_name
resource_group_name = var.resource_group_name

location = module.eventhub_setup_infrastructure.resource_group_location
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"

Expand All @@ -79,8 +70,8 @@ resource "azurerm_storage_blob" "sb" {

resource "azurerm_network_profile" "np" {
name = "${var.naming_prefix}-script"
location = module.eventhub_setup_infrastructure.resource_group_location
resource_group_name = module.eventhub_setup_infrastructure.resource_group_name
location = var.location
resource_group_name = var.resource_group_name

container_network_interface {
name = "${var.naming_prefix}-ni"
Expand All @@ -94,8 +85,8 @@ resource "azurerm_network_profile" "np" {

resource "azurerm_container_group" "cg" {
name = "${var.naming_prefix}-group"
location = module.eventhub_setup_infrastructure.resource_group_location
resource_group_name = module.eventhub_setup_infrastructure.resource_group_name
location = var.location
resource_group_name = var.resource_group_name
ip_address_type = "private"
os_type = "Linux"
network_profile_id = azurerm_network_profile.np.id
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
variable "subscription_id" {
type = string
description = "Subscription ID where apply the infrastructure"
}

variable "sysdig_secure_api_token" {
type = string
Expand Down Expand Up @@ -52,14 +48,23 @@ variable "image" {

variable "location" {
type = string
default = "centralus"
description = "Zone where the stack will be deployed"
}

variable "eventhub_connection_string" {
type = string
description = "The eventhub connection string"
}

variable "resource_group_name" {
type = string
description = "The resource group name to deploy cloud vision stack"
}

variable "tags" {
type = map(string)
description = "Tags to be added to the resources"
default = {
Team = "Sysdig"
Team = "CloudVision"
}
}
File renamed without changes.
Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ variable "tags" {
type = map(string)
description = "Tags to be added to the resources"
default = {
Team = "Cloud Vision"
Team = "CloudVision"
}
}

variable "resource_group_name" {
type = string
default = ""
description = "The resource group name to deploy cloud vision stack"
}

0 comments on commit e5cb1df

Please sign in to comment.