elasticsearch

package module
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	// Deprecated: it will be removed in the next major version
	DefaultBaseImage = "docker.elastic.co/elasticsearch/elasticsearch"
	// Deprecated: it will be removed in the next major version
	DefaultBaseImageOSS = "docker.elastic.co/elasticsearch/elasticsearch-oss"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ElasticsearchContainer

type ElasticsearchContainer struct {
	testcontainers.Container
	Settings Options
}

ElasticsearchContainer represents the Elasticsearch container type used in the module

func Run added in v0.32.0

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*ElasticsearchContainer, error)

Run creates an instance of the Elasticsearch container type

Example
// runElasticsearchContainer {
ctx := context.Background()
elasticsearchContainer, err := elasticsearch.Run(ctx, "docker.elastic.co/elasticsearch/elasticsearch:8.9.0")
defer func() {
	if err := testcontainers.TerminateContainer(elasticsearchContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

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

fmt.Println(state.Running)
Output:

true
Example (ConnectUsingElasticsearchClient)
// elasticsearchClient {
ctx := context.Background()
elasticsearchContainer, err := elasticsearch.Run(
	ctx,
	"docker.elastic.co/elasticsearch/elasticsearch:8.9.0",
	elasticsearch.WithPassword("foo"),
)
defer func() {
	if err := testcontainers.TerminateContainer(elasticsearchContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

cfg := es.Config{
	Addresses: []string{
		elasticsearchContainer.Settings.Address,
	},
	Username: "elastic",
	Password: elasticsearchContainer.Settings.Password,
	CACert:   elasticsearchContainer.Settings.CACert,
}

esClient, err := es.NewClient(cfg)
if err != nil {
	log.Printf("error creating the client: %s", err)
	return
}

resp, err := esClient.Info()
if err != nil {
	log.Printf("error getting response: %s", err)
	return
}
defer resp.Body.Close()
// }

var esResp ElasticsearchResponse
if err := json.NewDecoder(resp.Body).Decode(&esResp); err != nil {
	log.Printf("error decoding response: %s", err)
	return
}

fmt.Println(esResp.Tagline)
Output:

You Know, for Search
Example (WithUsingPassword)
// usingPassword {
ctx := context.Background()
elasticsearchContainer, err := elasticsearch.Run(
	ctx,
	"docker.elastic.co/elasticsearch/elasticsearch:7.9.2",
	elasticsearch.WithPassword("foo"),
)
defer func() {
	if err := testcontainers.TerminateContainer(elasticsearchContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

fmt.Println(strings.HasPrefix(elasticsearchContainer.Settings.Address, "http://"))
fmt.Println(elasticsearchContainer.Settings.Password)
Output:

true
foo

func RunContainer deprecated

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*ElasticsearchContainer, error)

Deprecated: use Run instead RunContainer creates an instance of the Elasticsearch container type

type Option

type Option func(*Options)

Option is an option for the Elasticsearch container.

func WithPassword

func WithPassword(password string) Option

WithPassword sets the password for the Elasticsearch container.

func (Option) Customize

func (o Option) Customize(*testcontainers.GenericContainerRequest) error

Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

type Options

type Options struct {
	Address  string
	CACert   []byte
	Password string
	Username string
}

Options is a struct for specifying options for the Elasticsearch container. It could be used to build an HTTP client for the Elasticsearch container, as it will hold information on how to connect to the container.

Jump to

Keyboard shortcuts

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