-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (74 loc) · 2.05 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
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/storage/v1"
)
// Get the project Number from the bucket Metadata
// For whatever reason this isn't surfaced in the storage API, so we have to use the Resource Manager API
func getProjectNumber(ctx context.Context, bucketName string) string {
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
service, err := storage.NewService(ctx)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
bucketService := storage.NewBucketsService(service)
resp, err := bucketService.Get(bucketName).Do()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
projectNumber := strconv.FormatUint(resp.ProjectNumber, 10)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return projectNumber
}
// Returns the project name from the numeric project ID
func getProjectIDFromNumber(ctx context.Context, projectNumber string) string {
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
cloudresourcemanagerService, err := cloudresourcemanager.NewService(ctx)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
req := cloudresourcemanagerService.Projects.List()
project := req.Filter("projectNumber=" + projectNumber)
resp, err := project.Do()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if len(resp.Projects) < 1 {
fmt.Printf("No project found for project Number %s\n", projectNumber)
os.Exit(1)
}
return resp.Projects[0].ProjectId
}
func main() {
if len(os.Args) == 1 {
fmt.Println("gwhere - find the GCP project ID associated with a cloud storage bucket.")
fmt.Println("usage: gwhere <bucket>")
os.Exit(0)
}
if len(os.Args) != 2 {
fmt.Printf("Exactly 1 argument required, %d provided\n", len(os.Args)-1)
os.Exit(0)
}
ctx := context.Background()
bucketName := os.Args[1]
bucketName = strings.TrimPrefix(bucketName, "gs://")
projectNumber := getProjectNumber(ctx, bucketName)
projectID := getProjectIDFromNumber(ctx, projectNumber)
fmt.Println(projectID)
os.Exit(0)
}