summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-03-21 17:45:19 -0700
committerGitHub <noreply@github.com>2017-03-21 17:45:19 -0700
commitbda5683231607ef55df0264dd6ed0af04c74c6b4 (patch)
tree1394c76aee7a7d70489529be6422f7ce70be007e
parentcab56af9f8e096f8ce0053e24d8f488c79708824 (diff)
parentefeb21b9251bc79c65f527bce23c3bcfd4a3e2fc (diff)
downloadohai-bda5683231607ef55df0264dd6ed0af04c74c6b4.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 76c718ea..1ad6b88a 100644
--- a/lib/ohai/plugins/ec2.rb
+++ b/lib/ohai/plugins/ec2.rb
@@ -99,6 +99,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 d726ead6..46888c6a 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