From 13e4b7713015a255c55a7c0d058a8586977f890f Mon Sep 17 00:00:00 2001 From: Kevin Yung Date: Fri, 2 Jan 2015 11:38:37 +1100 Subject: [PATCH] ec2: allow spaces seperated hostname in metadata AWS hostname metadata will return space seperated hostname and domain names when DHCPOptionSet is using multiple domain names. This patch will cater for this scenario. --- datasource/metadata/ec2/metadata.go | 2 +- datasource/metadata/ec2/metadata_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/datasource/metadata/ec2/metadata.go b/datasource/metadata/ec2/metadata.go index 039c994..85e973e 100644 --- a/datasource/metadata/ec2/metadata.go +++ b/datasource/metadata/ec2/metadata.go @@ -69,7 +69,7 @@ func (ms metadataService) FetchMetadata() ([]byte, error) { } if hostname, err := ms.fetchAttribute(fmt.Sprintf("%s/hostname", ms.MetadataUrl())); err == nil { - attrs["hostname"] = hostname + attrs["hostname"] = strings.Split(hostname, " ")[0] } else if _, ok := err.(pkg.ErrNotFound); !ok { return nil, err } diff --git a/datasource/metadata/ec2/metadata_test.go b/datasource/metadata/ec2/metadata_test.go index a2a30c3..5f4a538 100644 --- a/datasource/metadata/ec2/metadata_test.go +++ b/datasource/metadata/ec2/metadata_test.go @@ -173,6 +173,20 @@ func TestFetchMetadata(t *testing.T) { }, expect: []byte(`{"hostname":"host","local-ipv4":"1.2.3.4","network_config":{"content_path":"path"},"public-ipv4":"5.6.7.8","public_keys":{"test1":"key"}}`), }, + { + root: "/", + metadataPath: "2009-04-04/meta-data", + resources: map[string]string{ + "/2009-04-04/meta-data/hostname": "host domain another_domain", + "/2009-04-04/meta-data/local-ipv4": "1.2.3.4", + "/2009-04-04/meta-data/public-ipv4": "5.6.7.8", + "/2009-04-04/meta-data/public-keys": "0=test1\n", + "/2009-04-04/meta-data/public-keys/0": "openssh-key", + "/2009-04-04/meta-data/public-keys/0/openssh-key": "key", + "/2009-04-04/meta-data/network_config/content_path": "path", + }, + expect: []byte(`{"hostname":"host","local-ipv4":"1.2.3.4","network_config":{"content_path":"path"},"public-ipv4":"5.6.7.8","public_keys":{"test1":"key"}}`), + }, { clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")}, expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},