(cfg Config)
| 426 | } |
| 427 | |
| 428 | func 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)) |
no test coverage detected