-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (80 loc) · 2.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"strings"
"cloud.google.com/go/asset/apiv1"
assetpb "google.golang.org/genproto/googleapis/cloud/asset/v1"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"golang.org/x/oauth2/google"
)
type Resource struct {
Name string `json:"name"`
AssetType string `json:"asset_type"`
Project string `json:"project"`
ResourceID string `json:"resource_id"`
}
func main() {
http.HandleFunc("/assets", assetsHandler)
http.Handle("/", http.FileServer(http.Dir("./static")))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func assetsHandler(w http.ResponseWriter, r *http.Request) {
assetType := r.URL.Query().Get("type")
assets, err := getAssets()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if assetType != "" {
filteredAssets := []Resource{}
for _, asset := range assets {
if strings.Contains(asset.AssetType, assetType) {
filteredAssets = append(filteredAssets, asset)
}
}
assets = filteredAssets
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(assets)
}
func getAssets() ([]Resource, error) {
ctx := context.Background()
defaultCredentials, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return nil, err
}
assetClient, err := asset.NewClient(ctx, option.WithCredentials(defaultCredentials))
if err != nil {
return nil, err
}
defer assetClient.Close()
searchRequest := &assetpb.SearchAllResourcesRequest{
Scope: "projects/deans-playground",
}
resourceIterator := assetClient.SearchAllResources(ctx, searchRequest)
var resources []Resource
for {
resourceResponse, err := resourceIterator.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
// Extract the name from the full resource path
parts := strings.Split(resourceResponse.Name, "/")
name := parts[len(parts)-1]
resource := Resource{
Name: name,
AssetType: resourceResponse.AssetType,
Project: "deans-playground",
ResourceID: resourceResponse.AssetType,
}
resources = append(resources, resource)
}
return resources, nil
}