go.temporal.io/server/tools/elasticsearch/handler.go
242 LOC · 54 covered · 188 uncovered · 16 ranges · 4 concepts · 4 introducers · 3 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package elasticsearch
import (
"context"
"errors"
"fmt"
"net/url"
"github.com/urfave/cli"
"go.temporal.io/server/common/auth"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
esclient "go.temporal.io/server/common/persistence/visibility/store/elasticsearch/client"
"go.temporal.io/server/schema"
commonschema "go.temporal.io/server/tools/common/schema"
)
func createClient(cli *cli.Context, logger log.Logger) (esclient.CLIClient, error) {
client_v7.go ×10
cfg, err := parseElasticConfig(cli)
if err != nil {
logger.Error("Unable to parse elasticsearch config.", tag.Error(err))
return nil, err
}
awsHTTPClient, err := esclient.NewAwsHttpClient(cfg.AWSRequestSigning)
if err != nil {
logger.Error("Unable to create AWS HTTP client.", tag.Error(err))
return nil, err
}
cfg.SetHttpClient(awsHTTPClient)
}
if err != nil {
logger.Error("Unable to create elasticsearch client.", tag.Error(err))
return nil, err
}
}
// setupSchema creates cluster settings and index template, but not the index
client, err := createClient(cli, logger)
if err != nil {
logger.Error("Unable to read config.", tag.Error(commonschema.NewConfigError(err.Error())))
return err
}
if !cli.Bool(CLIOptSkipClusterSettings) {
settingsContent, err = schema.ElasticsearchClusterSettings()
if err != nil {
logger.Error("Unable to load embedded cluster settings.", tag.Error(err))
return err
}
}
if err != nil {
logger.Error("Unable to load embedded index template.", tag.Error(err))
return err
}
esClient: client,
logger: logger,
config: &SetupConfig{
SettingsContent: settingsContent,
TemplateContent: templateContent,
VisibilityIndex: "", // Don't create index in setup-schema
FailSilently: cli.Bool(CLIOptFailSilently),
},
}
return task.RunSchemaSetup()
}
// ping the elasticsearch host and return the json response
client, err := createClient(cli, logger)
if err != nil {
logger.Error("Unable to read config.", tag.Error(commonschema.NewConfigError(err.Error())))
return err
}
if err != nil {
logger.Error("Ping failed", tag.Error(err))
return err
}
logger.Info("Pong - Elasticsearch is reachable")
return nil
}
cfg := new(esclient.Config)
u, err := url.Parse(cli.GlobalString(commonschema.CLIOptEndpoint))
if err != nil {
return nil, fmt.Errorf("invalid elasticsearch URL %q: %w", cli.GlobalString(commonschema.CLIOptEndpoint), err)
}
cfg.Username = cli.GlobalString(commonschema.CLIOptUser)
cfg.Password = cli.GlobalString(commonschema.CLIOptPassword)
cfg.Version = "v7" // Fixed schema version 7
cfg.Indices = map[string]string{}
if cli.String(CLIOptVisibilityIndex) != "" {
cfg.Indices[esclient.VisibilityAppName] = cli.String(CLIOptVisibilityIndex)
}
cfg.AWSRequestSigning.CredentialProvider = cli.GlobalString(CLIOptAWSCredentials)
cfg.AWSRequestSigning.Enabled = true
if cfg.AWSRequestSigning.CredentialProvider == "static" {
cfg.AWSRequestSigning.Static.AccessKeyID = cfg.Username
cfg.AWSRequestSigning.Static.SecretAccessKey = cfg.Password
cfg.AWSRequestSigning.Static.Token = cli.GlobalString(CLIOptAWSToken)
}
}
cfg.TLS = &auth.TLS{
Enabled: true,
CertFile: cli.GlobalString(commonschema.CLIFlagTLSCertFile),
KeyFile: cli.GlobalString(commonschema.CLIFlagTLSKeyFile),
CaFile: cli.GlobalString(commonschema.CLIFlagTLSCaFile),
ServerName: cli.GlobalString(commonschema.CLIFlagTLSHostName),
EnableHostVerification: !cli.GlobalBool(commonschema.CLIFlagTLSDisableHostVerification),
}
}
}
// updateSchema updates the index template to the latest version, or index mappings if --index is specified
func updateSchema(cli *cli.Context, logger log.Logger) error {
client, err := createClient(cli, logger)
if err != nil {
logger.Error("Unable to read config.", tag.Error(commonschema.NewConfigError(err.Error())))
return err
}
templateContent, err := schema.ElasticsearchIndexTemplate()
if err != nil {
logger.Error("Unable to load embedded index template.", tag.Error(err))
return err
}
indexName := cli.String(CLIOptVisibilityIndex)
task := SetupTask{
esClient: client,
logger: logger,
config: &SetupConfig{
SettingsContent: "", // Don't update cluster settings
TemplateContent: templateContent,
VisibilityIndex: indexName,
FailSilently: cli.Bool(CLIOptFailSilently),
},
}
err = task.RunTemplateUpgrade()
if err != nil {
return err
}
if indexName != "" {
return task.RunIndexUpdate()
}
return nil
}
// createIndex creates a new visibility index
func createIndex(cli *cli.Context, logger log.Logger) error {
client, err := createClient(cli, logger)
if err != nil {
logger.Error("Unable to read config.", tag.Error(commonschema.NewConfigError(err.Error())))
return err
}
task := SetupTask{
esClient: client,
logger: logger,
config: &SetupConfig{
SettingsContent: "", // Don't update cluster settings
TemplateContent: "", // Don't update template
VisibilityIndex: cli.String(CLIOptVisibilityIndex),
FailSilently: cli.Bool(CLIOptFailSilently),
},
}
return task.RunIndexCreation(context.Background())
}
// dropIndex deletes a visibility index
func dropIndex(cli *cli.Context, logger log.Logger) error {
client, err := createClient(cli, logger)
if err != nil {
logger.Error("Unable to read config.", tag.Error(commonschema.NewConfigError(err.Error())))
return err
}
indexName := cli.String(CLIOptVisibilityIndex)
if indexName == "" {
err := errors.New("index name is required")
logger.Error("Missing index name.", tag.Error(err))
return err
}
failSilently := cli.Bool(CLIOptFailSilently)
success, err := client.DeleteIndex(context.TODO(), indexName)
if err != nil {
if !failSilently {
logger.Error("Index deletion failed", tag.Error(err), tag.String("indexName", indexName))
return err
}
logger.Warn("Index deletion failed", tag.Error(err), tag.String("indexName", indexName))
return nil
} else if !success {
err := errors.New("acknowledged=false")
if !failSilently {
logger.Error("Index deletion failed without error", tag.Error(err), tag.String("indexName", indexName))
return err
}
logger.Warn("Index deletion failed without error", tag.Error(err), tag.String("indexName", indexName))
return nil
}
logger.Info("Index deleted successfully", tag.String("indexName", indexName))
return nil
}
return fmt.Sprintf("(--%s)", opt)
}