2019-03-04 17:41:24 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2019-03-25 16:03:39 +00:00
|
|
|
// ErrDocumentNotFound is the error msg for a missing document.
|
|
|
|
const ErrDocumentNotFound = "document not found"
|
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
// DocumentService is used to create/find instances of document stores.
|
|
|
|
type DocumentService interface {
|
|
|
|
CreateDocumentStore(ctx context.Context, name string) (DocumentStore, error)
|
|
|
|
FindDocumentStore(ctx context.Context, name string) (DocumentStore, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Document is a generic structure for stating data.
|
|
|
|
type Document struct {
|
|
|
|
ID ID `json:"id"`
|
|
|
|
Meta DocumentMeta `json:"meta"`
|
|
|
|
Content interface{} `json:"content,omitempty"` // TODO(desa): maybe this needs to be json.Marshaller & json.Unmarshaler
|
|
|
|
Labels []*Label `json:"labels,omitempty"` // read only
|
2020-03-04 20:03:07 +00:00
|
|
|
|
|
|
|
// This is needed for authorization.
|
|
|
|
// The service that passes documents around will take care of filling it
|
|
|
|
// via request parameters or others, as the kv store will take care of
|
|
|
|
// filling it once it returns a document.
|
|
|
|
// This is not stored in the kv store neither required in the API.
|
|
|
|
Organizations map[ID]UserType `json:"-"`
|
2019-03-04 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentMeta is information that is universal across documents. Ideally
|
|
|
|
// data in the meta should be indexed and queryable.
|
|
|
|
type DocumentMeta struct {
|
2019-04-19 16:45:47 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Version string `json:"version,omitempty"`
|
2019-04-19 19:46:58 +00:00
|
|
|
CRUDLog
|
2019-03-04 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentStore is used to perform CRUD operations on documents. It follows an options
|
|
|
|
// pattern that allows users to perform actions related to documents in a transactional way.
|
|
|
|
type DocumentStore interface {
|
2020-03-04 20:03:07 +00:00
|
|
|
CreateDocument(ctx context.Context, d *Document) error
|
|
|
|
FindDocument(ctx context.Context, id ID) (*Document, error)
|
|
|
|
UpdateDocument(ctx context.Context, d *Document) error
|
|
|
|
DeleteDocument(ctx context.Context, id ID) error
|
2019-03-04 17:41:24 +00:00
|
|
|
|
|
|
|
FindDocuments(ctx context.Context, opts ...DocumentFindOptions) ([]*Document, error)
|
|
|
|
DeleteDocuments(ctx context.Context, opts ...DocumentFindOptions) error
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:03:07 +00:00
|
|
|
// DocumentIndex is a structure that is used in DocumentFindOptions to filter out
|
|
|
|
// documents based on some criteria.
|
2019-03-04 17:41:24 +00:00
|
|
|
type DocumentIndex interface {
|
|
|
|
GetAccessorsDocuments(ownerType string, ownerID ID) ([]ID, error)
|
|
|
|
GetDocumentsAccessors(docID ID) ([]ID, error)
|
|
|
|
|
|
|
|
UsersOrgs(userID ID) ([]ID, error)
|
|
|
|
// IsOrgAccessor checks to see if the userID provided is allowed to access
|
|
|
|
// the orgID privided. If the lookup is done in a writable operation
|
|
|
|
// then this method should ensure that the user is an org owner. If the
|
|
|
|
// operation is readable, then it should only require that the user is an org
|
|
|
|
// member.
|
|
|
|
IsOrgAccessor(userID, orgID ID) error
|
|
|
|
|
2020-03-04 20:03:07 +00:00
|
|
|
// TODO(desa): support finding document by label
|
2019-03-04 17:41:24 +00:00
|
|
|
FindOrganizationByName(n string) (ID, error)
|
2019-04-02 12:47:22 +00:00
|
|
|
FindOrganizationByID(id ID) error
|
2019-04-11 15:17:05 +00:00
|
|
|
FindLabelByID(id ID) error
|
2019-03-04 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentDecorator passes information to the DocumentStore about the presentation
|
2020-03-04 20:03:07 +00:00
|
|
|
// of the data being retrieved.
|
2019-03-04 17:41:24 +00:00
|
|
|
type DocumentDecorator interface {
|
|
|
|
IncludeContent() error
|
|
|
|
IncludeLabels() error
|
2020-03-04 20:03:07 +00:00
|
|
|
IncludeOrganizations() error
|
2019-03-04 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 20:03:07 +00:00
|
|
|
// DocumentFindOptions are used to lookup documents.
|
|
|
|
// TODO(desa): consider changing this to have a single struct that has both
|
|
|
|
// the decorator and the index on it.
|
|
|
|
type DocumentFindOptions func(DocumentIndex, DocumentDecorator) ([]ID, error)
|
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
// IncludeContent signals to the DocumentStore that the content of the document
|
|
|
|
// should be included.
|
|
|
|
func IncludeContent(_ DocumentIndex, dd DocumentDecorator) ([]ID, error) {
|
|
|
|
return nil, dd.IncludeContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncludeLabels signals to the DocumentStore that the documents labels
|
|
|
|
// should be included.
|
|
|
|
func IncludeLabels(_ DocumentIndex, dd DocumentDecorator) ([]ID, error) {
|
|
|
|
return nil, dd.IncludeLabels()
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:03:07 +00:00
|
|
|
// IncludeOwner signals to the DocumentStore that the owner
|
|
|
|
// should be included.
|
|
|
|
func IncludeOrganizations(_ DocumentIndex, dd DocumentDecorator) ([]ID, error) {
|
|
|
|
return nil, dd.IncludeOrganizations()
|
2019-03-04 17:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WhereOrg retrieves a list of the ids of the documents that belong to the provided org.
|
|
|
|
func WhereOrg(org string) func(DocumentIndex, DocumentDecorator) ([]ID, error) {
|
2020-03-04 20:03:07 +00:00
|
|
|
return func(idx DocumentIndex, dec DocumentDecorator) ([]ID, error) {
|
2019-03-04 17:41:24 +00:00
|
|
|
oid, err := idx.FindOrganizationByName(org)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-04 20:03:07 +00:00
|
|
|
return WhereOrgID(oid)(idx, dec)
|
2019-03-04 17:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:03:07 +00:00
|
|
|
// WhereOrgID retrieves a list of the ids of the documents that belong to the provided orgID.
|
|
|
|
func WhereOrgID(orgID ID) func(DocumentIndex, DocumentDecorator) ([]ID, error) {
|
2019-03-04 17:41:24 +00:00
|
|
|
return func(idx DocumentIndex, _ DocumentDecorator) ([]ID, error) {
|
2020-03-04 20:03:07 +00:00
|
|
|
return idx.GetAccessorsDocuments("org", orgID)
|
2019-03-04 17:41:24 +00:00
|
|
|
}
|
|
|
|
}
|