elasticsearch

package module
v0.24.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 8 Imported by: 5

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 {
	panic(err)
}
defer func() {
	if err := elasticsearchContainer.Terminate(ctx); err != nil {
		panic(err)
	}
}()
// }

state, err := elasticsearchContainer.State(ctx)
if err != nil {
	panic(err)
}

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 {
	panic(err)
}
defer func() {
	err := elasticsearchContainer.Terminate(ctx)
	if err != nil {
		panic(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 {
	panic(err)
}

resp, err := esClient.Info()
if err != nil {
	panic(err)
}
defer resp.Body.Close()
// }

var esResp ElasticsearchResponse
if err := json.NewDecoder(resp.Body).Decode(&esResp); err != nil {
	panic(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 {
	panic(err)
}
defer func() {
	err := elasticsearchContainer.Terminate(ctx)
	if err != nil {
		panic(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 Redpanda 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