From 18eebe1d7db35cf82d172db46fc5a592f122f231 Mon Sep 17 00:00:00 2001 From: Dmitry Kolesnikov Date: Wed, 16 Jan 2013 23:31:17 +0200 Subject: [PATCH] fix crash on erlcloud_ec2:describe_instance_attribute if attribute is empty --- .gitignore | 2 ++ Makefile | 6 +++--- src/erlcloud_ec2.erl | 30 +++++++++++++++++++----------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index a3db9623..8b3b4f0c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ _build ebin/*.beam ebin/*.app .eunit/ +rebar +*.sublime-* deps/ diff --git a/Makefile b/Makefile index 9d732e0c..7430175f 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ REBAR=$(shell which rebar || echo ./rebar) +all: get-deps compile + get-deps: @$(REBAR) get-deps -all: compile - clean: @$(REBAR) clean @@ -12,4 +12,4 @@ compile: @$(REBAR) compile eunit: compile - @$(REBAR) eunit skip_deps=true \ No newline at end of file + @$(REBAR) eunit skip_deps=true diff --git a/src/erlcloud_ec2.erl b/src/erlcloud_ec2.erl index c77498be..08dc2671 100644 --- a/src/erlcloud_ec2.erl +++ b/src/erlcloud_ec2.erl @@ -621,19 +621,27 @@ describe_instance_attribute(InstanceID, Attribute, Config) block_device_mapping -> "blockDeviceMapping" end, Doc = ec2_query(Config, "DescribeInstanceAttribute", [{"InstanceId", InstanceID}, {"Attribute", AttributeName}]), - Node = case Attribute of - block_device_mapping -> hd(xmerl_xpath:string("/DescribeInstanceAttributeResponse/" ++ AttributeName, Doc)); - _ -> hd(xmerl_xpath:string("/DescribeInstanceAttributeResponse/" ++ AttributeName ++ "/value", Doc)) - end, - case Attribute of - user_data -> base64:decode(get_text(Node)); - disable_api_termination -> list_to_existing_atom(get_text(Node)); - instance_initiated_shutdown_behavior -> list_to_existing_atom(get_text(Node)); - block_device_mapping -> - [extract_block_device_mapping_status(Item) || Item <- xmerl_xpath:string("item", Node)]; - _ -> get_text(Node) + case xmerl_xpath:string(attribute_xpath(Attribute, AttributeName), Doc) of + % attribute might not be defined + [] -> + undefined; + [Node | _] -> + case Attribute of + user_data -> base64:decode(get_text(Node)); + disable_api_termination -> list_to_existing_atom(get_text(Node)); + instance_initiated_shutdown_behavior -> list_to_existing_atom(get_text(Node)); + block_device_mapping -> + [extract_block_device_mapping_status(Item) || Item <- xmerl_xpath:string("item", Node)]; + _ -> get_text(Node) + end end. +attribute_xpath(block_device_mapping, AttributeName) -> + "/DescribeInstanceAttributeResponse/" ++ AttributeName; +attribute_xpath(_, AttributeName) -> + "/DescribeInstanceAttributeResponse/" ++ AttributeName ++ "/value". + + -spec(describe_instances/0 :: () -> proplist()). describe_instances() -> describe_instances([]).