Skip to content

Commit

Permalink
feat: Basic Go APP
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravpanda committed Feb 25, 2024
1 parent 3a4b23e commit 2f328c4
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.18' # Use the version of Go in your project
go-version: '1.22' # Use the version of Go in your project

- name: Check out code
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Instructions on how to install your project. This section usually starts with cl

### Prerequisites

- Ensure Go 1.18 is installed
- Ensure Go 1.22 is installed

### Build Project

Expand Down
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/Cloud-Code-AI/cloudstate/services/awshandler"
"github.com/Cloud-Code-AI/cloudstate/services/gcphandler"
)

var (
Expand Down Expand Up @@ -89,6 +90,7 @@ func handleAWS(region, resourceType string, outFolder string) {
func handleGCP(region, resourceType string, outFolder string) {
// Implement GCP-specific logic here
fmt.Printf("Provider: GCP \n region: %s on resource: %s\n", region, resourceType)
gcphandler.StoreGoogleData(region, outFolder)

}

Expand Down
31 changes: 30 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,36 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.45.0
)

require github.com/aws/aws-sdk-go-v2/service/autoscaling v1.38.0 // indirect
require (
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.38.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/api v0.162.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240221002015-b0ce06bbee7c // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/grpc v1.61.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
)

require (
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.42.5
Expand Down
174 changes: 174 additions & 0 deletions go.sum

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions services/gcphandler/compute.go
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
}
44 changes: 44 additions & 0 deletions services/gcphandler/initialize.go
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)

}

}

0 comments on commit 2f328c4

Please sign in to comment.