go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/failureattributes/client.go (about) 1 // Copyright 2023 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 failureattributes 16 17 import ( 18 "context" 19 20 "cloud.google.com/go/bigquery" 21 22 "go.chromium.org/luci/common/errors" 23 24 "go.chromium.org/luci/analysis/internal/bqutil" 25 ) 26 27 // NewClient creates a new client for updating BigQuery failure_attributes 28 // table. 29 func NewClient(ctx context.Context, projectID string) (s *Client, reterr error) { 30 if projectID == "" { 31 return nil, errors.New("GCP Project must be specified") 32 } 33 34 bqClient, err := bqutil.Client(ctx, projectID) 35 if err != nil { 36 return nil, errors.Annotate(err, "creating BQ client").Err() 37 } 38 defer func() { 39 if reterr != nil { 40 bqClient.Close() 41 } 42 }() 43 44 return &Client{ 45 projectID: projectID, 46 bqClient: bqClient, 47 }, nil 48 } 49 50 // Close releases resources held by the client. 51 func (s *Client) Close() (reterr error) { 52 return s.bqClient.Close() 53 } 54 55 // Client provides methods to update BigQuery failure_attributes table. 56 type Client struct { 57 // projectID is the name of the GCP project that contains LUCI Analysis datasets. 58 projectID string 59 bqClient *bigquery.Client 60 } 61 62 func (s *Client) ensureSchema(ctx context.Context) error { 63 table := s.bqClient.Dataset(bqutil.InternalDatasetID).Table(tableName) 64 if err := schemaApplyer.EnsureTable(ctx, table, tableMetadata); err != nil { 65 return errors.Annotate(err, "ensuring failure attributes table").Err() 66 } 67 return nil 68 }