Created
March 11, 2019 23:13
-
-
Save joaoferrao/4a44eb1630bfcb670249862731ee256e to your computer and use it in GitHub Desktop.
caws_part_4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
var resources []*SingleResource | |
for _, region := range TraceableRegions { | |
// We need to create a new CFG for each region. We | |
// could actually update the region after the fact | |
// but let's focus on the purpose, here :) | |
cfg := aws.Config{Region: aws.String(region)} | |
s := session.Must(session.NewSessionWithOptions(session.Options{ | |
SharedConfigState: session.SharedConfigEnable, | |
Config: cfg, | |
})) | |
// Creating the actual AWS client from the SDK | |
r := resourcegroupstaggingapi.New(s) | |
// The results will come paginated, so we create an empty | |
// one outside the next for loop so we can keep updating | |
// it and check if there are still more results to come or | |
// not. We could isolate this function and call it recursively | |
// if we wanted to tidy up our code. | |
var paginationToken string = "" | |
var in *resourcegroupstaggingapi.GetResourcesInput | |
var out *resourcegroupstaggingapi.GetResourcesOutput | |
var err error | |
// Let's start an infinite for loop until there are no | |
for { | |
if len(paginationToken) == 0 { | |
in = &resourcegroupstaggingapi.GetResourcesInput{ | |
ResourcesPerPage: aws.Int64(50), | |
} | |
out, err = r.GetResources(in) | |
if err != nil { | |
fmt.Println(err) | |
} | |
} else { | |
in = &resourcegroupstaggingapi.GetResourcesInput{ | |
ResourcesPerPage: aws.Int64(50), | |
PaginationToken: &paginationToken, | |
} | |
} | |
out, err = r.GetResources(in) | |
if err != nil { | |
fmt.Println(err) | |
} | |
for _, resource := range out.ResourceTagMappingList { | |
svc := ServiceNameFromARN(resource.ResourceARN) | |
rgn := region | |
resources = append(resources, ConvertArnToSingleResource(resource.ResourceARN, svc, &rgn)) | |
} | |
paginationToken = *out.PaginationToken | |
if *out.PaginationToken == "" { | |
break | |
} | |
} | |
} | |
// Finally print the results | |
PrettyPrintResources(resources) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment