MCPcopy
hub / github.com/elastic/go-elasticsearch / newTransport

Function newTransport

elasticsearch.go:428–545  ·  view source on GitHub ↗
(cfg Config)

Source from the content-addressed store, hash-verified

426}
427
428func newTransport(cfg Config) (*elastictransport.Client, error) {
429 var addrs []string
430
431 if len(cfg.Addresses) == 0 && cfg.CloudID == "" {
432 addrs = addrsFromEnvironment()
433 } else {
434 if len(cfg.Addresses) > 0 && cfg.CloudID != "" {
435 return nil, errors.New("cannot create client: both Addresses and CloudID are set")
436 }
437
438 if cfg.CloudID != "" {
439 cloudAddr, err := addrFromCloudID(cfg.CloudID)
440 if err != nil {
441 return nil, fmt.Errorf("cannot create client: cannot parse CloudID: %s", err)
442 }
443 addrs = append(addrs, cloudAddr)
444 }
445
446 if len(cfg.Addresses) > 0 {
447 addrs = append(addrs, cfg.Addresses...)
448 }
449 }
450
451 urls, err := addrsToURLs(addrs)
452 if err != nil {
453 return nil, fmt.Errorf("cannot create client: %s", err)
454 }
455
456 if len(urls) == 0 {
457 u, parseErr := parseDefaultURL()
458 if parseErr != nil {
459 return nil, parseErr
460 }
461 urls = append(urls, u)
462 }
463
464 opts := []elastictransport.Option{
465 elastictransport.WithUserAgent(userAgent),
466 elastictransport.WithURLs(urls...),
467 }
468
469 // TODO(karmi): Refactor
470 if urls[0].User != nil {
471 cfg.Username = urls[0].User.Username()
472 pw, _ := urls[0].User.Password()
473 cfg.Password = pw
474 }
475
476 if cfg.APIKey != "" {
477 opts = append(opts, elastictransport.WithAPIKey(cfg.APIKey))
478 } else if cfg.Username != "" || cfg.Password != "" {
479 opts = append(opts, elastictransport.WithBasicAuth(cfg.Username, cfg.Password))
480 }
481 if cfg.ServiceToken != "" {
482 opts = append(opts, elastictransport.WithServiceToken(cfg.ServiceToken))
483 }
484 if cfg.CertificateFingerprint != "" {
485 opts = append(opts, elastictransport.WithCertificateFingerprint(cfg.CertificateFingerprint))

Callers 3

NewBaseClientFunction · 0.85
NewClientFunction · 0.85
NewTypedClientFunction · 0.85

Calls 7

addrsFromEnvironmentFunction · 0.85
addrFromCloudIDFunction · 0.85
addrsToURLsFunction · 0.85
parseDefaultURLFunction · 0.85
UsernameMethod · 0.45
PasswordMethod · 0.45
WithHeaderMethod · 0.45

Tested by

no test coverage detected