Documentation
¶
Overview ¶
Package cosmosdb provides a local, in-memory emulation of Azure Cosmos DB's Core (SQL) API REST+JSON wire protocol -- database/container CRUD, document CRUD (including upsert and optimistic concurrency via If-Match), and a SQL-subset query engine (SELECT/FROM/WHERE/ORDER BY/TOP) -- close enough to the real Cosmos DB Local Emulator's Gateway (REST) mode for unmodified azure-sdk-for-go/-js/-python clients to operate against. See AZURE.md (M3) and PARITY.md for scope and known gaps.
Unlike services/azuretable, this package has no janitor.go: Cosmos documents carry no TTL/expiry concept this emulator enforces (real Cosmos supports a "DefaultTimeToLive" container setting, but honoring it is out of scope for this milestone -- see PARITY.md's deferred section).
Index ¶
- Constants
- Variables
- func ExecuteQuery(query string, params []QueryParameter, docs []DocumentInfo) ([]map[string]any, error)
- func VerifyMasterKey(masterKey string, r *http.Request) (bool, error)
- type ConfigProvider
- type ContainerInfo
- type ContainerSpec
- type DatabaseInfo
- type DocumentInfo
- type Handler
- func (h *Handler) ExtractOperation(c *echo.Context) string
- func (h *Handler) ExtractResource(c *echo.Context) string
- func (h *Handler) GetSupportedOperations() []string
- func (h *Handler) Handler() echo.HandlerFunc
- func (h *Handler) MatchPriority() int
- func (h *Handler) Name() string
- func (h *Handler) Reset()
- func (h *Handler) Restore(ctx context.Context, data []byte) error
- func (h *Handler) RouteMatcher() service.Matcher
- func (h *Handler) Shutdown(ctx context.Context)
- func (h *Handler) Snapshot(ctx context.Context) []byte
- func (h *Handler) StartWorker(ctx context.Context) error
- type InMemoryBackend
- func (b *InMemoryBackend) CreateContainer(dbID string, spec ContainerSpec) (ContainerInfo, error)
- func (b *InMemoryBackend) CreateDatabase(id string) (DatabaseInfo, error)
- func (b *InMemoryBackend) CreateDocument(dbID, containerID string, body map[string]any, upsert bool) (DocumentInfo, error)
- func (b *InMemoryBackend) DeleteContainer(dbID, containerID string) error
- func (b *InMemoryBackend) DeleteDatabase(id string) error
- func (b *InMemoryBackend) DeleteDocument(dbID, containerID, partitionKey, id, ifMatch string) error
- func (b *InMemoryBackend) GetContainer(dbID, containerID string) (ContainerInfo, error)
- func (b *InMemoryBackend) GetDatabase(id string) (DatabaseInfo, error)
- func (b *InMemoryBackend) GetDocument(dbID, containerID, partitionKey, id string) (DocumentInfo, error)
- func (b *InMemoryBackend) ListContainers(dbID string) ([]ContainerInfo, error)
- func (b *InMemoryBackend) ListDatabases() []DatabaseInfo
- func (b *InMemoryBackend) ListDocuments(dbID, containerID string) ([]DocumentInfo, error)
- func (b *InMemoryBackend) ReplaceDocument(dbID, containerID, partitionKey, id string, body map[string]any, ...) (DocumentInfo, error)
- func (b *InMemoryBackend) Reset()
- func (b *InMemoryBackend) Restore(ctx context.Context, data []byte) error
- func (b *InMemoryBackend) Snapshot(ctx context.Context) []byte
- type Provider
- type QueryParameter
- type Settings
- type StorageBackend
Constants ¶
const DefaultMasterKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
DefaultMasterKey is the real Cosmos DB Local Emulator's well-known, publicly documented fixed master key. Accepting it by default (like services/azureblob's/azurequeue's/azuretable's fixed devstoreaccount1 key) means unmodified SDK configuration pointed at the real emulator's documented connection string works out of the box against gopherstack too. See masterkey.go and AZURE.md section 5.
const DefaultPort = 8081
DefaultPort is the real Cosmos DB Local Emulator's own fixed, protocol-conventional TCP port (8081). Unlike services/azureblob's 10000, services/azurequeue's 10001, and services/azuretable's 10002 -- all of which mirror Azurite's own port convention -- 8081 mirrors the real Cosmos DB Emulator's own published default instead, since Cosmos's emulator (unlike Azurite) is a single-service, single-port tool with no analogous three-port split to imitate. This deliberately sits OUTSIDE --port-range-start/--port-range-end's own default range (10000-10100), exactly like services/iot's MQTT broker default (1883) -- see AZURE.md section 4 and handler.go's StartWorker for the synchronous-bind, no-fallback-pool rationale this follows.
Variables ¶
var ( ErrDatabaseNotFound = errors.New("cosmosdb: database not found") ErrDatabaseAlreadyExists = errors.New("cosmosdb: database already exists") ErrContainerNotFound = errors.New("cosmosdb: container not found") ErrContainerAlreadyExists = errors.New("cosmosdb: container already exists") ErrDocumentNotFound = errors.New("cosmosdb: document not found") ErrDocumentAlreadyExists = errors.New("cosmosdb: document already exists") ErrETagMismatch = errors.New("cosmosdb: etag mismatch") // ErrInvalidDocument is returned when a document body is not a JSON // object, or its "id" field is present but not a non-empty string. ErrInvalidDocument = errors.New("cosmosdb: invalid document body") // ErrInvalidPartitionKeyPath is returned when a container is created // with a partitionKey.paths that is empty or not exactly one path -- // hierarchical (multi-path) partition keys are out of scope for this // milestone; see PARITY.md's deferred section. ErrInvalidPartitionKeyPath = errors.New("cosmosdb: partitionKey.paths must contain exactly one path") // ErrQueryParse and ErrQueryTooDeep are returned by ParseQuery. A parse // error always surfaces as 400 BadRequest, never a panic -- see // document_ops.go's handleQuery. ErrQueryParse = errors.New("cosmosdb: query parse error") ErrQueryTooDeep = errors.New("cosmosdb: query expression nested too deeply") // ErrSnapshotNull* are returned by Restore when a snapshot's nested map // holds a JSON null entry, which decodes to a nil pointer that would // panic on first dereference if stored as-is. See persistence.go. ErrSnapshotDatabaseNull = errors.New("cosmosdb: restore snapshot: database is null") ErrSnapshotContainerNull = errors.New("cosmosdb: restore snapshot: container is null") ErrSnapshotDocumentNull = errors.New("cosmosdb: restore snapshot: document is null") // ErrPartitionKeyMismatch is returned by ReplaceDocument when the // replacement body's own partition-key-path field is present but // disagrees with the caller-supplied partition key (the // x-ms-documentdb-partitionkey header value, canonicalized). Without // this check, a document keyed under partition "a" could be silently // rewritten to claim partition "b" in its own body while remaining // stored (and only ever findable) under "a" -- an internally // inconsistent document real Cosmos DB never allows to exist. ErrPartitionKeyMismatch = errors.New( "cosmosdb: replacement body's partition key does not match the request's partition key", ) // ErrSnapshotDocumentNullBody is returned by storedDocument.UnmarshalJSON // when a persisted document's "Body" field is itself JSON null. // encoding/json decodes a null-into-map with no error (it just sets the // map to nil), so without this explicit check a document with a null // Body would silently restore as an empty document, discarding every // field it used to have -- not a decode failure, so nothing would ever // surface the data loss. ErrSnapshotDocumentNullBody = errors.New("cosmosdb: restore snapshot: document body is null") // ErrUnexpectedTrailingJSON is returned by rejectTrailingJSON when a // second decode off the same stream succeeds instead of hitting EOF, // meaning the input carried more than the one JSON value callers // require. ErrUnexpectedTrailingJSON = errors.New("cosmosdb: unexpected additional JSON value") )
Sentinel errors for Azure Cosmos DB (Core/SQL API) operations.
var ErrMalformedAuthorization = errors.New("cosmosdb: malformed Authorization header")
ErrMalformedAuthorization is returned by VerifyMasterKey when the request's Authorization header cannot be parsed.
var ErrNilAppContext = errors.New("cosmosdb: nil app context")
ErrNilAppContext is returned when Init is called with a nil AppContext.
Functions ¶
func ExecuteQuery ¶
func ExecuteQuery(query string, params []QueryParameter, docs []DocumentInfo) ([]map[string]any, error)
ExecuteQuery parses and runs a Cosmos SQL query against docs, returning the projected result rows in Cosmos's own {"Documents": [...]} response shape (see document_ops.go's queryDocuments). Never panics: a malformed query returns (nil, ErrQueryParse-wrapped error); a runtime type mismatch during WHERE evaluation simply excludes that document, exactly like services/azuretable's EvaluateFilter.
func VerifyMasterKey ¶
VerifyMasterKey recomputes the master-key signature for r using masterKey, and reports whether it matches the signature the client sent. This is an explicit, opt-in check: nothing in this package calls it implicitly -- see handler.go's checkAuth, which only calls this when Settings.ValidateAuth is true, mirroring pkgs/azureauth.VerifySharedKey's identical opt-in stance.
Types ¶
type ConfigProvider ¶
type ConfigProvider interface {
GetCosmosDBSettings() Settings
}
ConfigProvider is a private interface to extract CosmosDB configuration from the abstract AppContext Config, mirroring services/azuretable.ConfigProvider.
type ContainerInfo ¶
ContainerInfo is a read-only snapshot of a container, returned by the StorageBackend container accessors.
type ContainerSpec ¶
ContainerSpec is the caller-supplied shape for creating a container: its ID and single partition key path (e.g. "/pk"). See ErrInvalidPartitionKeyPath for why exactly one path is required.
type DatabaseInfo ¶
DatabaseInfo is a read-only snapshot of a database, returned by the StorageBackend database accessors.
type DocumentInfo ¶
type DocumentInfo struct {
Timestamp time.Time
Body map[string]any
ID string
RID string
Self string
ETag string
PartitionKeyJSON string
}
DocumentInfo is a read-only snapshot of a document, returned by the StorageBackend document accessors. Body holds the document's user-defined fields only (never the system properties, which callers -- handler.go's encodeDocument -- overlay from the other fields at response-encode time, mirroring services/azuretable's EntityInfo/encodeEntity split).
type Handler ¶
type Handler struct {
Backend StorageBackend
// TableBackend holds Table API tables/entities -- a completely
// independent odatatable.InMemoryBackend instance from Backend's own
// database/container/document state (see table_api.go and AZURE.md
// section 9's M6 milestone). It is not yet included in
// Handler.Snapshot/Restore's persistence lifecycle -- see PARITY.md's
// Table API addendum.
TableBackend *odatatable.InMemoryBackend
// MasterKey is the base64-encoded master key checkAuth verifies
// against when ValidateAuth is true.
MasterKey string
// Port is the TCP port StartWorker binds. Set from Settings at Init time
// (see provider.go); defaults to DefaultPort. Like services/azuretable,
// this is a single fixed, protocol-conventional port -- no fallback
// pool, so StartWorker fails fast if it's unavailable.
Port int
// ValidateAuth opts into cryptographic master-key signature
// verification. See masterkey.go and checkAuth.
ValidateAuth bool
// contains filtered or unexported fields
}
Handler is the Echo HTTP handler for Azure Cosmos DB (Core/SQL API) operations.
func NewHandler ¶
func NewHandler(backend StorageBackend) *Handler
NewHandler creates a new Cosmos DB Handler. Port/MasterKey default to DefaultPort/DefaultMasterKey; callers (typically provider.go) override them from Settings.
func (*Handler) ExtractOperation ¶
ExtractOperation extracts the Cosmos DB operation name from the request, for metrics labeling.
func (*Handler) ExtractResource ¶
ExtractResource extracts the resource path, for metrics labeling.
func (*Handler) GetSupportedOperations ¶
GetSupportedOperations returns the list of supported Cosmos DB operations, Core/SQL and Table API combined.
func (*Handler) Handler ¶
func (h *Handler) Handler() echo.HandlerFunc
Handler returns the Echo handler function for Cosmos DB operations.
func (*Handler) MatchPriority ¶
MatchPriority returns the routing priority for the CosmosDB handler. Irrelevant in practice since RouteMatcher never matches; 0 (lowest) is the safe default.
func (*Handler) Reset ¶
func (h *Handler) Reset()
Reset clears all in-memory state, Core/SQL and Table API alike.
func (*Handler) RouteMatcher ¶
RouteMatcher exists only to satisfy service.Registerable's interface contract -- see provider.go's Provider doc comment. CosmosDB never matches on the shared AWS single-port Router; it runs on its own dedicated listener started by StartWorker.
func (*Handler) Snapshot ¶
Snapshot implements persistence.Persistable by delegating to the backend.
func (*Handler) StartWorker ¶
StartWorker binds the dedicated Cosmos DB listener and starts serving on it. See provider.go's Provider doc comment for why CosmosDB needs its own listener, and services/azuretable's StartWorker for the synchronous-bind rationale this mirrors exactly.
type InMemoryBackend ¶
type InMemoryBackend struct {
// contains filtered or unexported fields
}
InMemoryBackend implements StorageBackend using an in-memory map guarded by a single RWMutex. Shaped after services/azuretable's InMemoryBackend.
func NewInMemoryBackend ¶
func NewInMemoryBackend() *InMemoryBackend
NewInMemoryBackend creates a new empty InMemoryBackend.
func (*InMemoryBackend) CreateContainer ¶
func (b *InMemoryBackend) CreateContainer(dbID string, spec ContainerSpec) (ContainerInfo, error)
CreateContainer creates a new, empty container within dbID. Returns ErrDatabaseNotFound if dbID doesn't exist, or ErrContainerAlreadyExists if a container with the same ID already exists in it.
func (*InMemoryBackend) CreateDatabase ¶
func (b *InMemoryBackend) CreateDatabase(id string) (DatabaseInfo, error)
CreateDatabase creates a new, empty database. Returns ErrDatabaseAlreadyExists if a database with the same ID already exists.
func (*InMemoryBackend) CreateDocument ¶
func (b *InMemoryBackend) CreateDocument( dbID, containerID string, body map[string]any, upsert bool, ) (DocumentInfo, error)
CreateDocument creates (or, if upsert, inserts-or-replaces) a document. See StorageBackend's doc comment.
func (*InMemoryBackend) DeleteContainer ¶
func (b *InMemoryBackend) DeleteContainer(dbID, containerID string) error
DeleteContainer removes a container and all of its documents.
func (*InMemoryBackend) DeleteDatabase ¶
func (b *InMemoryBackend) DeleteDatabase(id string) error
DeleteDatabase removes a database and all of its containers/documents. Returns ErrDatabaseNotFound if absent.
func (*InMemoryBackend) DeleteDocument ¶
func (b *InMemoryBackend) DeleteDocument(dbID, containerID, partitionKey, id, ifMatch string) error
DeleteDocument removes a document after verifying ifMatch.
func (*InMemoryBackend) GetContainer ¶
func (b *InMemoryBackend) GetContainer(dbID, containerID string) (ContainerInfo, error)
GetContainer retrieves a single container.
func (*InMemoryBackend) GetDatabase ¶
func (b *InMemoryBackend) GetDatabase(id string) (DatabaseInfo, error)
GetDatabase retrieves a single database. Returns ErrDatabaseNotFound if absent.
func (*InMemoryBackend) GetDocument ¶
func (b *InMemoryBackend) GetDocument(dbID, containerID, partitionKey, id string) (DocumentInfo, error)
GetDocument retrieves a single document by partition key and ID.
func (*InMemoryBackend) ListContainers ¶
func (b *InMemoryBackend) ListContainers(dbID string) ([]ContainerInfo, error)
ListContainers returns a snapshot of all containers in dbID, sorted by ID.
func (*InMemoryBackend) ListDatabases ¶
func (b *InMemoryBackend) ListDatabases() []DatabaseInfo
ListDatabases returns a snapshot of all databases, sorted by ID.
func (*InMemoryBackend) ListDocuments ¶
func (b *InMemoryBackend) ListDocuments(dbID, containerID string) ([]DocumentInfo, error)
ListDocuments returns every document in containerID, ordered by ID.
func (*InMemoryBackend) ReplaceDocument ¶
func (b *InMemoryBackend) ReplaceDocument( dbID, containerID, partitionKey, id string, body map[string]any, ifMatch string, ) (DocumentInfo, error)
ReplaceDocument fully replaces an existing document's body (its "id" and partition key value are fixed by the path/header and cannot be changed by the replacement body's own "id"/partition key field -- consistent with real Cosmos DB, which rejects a partition-key-changing Replace outright; this emulator instead just always keys off the caller-supplied partitionKey/id).
func (*InMemoryBackend) Restore ¶
func (b *InMemoryBackend) Restore(ctx context.Context, data []byte) error
Restore loads backend state from a JSON snapshot. It implements persistence.Persistable. A snapshot's "databases" map, or any database's "Containers" map, or any container's "Documents" map, may legally be JSON null (a nil Go map after decode) rather than absent -- this is tolerated by initializing each to an empty map rather than leaving it nil, since a nil map is safe to range/read but panics on insertion (this was a real M2 bug in services/azuretable's Restore -- see AZURE.md's process rules).
type Provider ¶
type Provider struct{}
Provider implements service.Provider for the Azure Cosmos DB (Core/SQL API) service.
Like services/azureblob, services/azurequeue, and services/azuretable, CosmosDB does not register a RouteMatcher into the shared AWS single-port Router: its /dbs/... resource hierarchy has no service-identifying header the way AWS's X-Amz-Target does, and there is no reason to multiplex it onto any of those three services' own dedicated ports either. Instead the returned Handler implements service.BackgroundWorker and stands up its own dedicated *echo.Echo/*http.Server, listening on a fixed, protocol-conventional port (the real Cosmos DB Local Emulator's own default, 8081 -- not any of Azurite's 10000/10001/10002, since Cosmos's real emulator is a different tool with its own different default; see AZURE.md section 4). It is registered in cli.go's getMostRecentServiceProviders like every other provider; only its RouteMatcher (which always returns false) is inert.
Like services/azuretable, CosmosDB has no janitor: honoring a container's optional DefaultTimeToLive setting is out of scope for this milestone (see PARITY.md's deferred section), so there is nothing for a background sweep to do.
func (*Provider) Init ¶
func (p *Provider) Init(ctx *service.AppContext) (service.Registerable, error)
Init initializes the CosmosDB service backend and handler. The configured port (Settings.Port, default DefaultPort) is only recorded here; the actual TCP bind happens synchronously in Handler.StartWorker, so a port-in-use failure is returned to the caller directly instead of being discovered later from a background goroutine.
type QueryParameter ¶
QueryParameter is one "@name"/value binding from a query request body's "parameters" array.
type Settings ¶
type Settings struct {
// MasterKey is the base64-encoded master key checkAuth verifies request
// signatures against when ValidateAuth is true. Defaults to the real
// emulator's own well-known key (DefaultMasterKey).
MasterKey string `` //nolint:lll // config struct tags are intentionally verbose
/* 248-byte string literal not displayed */
// Port is the fixed TCP port for the dedicated Cosmos DB listener. See
// handler.go's StartWorker for what happens when it's unavailable
// (fails fast; no fallback pool, matching services/azureblob,
// services/azurequeue, and services/azuretable).
Port int `` //nolint:lll // config struct tags are intentionally verbose
/* 172-byte string literal not displayed */
// ValidateAuth opts into cryptographic master-key signature
// verification of the Authorization header (opt-in, off by default --
// mirrors services/s3's WithPresignValidation and pkgs/azureauth's
// VerifySharedKey opt-in pattern). See masterkey.go.
ValidateAuth bool `` //nolint:lll // config struct tags are intentionally verbose
/* 169-byte string literal not displayed */
}
Settings holds service-level configuration for the Cosmos DB backend. Fields are picked up by the Kong CLI parser when this struct is embedded in the root CLI command (see cli.go's CLI.CosmosDB field), mirroring services/azuretable's Settings pattern.
func DefaultSettings ¶
func DefaultSettings() Settings
DefaultSettings returns the default Settings. Used when no ConfigProvider is available at init time (e.g. tests constructing a Provider directly).
type StorageBackend ¶
type StorageBackend interface {
CreateDatabase(id string) (DatabaseInfo, error)
GetDatabase(id string) (DatabaseInfo, error)
ListDatabases() []DatabaseInfo
DeleteDatabase(id string) error
CreateContainer(dbID string, spec ContainerSpec) (ContainerInfo, error)
GetContainer(dbID, containerID string) (ContainerInfo, error)
ListContainers(dbID string) ([]ContainerInfo, error)
DeleteContainer(dbID, containerID string) error
// CreateDocument creates (or, if upsert, inserts-or-replaces) a document
// in containerID. The document's partition key value is derived from
// body per the container's declared partition key path; body's "id"
// field is used verbatim if present (a non-string "id" is rejected), or
// generated if absent.
CreateDocument(dbID, containerID string, body map[string]any, upsert bool) (DocumentInfo, error)
GetDocument(dbID, containerID, partitionKey, id string) (DocumentInfo, error)
ReplaceDocument(
dbID, containerID, partitionKey, id string, body map[string]any, ifMatch string,
) (DocumentInfo, error)
DeleteDocument(dbID, containerID, partitionKey, id, ifMatch string) error
// ListDocuments returns every document in containerID, ordered by ID,
// for both the read-feed (GET .../docs) and the SQL query engine's FROM
// clause. Cross-partition: it is not filtered by partition key.
ListDocuments(dbID, containerID string) ([]DocumentInfo, error)
// Reset clears all in-memory state. Used by the
// POST /_gopherstack/reset endpoint for CI pipelines and rapid local development.
Reset()
}
StorageBackend defines the interface for an Azure Cosmos DB (Core/SQL API) backend. Shaped after services/azuretable's StorageBackend: a narrow, testable seam between the wire handler and storage, so handler tests can substitute a fake.
ifMatch on ReplaceDocument/DeleteDocument threads through the same If-Match states services/azuretable's StorageBackend documents:
- "" (no If-Match header): unconditional mutation.
- any other string: the document must exist AND its current ETag must equal this value, else ErrETagMismatch.
partitionKey parameters are the caller-supplied partition key value's canonical JSON encoding (see canonicalPartitionKeyJSON), extracted from the mandatory x-ms-documentdb-partitionkey header on every point operation.