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

Added: Grouping for VPCs on AWS #37

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

### Added

- Add Groups logic for VPCs on AWS
([Issue #6](https://github.com/cycloidio/inframap/issues/6))

### Fixed

- Google graph generation from HCL
Expand Down
9 changes: 9 additions & 0 deletions generate/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ func FromHCL(fs afero.Fs, path string, opt Options) (*graph.Graph, error) {
}
}

// Now that we have the full config we can set the Groups
for _, n := range g.Nodes {
pv, _, err := getProviderAndResource(n.Canonical, opt)
if err != nil {
return nil, err
}
n.AddGroupIDs(pv.Groups(n.ID, resourcesRawConfig)...)
}

for nid, resources := range nodeIDEdges {
for _, rkattr := range resources {
keys := strings.Split(rkattr, ".")
Expand Down
6 changes: 1 addition & 5 deletions generate/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ func assertEqualGraph(t *testing.T, expected, actual *graph.Graph, actualCfg map

for _, en := range expected.Nodes {
if an, ok := nodeCans[en.Canonical]; ok {
en.ID = an.ID
en.TFID = an.TFID
en.Resource = an.Resource
en.Weight = an.Weight
assert.Equal(t, en, an)
assert.Equal(t, en.GroupIDs, an.GroupIDs)
} else {
assert.Failf(t, "Fail", "The Node with Canonical %q is missing", en.Canonical)
}
Expand Down
9 changes: 9 additions & 0 deletions generate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func FromState(tfstate json.RawMessage, opt Options) (*graph.Graph, map[string]i
}
}

// Now that we have the full config we can set the Groups
for _, n := range g.Nodes {
pv, _, err := getProviderAndResource(n.Canonical, opt)
if err != nil {
return nil, nil, err
}
n.AddGroupIDs(pv.Groups(n.ID, cfg)...)
}

for sourceID, edges := range nodeIDEdges {
edgeIDs := make([]string, 0)
for _, e := range edges {
Expand Down
59 changes: 59 additions & 0 deletions generate/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,65 @@ func TestFromState_AWS(t *testing.T) {
require.NotNil(t, cfg)
assert.Len(t, g.Nodes, 2)
})
t.Run("SuccessVPC", func(t *testing.T) {
src, err := ioutil.ReadFile("./testdata/aws_state_vpc.json")
require.NoError(t, err)

g, cfg, err := generate.FromState(src, generate.Options{Clean: true, Connections: true})
require.NoError(t, err)
require.NotNil(t, g)
require.NotNil(t, cfg)

eg := &graph.Graph{
Nodes: []*graph.Node{
&graph.Node{
Canonical: "aws_elb.front",
GroupIDs: []string{"vpc-0d96ad69"},
},
&graph.Node{
Canonical: "aws_instance.front",
GroupIDs: []string{"vpc-0d96ad69"},
},
&graph.Node{
Canonical: "aws_db_instance.magento",
GroupIDs: []string{"vpc-0d96ad69"},
},
&graph.Node{
Canonical: "aws_elasticache_cluster.redis",
GroupIDs: []string{"vpc-0d96ad69"},
},
},
Edges: []*graph.Edge{
&graph.Edge{
Source: "aws_elb.front",
Target: "aws_instance.front",
Canonicals: []string{
"aws_security_group.elb-front",
"aws_security_group.front",
"aws_security_group_rule.elb_to_front_http",
},
},
&graph.Edge{
Source: "aws_instance.front",
Target: "aws_db_instance.magento",
Canonicals: []string{
"aws_security_group.front",
"aws_security_group.rds",
},
},
&graph.Edge{
Source: "aws_instance.front",
Target: "aws_elasticache_cluster.redis",
Canonicals: []string{
"aws_security_group.front",
"aws_security_group.redis",
},
},
},
}

assertEqualGraph(t, eg, g, cfg)
})
}

func TestFromState_OpenStack(t *testing.T) {
Expand Down
Loading