go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/bqutil/dataset.go (about) 1 // Copyright 2022 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 bqutil provides utility functions to interact with BigQuery. 16 package bqutil 17 18 import ( 19 "strings" 20 21 "go.chromium.org/luci/analysis/pbutil" 22 ) 23 24 // InternalDatasetID is the name of the BigQuery dataset which is intended 25 // for internal service use only. 26 const InternalDatasetID = "internal" 27 28 // DatasetForProject returns the name of the BigQuery dataset that contains 29 // the given project's data, in the LUCI Analysis GCP project. 30 func DatasetForProject(luciProject string) (string, error) { 31 // The returned dataset may be used in SQL expressions, so we want to 32 // be absolutely sure no SQL Injection is possible. 33 if err := pbutil.ValidateProject(luciProject); err != nil { 34 return "", err 35 } 36 37 // The valid alphabet of LUCI project names [1] is [a-z0-9-] whereas 38 // the valid alphabet of BQ dataset names [2] is [a-zA-Z0-9_]. 39 // [1]: https://source.chromium.org/chromium/infra/infra/+/main:luci/appengine/components/components/config/common.py?q=PROJECT_ID_PATTERN 40 // [2]: https://cloud.google.com/bigquery/docs/datasets#dataset-naming 41 return strings.ReplaceAll(luciProject, "-", "_"), nil 42 } 43 44 // ProjectForDataset returns the name of the LUCI Project that corresponds 45 // to the given BigQuery dataset. 46 func ProjectForDataset(dataset string) (string, error) { 47 project := strings.ReplaceAll(dataset, "_", "-") 48 49 if err := pbutil.ValidateProject(project); err != nil { 50 return "", err 51 } 52 53 return project, nil 54 }