/**
  * Handle the event.
  *
  * @param  UserWasCreated $event
  * @return void
  */
 public function handle(UserWasCreated $event, CloudStackClient $acs)
 {
     // $event contains a user object at $event->user
     $groups = $this->repo->all();
     $domainId = SiteConfig::whereParameter('domainId')->first();
     foreach ($groups as $group) {
         // Add each group to the user.
         if (strcasecmp($group->name, 'default') != 0) {
             // Default group doesn't need creating.
             $acs->createSecurityGroup(['name' => $group->name, 'description' => $group->description, 'account' => $event->user->email, 'domainid' => $domainId->data]);
             sleep(5);
             // Make sure the group gets created.
         }
         foreach ($group->ingressRules as $rule) {
             if ($rule->protocol == 'ICMP') {
                 $acs->authorizeSecurityGroupIngress(['account' => $event->user->email, 'domainid' => $domainId->data, 'cidrlist' => $rule->cidr, 'icmpcode' => $rule->icmp_code, 'icmptype' => $rule->icmp_type, 'protocol' => $rule->protocol, 'securitygroupname' => $group->name]);
             } else {
                 $acs->authorizeSecurityGroupIngress(['account' => $event->user->email, 'domainid' => $domainId->data, 'cidrlist' => $rule->cidr, 'startport' => $rule->start_port, 'endport' => $rule->end_port, 'protocol' => $rule->protocol, 'securitygroupname' => $group->name]);
             }
         }
     }
 }
 /**
  * Handle the event.
  *
  * @param  NewAsyncJob  $event
  * @return void
  */
 public function handle(NewAsyncJob $event, CloudStackClient $acs)
 {
     Log::debug('Checking job status!');
     //
     $loopComplete = 0;
     while ($loopComplete == 0) {
         $result = $acs->queryAsyncJobResult(['jobid' => $event->jobId]);
         if ($result->jobstatus == 1) {
             Log::debug('Job finished! We are inside CheckJobStatus.');
             $loopComplete = 1;
             event(new JobFinished($result, $event->userId, $event->userIpAddress));
             switch ($result->jobinstancetype) {
                 case 'VirtualMachine':
                     if ($result->cmd == 'org.apache.cloudstack.api.command.user.vm.DeployVMCmd') {
                         event(new InstanceWasCreated($result->jobresult->virtualmachine));
                     }
                     break;
             }
         }
         sleep(5);
     }
 }