Skip to content

Commit

Permalink
Refactor meraki_organization.tf and merge.tf files
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-humphries committed Oct 11, 2024
1 parent 7435a51 commit b8af5ca
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module "meraki" {
| <a name="input_api_key"></a> [api\_key](#input\_api\_key) | n/a | `string` | n/a | yes |
| <a name="input_model"></a> [model](#input\_model) | As an alternative to YAML files, a native Terraform data structure can be provided as well. | `map(any)` | `{}` | no |
| <a name="input_write_default_values_file"></a> [write\_default\_values\_file](#input\_write\_default\_values\_file) | Write all default values to a YAML file. Value is a path pointing to the file to be created. | `string` | `""` | no |
| <a name="input_write_merged_yaml_file"></a> [write\_merged\_yaml\_file](#input\_write\_merged\_yaml\_file) | The path where the merged YAML output should be written | `string` | `""` | no |
| <a name="input_yaml_directories"></a> [yaml\_directories](#input\_yaml\_directories) | List of paths to YAML directories. | `list(string)` | `[]` | no |
| <a name="input_yaml_files"></a> [yaml\_files](#input\_yaml\_files) | List of paths to YAML files. | `list(string)` | `[]` | no |
## Outputs
Expand All @@ -66,6 +67,7 @@ module "meraki" {

| Name | Type |
|------|------|
| [local_file.merged_yaml_output](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
| [local_sensitive_file.defaults](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/sensitive_file) | resource |
| [meraki_network.network](https://registry.terraform.io/providers/CiscoDevNet/meraki/0.1.2/docs/resources/network) | resource |
| [meraki_network_device_claim.net_device_claim](https://registry.terraform.io/providers/CiscoDevNet/meraki/0.1.2/docs/resources/network_device_claim) | resource |
Expand Down
34 changes: 11 additions & 23 deletions meraki_organization.tf
Original file line number Diff line number Diff line change
Expand Up @@ -362,47 +362,35 @@ resource "meraki_organization_policy_object" "policy_object" {
category = each.value.category
name = each.value.name
type = each.value.type

# Conditionally apply fields
cidr = try(each.value.cidr, null)
fqdn = try(each.value.fqdn, null)
mask = try(each.value.mask, null)
ip = try(each.value.ip, null)
cidr = try(each.value.cidr, null)
fqdn = try(each.value.fqdn, null)
mask = try(each.value.mask, null)
ip = try(each.value.ip, null)
}

locals {
policy_object_groups = flatten([
for domain in try(local.meraki.domains, []) : [
for organization in try(domain.organizations, []) : [
for group in try(organization.policy_objects_groups, []) : {
org_id = data.meraki_organization.organization[organization.name].id
name = group.name
category = group.category
object_names = try(group.object_ids, []) # This refers to the names from YAML, which will be mapped to IDs
org_id = data.meraki_organization.organization[organization.name].id
name = group.name
category = group.category
object_ids = try(group.object_names, [])
} if try(organization.policy_objects_groups, null) != null
]
]
])

# Map object names to their IDs
policy_object_id_map = {
for obj in meraki_organization_policy_object.policy_object : obj.name => obj.id
}
}

# Create Policy Object Groups
# Create Policy Object Groups (if applicable)
resource "meraki_organization_policy_object_group" "policy_object_group" {
for_each = { for group in local.policy_object_groups : group.name => group }

organization_id = each.value.org_id
category = each.value.category
name = each.value.name

# Use the object names provided in YAML and map them to their corresponding object IDs
object_ids = [
for obj_name in each.value.object_names : local.policy_object_id_map[obj_name]
]
depends_on = [meraki_organization_policy_object.policy_object]
category = each.value.category
object_ids = each.value.object_ids
}

//TODO Organization Appliance VPN Settings
Expand Down
6 changes: 6 additions & 0 deletions merge.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ data "utils_yaml_merge" "defaults" {
input = [file("${path.module}/defaults/defaults.yaml"), yamlencode(local.user_defaults)]
}

resource "local_file" "merged_yaml_output" {
count = var.write_merged_yaml_file != "" ? 1 : 0
content = data.utils_yaml_merge.model.output
filename = var.write_merged_yaml_file
}

resource "local_sensitive_file" "defaults" {
count = var.write_default_values_file != "" ? 1 : 0
content = data.utils_yaml_merge.defaults.output
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ variable "write_default_values_file" {
variable "api_key" {
type = string
}

variable "write_merged_yaml_file" {
type = string
description = "The path where the merged YAML output should be written"
default = ""
}

0 comments on commit b8af5ca

Please sign in to comment.