summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-03-21 17:45:19 -0700
committerTim Smith <tsmith@chef.io>2017-05-08 12:32:12 -0700
commit50182f63d79858ab9bba15a0c48c286036f5beb6 (patch)
treec20af79a2c31458bf902d9397b10c90d01bc4747
parent77b6afc263691640e59f4d0574f659b5466e3aef (diff)
downloadohai-50182f63d79858ab9bba15a0c48c286036f5beb6.tar.gz
Merge pull request #964 from webframp/ec2/region-and-zone
[ec2] Add additional data from identity document
-rw-r--r--lib/ohai/plugins/ec2.rb2
-rw-r--r--spec/unit/plugins/ec2_spec.rb43
2 files changed, 45 insertions, 0 deletions
diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb
index 583480a4..4d647fb2 100644
--- a/lib/ohai/plugins/ec2.rb
+++ b/lib/ohai/plugins/ec2.rb
@@ -97,6 +97,8 @@ Ohai.plugin(:EC2) do
end
ec2[:userdata] = fetch_userdata
ec2[:account_id] = fetch_dynamic_data["accountId"]
+ ec2[:availability_zone] = fetch_dynamic_data["availabilityZone"]
+ ec2[:region] = fetch_dynamic_data["region"]
# ASCII-8BIT is equivalent to BINARY in this case
if ec2[:userdata] && ec2[:userdata].encoding.to_s == "ASCII-8BIT"
Ohai::Log.debug("Plugin EC2: Binary UserData Found. Storing in base64")
diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb
index fc83c8cc..ef35cee8 100644
--- a/spec/unit/plugins/ec2_spec.rb
+++ b/spec/unit/plugins/ec2_spec.rb
@@ -127,6 +127,49 @@ describe Ohai::System, "plugin ec2" do
expect(plugin[:ec2]["account_id"]).to eq("4815162342")
end
+ it "fetches AWS region" do
+ paths.each do |name, body|
+ expect(@http_client).to receive(:get).
+ with("/2012-01-12/#{name}").
+ and_return(double("Net::HTTP Response", :body => body, :code => "200"))
+ end
+ expect(@http_client).to receive(:get).
+ with("/2012-01-12/user-data/").
+ and_return(double("Net::HTTP Response", :body => "^_<8B>^H^H<C7>U^@^Csomething^@KT<C8><C9>,)<C9>IU(I-.I<CB><CC>I<E5>^B^@^Qz<BF><B0>^R^@^@^@", :code => "200"))
+ expect(@http_client).to receive(:get).
+ with("/2012-01-12/dynamic/instance-identity/document/").
+ and_return(double("Net::HTTP Response", :body => "{\"region\":\"us-east-1\"}", :code => "200"))
+
+ plugin.run
+
+ expect(plugin[:ec2]).not_to be_nil
+ expect(plugin[:ec2]["instance_type"]).to eq("c1.medium")
+ expect(plugin[:ec2]["ami_id"]).to eq("ami-5d2dc934")
+ expect(plugin[:ec2]["security_groups"]).to eql %w{group1 group2}
+ expect(plugin[:ec2]["region"]).to eq("us-east-1")
+ end
+
+ it "fetches AWS availability zone" do
+ paths.each do |name, body|
+ expect(@http_client).to receive(:get).
+ with("/2012-01-12/#{name}").
+ and_return(double("Net::HTTP Response", :body => body, :code => "200"))
+ end
+ expect(@http_client).to receive(:get).
+ with("/2012-01-12/user-data/").
+ and_return(double("Net::HTTP Response", :body => "^_<8B>^H^H<C7>U^@^Csomething^@KT<C8><C9>,)<C9>IU(I-.I<CB><CC>I<E5>^B^@^Qz<BF><B0>^R^@^@^@", :code => "200"))
+ expect(@http_client).to receive(:get).
+ with("/2012-01-12/dynamic/instance-identity/document/").
+ and_return(double("Net::HTTP Response", :body => "{\"availabilityZone\":\"us-east-1d\"}", :code => "200"))
+
+ plugin.run
+
+ expect(plugin[:ec2]).not_to be_nil
+ expect(plugin[:ec2]["instance_type"]).to eq("c1.medium")
+ expect(plugin[:ec2]["ami_id"]).to eq("ami-5d2dc934")
+ expect(plugin[:ec2]["security_groups"]).to eql %w{group1 group2}
+ expect(plugin[:ec2]["availability_zone"]).to eq("us-east-1d")
+ end
end
it "parses ec2 network/ directory as a multi-level hash" do