Passing parameter to RequestScan method of Network Manager service - glib

I want to create a tuple of array of dictionary to pass argument to method RequestScan (IN a{sv} options). I write code as below.
/* Create empty dictionary */
GVariantBuilder *b;
GVariant *option;
b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
option = g_variant_builder_end (b);
/* Start to scan */
dbusRet = g_dbus_proxy_call_sync(wIfProxy,
"RequestScan",
g_variant_new("(a{sv})", option),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
I can build the code, but when run program it output error as below.
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_builder_end: assertion 'ensure_valid_builder (builder)' failed
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_get_type: assertion 'value != NULL' failed
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_type_is_array: assertion 'g_variant_type_check (type)' failed
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_get_type_string: assertion 'value != NULL' failed
(process:35140): GLib-ERROR **: 21:45:35.337: g_variant_new: expected array GVariantBuilder but the built value has type '(null)'
Could you tell me what is the error in the implementation?

Related

Ruby - no implicit conversion of Symbol into Integer

I'm trying to create AWS Security Group but I'm getting the error no implicit conversion of Symbol into Integer during ec2.create_security_group(GroupName=sg_name, VpcId=vpc_id)
I'm following this documentation. Any idea how to resolve the error?
begin
ec2 = get_aws_client
prov = $evm.root['miq_provision_request'] || \
$evm.root['miq_provision'] || \
$evm.root['miq_provision_request_template']
vpc_id = prov.get_option(:dialog_cloud_network).to_s.strip
#prov.get_option(:dialog_vm_name).to_s.strip
sg_name = 'test_sg_manageiq'
#ec2.create_security_group({vpcid: vpc_id, groupname: sg_name})
ec2.create_security_group(GroupName=sg_name, VpcId=vpc_id)
rescue => err
log(:error, "[#{err}]\n#{err.backtrace.join("\n")}")
exit MIQ_ABORT
end
Error:
[----] I, [2022-02-15T16:17:26.360528 #326:2ad4a042a1e0] INFO -- automation: Q-task_id([r49_miq_provision_183]) <AEMethod sg_preprovision> Found provider: AWS via default method
[----] E, [2022-02-15T16:17:26.660450 #326:2ad4a042a1e0] ERROR -- automation: Q-task_id([r49_miq_provision_183]) <AEMethod sg_preprovision> [no implicit conversion of Symbol into Integer]
/ManageIQ_Custom/Cloud/VM/Provisioning/StateMachines/Methods/SG_Preprovision:143:in `<main>'
[----] I, [2022-02-15T16:17:26.707133 #326:2ad4959cb960] INFO -- automation: Q-task_id([r49_miq_provision_183]) <AEMethod [/ManageIQ_Custom/Cloud/VM/Provisioning/StateMachines/Methods/SG_Preprovision]> Ending
[----] E, [2022-02-15T16:17:26.707734 #326:2ad4959cb960] ERROR -- automation: Q-task_id([r49_miq_provision_183]) State=<PreProvision> running raised exception: <Method exited with rc=MIQ_ABORT>
[----] W, [2022-02-15T16:17:26.707927 #326:2ad4959cb960] WARN -- automation: Q-task_id([r49_miq_provision_183]) Error in State=[PreProvision]
An "no implicit conversion of Symbol into Integer" error almost always means that you have an array or string where you're expecting to have a hash. In your case, the suspicious code is:
#ec2.create_security_group({vpcid: vpc_id, groupname: sg_name})
ec2.create_security_group(GroupName=sg_name, VpcId=vpc_id)
The create_security_group call you're using looks more like Python than Ruby to me and is equivalent to:
GroupName = sg_name
VpcId = vpc_id
ec2.create_security_group(GroupName, VpcId)
so you're passing a string (sg_name) to a method that expects a hash of parameters:
#create_security_group(params = {}) ⇒ Types::CreateSecurityGroupResult
Creates a security group.
[...]
Then create_security_group tries to treat sg_name as the params hash it is expecting and that triggers the exception.
I think you want to use the code you commented out and fix the keys to include underscores as per the documentation:
ec2.create_security_group(vpc_id: vpc_id, group_name: sg_name)

Can not run fsm in Erlang

Hello i am trying to run a fsm using gen_statem in a separate process from the caller.I am also trying to keep in its state arguments the caller, so it can be updated after each state change.
I keep getting this error :
** State machine <0.123.0> terminating
** Last event = {internal,init_state}
** When server state = {sitting_home,{state,"None",0,0,[<0.116.0>]}}
** Reason for termination = error:{'function not exported',
{fsm,callback_mode,0}}
** Callback mode = undefined
** Stacktrace =
** [{gen_statem,call_callback_mode,1,[{file,"gen_statem.erl"},{line,1610}]},
{gen_statem,enter,7,[{file,"gen_statem.erl"},{line,679}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,249}]}]
=CRASH REPORT==== 12-Dec-2019::00:14:42.717000 ===
crasher:
initial call: fsm:init/1
pid: <0.123.0>
registered_name: []
exception error: undefined function fsm:callback_mode/0
in function gen_statem:call_callback_mode/1 (gen_statem.erl, line 1610)
in call from gen_statem:enter/7 (gen_statem.erl, line 679)
ancestors: [<0.116.0>]
message_queue_len: 0
messages: []
links: []
dictionary: []
trap_exit: false
status: running
heap_size: 1598
stack_size: 27
reductions: 5805
neighbours:
I have looked in the erlang documentation and i can see no callback mode that needs to be implemented here.
Module
-module(fsm).
-record(state,{
current="None",
intvCount=0,
jobCount=0,
monitor
}).
-export([init/1,start/0]).
-export([hire/2,fire/1,interview/2]).
-behaviour(gen_statem).
start()->
gen_statem:start(?MODULE,[self()],[]).
init(Monitor)->
{ok,sitting_home,#state{current="None",jobCount=0,intvCount=0,monitor=Monitor}}.
sitting_home({intv,Company},State=#state{intvCount=C,monitor=M})->
gen_statem:reply(M,"Got an interview from:"++Company++" going interviewing"),
{next_state,interviewing,State#state{intvCount=C+1}};
sitting_home(Event,State)->
gen_statem:reply(State#state.monitor,{unexpected , Event}),
{next_state,sitting_home,State}.
interviewing({rejected,Company},State)->
gen_statem:reply("Booh got rejected by :"++ Company),
{next_state,sitting_home,State};
interviewing({accepted,Company},State=#state{jobCount=J})->
gen_statem:reply("Hooray got accepted"),
{next_state,working,State#state{jobCount=J+1,current=Company}};
interviewing(_,State)->
gen_statem:reply("Unknown message"),
{next_state,interviewing,State}.
working(fire,State=#state{current=C})->
gen_statem:reply("Unexpected event"),
{next_state,working,State#state{current="None"}};
working(Event,State)->
gen_statem:reply("Unexpected event"),
{next_state,working,State}.
Events
hire(Company,PID)->
gen_statem:sync_send_event(PID,{hire,self(),Company},0).
fire(PID)->
gen_statem:sync_send_event(PID,{fire,self()},0).
interview(Company,PID)->
gen_state:sync_send_event(PID,{intv,Company},0).
P.S I use gen_statem because the shell told me the gen_fsm is deprecated.I do not know the equivalent for sync_send_event.Could this be the problem?
Module:callback_mode is documented at the page you linked. When you have a function for each state (sitting_home, interviewing, etc.) it should be
callback_mode() -> state_functions.
The state functions take 3 arguments, not 2. You are missing the first one, EventType which should be {call, From} in your case.
The equivalent for sync_send_event can just be call, but then your state functions should always reply.
You can combine reply and next_state in the return value of a callback, e.g.
working(Event,State)->
gen_statem:reply("Unexpected event"),
{next_state,working,State}.
can become
working({call, From}, Event, State)->
{next_state, working, State, {reply, From, "Unexpected event"}}.
Even if you don't do this, reply needs a From argument:
working({call, From}, Event, State)->
gen_statem:reply(From, "Unexpected event"),
{next_state,working,State}.

How do I fail a script running in Bitbucket Pipelines?

When a pipeline runs a node series of commands, how can I trigger a fail within the pipeline?
I have tried the following:
const failBuild = function(message) {
console.error('Deploy failed: ', message)
throw new Error('Deploy failed')
}
I see the "Deploy failed" message, but the pipeline still says "Success".
Bb Pipelines fail when a command exits with a non-zero exit code. So, if you want the pipeline to fail, you have to make sure the code is not 0.
In your case (note for people reading this later: see comments), you get 0 as exit status, because the throw is executed in a promise, but then catched in the promise’s catch() function – which does neither stop execution nor have any influence on the exit code.
Solution: explicitly throw an error in the catch() function.
For anyone else who might be struggling with this...
You need to return a non zero as already mentioned, I find the easiest way to do this is by passing a negative integer to PHP's exit() function.
https://php.net/manual/en/function.exit.php
if($condition == true)
{
// Whatever we were doing, it worked YAY!!
exit();
}
else
{
// Something went wrong so fail the step in the pipeline
exit(-1);
}
The accepted answer states:
Solution: explicitly throw an error in the catch() function.
So if I understand that correctly, it suggests you should write the script as:
async function main() { throw "err"; }
main().catch(e => { throw e; });
However, this does not work: the exit code is still 0, and the console displays a nasty warning:
> node "main.js"
(node:32996) UnhandledPromiseRejectionWarning: err
(node:32996) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:32996) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
> $?
0
The correct way to bubble up the error to the node process is:
process.on('unhandledRejection', up => { throw up });
async function main() { throw "err"; }
main();
This way, you get teh following result:
> node "main.js"
test2.js:1
process.on('unhandledRejection', up => { throw up });
^
err
> $?
1
Which is a little bit better (except the stacktrace is not very clear).

Print the exmpp Packet to Raw XML string

How to print the Packet type to raw XML string ? In the example https://github.com/processone/exmpp/blob/master/examples/echo_client.erl the echo_packet(MySession, Packet) -> function takes the parameter Packet which is of xmlel record type. As mentioned in the post https://stackoverflow.com/a/31020654/579689 tried the function xml:element_to_binary/1 but it did not work.
When i tried to print using the following expression
io:format("Received Presence stanza:~n~p~n~n", [xml:element_to_binary({xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"type">>,<<"chat">>},{<<"to">>,<<"x">>},{<<"id">>,<<"aaf6a">>}],[{xmlcdata,<<"\n">>},{xmlel,<<"body">>,[],[{xmlcdata,<<"wewe">>}]},{xmlcdata,<<"\n">>},{xmlel,<<"active">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/chatstates">>}],[]},{xmlcdata,<<"\n">>},{xmlel,<<"time">>,[{<<"xmlns">>,<<"urn:server-time">>},{<<"stamp">>,"2015-06-23T22:48:24Z"}],[]}]})]).
received following error
=ERROR REPORT==== 12-Oct-2015::09:06:01 ===
Error in process <0.30.0> with exit value: {undef,[{xml,element_to_binary,[{xmlel,<<7 bytes>>,[{<<8 bytes>>,<<2 bytes>>},{<<4 bytes>>,<<4 bytes>>},{<<2 bytes>>,<<1 byte>>},{<<2 bytes>>,<<5 bytes>>}],[{xmlcdata,<<1 byte>>},{xmlel,<<4 bytes>>,[],[{xmlcdata,<<4 bytes>>}]},{xmlcdata,<<1 byte>>},{xmlel,<<6 bytes>>,[{<<5 bytes>>,<<37 bytes>>}],[]},{xmlcdata,<<1 byte>>},{xmlel,<<4 bytes>>,[{<<5 bytes>>,<<15 bytes>>},{<<5 bytes>>,"2015-06-23T22:48:24Z"}],[]}]}],[]},{echo_client,loop,1,[{file,"../examples/ech...
Error {undef,[{xml,element_to_binary,[list of N args]}]} tells you that function xml:element_to_binary/N is undefined.
Looking through xml module of the project you may try element_to_string instead of element_to_binary.
However from the point of efficiency exmpp_xml:document_to_iolist/1 will suit you better

Get the remote address on a GSocket

I have a GSocket which is accepted with g_socket_listener_accept_socket(). I want to get the remote address of the connected client, and thought the following code would do it:
GSocket *socket;
GSocketAddress *socket_address;
GInetAddress *inet_address;
gchar *ip_addr;
[...]
socket_address = g_socket_get_remote_address(socket_client, &err);
inet_address = g_object_ref(g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(socket_address)));
ip_addr = g_inet_address_to_string(inet_address);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Login from %s", ip_addr);
g_free(ip_addr);
g_object_unref(socket_address);
However, I get the following error:
(process:15761): GLib-GIO-CRITICAL **: g_inet_address_to_string: assertion `G_IS_INET_ADDRESS (address)' failed
After some debugging, it turned out that the above message is generated by g_inet_address_to_string().
Am I doing something wrong?

Resources