-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a4b23e
commit 2f328c4
Showing
7 changed files
with
330 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package gcphandler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
compute "cloud.google.com/go/compute/apiv1" | ||
"github.com/Cloud-Code-AI/cloudstate/services/utils" | ||
"google.golang.org/api/iterator" | ||
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1" | ||
) | ||
|
||
type ec2Info struct { | ||
Instances []*computepb.Instance `json:"Instances"` | ||
} | ||
|
||
// Gets all the EC2 instance for a given regions and | ||
// stores the results in output/{region}/ec2/instances.json file | ||
func getComputeInfo(projectID string, parentpath string, region string) { | ||
const maxItems = 50 | ||
|
||
// Create EC2 service client | ||
ctx := context.Background() | ||
instancesClient, err := compute.NewInstancesRESTClient(ctx) | ||
if err != nil { | ||
fmt.Println("NewInstancesRESTClient:") | ||
} | ||
defer instancesClient.Close() | ||
|
||
ec2Data := ec2Info{ | ||
Instances: getComputeInstances(projectID, region, ctx, instancesClient), | ||
} | ||
|
||
const ( | ||
path = "/compute/instances.json" | ||
) | ||
|
||
stats := addEc2stats(ec2Data) | ||
output := BasicTemplate{ | ||
Data: ec2Data, | ||
Stats: stats, | ||
} | ||
|
||
filepath := parentpath + region + path | ||
|
||
err = utils.WriteJSONToFile(filepath, output) | ||
if err != nil { | ||
fmt.Println("Error writing lambda function lists") | ||
} | ||
|
||
} | ||
|
||
func addEc2stats(inp ec2Info) interface{} { | ||
s := make(map[string]float64) | ||
s["instances"] = float64(len(inp.Instances)) | ||
return s | ||
} | ||
|
||
func getComputeInstances(projectID string, zone string, ctx context.Context, instancesClient *compute.InstancesClient) []*computepb.Instance { | ||
req := &computepb.ListInstancesRequest{ | ||
Zone: zone, | ||
} | ||
var instances []*computepb.Instance | ||
it := instancesClient.List(ctx, req) | ||
for { | ||
instance, err := it.Next() | ||
if err == iterator.Done { | ||
break | ||
} | ||
if err != nil { | ||
fmt.Println("Error fetching instance: ") | ||
fmt.Println(err) | ||
break | ||
} | ||
instances = append(instances, instance) | ||
} | ||
return instances | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package gcphandler | ||
|
||
// A function to get All the AWS regions | ||
func getGoogleRegions() []string { | ||
// Currently supports US Only | ||
return []string{ | ||
"us-central1", // US East (Ohio) | ||
} | ||
} | ||
|
||
// Creating a common interface for all the data points | ||
type BasicTemplate struct { | ||
Stats interface{} `json:"stats"` | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
func StoreGoogleData(region string, outFolder string) { | ||
|
||
var regions []string | ||
|
||
// If the user wants to fetch all the region, | ||
// load them to regions variable | ||
if region == "all" { | ||
regions = getGoogleRegions() | ||
} else { | ||
regions = append(regions, region) | ||
} | ||
|
||
projectID := "test" | ||
|
||
// parentpath := "output/aws/" + time.Now().Format("2006-01-02T15:04:05") + "/" | ||
if outFolder == "" { | ||
outFolder = "output" | ||
} | ||
parentpath := outFolder + "/gcp/" | ||
|
||
for _, region := range regions { | ||
|
||
// Run this only once as they are global resource | ||
getComputeInfo(projectID, parentpath, region) | ||
|
||
} | ||
|
||
} |