From 3a9ca05a075c026b2a550038f7baf328e526c317 Mon Sep 17 00:00:00 2001 From: Alexander van der Kolk Date: Tue, 28 Mar 2023 14:52:31 +0200 Subject: [PATCH 1/2] Added support for generating instances using indices and maps. And added more azurem nodes. --- generate/state.go | 29 +++++++++++++++++++++++++++-- provider/azurerm/graph.go | 8 ++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/generate/state.go b/generate/state.go index def33ce..2743b44 100644 --- a/generate/state.go +++ b/generate/state.go @@ -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 { @@ -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()) + } } } } @@ -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 @@ -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 +} \ No newline at end of file diff --git a/provider/azurerm/graph.go b/provider/azurerm/graph.go index de570da..4fc617e 100644 --- a/provider/azurerm/graph.go +++ b/provider/azurerm/graph.go @@ -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{}{}, @@ -24,9 +25,11 @@ 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_mariadb_database": struct{}{}, "azurerm_mariadb_server": struct{}{}, @@ -47,18 +50,23 @@ 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_virtual_machine": struct{}{}, + "azurerm_windows_function_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": {}, } ) From 8c6c6d7051ba2e0455a45876077cf1b03ee82150 Mon Sep 17 00:00:00 2001 From: Alexander van der Kolk Date: Tue, 28 Mar 2023 15:02:15 +0200 Subject: [PATCH 2/2] Updated azure nodes. --- provider/azurerm/graph.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/provider/azurerm/graph.go b/provider/azurerm/graph.go index 4fc617e..ad99fd2 100644 --- a/provider/azurerm/graph.go +++ b/provider/azurerm/graph.go @@ -31,6 +31,7 @@ var ( "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{}{}, @@ -59,8 +60,9 @@ var ( "azurerm_virtual_machine": struct{}{}, "azurerm_virtual_network_gateway": struct{}{}, "azurerm_vpn_gateway": struct{}{}, - "azurerm_windows_virtual_machine": struct{}{}, "azurerm_windows_function_app": struct{}{}, + "azurerm_windows_virtual_machine": struct{}{}, + "azurerm_windows_web_app": struct{}{}, "azurerm_virtual_network": struct{}{}, }