elasticsearch

package module
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	DefaultBaseImage    = "docker.elastic.co/elasticsearch/elasticsearch"
	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 RunContainer

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

RunContainer creates an instance of the Elasticsearch container type

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

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

fmt.Println(state.Running)
Output:

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

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.Fatalf("error creating the client: %s", err) // nolint:gocritic
}

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

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

fmt.Println(esResp.Tagline)
Output:

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

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

true
foo

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)

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