forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin.go
267 lines (234 loc) · 7.72 KB
/
admin.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package bigtable
import (
"fmt"
"regexp"
"strings"
"golang.org/x/net/context"
"google.golang.org/cloud"
btcspb "google.golang.org/cloud/bigtable/internal/cluster_service_proto"
bttspb "google.golang.org/cloud/bigtable/internal/table_service_proto"
"google.golang.org/cloud/internal/transport"
"google.golang.org/grpc"
)
const adminAddr = "bigtabletableadmin.googleapis.com:443"
// AdminClient is a client type for performing admin operations within a specific cluster.
type AdminClient struct {
conn *grpc.ClientConn
tClient bttspb.BigtableTableServiceClient
project, zone, cluster string
}
// NewAdminClient creates a new AdminClient for a given project, zone and cluster.
func NewAdminClient(ctx context.Context, project, zone, cluster string, opts ...cloud.ClientOption) (*AdminClient, error) {
o := []cloud.ClientOption{
cloud.WithEndpoint(adminAddr),
cloud.WithScopes(AdminScope),
cloud.WithUserAgent(clientUserAgent),
}
o = append(o, opts...)
conn, err := transport.DialGRPC(ctx, o...)
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
}
return &AdminClient{
conn: conn,
tClient: bttspb.NewBigtableTableServiceClient(conn),
project: project,
zone: zone,
cluster: cluster,
}, nil
}
// Close closes the AdminClient.
func (ac *AdminClient) Close() {
ac.conn.Close()
}
func (ac *AdminClient) clusterPrefix() string {
return fmt.Sprintf("projects/%s/zones/%s/clusters/%s", ac.project, ac.zone, ac.cluster)
}
// Tables returns a list of the tables in the cluster.
func (ac *AdminClient) Tables(ctx context.Context) ([]string, error) {
prefix := ac.clusterPrefix()
req := &bttspb.ListTablesRequest{
Name: prefix,
}
res, err := ac.tClient.ListTables(ctx, req)
if err != nil {
return nil, err
}
names := make([]string, 0, len(res.Tables))
for _, tbl := range res.Tables {
names = append(names, strings.TrimPrefix(tbl.Name, prefix+"/tables/"))
}
return names, nil
}
// CreateTable creates a new table in the cluster.
// This method may return before the table's creation is complete.
func (ac *AdminClient) CreateTable(ctx context.Context, table string) error {
prefix := ac.clusterPrefix()
req := &bttspb.CreateTableRequest{
Name: prefix,
TableId: table,
}
_, err := ac.tClient.CreateTable(ctx, req)
if err != nil {
return err
}
return nil
}
// CreateColumnFamily creates a new column family in a table.
func (ac *AdminClient) CreateColumnFamily(ctx context.Context, table, family string) error {
// TODO(dsymonds): Permit specifying gcexpr and any other family settings.
prefix := ac.clusterPrefix()
req := &bttspb.CreateColumnFamilyRequest{
Name: prefix + "/tables/" + table,
ColumnFamilyId: family,
}
_, err := ac.tClient.CreateColumnFamily(ctx, req)
return err
}
// DeleteTable deletes a table and all of its data.
func (ac *AdminClient) DeleteTable(ctx context.Context, table string) error {
prefix := ac.clusterPrefix()
req := &bttspb.DeleteTableRequest{
Name: prefix + "/tables/" + table,
}
_, err := ac.tClient.DeleteTable(ctx, req)
return err
}
// DeleteColumnFamily deletes a column family in a table and all of its data.
func (ac *AdminClient) DeleteColumnFamily(ctx context.Context, table, family string) error {
prefix := ac.clusterPrefix()
req := &bttspb.DeleteColumnFamilyRequest{
Name: prefix + "/tables/" + table + "/columnFamilies/" + family,
}
_, err := ac.tClient.DeleteColumnFamily(ctx, req)
return err
}
// TableInfo represents information about a table.
type TableInfo struct {
Families []string
}
// TableInfo retrieves information about a table.
func (ac *AdminClient) TableInfo(ctx context.Context, table string) (*TableInfo, error) {
prefix := ac.clusterPrefix()
req := &bttspb.GetTableRequest{
Name: prefix + "/tables/" + table,
}
res, err := ac.tClient.GetTable(ctx, req)
if err != nil {
return nil, err
}
ti := &TableInfo{}
for fam := range res.ColumnFamilies {
ti.Families = append(ti.Families, fam)
}
return ti, nil
}
// SetGCPolicy specifies which cells in a column family should be garbage collected.
// GC executes opportunistically in the background; table reads may return data
// matching the GC policy.
func (ac *AdminClient) SetGCPolicy(ctx context.Context, table, family string, policy GCPolicy) error {
prefix := ac.clusterPrefix()
tbl, err := ac.tClient.GetTable(ctx, &bttspb.GetTableRequest{
Name: prefix + "/tables/" + table,
})
if err != nil {
return err
}
fam, ok := tbl.ColumnFamilies[family]
if !ok {
return fmt.Errorf("unknown column family %q", family)
}
fam.GcRule = policy.proto()
_, err = ac.tClient.UpdateColumnFamily(ctx, fam)
return err
}
const clusterAdminAddr = "bigtableclusteradmin.googleapis.com:443"
// ClusterAdminClient is a client type for performing admin operations on clusters.
// These operations can be substantially more dangerous than those provided by AdminClient.
type ClusterAdminClient struct {
conn *grpc.ClientConn
cClient btcspb.BigtableClusterServiceClient
project string
}
// NewClusterAdminClient creates a new ClusterAdminClient for a given project.
func NewClusterAdminClient(ctx context.Context, project string, opts ...cloud.ClientOption) (*ClusterAdminClient, error) {
o := []cloud.ClientOption{
cloud.WithEndpoint(clusterAdminAddr),
cloud.WithScopes(ClusterAdminScope),
cloud.WithUserAgent(clientUserAgent),
}
o = append(o, opts...)
conn, err := transport.DialGRPC(ctx, o...)
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
}
return &ClusterAdminClient{
conn: conn,
cClient: btcspb.NewBigtableClusterServiceClient(conn),
project: project,
}, nil
}
// Close closes the ClusterAdminClient.
func (cac *ClusterAdminClient) Close() {
cac.conn.Close()
}
// ClusterInfo represents information about a cluster.
type ClusterInfo struct {
Name string // name of the cluster
Zone string // GCP zone of the cluster (e.g. "us-central1-a")
DisplayName string // display name for UIs
ServeNodes int // number of allocated serve nodes
}
var clusterNameRegexp = regexp.MustCompile(`^projects/([^/]+)/zones/([^/]+)/clusters/([a-z][-a-z0-9]*)$`)
// Clusters returns a list of clusters in the project.
func (cac *ClusterAdminClient) Clusters(ctx context.Context) ([]*ClusterInfo, error) {
req := &btcspb.ListClustersRequest{
Name: "projects/" + cac.project,
}
res, err := cac.cClient.ListClusters(ctx, req)
if err != nil {
return nil, err
}
// TODO(dsymonds): Deal with failed_zones.
var cis []*ClusterInfo
for _, c := range res.Clusters {
m := clusterNameRegexp.FindStringSubmatch(c.Name)
if m == nil {
return nil, fmt.Errorf("malformed cluster name %q", c.Name)
}
cis = append(cis, &ClusterInfo{
Name: m[3],
Zone: m[2],
DisplayName: c.DisplayName,
ServeNodes: int(c.ServeNodes),
})
}
return cis, nil
}
/* TODO(dsymonds): Re-enable when there's a ClusterAdmin API.
// SetClusterSize sets the number of server nodes for this cluster.
func (ac *AdminClient) SetClusterSize(ctx context.Context, nodes int) error {
req := &btcspb.GetClusterRequest{
Name: ac.clusterPrefix(),
}
clu, err := ac.cClient.GetCluster(ctx, req)
if err != nil {
return err
}
clu.ServeNodes = int32(nodes)
_, err = ac.cClient.UpdateCluster(ctx, clu)
return err
}
*/