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

Better support for "foreach" using sets or lists #224

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions generate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ func FromState(tfstate json.RawMessage, opt Options) (*graph.Graph, map[string]i
// The Instances is the representation of the
// 'count' on the Instance, could also be a 'for_each'
for id, iv := range rv.Instances {
if id != nil && id.String() != "[0]" {
continue
if id != nil {
matched, _ := regexp.MatchString("^\\[([0-9]*|\"(.*)\")\\]$", id.String())
if !matched {
continue
}
}
deps := make([]string, 0)
if len(iv.Current.Dependencies) != 0 {
Expand All @@ -100,6 +103,9 @@ func FromState(tfstate json.RawMessage, opt Options) (*graph.Graph, map[string]i
for i, d := range deps {
if !strings.HasPrefix(d, "module.") {
deps[i] = prefixWithModule(m.Addr.String(), d)
if id != nil {
deps[i] = postfixWithId(deps[i], id.String())
}
}
}
}
Expand Down Expand Up @@ -142,6 +148,10 @@ func FromState(tfstate json.RawMessage, opt Options) (*graph.Graph, map[string]i
Resource: *res,
}

if id != nil {
n.Canonical = postfixWithId(n.Canonical, id.String())
}

err = g.AddNode(n)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -844,5 +854,20 @@ func prefixWithModule(moduleName, resource string) string {
if moduleName != "" {
resource = fmt.Sprintf("%s.%s", moduleName, resource)
}

return resource
}

func postfixWithId(resource string, id string) string {
matched, err := regexp.MatchString("^\\[\"(.*)\"\\]$", id)
if matched && err == nil {
resource = fmt.Sprintf("%s!%s", resource, id[2:len(id) - 2])
}

matched, err = regexp.MatchString("^\\[([0-9]*)]$", id)
if matched && err == nil {
resource = fmt.Sprintf("%s!%s", resource, id[1:len(id) - 1])
}

return resource
}
10 changes: 10 additions & 0 deletions provider/azurerm/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var (
"azurerm_app_service_environment": struct{}{},
"azurerm_app_service_plan": struct{}{},
"azurerm_application_gateway": struct{}{},
"azurerm_application_insights": struct{}{},
"azurerm_bastion_host": struct{}{},
"azurerm_batch_account": struct{}{},
"azurerm_batch_application": struct{}{},
Expand All @@ -24,10 +25,13 @@ var (
"azurerm_frontdoor": struct{}{},
"azurerm_function_app": struct{}{},
"azurerm_image": struct{}{},
"azurerm_key_vault": struct{}{},
"azurerm_kubernetes_cluster": struct{}{},
"azurerm_kubernetes_cluster_node_pool": struct{}{},
"azurerm_lb": struct{}{},
"azurerm_linux_function_app": struct{}{},
"azurerm_linux_virtual_machine": struct{}{},
"azurerm_linux_web_app": struct{}{},
"azurerm_mariadb_database": struct{}{},
"azurerm_mariadb_server": struct{}{},
"azurerm_mssql_database": struct{}{},
Expand All @@ -47,18 +51,24 @@ var (
"azurerm_private_endpoint": struct{}{},
"azurerm_public_ip": struct{}{},
"azurerm_redis_cache": struct{}{},
"azurerm_resource_group": struct{}{},
"azurerm_service_plan": struct{}{},
"azurerm_sql_database": struct{}{},
"azurerm_sql_server": struct{}{},
"azurerm_storage_account": struct{}{},
"azurerm_storage_container": struct{}{},
"azurerm_virtual_machine": struct{}{},
"azurerm_virtual_network_gateway": struct{}{},
"azurerm_vpn_gateway": struct{}{},
"azurerm_windows_function_app": struct{}{},
"azurerm_windows_virtual_machine": struct{}{},
"azurerm_windows_web_app": struct{}{},
"azurerm_virtual_network": struct{}{},
}

// edgeTypes map of all the supported Edges
edgeTypes = map[string]struct{}{
"azurerm_virtual_network_peering": {},
"azurerm_key_vault_secret": {},
}
)