go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/common/storage/bigtable/flags.go (about) 1 // Copyright 2021 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package bigtable 16 17 import ( 18 "context" 19 "flag" 20 21 "cloud.google.com/go/bigtable" 22 "google.golang.org/api/option" 23 24 "go.chromium.org/luci/common/errors" 25 "go.chromium.org/luci/server/auth" 26 ) 27 28 // Flags contains the BigTable storage config. 29 type Flags struct { 30 // Project is the name of the Cloud Project containing the BigTable instance. 31 Project string 32 // Instance if the name of the BigTable instance within the project. 33 Instance string 34 // LogTable is the name of the BigTable instance's log table. 35 LogTable string 36 37 // AppProfile is the BigTable application profile name to use (or "" for 38 // default). 39 // 40 // This is INTENTIONALLY not wired to a CLI flag; The value here is tied to 41 // the _code_, not the runtime environment of the code. 42 // 43 // The application profile must be configured in the GCP BigTable settings 44 // before use. 45 // 46 // However, in the future it may become necessary to disambiguate between e.g. 47 // prod and dev. If this is the case, then I would recommend StorageFromFlags 48 // adding "-prod" and "-dev" to the given AppProfile name here, rather than 49 // making it fully configurable as a CLI flag (to reduce coupling during 50 // rollouts). 51 AppProfile string 52 } 53 54 // Register registers flags in the flag set. 55 func (f *Flags) Register(fs *flag.FlagSet) { 56 fs.StringVar(&f.Project, "bigtable-project", f.Project, 57 "Cloud Project containing the BigTable instance.") 58 fs.StringVar(&f.Instance, "bigtable-instance", f.Instance, 59 "BigTable instance within the project to use.") 60 fs.StringVar(&f.LogTable, "bigtable-log-table", f.LogTable, 61 "Name of the table with logs.") 62 } 63 64 // Validate returns an error if some parsed flags have invalid values. 65 func (f *Flags) Validate() error { 66 if f.Project == "" { 67 return errors.New("-bigtable-project is required") 68 } 69 if f.Instance == "" { 70 return errors.New("-bigtable-instance is required") 71 } 72 if f.LogTable == "" { 73 return errors.New("-bigtable-log-table is required") 74 } 75 return nil 76 } 77 78 // StorageFromFlags instantiates the *bigtable.Storage given parsed flags. 79 func StorageFromFlags(ctx context.Context, f *Flags) (*Storage, error) { 80 ts, err := auth.GetTokenSource(ctx, auth.AsSelf, auth.WithScopes(auth.CloudOAuthScopes...)) 81 if err != nil { 82 return nil, errors.Annotate(err, "failed to get the token source").Err() 83 } 84 cCfg := bigtable.ClientConfig{ 85 AppProfile: f.AppProfile, 86 } 87 client, err := bigtable.NewClientWithConfig(ctx, f.Project, f.Instance, cCfg, option.WithTokenSource(ts)) 88 if err != nil { 89 return nil, errors.Annotate(err, "failed to construct BigTable client").Err() 90 } 91 return &Storage{ 92 Client: client, 93 LogTable: f.LogTable, 94 }, nil 95 }