forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 1
/
standalone-projects.js
99 lines (90 loc) · 3.95 KB
/
standalone-projects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
* This is an example for rendering "standalone" projects. It requires -resource-data flag
* when creating the graph database.
*/
var resourceTypes = [
"cloudresourcemanager.googleapis.com/Organization",
"cloudresourcemanager.googleapis.com/Folder",
"cloudresourcemanager.googleapis.com/Project",
"compute.googleapis.com/Network",
];
var onlyStandaloneProjects = true;
var projectIdRegexp = new RegExp('/projects/([^/]+)/');
var nodes = [];
var getData = function (n) {
var dataStr = g.V(n.id).out("data").tagValue();
return JSON.parse(dataStr.id);
};
var follow = function (n, depth) {
var out = n.tag("parent").labelContext(resourceTypes, "type").out("child");
if (out.count() == 0) {
return;
}
if (onlyStandaloneProjects) {
var filteredNodes = [];
out.forEach(function (node) {
var nodeObj = g.V(node.id);
if (node.type == "cloudresourcemanager.googleapis.com/Project") {
var standaloneProject = true;
var projectId = getData(node).resource.data.projectId;
// Look up any IP addresses owned by the project, if any of them
// belong to a different host, it's using a Shared VPC from another project
var ipAddresses = nodeObj.labelContext(["compute.googleapis.com/Address"], "type").out("child").tagArray();
if (ipAddresses.length > 0) {
for (var i = 0; i < ipAddresses.length; i++) {
var ipAddressData = getData(ipAddresses[i]);
if (ipAddressData.resource.data.subnetwork) {
var ipAddressProjectId = projectIdRegexp.exec(ipAddressData.resource.data.subnetwork)[1];
if (projectId != ipAddressProjectId) {
standaloneProject = false;
break;
}
}
}
}
// Check if the project is a Shared VPC host project
var computeProject = nodeObj.labelContext(["compute.googleapis.com/Project"], "type").out("child").tagValue();
var computeProjectData = getData(computeProject);
if (computeProjectData.resource.data.xpnProjectStatus == "HOST") {
standaloneProject = false;
}
// Check if the networks in the project have any peerings
var computeNetworks = nodeObj.labelContext(["compute.googleapis.com/Network"], "type").out("child").tagArray();
computeNetworks.forEach(function (network) {
var networkData = getData(network);
if (networkData.resource.data.peerings && networkData.resource.data.peerings.length > 0) {
standaloneProject = false;
}
});
if (standaloneProject) {
filteredNodes.push(node);
}
} else {
filteredNodes.push(node);
}
});
nodes = nodes.concat(filteredNodes);
} else {
nodes = nodes.concat(out.tagArray());
}
follow(out, depth + 1);
};
var root = g.V("{{ index .Organizations 0 }}").tag("parent");
follow(root, 1);
root.tagArray().concat(nodes).forEach(function (node) {
g.emit(node);
});