-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp.go
172 lines (131 loc) · 3.54 KB
/
gcp.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"time"
"io/ioutil"
"context"
"golang.org/x/oauth2/google"
"google.golang.org/api/iterator"
"cloud.google.com/go/storage"
"google.golang.org/api/cloudresourcemanager/v1"
)
func GcpListBuckets() (error, []string) {
var bucketsList []string
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return err, nil
}
defer client.Close()
c, err := google.DefaultClient(ctx, cloudresourcemanager.CloudPlatformScope)
if err != nil {
return err, nil
}
cloudresourcemanagerService, err := cloudresourcemanager.New(c)
if err != nil {
return err, nil
}
req := cloudresourcemanagerService.Projects.List()
err = req.Pages(ctx,func(page *cloudresourcemanager.ListProjectsResponse) error {
for _, project := range page.Projects {
it := client.Buckets(ctx, project.ProjectId)
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
bucketsList = append(bucketsList, attrs.Name)
}
}
return nil
});
if err != nil {
return err, nil
}
return nil, bucketsList
}
func GcpListObjects(bucketName string) (error, []string) {
var objectsList []string
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return err, nil
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
it := client.Bucket(bucketName).Objects(ctx, nil)
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err, nil
}
objectsList = append(objectsList, attrs.Name)
}
return nil, objectsList
}
func GcpPresignObjectGet(bucketName, objectName string) (error, string) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return err, ""
}
defer client.Close()
jsonKey, err := ioutil.ReadFile(config.GoogleFile)
if err != nil {
return err, ""
}
conf, err := google.JWTConfigFromJSON(jsonKey)
if err != nil {
return err, ""
}
url, err := storage.SignedURL(bucketName, objectName, &storage.SignedURLOptions{
Scheme: storage.SigningSchemeV4,
Method: "GET",
GoogleAccessID: conf.Email,
PrivateKey: conf.PrivateKey,
Expires: time.Now().Add(signUrlValidMin * time.Minute),
})
if err != nil {
return err, ""
}
return nil, url
}
func GcpCheckBucketExist(bucketName string) (error, bool) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return err, false
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
_, err = client.Bucket(bucketName).Attrs(ctx)
if err != nil {
if err == storage.ErrBucketNotExist {
return nil, false
}
return err, false
}
return err, true
}
func checkAllGcpBuckets(buckets []string) (error, []string) {
var verifiedBuckets []string
var bucketProblems error
for _,bucket := range buckets {
err, exist := GcpCheckBucketExist(bucket)
if err != nil {
bucketProblems = err
continue
}
if exist == true {
verifiedBuckets = append(verifiedBuckets, bucket)
}
}
return bucketProblems, verifiedBuckets
}