Skip to content
Browse files

Add a convenience function wrapping *LookupBy...

  • Loading branch information...
1 parent 3f3b463 commit 64941c968f51f52b1db3b8874633b99ed9867eb2 @msantos committed Aug 30, 2012
Showing with 48 additions and 1 deletion.
  1. +47 −0 src/vert.erl
  2. +1 −1 src/vert_console.erl
View
47 src/vert.erl
@@ -40,6 +40,9 @@
-define(INT64(N), N:8/native-integer-unit:8).
-export([
+ lookup/2
+ ]).
+-export([
virNodeGetCellsFreeMemory/2,
virNodeGetFreeMemory/1,
virNodeGetInfo/1,
@@ -1120,6 +1123,50 @@ virConnectFindStoragePoolSources(#resource{type = connect, res = Res}, Type, Src
% Called by virConnectPtr dtor
%#%virConnectClose(#resource{type = connect, res = Res})
+%%-------------------------------------------------------------------------
+%%% Utility functions
+%%-------------------------------------------------------------------------
+
+% Lookup a resource
+lookup(Connect, {domain, Name}) ->
+ Fun = [ fun() -> vert:virDomainLookupByID(Connect, list_to_integer(Name)) end,
+ fun() -> vert:virDomainLookupByUUIDString(Connect, Name) end,
+ fun() -> vert:virDomainLookupByName(Connect, Name) end ],
+ lookup_1(Fun);
+lookup(Connect, {network, Name}) ->
+ Fun = [ fun() -> vert:virNetworkLookupByUUIDString(Connect, Name) end,
+ fun() -> vert:virNetworkLookupByName(Connect, Name) end ],
+ lookup_1(Fun);
+lookup(Connect, {nwfilter, Name}) ->
+ Fun = [ fun() -> vert:virNWFilterLookupByUUIDString(Connect, Name) end,
+ fun() -> vert:virNWFilterLookupByName(Connect, Name) end ],
+ lookup_1(Fun);
+lookup(Connect, {secret, Name}) ->
+ Fun = [ fun() -> vert:virSecretLookupByUUIDString(Connect, Name) end ],
+ lookup_1(Fun);
+lookup(Connect, {storagepool, Name}) ->
+ Fun = [ fun() -> vert:virStoragePoolLookupByUUIDString(Connect, Name) end,
+ fun() -> vert:virStoragePoolLookupByName(Connect, Name) end ],
+ lookup_1(Fun);
+lookup(Connect, {storagevol, Name}) ->
+ Fun = [ fun() -> vert:virStorageVolLookupByKey(Connect, Name) end,
+ fun() -> vert:virStorageVolLookupByPath(Connect, Name) end ],
+ lookup_1(Fun).
+
+lookup_1(Fun) ->
+ lookup_1(Fun, []).
+lookup_1([], [{error, Error}|_]) ->
+ {error, Error};
+lookup_1([Fun|Tail], Acc) ->
+ try Fun() of
+ {ok, Res} ->
+ {ok, Res};
+ {error, Error} ->
+ lookup_1(Tail, [{error, Error}|Acc])
+ catch
+ _:_ ->
+ lookup_1(Tail, Acc)
+ end.
%%-------------------------------------------------------------------------
%%% NIF stubs
View
2 src/vert_console.erl
@@ -168,7 +168,7 @@ init([Pid, Host,
process_flag(trap_exit, true),
{ok, Connect} = vert:virConnectOpen(URI),
- {ok, Domain} = vert:virDomainLookupByName(Connect, Host),
+ {ok, Domain} = vert:lookup(Connect, {domain, Host}),
{ok, Stream} = vert:virStreamNew(Connect),
ok = vert:virDomainOpenConsole(Domain, Stream),

0 comments on commit 64941c9

Please sign in to comment.
Something went wrong with that request. Please try again.