Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/src/main/java/com/cloud/template/TemplateApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd;
import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoPermissionsCmd;
import org.apache.cloudstack.api.command.admin.template.GetSystemVMTemplateDefaultURLCmd;
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
import org.apache.cloudstack.api.command.user.iso.ExtractIsoCmd;
import org.apache.cloudstack.api.command.user.iso.GetUploadParamsForIsoCmd;
Expand All @@ -40,6 +41,7 @@
import com.cloud.exception.StorageUnavailableException;
import com.cloud.user.Account;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.api.response.GetSystemVMTemplateDefaultURLResponse;
import org.apache.cloudstack.api.response.GetUploadParamsResponse;

public interface TemplateApiService {
Expand All @@ -48,6 +50,10 @@ public interface TemplateApiService {

GetUploadParamsResponse registerTemplateForPostUpload(GetUploadParamsForTemplateCmd cmd) throws ResourceAllocationException, MalformedURLException;

GetSystemVMTemplateDefaultURLResponse getSystemVMTemplateDefaultURL(GetSystemVMTemplateDefaultURLCmd cmd);

VirtualMachineTemplate activateSystemVMTemplate(long templateId);

VirtualMachineTemplate registerIso(RegisterIsoCmd cmd) throws IllegalArgumentException, ResourceAllocationException;

GetUploadParamsResponse registerIsoForPostUpload(GetUploadParamsForIsoCmd cmd) throws ResourceAllocationException, MalformedURLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Storage.TemplateType;
import com.cloud.storage.Volume.Event;
import com.cloud.storage.Volume.State;
import com.cloud.utils.fsm.StateMachine2;
import com.cloud.utils.fsm.StateObject;

Expand Down Expand Up @@ -75,6 +73,7 @@ public enum TemplateFilter {
sharedexecutable, // ready templates that have been granted to the calling user by another user
executable, // templates that are owned by the calling user, or public templates, that can be used to deploy a
community, // returns templates that have been marked as public but not featured
system, // system vm templates
all // all templates (only usable by admins)
}

Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public class ApiConstants {
public static final String TARGET_IQN = "targetiqn";
public static final String TEMPLATE_FILTER = "templatefilter";
public static final String TEMPLATE_ID = "templateid";
public static final String SYSTEM = "system";
public static final String ISO_ID = "isoid";
public static final String TIMEOUT = "timeout";
public static final String TIMEZONE = "timezone";
Expand Down Expand Up @@ -629,6 +630,7 @@ public class ApiConstants {
public static final String INTERVAL = "interval";
public static final String QUIETTIME = "quiettime";
public static final String ACTION = "action";
public static final String ACTIVATE = "activate";
public static final String CONDITION_ID = "conditionid";
public static final String CONDITION_IDS = "conditionids";
public static final String COUNTERPARAM_LIST = "counterparam";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.admin.template;

import java.util.List;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiArgValidator;
import org.apache.cloudstack.api.ApiCommandJobType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.TemplateResponse;
import org.apache.log4j.Logger;

import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;

@APICommand(name = ActivateSystemVMTemplateCmd.APINAME,
description = "Activates an existing system virtual machine template to be used by CloudStack to create system virtual machines.",
responseObject = TemplateResponse.class,
authorized = {RoleType.Admin})
public class ActivateSystemVMTemplateCmd extends BaseCmd {

public static final Logger LOGGER = Logger.getLogger(ActivateSystemVMTemplateCmd.class.getName());
public static final String APINAME = "activateSystemVMTemplate";

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.ID,
type = CommandType.UUID,
entityType = TemplateResponse.class,
validations = {ApiArgValidator.PositiveNumber},
required = true,
description = "The template ID of the System VM Template to activate.")
private Long id;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getId() {
return id;
}

/////////////////////////////////////////////////////
/////////////////// Implementation //////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
VirtualMachineTemplate template = _templateService.activateSystemVMTemplate(id);
if (template != null) {
ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
List<TemplateResponse> templateResponses = _responseGenerator.createTemplateResponses(ResponseObject.ResponseView.Restricted,
template, 1L, false);
response.setResponses(templateResponses);
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to activate template.");
}
}

@Override
public String getCommandName() {
return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX;
}


public ApiCommandJobType getInstanceType() {
return ApiCommandJobType.Template;
}

@Override
public long getEntityOwnerId() {
VirtualMachineTemplate template = _entityMgr.findById(VirtualMachineTemplate.class, id);
if (template != null) {
return template.getAccountId();
}
// bad id given, parent this command to SYSTEM so ERROR events are tracked
return Account.ACCOUNT_ID_SYSTEM;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.admin.template;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiArgValidator;
import org.apache.cloudstack.api.ApiCommandJobType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.GetSystemVMTemplateDefaultURLResponse;
import org.apache.cloudstack.api.response.TemplateResponse;
import org.apache.log4j.Logger;

import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;

@APICommand(name = GetSystemVMTemplateDefaultURLCmd.APINAME,
description = "Gets the system virtual machine template's default download URL.",
responseObject = GetSystemVMTemplateDefaultURLResponse.class,
authorized = {RoleType.Admin})
public class GetSystemVMTemplateDefaultURLCmd extends BaseCmd {

public static final Logger LOGGER = Logger.getLogger(GetSystemVMTemplateDefaultURLCmd.class.getName());
public static final String APINAME = "getSystemVMTemplateDefaultUrl";

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.VERSION,
type = CommandType.STRING,
entityType = TemplateResponse.class,
description = "The CloudStack version for which to get the System VM Template URL.")
private String version;

@Parameter(name = ApiConstants.HYPERVISOR,
type = CommandType.STRING,
entityType = TemplateResponse.class,
validations = {ApiArgValidator.NotNullOrEmpty},
required = true,
description = "The hypervisor for which to get the System VM Template URL.")
private String hypervisor;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public String getVersion() {
return version;
}

public String getHypervisor() {
return hypervisor;
}

/////////////////////////////////////////////////////
/////////////////// Implementation //////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
GetSystemVMTemplateDefaultURLResponse response = _templateService.getSystemVMTemplateDefaultURL(this);
if (response != null) {
response.setObjectName("url");
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to find the URL for version '%s' and hypervisor '%s'", version, hypervisor));
}
}

@Override
public String getCommandName() {
return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX;
}

public ApiCommandJobType getInstanceType() {
return ApiCommandJobType.Template;
}

@Override
public long getEntityOwnerId() {
return Account.ACCOUNT_ID_SYSTEM;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public class GetUploadParamsForTemplateCmd extends AbstractGetUploadParamsCmd {
@Parameter(name = ApiConstants.TEMPLATE_TAG, type = CommandType.STRING, description = "the tag for this template.")
private String templateTag;

@Parameter(name = ApiConstants.SYSTEM, type = CommandType.BOOLEAN, authorized = {RoleType.Admin}, description = "true if it is a system vm template.")
private Boolean isSystem;

public String getDisplayText() {
return displayText;
}
Expand Down Expand Up @@ -151,6 +154,14 @@ public String getTemplateTag() {
return templateTag;
}

public Boolean getSystem() {
return isSystem;
}

public void setSystem(Boolean system) {
isSystem = system;
}

@Override
public void execute() throws ServerApiException {
validateRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
// under the License.
package org.apache.cloudstack.api.command.user.template;

import org.apache.log4j.Logger;

import java.util.List;

import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiCommandJobType;
import org.apache.cloudstack.api.ApiConstants;
Expand All @@ -29,6 +28,7 @@
import org.apache.cloudstack.api.response.TemplateResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.log4j.Logger;

import com.cloud.template.VirtualMachineTemplate;
import com.cloud.template.VirtualMachineTemplate.TemplateFilter;
Expand Down Expand Up @@ -60,13 +60,14 @@ public class ListTemplatesCmd extends BaseListTaggedResourcesCmd {
@Parameter(name = ApiConstants.TEMPLATE_FILTER,
type = CommandType.STRING,
required = true,
description = "possible values are \"featured\", \"self\", \"selfexecutable\",\"sharedexecutable\",\"executable\", and \"community\". "
description = "possible values are \"featured\", \"self\", \"selfexecutable\",\"sharedexecutable\",\"executable\",\"community\" and \"system\". "
+ "* featured : templates that have been marked as featured and public. "
+ "* self : templates that have been registered or created by the calling user. "
+ "* selfexecutable : same as self, but only returns templates that can be used to deploy a new VM. "
+ "* sharedexecutable : templates ready to be deployed that have been granted to the calling user by another user. "
+ "* executable : templates that are owned by the calling user, or public templates, that can be used to deploy a VM. "
+ "* community : templates that have been marked as public but not featured. " + "* all : all templates (only usable by admins).")
+ "* community : templates that have been marked as public but not featured. " + "* all : all templates (only usable by admins)."
+ "* system: system templates for console proxy, secondary storage and routers.")
private String templateFilter;

@Parameter(name = ApiConstants.ZONE_ID, type = CommandType.UUID, entityType = ZoneResponse.class, description = "list templates by zoneId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiCommandJobType;
import org.apache.cloudstack.api.ApiConstants;
Expand Down Expand Up @@ -158,9 +160,22 @@ public class RegisterTemplateCmd extends BaseCmd {

@Parameter(name=ApiConstants.DIRECT_DOWNLOAD,
type = CommandType.BOOLEAN,
authorized = {RoleType.Admin},
description = "true if template should bypass Secondary Storage and be downloaded to Primary Storage on deployment")
private Boolean directDownload;

@Parameter(name = ApiConstants.SYSTEM,
type = CommandType.BOOLEAN,
authorized = {RoleType.Admin},
description = "true if it is a system vm template.")
private Boolean isSystem;

@Parameter(name=ApiConstants.ACTIVATE,
type = CommandType.BOOLEAN,
authorized = {RoleType.Admin},
description = "true if this template should be used by CloudStack to create System VMs. Must be used with template type of 'system'.")
private Boolean activate;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand Down Expand Up @@ -273,6 +288,14 @@ public boolean isDirectDownload() {
return directDownload == null ? false : directDownload;
}

public Boolean isSystem() {
return Optional.ofNullable(isSystem).orElse(false);
}

public Boolean isActivate() {
return Optional.ofNullable(activate).orElse(false);
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand Down Expand Up @@ -323,7 +346,7 @@ protected void validateParameters() {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR,
"Both zoneid and zoneids cannot be specified at the same time");

if (zoneId == null && (zoneIds == null || zoneIds.isEmpty()))
if ((zoneId == null && (zoneIds == null || zoneIds.isEmpty())) && !isSystem())
throw new ServerApiException(ApiErrorCode.PARAM_ERROR,
"Either zoneid or zoneids is required. Both cannot be null.");

Expand Down
Loading