Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): Add support for authorized UDFs #3084

Merged
merged 5 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bigquery/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ type AccessEntry struct {
EntityType EntityType // The type of entity
Entity string // The entity (individual or group) granted access
View *Table // The view granted access (EntityType must be ViewEntity)
Routine *Routine // The routine granted access (only UDF currently supported)
}

// AccessRole is the level of access to grant to a dataset.
Expand Down Expand Up @@ -679,6 +680,9 @@ const (
// IAMMemberEntity represents entities present in IAM but not represented using
// the other entity types.
IAMMemberEntity

// RoutineEntity is a BigQuery routine, referencing a User Defined Function (UDF).
RoutineEntity
)

func (e *AccessEntry) toBQ() (*bq.DatasetAccess, error) {
Expand All @@ -696,6 +700,8 @@ func (e *AccessEntry) toBQ() (*bq.DatasetAccess, error) {
q.View = e.View.toBQ()
case IAMMemberEntity:
q.IamMember = e.Entity
case RoutineEntity:
q.Routine = e.Routine.toBQ()
default:
return nil, fmt.Errorf("bigquery: unknown entity type %d", e.EntityType)
}
Expand Down Expand Up @@ -723,6 +729,9 @@ func bqToAccessEntry(q *bq.DatasetAccess, c *Client) (*AccessEntry, error) {
case q.IamMember != "":
e.Entity = q.IamMember
e.EntityType = IAMMemberEntity
case q.Routine != nil:
e.Routine = c.DatasetInProject(q.Routine.ProjectId, q.Routine.DatasetId).Routine(q.Routine.RoutineId)
e.EntityType = RoutineEntity
default:
return nil, errors.New("bigquery: invalid access value")
}
Expand Down
4 changes: 3 additions & 1 deletion bigquery/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ func TestConvertAccessEntry(t *testing.T) {
{Role: ReaderRole, Entity: "e", EntityType: IAMMemberEntity},
{Role: ReaderRole, EntityType: ViewEntity,
View: &Table{ProjectID: "p", DatasetID: "d", TableID: "t", c: c}},
{Role: ReaderRole, EntityType: RoutineEntity,
Routine: &Routine{ProjectID: "p", DatasetID: "d", RoutineID: "r", c: c}},
} {
q, err := e.toBQ()
if err != nil {
Expand All @@ -461,7 +463,7 @@ func TestConvertAccessEntry(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if diff := testutil.Diff(got, e, cmp.AllowUnexported(Table{}, Client{})); diff != "" {
if diff := testutil.Diff(got, e, cmp.AllowUnexported(Table{}, Client{}, Routine{})); diff != "" {
t.Errorf("got=-, want=+:\n%s", diff)
}
}
Expand Down
19 changes: 18 additions & 1 deletion bigquery/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,19 @@ func TestIntegration_DatasetUpdateAccess(t *testing.T) {
if err != nil {
t.Fatal(err)
}

// Create a sample UDF so we can verify adding authorized UDFs
routineID := routineIDs.New()
routine := dataset.Routine(routineID)

sql := fmt.Sprintf(`
CREATE FUNCTION `+"`%s`"+`(x INT64) AS (x * 3);`,
routine.FullyQualifiedName())
if err := runQueryJob(ctx, sql); err != nil {
t.Fatal(err)
}
defer routine.Delete(ctx)

origAccess := append([]*AccessEntry(nil), md.Access...)
newEntries := []*AccessEntry{
{
Expand All @@ -728,6 +741,10 @@ func TestIntegration_DatasetUpdateAccess(t *testing.T) {
Entity: "allUsers",
EntityType: IAMMemberEntity,
},
{
EntityType: RoutineEntity,
Routine: routine,
},
}

newAccess := append(md.Access, newEntries...)
Expand All @@ -743,7 +760,7 @@ func TestIntegration_DatasetUpdateAccess(t *testing.T) {
}
}()

if diff := testutil.Diff(md.Access, newAccess, cmpopts.SortSlices(lessAccessEntries)); diff != "" {
if diff := testutil.Diff(md.Access, newAccess, cmpopts.SortSlices(lessAccessEntries), cmpopts.IgnoreUnexported(Routine{})); diff != "" {
t.Fatalf("got=-, want=+:\n%s", diff)
}
}
Expand Down
8 changes: 8 additions & 0 deletions bigquery/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ type Routine struct {
c *Client
}

func (r *Routine) toBQ() *bq.RoutineReference {
return &bq.RoutineReference{
ProjectId: r.ProjectID,
DatasetId: r.DatasetID,
RoutineId: r.RoutineID,
}
}

// FullyQualifiedName returns an identifer for the routine in project.dataset.routine format.
func (r *Routine) FullyQualifiedName() string {
return fmt.Sprintf("%s.%s.%s", r.ProjectID, r.DatasetID, r.RoutineID)
Expand Down