/** * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. * * This software is licensed under the GNU General Public License v3 or later. * * It is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ package com.cloud.api.commands; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; import com.cloud.async.executor.VolumeOperationResultObject; import com.cloud.exception.InvalidParameterValueException; import com.cloud.hypervisor.Hypervisor; import com.cloud.serializer.SerializerHelper; import com.cloud.storage.DiskOfferingVO; import com.cloud.storage.Snapshot; import com.cloud.user.Account; import com.cloud.utils.Pair; public class CreateVolumeCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(CreateVolumeCmd.class.getName()); private static final String s_name = "createvolumeresponse"; private static final List> s_properties = new ArrayList>(); static { s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.TRUE)); s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.DISK_OFFERING_ID, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.SNAPSHOT_ID, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.SIZE, Boolean.FALSE)); } public String getName() { return s_name; } public static String getResultObjectName() { return "volume"; } public List> getProperties() { return s_properties; } @Override public List> execute(Map params) { Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); Long userId = (Long) params.get(BaseCmd.Properties.USER_ID.getName()); String accountName = (String) params.get(BaseCmd.Properties.ACCOUNT.getName()); Long domainId = (Long) params.get(BaseCmd.Properties.DOMAIN_ID.getName()); String name = (String) params.get(BaseCmd.Properties.NAME.getName()); Long zoneId = (Long) params.get(BaseCmd.Properties.ZONE_ID.getName()); Long diskOfferingId = (Long) params.get(BaseCmd.Properties.DISK_OFFERING_ID.getName()); Long snapshotId = (Long)params.get(BaseCmd.Properties.SNAPSHOT_ID.getName()); Long size = (Long)params.get(BaseCmd.Properties.SIZE.getName()); if (account == null) { // Admin API call // Check if accountName was passed in if ((accountName == null) || (domainId == null)) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Account and domainId must be passed in."); } // Look up the account by name and domain ID account = getManagementServer().findActiveAccount(accountName, domainId); // If the account is null, this means that the accountName and domainId passed in were invalid if (account == null) throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account with name: " + accountName + " and domain ID: " + domainId); } else { // User API call // If the account is an admin, and accountName/domainId were passed in, use the account specified by these parameters if (isAdmin(account.getType())) { if (domainId != null) { if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) { throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to create volume in domain " + domainId + ", permission denied."); } if (accountName != null) { account = getManagementServer().findActiveAccount(accountName, domainId); if (account == null) throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account with name: " + accountName + " and domain ID: " + domainId); } } } } // If command is executed via the Admin API, set userId to the id of System account (1) if (userId == null) { userId = Long.valueOf(Account.ACCOUNT_ID_SYSTEM); } if(size==null){ size = Long.valueOf(0); } if(diskOfferingId != null){ DiskOfferingVO dOffering = getManagementServer().findDiskOfferingById(diskOfferingId.longValue()); if(dOffering == null){ throw new ServerApiException(BaseCmd.PARAM_ERROR,"Diskoffering id:"+diskOfferingId+" is invalid"); } } boolean useSnapshot = false; if (snapshotId == null) { if ((zoneId == null)) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Missing parameter,zoneid must be specified."); } if(diskOfferingId == null && size == 0) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Missing parameter(s),either a positive volume size or a valid disk offering id must be specified."); } else if(diskOfferingId == null && size != 0) { //validate the size to ensure between min and max size range try { boolean ok = getManagementServer().validateCustomVolumeSizeRange(size); if(!ok) throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid size for custom volume creation:"); } catch (InvalidParameterValueException e) { s_logger.warn("Invalid size for custom volume creation"); throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid size for custom volume creation:"+e.getMessage()); } //this is the case of creating var size vol with private disk offering List privateTemplateList = getManagementServer().findPrivateDiskOffering(); diskOfferingId = privateTemplateList.get(0).getId(); //we use this id for creating volume, randomly tagging it to a pool with an offering } } else { useSnapshot = true; //Verify parameters Snapshot snapshotCheck = getManagementServer().findSnapshotById(snapshotId); if (snapshotCheck == null) { throw new ServerApiException (BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR, "unable to find a snapshot with id " + snapshotId); } if (account != null) { if (isAdmin(account.getType())) { Account snapshotOwner = getManagementServer().findAccountById(snapshotCheck.getAccountId()); if (!getManagementServer().isChildDomain(account.getDomainId(), snapshotOwner.getDomainId())) { throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to create volume from snapshot with id " + snapshotId + ", permission denied."); } } else if (account.getId() != snapshotCheck.getAccountId()) { throw new ServerApiException(BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR, "unable to find a snapshot with id " + snapshotId + " for this account"); } } } try { long jobId = 0; if (useSnapshot) { jobId = getManagementServer().createVolumeFromSnapshotAsync(userId, account.getId(), snapshotId, name); } else { jobId = getManagementServer().createVolumeAsync(userId, account.getId(), name, zoneId, diskOfferingId, size); } if (jobId == 0) { s_logger.warn("Unable to schedule async-job for CreateVolume command"); } else { if(s_logger.isDebugEnabled()) s_logger.debug("CreateVolume command has been accepted, job id: " + jobId); } long volumeId = waitInstanceCreation(jobId); List> returnValues = new ArrayList>(); returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); returnValues.add(new Pair(BaseCmd.Properties.VOLUME_ID.getName(), Long.valueOf(volumeId))); return returnValues; } catch (Exception ex) { s_logger.error("Failed to create volume " + (useSnapshot ? ("from snapshot " + snapshotId) : ("in zone " + zoneId + " with disk offering " + diskOfferingId)), ex); if (useSnapshot) { throw new ServerApiException(BaseCmd.CREATE_VOLUME_FROM_SNAPSHOT_ERROR, "Unable to create a volume from snapshot with id " + snapshotId + " for this account."); } else { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create volume: " + ex.getMessage()); } } } protected long getInstanceIdFromJobSuccessResult(String result) { VolumeOperationResultObject resultObject = (VolumeOperationResultObject) SerializerHelper.fromSerializedString(result); if(resultObject != null) { return resultObject.getId(); } return 0; } }