cosmosdb

package
v0.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	testcontainers.Container
}

Container represents the CosmosDB container type used in the module

func Run

Run creates an instance of the CosmosDB container type

Example
ctx := context.Background()

cosmosdbContainer, err := cosmosdb.Run(ctx, "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview")
defer func() {
	if err := testcontainers.TerminateContainer(cosmosdbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

state, err := cosmosdbContainer.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:

true
Example (Connect)
ctx := context.Background()

cosmosdbContainer, err := cosmosdb.Run(ctx, "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview")
defer func() {
	if err := testcontainers.TerminateContainer(cosmosdbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

connString, err := cosmosdbContainer.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

p, err := cosmosdb.NewContainerPolicy(ctx, cosmosdbContainer)
if err != nil {
	log.Printf("failed to create policy: %s", err)
	return
}

client, err := azcosmos.NewClientFromConnectionString(connString, p.ClientOptions())
if err != nil {
	log.Printf("failed to create cosmosdb client: %s", err)
	return
}

createDatabaseResp, err := client.CreateDatabase(ctx, azcosmos.DatabaseProperties{ID: "myDatabase"}, nil)
if err != nil {
	log.Printf("failed to create database: %s", err)
	return
}
// }

fmt.Println(createDatabaseResp.RawResponse.StatusCode == http.StatusCreated)
Output:

true

func (*Container) ConnectionString

func (c *Container) ConnectionString(ctx context.Context) (string, error)

ConnectionString returns a connection string that can be used to connect to the CosmosDB emulator. The connection string includes the account endpoint (host:port) and the default test account key. It returns an error if the port endpoint cannot be determined.

Format: "AccountEndpoint=<host>:<port>;AccountKey=<accountKey>"

type ContainerPolicy

type ContainerPolicy struct {
	// contains filtered or unexported fields
}

ContainerPolicy ensures that requests always target the CosmosDB emulator container endpoint. It overrides the CosmosDB client's globalEndpointManager, which would otherwise dynamically update http.Request.Host based on global endpoint discovery, pinning all requests to the container.

func NewContainerPolicy

func NewContainerPolicy(ctx context.Context, c *Container) (*ContainerPolicy, error)

func (*ContainerPolicy) ClientOptions

func (p *ContainerPolicy) ClientOptions() *azcosmos.ClientOptions

ClientOptions returns Azure CosmosDB client options that contain ContainerPolicy.

func (*ContainerPolicy) Do

func (p *ContainerPolicy) Do(req *policy.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL