Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Last active May 11, 2022 13:11
Show Gist options
  • Save salrashid123/b9956c273ef9d7825dbf34a879918718 to your computer and use it in GitHub Desktop.
Save salrashid123/b9956c273ef9d7825dbf34a879918718 to your computer and use it in GitHub Desktop.
Using GCP Asset Inventory Feed to monitor resources (https://blog.salrashid.dev/articles/2022/asset_monitor/
package main
import (
"fmt"
"time"
"flag"
"cloud.google.com/go/pubsub"
"golang.org/x/net/context"
assetpb "google.golang.org/genproto/googleapis/cloud/asset/v1"
"google.golang.org/protobuf/encoding/protojson"
)
const ()
var (
projectID = flag.String("projectID", "", "projectID")
subID = flag.String("subscription", "mysub", "PubSub Subscriber")
)
func main() {
flag.Parse()
if *projectID == "" {
fmt.Println("projectID must be set")
return
}
ctx := context.Background()
client, err := pubsub.NewClient(ctx, *projectID)
if err != nil {
fmt.Printf("pubsub.NewClient: %v", err)
return
}
defer client.Close()
sub := client.Subscription(*subID)
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
err = sub.Receive(ctx, func(_ context.Context, msg *pubsub.Message) {
a := &assetpb.TemporalAsset{}
err := protojson.Unmarshal(msg.Data, a)
if err != nil {
fmt.Println("Error in JSON unmarshalling from json marshalled object:", err)
return
}
fmt.Printf("Prior Asset %v\n", a.PriorAsset)
fmt.Printf("Asset %v\n", a.Asset)
msg.Ack()
})
if err != nil {
fmt.Printf("sub.Receive: %v", err)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment