Skip to content

Commit 3a25c42

Browse files
author
Corbin Phelps
authored
Merge pull request #24 from newrelic/inventory-host
Add LocalHostname argument so that inventory host can be overridden
2 parents 071cb7f + f9537ca commit 3a25c42

File tree

5 files changed

+12
-29
lines changed

5 files changed

+12
-29
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## 0.1.3 - 2018-09-25
9+
### Added
10+
- Added local hostname argument to allow for overriding "localhost" as the host from which to collect inventory data.
11+
812
## 0.1.2 - 2018-09-14
913
### Changed
1014
- Removed IP field from Node struct. It was not required as part of collection and could cause an error as the value could be a single string or an array of strings.

elasticsearch-config.yml.sample

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ instances:
66
arguments:
77
config_path: <absolute path to the ElasticSearch configuration .yml file. (default "/etc/elasticsearch/elasticsearch.yml")>
88
hostname: <hostname or IP where Elasticsearch Node is running. (default "localhost")>
9+
local_hostname: <hostname or IP of the Elasticsearch node from which to collect inventory data. (default "localhost")>
910
username: <username for accessing Elasticsearch Node>
1011
password: <password for the given user.>
1112
port: <port on which Elasticsearch Node is listening. (default 9200)>

src/client.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ type errorBody struct {
4242
}
4343

4444
// NewClient creates a new Elasticsearch http client.
45-
// The hostnameOverride parameter specifies a hostname that the client should connect to.
45+
// The hostname parameter specifies the hostname that the client should connect to.
4646
// Passing in an empty string causes the client to use the hostname specified in the command-line args. (default behavior)
47-
func NewClient(hostnameOverride string) (*HTTPClient, error) {
47+
func NewClient(hostname string) (*HTTPClient, error) {
4848
httpClient, err := nrHttp.New(args.CABundleFile, args.CABundleDir, time.Duration(args.Timeout)*time.Second)
4949
if err != nil {
5050
return nil, err
@@ -60,12 +60,6 @@ func NewClient(hostnameOverride string) (*HTTPClient, error) {
6060
if args.UseSSL {
6161
protocol = "https"
6262
}
63-
64-
hostname := args.Hostname
65-
if hostnameOverride != "" {
66-
hostname = hostnameOverride
67-
}
68-
6963
return fmt.Sprintf("%s://%s:%d", protocol, hostname, args.Port)
7064
}(),
7165
}, nil

src/client_test.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNewClient(t *testing.T) {
3131
for _, tc := range testCases {
3232
setupTestArgs()
3333
args.Hostname, args.Port, args.UseSSL = hostname, port, tc.useSSL
34-
client, err := NewClient("")
34+
client, err := NewClient(args.Hostname)
3535
if err != nil {
3636
t.Errorf("Unexpected error: %s", err.Error())
3737
} else {
@@ -42,23 +42,6 @@ func TestNewClient(t *testing.T) {
4242
}
4343
}
4444

45-
func TestHostnameOverride(t *testing.T) {
46-
hostname, port, ssl := "host", 9, false
47-
hostOverride := "overridden"
48-
expectedURL := fmt.Sprintf("http://%s:%d", hostOverride, port)
49-
50-
setupTestArgs()
51-
args.Hostname, args.Port, args.UseSSL = hostname, port, ssl
52-
client, err := NewClient(hostOverride)
53-
if err != nil {
54-
t.Errorf("Unexpected error: %s", err.Error())
55-
} else {
56-
if client.baseURL != expectedURL {
57-
t.Errorf("Expected BaseURL '%s' got '%s'", expectedURL, client.baseURL)
58-
}
59-
}
60-
}
61-
6245
func TestBadCertFile(t *testing.T) {
6346
setupTestArgs()
6447
args.UseSSL = true

src/elasticsearch.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type argumentList struct {
1212
sdkArgs.DefaultArgumentList
1313
Hostname string `default:"localhost" help:"Hostname or IP where Elasticsearch Node is running."`
14+
LocalHostname string `default:"localhost" help:"Hostname or IP of the Elasticsearch node from which to collect inventory."`
1415
Port int `default:"9200" help:"Port on which Elasticsearch Node is listening."`
1516
Username string `default:"" help:"Username for accessing Elasticsearch Node"`
1617
Password string `default:"" help:"Password for the given user."`
@@ -25,7 +26,7 @@ type argumentList struct {
2526

2627
const (
2728
integrationName = "com.newrelic.elasticsearch"
28-
integrationVersion = "0.1.2"
29+
integrationVersion = "0.1.3"
2930
)
3031

3132
var (
@@ -38,7 +39,7 @@ func main() {
3839
logErrorAndExit(err)
3940

4041
// Create a client for metrics
41-
metricsClient, err := NewClient("")
42+
metricsClient, err := NewClient(args.Hostname)
4243
logErrorAndExit(err)
4344

4445
if args.All() || args.Metrics {
@@ -47,7 +48,7 @@ func main() {
4748

4849
// Create a client for inventory. Inventory needs to make REST calls against
4950
// localhost to get information relative to this node only.
50-
inventoryClient, err := NewClient("localhost")
51+
inventoryClient, err := NewClient(args.LocalHostname)
5152
logErrorAndExit(err)
5253

5354
if args.All() || args.Inventory {

0 commit comments

Comments
 (0)