| // Copyright 2015 The Vanadium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // Utilities for creating and opening the database in Syncbase. |
| package util |
| |
| import ( |
| "v.io/v23" |
| "v.io/v23/context" |
| "v.io/v23/security" |
| "v.io/v23/security/access" |
| "v.io/v23/syncbase" |
| "v.io/v23/syncbase/nosql" |
| "v.io/v23/verror" |
| "v.io/x/sensorlog_lite/internal/config" |
| ) |
| |
| // CreateOrOpenDB opens the Sensor Log database hosted on specified Syncbase |
| // instance, creating it if missing. |
| // TODO(ivanpi): Syncbase client helpers for MustExist / CreateIfMissing. |
| func CreateOrOpenDB(ctx *context.T, sbService string) (nosql.Database, error) { |
| patterns := security.DefaultBlessingPatterns(v23.GetPrincipal(ctx)) |
| acl := access.Permissions{} |
| // Allow everyone to resolve app/database to allow joining syncgroups. |
| acl.Add(security.AllPrincipals, string(access.Resolve)) |
| // Restrict other permissions to self. |
| for _, tag := range access.AllTypicalTags() { |
| if tag != access.Resolve { |
| for _, pattern := range patterns { |
| acl.Add(pattern, string(tag)) |
| } |
| } |
| } |
| |
| sbs := syncbase.NewService(sbService) |
| app := sbs.App(config.AppName) |
| if err := app.Create(ctx, acl); err != nil && verror.ErrorID(err) != verror.ErrExist.ID { |
| return nil, err |
| } |
| // TODO(ivanpi): Add schema version. |
| db := app.NoSQLDatabase(config.DBName, nil) |
| if err := db.Create(ctx, acl); err != nil && verror.ErrorID(err) != verror.ErrExist.ID { |
| return nil, err |
| } |
| return db, nil |
| } |