Skip to content

Commit 6351aca

Browse files
authored
Merge pull request #85 from Cloud-Code-AI/84-refc-add-regions-in-aws-report-generation
Added region based data in report
2 parents 3c96a41 + 8bbaaf0 commit 6351aca

File tree

3 files changed

+108
-24
lines changed

3 files changed

+108
-24
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,78 @@ Note: Make sure to run gather command first as report runs on local meta data ga
4343
./main report --provider=aws
4444
```
4545

46+
You should see an output like this:
47+
```
48+
us-east-1
49+
kms
50+
keys 2
51+
rds
52+
instances 0
53+
route53
54+
domains 0
55+
lambda
56+
functions 4
57+
layers 0
58+
s3
59+
buckets 9
60+
cloudfront
61+
websites 2
62+
ec2
63+
amis 0
64+
instances 0
65+
snapshots 0
66+
elasticache
67+
cache_clusters 0
68+
snapshots 0
69+
codebuild
70+
builds 0
71+
enviroment_images 5
72+
projects 0
73+
dynamodb
74+
tables 2
75+
ess
76+
domains 0
77+
ecr
78+
repositories 0
79+
iam
80+
policies 29
81+
roles 37
82+
unused_policies 3
83+
users 5
84+
apigateway
85+
stacks 1
86+
cloudformation
87+
stacks 0
88+
cloudwatch
89+
metrics 272
90+
dashboards 0
91+
metric_streams 0
92+
us-east-2
93+
cloudfront
94+
websites 2
95+
dynamodb
96+
tables 3
97+
ec2
98+
amis 0
99+
instances 1
100+
snapshots 0
101+
iam
102+
policies 29
103+
roles 38
104+
unused_policies 3
105+
users 5
106+
lambda
107+
functions 5
108+
layers 1
109+
rds
110+
instances 1
111+
s3
112+
buckets 9
113+
```
114+
115+
116+
And you can also find your results at `output/aws_report.json`
117+
46118

47119
## Contributing:
48120
We welcome contributions from the community! If you're interested in contributing to CloudState, please check out our contributing guidelines [here]. Whether it's adding new features, fixing bugs, or improving documentation, your help is greatly appreciated.

services/awshandler/generatereport.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ import (
1111

1212
func GenerateAWSReport() {
1313
// Get the most recent data stored for a provider
14-
dir, err := utils.GetMostRecentDirectory("./output/aws/")
15-
if err != nil {
16-
fmt.Println("Failed to find the resource meta data locally. Make sure to run gather command before running repor!")
17-
fmt.Println(err)
18-
return
19-
}
14+
dir := "./output/aws/"
2015

2116
// Compiles and list all the stats in a single file.
22-
allStats := make(map[string]interface{})
23-
var serviceName string
24-
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
17+
regionStats := make(map[string]map[string]interface{})
18+
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
2519
if err != nil {
2620
return err
2721
}
@@ -31,20 +25,28 @@ func GenerateAWSReport() {
3125
if err != nil {
3226
return err
3327
}
34-
serviceName = strings.Split(path, "/")[3]
28+
// Extract service name and region from path
29+
pathParts := strings.Split(path, "/")
30+
if len(pathParts) < 4 {
31+
return fmt.Errorf("unexpected path format: %s", path)
32+
}
33+
serviceName := pathParts[3]
34+
regionName := pathParts[2]
3535

3636
// Print the entire data for debugging
3737
fmt.Printf("Service name %v, data is %v, \n", serviceName, data["stats"])
3838

3939
// Type assert 'stats' as map[string]int
4040
stats, ok := data["stats"].(map[string]interface{})
4141
if !ok {
42-
// handle the error: data["stats"] is not of type map[string]int
43-
fmt.Printf("Service name %v, data is %v, \n", serviceName, data["stats"])
44-
fmt.Printf("Error in parsing stats data for service %v\n", serviceName)
45-
} else {
46-
allStats[serviceName] = stats
42+
return fmt.Errorf("invalid stats format for service: %s, region: %s", serviceName, regionName)
43+
}
44+
45+
// Group stats by region
46+
if _, exists := regionStats[regionName]; !exists {
47+
regionStats[regionName] = make(map[string]interface{})
4748
}
49+
regionStats[regionName][serviceName] = stats
4850
}
4951

5052
return nil
@@ -54,11 +56,13 @@ func GenerateAWSReport() {
5456
return
5557
}
5658

57-
utils.PrintNested(allStats, "", 0)
59+
// Print and write the report
60+
utils.PrintNested(regionStats, "", 0)
5861

59-
err = utils.WriteJSONToFile("output/aws/report.json", allStats)
62+
err = utils.WriteJSONToFile("output/aws_report.json", regionStats)
6063
if err != nil {
6164
fmt.Println("Failed to Write the report file to json")
65+
fmt.Println(err)
6266
}
6367

6468
}

services/utils/statshandler.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,34 @@ import (
88

99
func PrintNested(data interface{}, prefix string, level int) {
1010
indent := " " // 4 spaces
11-
if reflect.TypeOf(data).Kind() == reflect.Map {
11+
if level == 0 {
12+
// Assuming top-level is map[string]map[string]interface{}
13+
for region, services := range data.(map[string]map[string]interface{}) {
14+
fmt.Println(prefix + strings.Repeat(indent, level) + region)
15+
PrintNested(services, prefix, level+1)
16+
}
17+
} else if reflect.TypeOf(data).Kind() == reflect.Map {
18+
// Handle nested maps
1219
for k, v := range data.(map[string]interface{}) {
1320
valueType := reflect.TypeOf(v).Kind()
1421
keyString := fmt.Sprintf("%-30s", prefix+strings.Repeat(indent, level)+k)
1522
if valueType == reflect.Map || valueType == reflect.Slice {
16-
fmt.Println(keyString, "(nested)")
23+
fmt.Println(keyString)
1724
PrintNested(v, prefix, level+1)
1825
} else {
19-
fmt.Println(keyString, fmt.Sprintf("%-20v", v))
26+
fmt.Printf("%s %-20v \n", keyString, v)
2027
}
2128
}
2229
} else if reflect.TypeOf(data).Kind() == reflect.Slice {
23-
for i, v := range data.([]interface{}) {
30+
// Handle slices
31+
for _, v := range data.([]interface{}) {
2432
valueType := reflect.TypeOf(v).Kind()
25-
keyString := fmt.Sprintf("%-30s", prefix+strings.Repeat(indent, level)+fmt.Sprintf("[%d]", i))
33+
keyString := fmt.Sprintf("%-30s", prefix+strings.Repeat(indent, level))
2634
if valueType == reflect.Map || valueType == reflect.Slice {
27-
fmt.Println(keyString, "(nested)")
35+
fmt.Println(keyString)
2836
PrintNested(v, prefix, level+1)
2937
} else {
30-
fmt.Println(keyString, fmt.Sprintf("%-20v", v))
38+
fmt.Printf("%s %-20v \n", keyString, v)
3139
}
3240
}
3341
}

0 commit comments

Comments
 (0)