docsterra 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +133 -0
- data/Rakefile +8 -0
- data/exe/docsterra +6 -0
- data/lib/docsterra/analyzer/cost_analyzer.rb +104 -0
- data/lib/docsterra/analyzer/dependency_analyzer.rb +192 -0
- data/lib/docsterra/analyzer/network_analyzer.rb +78 -0
- data/lib/docsterra/analyzer/resource_analyzer.rb +95 -0
- data/lib/docsterra/analyzer/security_analyzer.rb +172 -0
- data/lib/docsterra/cli.rb +91 -0
- data/lib/docsterra/config.rb +175 -0
- data/lib/docsterra/document.rb +38 -0
- data/lib/docsterra/model/network.rb +22 -0
- data/lib/docsterra/model/project.rb +98 -0
- data/lib/docsterra/model/relationship.rb +42 -0
- data/lib/docsterra/model/resource.rb +133 -0
- data/lib/docsterra/parser/expression_inspector.rb +122 -0
- data/lib/docsterra/parser/hcl_ast.rb +31 -0
- data/lib/docsterra/parser/hcl_lexer.rb +378 -0
- data/lib/docsterra/parser/hcl_parser.rb +464 -0
- data/lib/docsterra/parser/module_resolver.rb +89 -0
- data/lib/docsterra/parser/resource_registry.rb +196 -0
- data/lib/docsterra/renderer/cost_section.rb +24 -0
- data/lib/docsterra/renderer/markdown_renderer.rb +184 -0
- data/lib/docsterra/renderer/mermaid_diagram.rb +141 -0
- data/lib/docsterra/renderer/resource_table.rb +63 -0
- data/lib/docsterra/renderer/security_section.rb +82 -0
- data/lib/docsterra/version.rb +5 -0
- data/lib/docsterra.rb +227 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 778f499c80a748af7e5848eb9a68287b1e299a47afa54a0ff20e9de9182053a3
|
|
4
|
+
data.tar.gz: 8a284d3e4dcbba8921270f6211a3f5b7dde5eb5519d64e7f91f893031d2967eb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4dbcbab92c1caa74ddb7f3c321c21da185ed1598211e5d02b6bee9291dfb1a0bca9937531e59744ca4d7cc53d1f6dce034ec48a97b73915ec8521a589b9feb39
|
|
7
|
+
data.tar.gz: aa5fc647fd2545013654b6c08955c94cc70222061bba44b6025cccf814248e7de65cc1eaa20ad4d999a34b1c51aaf7932d6133daa7c5606eb3535ba616327ebe
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yudai Takada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Docsterra
|
|
2
|
+
|
|
3
|
+
`docsterra` is a Ruby gem / CLI that parses Terraform (`.tf`) files and generates infrastructure design documentation in Markdown.
|
|
4
|
+
|
|
5
|
+
Current implementation focuses on GCP Terraform resources and produces:
|
|
6
|
+
|
|
7
|
+
- resource inventory tables
|
|
8
|
+
- Mermaid network diagrams
|
|
9
|
+
- cross-product dependency diagrams
|
|
10
|
+
- security summaries (IAM / firewall / warnings)
|
|
11
|
+
- cost-related spec summaries (non-billing estimate metadata)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install docsterra
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or from a Gemfile:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem "docsterra"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
Single Terraform root:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
docsterra generate ./terraform
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Multiple products (cross-product dependency analysis enabled):
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
docsterra generate ./product-a/terraform ./product-b/terraform ./shared/terraform -o ./docs/infrastructure.md
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Dry-run summary:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
docsterra check ./terraform
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## CLI Reference
|
|
46
|
+
|
|
47
|
+
### Commands
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
docsterra generate [PATHS...]
|
|
51
|
+
docsterra check [PATHS...]
|
|
52
|
+
docsterra version
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Options
|
|
56
|
+
|
|
57
|
+
- `-o`, `--output`: output markdown path (default: `./infrastructure.md`)
|
|
58
|
+
- `-c`, `--config`: config file path (default: `.docsterra.yml`)
|
|
59
|
+
- `-s`, `--sections`: comma-separated sections (`all`, `resources`, `network`, `security`, `cost`)
|
|
60
|
+
- `-f`, `--format`: output format (currently only `markdown`)
|
|
61
|
+
- `-v`, `--verbose`: verbose output
|
|
62
|
+
- `--ignore`: ignore glob patterns (repeatable via Thor array syntax)
|
|
63
|
+
|
|
64
|
+
## Library Usage
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
require "docsterra"
|
|
68
|
+
|
|
69
|
+
doc = Docsterra.generate("./terraform")
|
|
70
|
+
doc.save("./docs/infrastructure.md")
|
|
71
|
+
puts doc.to_markdown
|
|
72
|
+
|
|
73
|
+
summary = Docsterra.check("./terraform")
|
|
74
|
+
puts summary[:resource_count]
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Configuration (`.docsterra.yml`)
|
|
78
|
+
|
|
79
|
+
```yaml
|
|
80
|
+
products:
|
|
81
|
+
- name: "Web Application"
|
|
82
|
+
path: "./product-web/terraform"
|
|
83
|
+
- name: "Shared Infrastructure"
|
|
84
|
+
path: "./shared/terraform"
|
|
85
|
+
shared: true
|
|
86
|
+
|
|
87
|
+
output:
|
|
88
|
+
path: "./docs/infrastructure.md"
|
|
89
|
+
sections:
|
|
90
|
+
- resources
|
|
91
|
+
- network
|
|
92
|
+
- security
|
|
93
|
+
- cost
|
|
94
|
+
|
|
95
|
+
resource_attributes:
|
|
96
|
+
google_compute_instance:
|
|
97
|
+
- machine_type
|
|
98
|
+
- zone
|
|
99
|
+
- labels.env
|
|
100
|
+
|
|
101
|
+
ignore:
|
|
102
|
+
- "**/examples/**"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Output Example (Sections)
|
|
106
|
+
|
|
107
|
+
- `## Overview`
|
|
108
|
+
- `## Cross-Product Dependencies` (Mermaid)
|
|
109
|
+
- `## {Product}`
|
|
110
|
+
- `### Resources`
|
|
111
|
+
- `### Network Configuration` (Mermaid)
|
|
112
|
+
- `### Security Settings`
|
|
113
|
+
- `### Cost Estimation`
|
|
114
|
+
- `## Appendix`
|
|
115
|
+
|
|
116
|
+
## Limitations
|
|
117
|
+
|
|
118
|
+
- HCL parsing is best-effort. Unsupported expressions are preserved as `RawExpr` and rendered as text.
|
|
119
|
+
- Remote modules are not parsed (metadata only).
|
|
120
|
+
- Local modules are parsed recursively, but complex module evaluation/output resolution is not performed.
|
|
121
|
+
- No real-time GCP pricing lookup; cost output is resource spec metadata only.
|
|
122
|
+
- Terraform state / `terraform plan` JSON is not consumed.
|
|
123
|
+
|
|
124
|
+
## Development
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
bundle install
|
|
128
|
+
bundle exec rspec
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT
|
data/Rakefile
ADDED
data/exe/docsterra
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Docsterra
|
|
4
|
+
module Analyzer
|
|
5
|
+
class CostAnalyzer
|
|
6
|
+
CostItem = Struct.new(:resource_name, :resource_type, :spec, :region, :note, :resource, keyword_init: true)
|
|
7
|
+
|
|
8
|
+
def analyze(resources)
|
|
9
|
+
resources.filter_map do |resource|
|
|
10
|
+
build_cost_item(resource)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def build_cost_item(resource)
|
|
17
|
+
case resource.type
|
|
18
|
+
when "google_compute_instance"
|
|
19
|
+
CostItem.new(
|
|
20
|
+
resource_name: resource.attribute_text("name") || resource.name,
|
|
21
|
+
resource_type: resource.type,
|
|
22
|
+
spec: resource.attribute_text("machine_type"),
|
|
23
|
+
region: resource.attribute_text("zone"),
|
|
24
|
+
note: nil,
|
|
25
|
+
resource: resource
|
|
26
|
+
)
|
|
27
|
+
when "google_container_cluster"
|
|
28
|
+
node_type = resource.attribute_text("node_config.machine_type")
|
|
29
|
+
node_count = resource.attribute_text("initial_node_count")
|
|
30
|
+
CostItem.new(
|
|
31
|
+
resource_name: resource.attribute_text("name") || resource.name,
|
|
32
|
+
resource_type: resource.type,
|
|
33
|
+
spec: [node_type, node_count && "x #{node_count}"].compact.join(" "),
|
|
34
|
+
region: resource.attribute_text("location"),
|
|
35
|
+
note: "Autopilot: #{truthy_string(resource.attribute_ruby('enable_autopilot'))}",
|
|
36
|
+
resource: resource
|
|
37
|
+
)
|
|
38
|
+
when "google_container_node_pool"
|
|
39
|
+
CostItem.new(
|
|
40
|
+
resource_name: resource.attribute_text("name") || resource.name,
|
|
41
|
+
resource_type: resource.type,
|
|
42
|
+
spec: resource.attribute_text("node_config.machine_type"),
|
|
43
|
+
region: resource.attribute_text("location") || resource.attribute_text("region"),
|
|
44
|
+
note: node_pool_note(resource),
|
|
45
|
+
resource: resource
|
|
46
|
+
)
|
|
47
|
+
when "google_sql_database_instance"
|
|
48
|
+
CostItem.new(
|
|
49
|
+
resource_name: resource.attribute_text("name") || resource.name,
|
|
50
|
+
resource_type: resource.type,
|
|
51
|
+
spec: resource.attribute_text("settings.tier") || resource.attribute_text("tier"),
|
|
52
|
+
region: resource.attribute_text("region"),
|
|
53
|
+
note: "HA: #{truthy_string((resource.attribute_text('settings.availability_type') || resource.attribute_text('availability_type')).to_s == 'REGIONAL')}",
|
|
54
|
+
resource: resource
|
|
55
|
+
)
|
|
56
|
+
when "google_storage_bucket"
|
|
57
|
+
CostItem.new(
|
|
58
|
+
resource_name: resource.attribute_text("name") || resource.name,
|
|
59
|
+
resource_type: resource.type,
|
|
60
|
+
spec: nil,
|
|
61
|
+
region: resource.attribute_text("location"),
|
|
62
|
+
note: "class: #{resource.attribute_text('storage_class')}",
|
|
63
|
+
resource: resource
|
|
64
|
+
)
|
|
65
|
+
when "google_redis_instance"
|
|
66
|
+
CostItem.new(
|
|
67
|
+
resource_name: resource.attribute_text("name") || resource.name,
|
|
68
|
+
resource_type: resource.type,
|
|
69
|
+
spec: "memory_size_gb=#{resource.attribute_text('memory_size_gb')}",
|
|
70
|
+
region: resource.attribute_text("region"),
|
|
71
|
+
note: "tier: #{resource.attribute_text('tier')}",
|
|
72
|
+
resource: resource
|
|
73
|
+
)
|
|
74
|
+
when "google_compute_disk"
|
|
75
|
+
CostItem.new(
|
|
76
|
+
resource_name: resource.attribute_text("name") || resource.name,
|
|
77
|
+
resource_type: resource.type,
|
|
78
|
+
spec: "#{resource.attribute_text('size')}GB #{resource.attribute_text('type')}".strip,
|
|
79
|
+
region: resource.attribute_text("zone"),
|
|
80
|
+
note: nil,
|
|
81
|
+
resource: resource
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def truthy_string(value)
|
|
87
|
+
case value
|
|
88
|
+
when true then "yes"
|
|
89
|
+
when false then "no"
|
|
90
|
+
else
|
|
91
|
+
value.nil? ? "unknown" : value.to_s
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def node_pool_note(resource)
|
|
96
|
+
min = resource.attribute_text("autoscaling.min_node_count")
|
|
97
|
+
max = resource.attribute_text("autoscaling.max_node_count")
|
|
98
|
+
return nil if min.nil? && max.nil?
|
|
99
|
+
|
|
100
|
+
"autoscaling: #{min || '?'}-#{max || '?'}"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Docsterra
|
|
4
|
+
module Analyzer
|
|
5
|
+
class DependencyAnalyzer
|
|
6
|
+
def analyze(projects)
|
|
7
|
+
relationships = []
|
|
8
|
+
resource_index = build_resource_index(projects)
|
|
9
|
+
name_index = build_name_index(projects)
|
|
10
|
+
string_name_index = build_string_name_index(projects)
|
|
11
|
+
service_account_index = build_service_account_index(projects)
|
|
12
|
+
|
|
13
|
+
projects.each do |project|
|
|
14
|
+
relationships.concat(detect_reference_relationships(project, resource_index))
|
|
15
|
+
relationships.concat(detect_data_source_relationships(project, name_index))
|
|
16
|
+
relationships.concat(detect_shared_name_relationships(project, string_name_index))
|
|
17
|
+
relationships.concat(detect_service_account_relationships(project, service_account_index))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
deduplicate(relationships)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def build_resource_index(projects)
|
|
26
|
+
projects.each_with_object({}) do |project, index|
|
|
27
|
+
project.resources.each { |resource| index[resource.identifier] = resource }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def build_name_index(projects)
|
|
32
|
+
projects.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |project, index|
|
|
33
|
+
project.resources.each do |resource|
|
|
34
|
+
key = [resource.type, resource.attribute_text("name") || resource.attribute_text("dataset_id") || resource.name]
|
|
35
|
+
index[key] << resource
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def build_service_account_index(projects)
|
|
41
|
+
projects.each_with_object({}) do |project, index|
|
|
42
|
+
project.resources.select { |resource| resource.type == "google_service_account" }.each do |sa|
|
|
43
|
+
account_id = sa.attribute_text("account_id")
|
|
44
|
+
email = sa.attribute_text("email")
|
|
45
|
+
[account_id, email].compact.each { |key| index[key] = sa }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def build_string_name_index(projects)
|
|
51
|
+
projects.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |project, index|
|
|
52
|
+
project.resources.each do |resource|
|
|
53
|
+
next unless shareable_name_target?(resource)
|
|
54
|
+
|
|
55
|
+
names_for_resource(resource).each do |name|
|
|
56
|
+
next if name.nil? || name.empty?
|
|
57
|
+
|
|
58
|
+
index[name] << resource
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def detect_reference_relationships(project, resource_index)
|
|
65
|
+
project.resources.flat_map do |resource|
|
|
66
|
+
resource.references.filter_map do |ref|
|
|
67
|
+
target = resource_index[reference_identifier(ref)]
|
|
68
|
+
next unless target
|
|
69
|
+
next if target.project == project
|
|
70
|
+
|
|
71
|
+
rel_type = target.project.shared? ? :shared_resource : relation_type_for_target(target)
|
|
72
|
+
Model::Relationship.new(
|
|
73
|
+
source: resource,
|
|
74
|
+
target: target,
|
|
75
|
+
type: rel_type,
|
|
76
|
+
detail: "Reference #{ref}"
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def detect_data_source_relationships(project, name_index)
|
|
83
|
+
project.data_sources.filter_map do |data_source|
|
|
84
|
+
lookup_key = [data_source.type, data_source.attribute_text("name") || data_source.attribute_text("dataset_id")]
|
|
85
|
+
matches = name_index[lookup_key].reject { |resource| resource.project == project }
|
|
86
|
+
next if matches.empty?
|
|
87
|
+
|
|
88
|
+
target = matches.first
|
|
89
|
+
rel_type = target.project.shared? ? :shared_resource : relation_type_for_target(target)
|
|
90
|
+
Model::Relationship.new(
|
|
91
|
+
source: project,
|
|
92
|
+
target: target.project,
|
|
93
|
+
type: rel_type,
|
|
94
|
+
detail: "Data source #{data_source.type}.#{data_source.name} uses #{lookup_key[1]}"
|
|
95
|
+
)
|
|
96
|
+
end.compact
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def detect_service_account_relationships(project, service_account_index)
|
|
100
|
+
project.resources.filter { |resource| resource.type.match?(/_iam_(member|binding)\z/) }.filter_map do |resource|
|
|
101
|
+
members = Array(resource.attribute_ruby("members")) + [resource.attribute_text("member")]
|
|
102
|
+
target_sa = members.compact.lazy.map { |member| find_service_account_in_member(member.to_s, service_account_index) }.find(&:itself)
|
|
103
|
+
next unless target_sa
|
|
104
|
+
next if target_sa.project == project
|
|
105
|
+
|
|
106
|
+
Model::Relationship.new(
|
|
107
|
+
source: resource,
|
|
108
|
+
target: target_sa,
|
|
109
|
+
type: :iam,
|
|
110
|
+
detail: "IAM member uses service account from #{target_sa.project.name}"
|
|
111
|
+
)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def detect_shared_name_relationships(project, string_name_index)
|
|
116
|
+
project.resources.flat_map do |resource|
|
|
117
|
+
flattened_attribute_strings(resource.attributes).filter_map do |text|
|
|
118
|
+
next unless candidate_shared_name?(text)
|
|
119
|
+
|
|
120
|
+
target = string_name_index[text].find { |candidate| candidate.project != project }
|
|
121
|
+
next unless target
|
|
122
|
+
|
|
123
|
+
rel_type = target.project.shared? ? :shared_resource : relation_type_for_target(target)
|
|
124
|
+
Model::Relationship.new(
|
|
125
|
+
source: resource,
|
|
126
|
+
target: target,
|
|
127
|
+
type: rel_type,
|
|
128
|
+
detail: "String reference uses shared resource name #{text}"
|
|
129
|
+
)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def find_service_account_in_member(member, service_account_index)
|
|
135
|
+
service_account_index.each do |key, resource|
|
|
136
|
+
return resource if key && member.include?(key)
|
|
137
|
+
end
|
|
138
|
+
nil
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def reference_identifier(ref)
|
|
142
|
+
return nil unless ref
|
|
143
|
+
|
|
144
|
+
parts = ref.split(".")
|
|
145
|
+
return nil if parts.length < 2
|
|
146
|
+
|
|
147
|
+
parts[0, 2].join(".")
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def relation_type_for_target(target)
|
|
151
|
+
target.category == :networking ? :network : :reference
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def names_for_resource(resource)
|
|
155
|
+
[
|
|
156
|
+
resource.attribute_text("name"),
|
|
157
|
+
resource.attribute_text("dataset_id"),
|
|
158
|
+
resource.attribute_text("account_id"),
|
|
159
|
+
resource.name
|
|
160
|
+
].compact.uniq
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def shareable_name_target?(resource)
|
|
164
|
+
%i[networking iam messaging].include?(resource.category) || resource.type == "google_pubsub_topic"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def flattened_attribute_strings(value)
|
|
168
|
+
case value
|
|
169
|
+
when Hash
|
|
170
|
+
value.values.flat_map { |child| flattened_attribute_strings(child) }
|
|
171
|
+
when Array
|
|
172
|
+
value.flat_map { |child| flattened_attribute_strings(child) }
|
|
173
|
+
else
|
|
174
|
+
[Parser::ExpressionInspector.to_ruby(value)]
|
|
175
|
+
end.map(&:to_s).reject(&:empty?)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def candidate_shared_name?(text)
|
|
179
|
+
return false if text.start_with?("http://", "https://")
|
|
180
|
+
return false if text.match?(/\A\d+\z/)
|
|
181
|
+
|
|
182
|
+
text.length >= 3
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def deduplicate(relationships)
|
|
186
|
+
relationships.each_with_object([]) do |relationship, unique|
|
|
187
|
+
unique << relationship unless unique.any? { |existing| existing.key == relationship.key }
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Docsterra
|
|
4
|
+
module Analyzer
|
|
5
|
+
class NetworkAnalyzer
|
|
6
|
+
NETWORK_TYPES = %w[
|
|
7
|
+
google_compute_network
|
|
8
|
+
google_compute_subnetwork
|
|
9
|
+
google_compute_firewall
|
|
10
|
+
google_compute_global_address
|
|
11
|
+
google_compute_address
|
|
12
|
+
google_compute_forwarding_rule
|
|
13
|
+
google_compute_global_forwarding_rule
|
|
14
|
+
google_compute_router
|
|
15
|
+
google_compute_router_nat
|
|
16
|
+
google_dns_managed_zone
|
|
17
|
+
google_dns_record_set
|
|
18
|
+
].freeze
|
|
19
|
+
|
|
20
|
+
LOAD_BALANCER_TYPES = %w[
|
|
21
|
+
google_compute_forwarding_rule
|
|
22
|
+
google_compute_global_forwarding_rule
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
25
|
+
def analyze(resources)
|
|
26
|
+
vpcs = resources.select { |resource| resource.type == "google_compute_network" }
|
|
27
|
+
subnets = resources.select { |resource| resource.type == "google_compute_subnetwork" }
|
|
28
|
+
firewalls = resources.select { |resource| resource.type == "google_compute_firewall" }
|
|
29
|
+
load_balancers = resources.select { |resource| LOAD_BALANCER_TYPES.include?(resource.type) }
|
|
30
|
+
endpoints = resources.reject { |resource| NETWORK_TYPES.include?(resource.type) }
|
|
31
|
+
links = build_links(resources, vpcs)
|
|
32
|
+
|
|
33
|
+
Model::Network.new(
|
|
34
|
+
vpcs: vpcs,
|
|
35
|
+
subnets: subnets,
|
|
36
|
+
firewall_rules: firewalls,
|
|
37
|
+
load_balancers: load_balancers,
|
|
38
|
+
endpoints: endpoints,
|
|
39
|
+
links: links
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def build_links(resources, vpcs)
|
|
46
|
+
vpc_lookup = vpcs.each_with_object({}) do |vpc, index|
|
|
47
|
+
index[vpc.identifier] = vpc
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
resources.flat_map do |resource|
|
|
51
|
+
refs = Array(resource.references)
|
|
52
|
+
refs.filter_map do |ref|
|
|
53
|
+
target = target_from_reference(ref, vpc_lookup)
|
|
54
|
+
next unless target
|
|
55
|
+
|
|
56
|
+
{
|
|
57
|
+
source: resource,
|
|
58
|
+
target: target,
|
|
59
|
+
type: :network,
|
|
60
|
+
detail: "references #{ref}"
|
|
61
|
+
}
|
|
62
|
+
end
|
|
63
|
+
end.uniq
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def target_from_reference(ref, vpc_lookup)
|
|
67
|
+
return nil unless ref
|
|
68
|
+
|
|
69
|
+
# ex: google_compute_network.shared.id -> google_compute_network.shared
|
|
70
|
+
parts = ref.split(".")
|
|
71
|
+
return nil if parts.length < 2
|
|
72
|
+
|
|
73
|
+
identifier = parts[0, 2].join(".")
|
|
74
|
+
vpc_lookup[identifier]
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Docsterra
|
|
4
|
+
module Analyzer
|
|
5
|
+
class ResourceAnalyzer
|
|
6
|
+
def initialize(registry: Parser::ResourceRegistry.new)
|
|
7
|
+
@registry = registry
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def analyze(project)
|
|
11
|
+
project.resources = build_resources(project.resource_blocks, project, kind: :resource)
|
|
12
|
+
project.data_sources = build_resources(project.data_blocks, project, kind: :data)
|
|
13
|
+
project.resources
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def build_resources(entries, project, kind:)
|
|
19
|
+
entries.map do |entry|
|
|
20
|
+
block = entry.block
|
|
21
|
+
resource_type = block.labels[0]
|
|
22
|
+
name = block.labels[1] || block.labels[0]
|
|
23
|
+
attributes = build_attribute_tree(block.body)
|
|
24
|
+
refs = collect_references_from_attributes(attributes)
|
|
25
|
+
meta = extract_meta(attributes)
|
|
26
|
+
|
|
27
|
+
Model::Resource.new(
|
|
28
|
+
type: resource_type,
|
|
29
|
+
name: name,
|
|
30
|
+
attributes: attributes,
|
|
31
|
+
project: project,
|
|
32
|
+
references: refs + Array(meta[:depends_on]),
|
|
33
|
+
meta: meta,
|
|
34
|
+
source_file: entry.file,
|
|
35
|
+
comment: block.comment,
|
|
36
|
+
kind: kind,
|
|
37
|
+
display_name: @registry.display_name_for(resource_type),
|
|
38
|
+
category: @registry.category_for(resource_type),
|
|
39
|
+
key_attributes: @registry.key_attributes_for(resource_type)
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def build_attribute_tree(items)
|
|
45
|
+
tree = {}
|
|
46
|
+
|
|
47
|
+
Array(items).each do |item|
|
|
48
|
+
case item
|
|
49
|
+
when Parser::AST::Attribute
|
|
50
|
+
tree[item.key] = item.value
|
|
51
|
+
when Parser::AST::Block
|
|
52
|
+
block_hash = build_attribute_tree(item.body)
|
|
53
|
+
block_hash["__labels"] = item.labels unless item.labels.nil? || item.labels.empty?
|
|
54
|
+
tree[item.type] ||= []
|
|
55
|
+
tree[item.type] << block_hash
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
tree
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def collect_references_from_attributes(value)
|
|
63
|
+
case value
|
|
64
|
+
when Hash
|
|
65
|
+
value.values.flat_map { |child| collect_references_from_attributes(child) }.uniq
|
|
66
|
+
when Array
|
|
67
|
+
value.flat_map { |child| collect_references_from_attributes(child) }.uniq
|
|
68
|
+
else
|
|
69
|
+
Parser::ExpressionInspector.collect_references(value)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def extract_meta(attributes)
|
|
74
|
+
{
|
|
75
|
+
count: attributes["count"],
|
|
76
|
+
for_each: attributes["for_each"],
|
|
77
|
+
depends_on: normalize_depends_on(attributes["depends_on"])
|
|
78
|
+
}.compact
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def normalize_depends_on(node)
|
|
82
|
+
return [] if node.nil?
|
|
83
|
+
|
|
84
|
+
case node
|
|
85
|
+
when Parser::AST::ListExpr
|
|
86
|
+
node.elements.map { |element| Parser::ExpressionInspector.to_text(element) }.compact
|
|
87
|
+
when Parser::AST::RawExpr
|
|
88
|
+
[node.text]
|
|
89
|
+
else
|
|
90
|
+
[Parser::ExpressionInspector.to_text(node)]
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|