コード例 #1
0
ファイル: ApiServer.java プロジェクト: ngtuna/cloudstack-gre
  @SuppressWarnings("unchecked")
  private void buildAsyncListResponse(BaseListCmd command, Account account) {
    List<ResponseObject> responses = ((ListResponse) command.getResponseObject()).getResponses();
    if (responses != null && responses.size() > 0) {
      List<? extends AsyncJob> jobs = null;

      // list all jobs for ROOT admin
      if (account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
        jobs = _asyncMgr.findInstancePendingAsyncJobs(command.getInstanceType().toString(), null);
      } else {
        jobs =
            _asyncMgr.findInstancePendingAsyncJobs(
                command.getInstanceType().toString(), account.getId());
      }

      if (jobs.size() == 0) {
        return;
      }

      Map<String, AsyncJob> objectJobMap = new HashMap<String, AsyncJob>();
      for (AsyncJob job : jobs) {
        if (job.getInstanceId() == null) {
          continue;
        }
        String instanceUuid = ApiDBUtils.findJobInstanceUuid(job);
        objectJobMap.put(instanceUuid, job);
      }

      for (ResponseObject response : responses) {
        if (response.getObjectId() != null && objectJobMap.containsKey(response.getObjectId())) {
          AsyncJob job = objectJobMap.get(response.getObjectId());
          response.setJobId(job.getUuid());
          response.setJobStatus(job.getStatus().ordinal());
        }
      }
    }
  }
コード例 #2
0
ファイル: ApiServer.java プロジェクト: ngtuna/cloudstack-gre
  private String queueCommand(BaseCmd cmdObj, Map<String, String> params) throws Exception {
    CallContext ctx = CallContext.current();
    Long callerUserId = ctx.getCallingUserId();
    Account caller = ctx.getCallingAccount();

    // Queue command based on Cmd super class:
    // BaseCmd: cmd is dispatched to ApiDispatcher, executed, serialized and returned.
    // BaseAsyncCreateCmd: cmd params are processed and create() is called, then same workflow as
    // BaseAsyncCmd.
    // BaseAsyncCmd: cmd is processed and submitted as an AsyncJob, job related info is serialized
    // and returned.
    if (cmdObj instanceof BaseAsyncCmd) {
      Long objectId = null;
      String objectUuid = null;
      if (cmdObj instanceof BaseAsyncCreateCmd) {
        BaseAsyncCreateCmd createCmd = (BaseAsyncCreateCmd) cmdObj;
        _dispatcher.dispatchCreateCmd(createCmd, params);
        objectId = createCmd.getEntityId();
        objectUuid = createCmd.getEntityUuid();
        params.put("id", objectId.toString());
      } else {
        ApiDispatcher.processParameters(cmdObj, params);
      }

      BaseAsyncCmd asyncCmd = (BaseAsyncCmd) cmdObj;

      if (callerUserId != null) {
        params.put("ctxUserId", callerUserId.toString());
      }
      if (caller != null) {
        params.put("ctxAccountId", String.valueOf(caller.getId()));
      }

      long startEventId = ctx.getStartEventId();
      asyncCmd.setStartEventId(startEventId);

      // save the scheduled event
      Long eventId =
          ActionEventUtils.onScheduledActionEvent(
              (callerUserId == null) ? User.UID_SYSTEM : callerUserId,
              asyncCmd.getEntityOwnerId(),
              asyncCmd.getEventType(),
              asyncCmd.getEventDescription(),
              startEventId);
      if (startEventId == 0) {
        // There was no create event before, set current event id as start eventId
        startEventId = eventId;
      }

      params.put("ctxStartEventId", String.valueOf(startEventId));
      params.put("cmdEventType", asyncCmd.getEventType().toString());

      Long instanceId = (objectId == null) ? asyncCmd.getInstanceId() : objectId;
      AsyncJobVO job =
          new AsyncJobVO(
              ctx.getContextId(),
              callerUserId,
              caller.getId(),
              cmdObj.getClass().getName(),
              ApiGsonHelper.getBuilder().create().toJson(params),
              instanceId,
              asyncCmd.getInstanceType() != null ? asyncCmd.getInstanceType().toString() : null);
      job.setDispatcher(_asyncDispatcher.getName());

      long jobId = _asyncMgr.submitAsyncJob(job);

      if (jobId == 0L) {
        String errorMsg = "Unable to schedule async job for command " + job.getCmd();
        s_logger.warn(errorMsg);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, errorMsg);
      }

      if (objectId != null) {
        String objUuid = (objectUuid == null) ? objectId.toString() : objectUuid;
        return getBaseAsyncCreateResponse(jobId, (BaseAsyncCreateCmd) asyncCmd, objUuid);
      } else {
        SerializationContext.current().setUuidTranslation(true);
        return getBaseAsyncResponse(jobId, asyncCmd);
      }
    } else {
      _dispatcher.dispatch(cmdObj, params, false);

      // if the command is of the listXXXCommand, we will need to also return the
      // the job id and status if possible
      // For those listXXXCommand which we have already created DB views, this step is not needed
      // since async job is joined in their db views.
      if (cmdObj instanceof BaseListCmd
          && !(cmdObj instanceof ListVMsCmd)
          && !(cmdObj instanceof ListRoutersCmd)
          && !(cmdObj instanceof ListSecurityGroupsCmd)
          && !(cmdObj instanceof ListTagsCmd)
          && !(cmdObj instanceof ListEventsCmd)
          && !(cmdObj instanceof ListVMGroupsCmd)
          && !(cmdObj instanceof ListProjectsCmd)
          && !(cmdObj instanceof ListProjectAccountsCmd)
          && !(cmdObj instanceof ListProjectInvitationsCmd)
          && !(cmdObj instanceof ListHostsCmd)
          && !(cmdObj instanceof ListVolumesCmd)
          && !(cmdObj instanceof ListUsersCmd)
          && !(cmdObj instanceof ListAccountsCmd)
          && !(cmdObj instanceof ListStoragePoolsCmd)
          && !(cmdObj instanceof ListDiskOfferingsCmd)
          && !(cmdObj instanceof ListServiceOfferingsCmd)
          && !(cmdObj instanceof ListZonesByCmd)) {
        buildAsyncListResponse((BaseListCmd) cmdObj, caller);
      }

      SerializationContext.current().setUuidTranslation(true);
      return ApiResponseSerializer.toSerializedString(
          (ResponseObject) cmdObj.getResponseObject(), cmdObj.getResponseType());
    }
  }