|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command.user.schedule; |
| 18 | + |
| 19 | +import com.cloud.exception.InvalidParameterValueException; |
| 20 | +import org.apache.cloudstack.acl.RoleType; |
| 21 | +import org.apache.cloudstack.api.APICommand; |
| 22 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 23 | +import org.apache.cloudstack.api.ApiConstants; |
| 24 | +import org.apache.cloudstack.api.BaseCmd; |
| 25 | +import org.apache.cloudstack.api.Parameter; |
| 26 | +import org.apache.cloudstack.api.response.ResourceScheduleResponse; |
| 27 | +import org.apache.cloudstack.context.CallContext; |
| 28 | +import org.apache.cloudstack.schedule.ResourceScheduleManager; |
| 29 | +import org.apache.commons.lang3.EnumUtils; |
| 30 | + |
| 31 | +import javax.inject.Inject; |
| 32 | +import java.util.Date; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +@APICommand(name = "createResourceSchedule", description = "Create Resource Schedule", responseObject = ResourceScheduleResponse.class, |
| 36 | + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, since = "4.23.0", |
| 37 | + authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User}) |
| 38 | +public class CreateResourceScheduleCmd extends BaseCmd { |
| 39 | + |
| 40 | + @Inject |
| 41 | + ResourceScheduleManager resourceScheduleManager; |
| 42 | + |
| 43 | + @Parameter(name = ApiConstants.RESOURCE_TYPE, type = CommandType.STRING, required = true, description = "Type of the resource") |
| 44 | + private String resourceType; |
| 45 | + |
| 46 | + @Parameter(name = ApiConstants.RESOURCE_ID, type = CommandType.STRING, required = true, description = "ID of the resource for which schedule is to be defined") |
| 47 | + private String resourceId; |
| 48 | + |
| 49 | + @Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, required = false, description = "Description of the schedule") |
| 50 | + private String description; |
| 51 | + |
| 52 | + @Parameter(name = ApiConstants.SCHEDULE, type = CommandType.STRING, required = true, description = "Schedule for action on resource in cron format. e.g. '0 15 10 * *' for 'at 15:00 on 10th day of every month'") |
| 53 | + private String schedule; |
| 54 | + |
| 55 | + @Parameter(name = ApiConstants.TIMEZONE, type = CommandType.STRING, required = true, description = "Specifies a timezone for this command. For more information on the timezone parameter, see TimeZone Format.") |
| 56 | + private String timeZone; |
| 57 | + |
| 58 | + @Parameter(name = ApiConstants.ACTION, type = CommandType.STRING, required = true, description = "Action to take on the resource.") |
| 59 | + private String action; |
| 60 | + |
| 61 | + @Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, required = false, description = "Start date from which the schedule becomes active. Defaults to current date plus 1 minute. (Format \"yyyy-MM-dd hh:mm:ss\")") |
| 62 | + private Date startDate; |
| 63 | + |
| 64 | + @Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, required = false, description = "End date after which the schedule becomes inactive. (Format \"yyyy-MM-dd hh:mm:ss\")") |
| 65 | + private Date endDate; |
| 66 | + |
| 67 | + @Parameter(name = ApiConstants.ENABLED, type = CommandType.BOOLEAN, required = false, description = "Enable schedule. Defaults to true") |
| 68 | + private Boolean enabled; |
| 69 | + |
| 70 | + @Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, required = false, description = "Map of (key/value pairs) details for the schedule.") |
| 71 | + private Map details; |
| 72 | + |
| 73 | + public ApiCommandResourceType getResourceType() { |
| 74 | + ApiCommandResourceType type = EnumUtils.getEnumIgnoreCase(ApiCommandResourceType.class, resourceType); |
| 75 | + if (type == null) { |
| 76 | + throw new InvalidParameterValueException("Unknown resource type: " + resourceType); |
| 77 | + } |
| 78 | + return type; |
| 79 | + } |
| 80 | + |
| 81 | + public String getResourceId() { |
| 82 | + return resourceId; |
| 83 | + } |
| 84 | + |
| 85 | + public String getDescription() { |
| 86 | + return description; |
| 87 | + } |
| 88 | + |
| 89 | + public String getSchedule() { |
| 90 | + return schedule; |
| 91 | + } |
| 92 | + |
| 93 | + public String getTimeZone() { |
| 94 | + return timeZone; |
| 95 | + } |
| 96 | + |
| 97 | + public String getAction() { |
| 98 | + return action; |
| 99 | + } |
| 100 | + |
| 101 | + public Date getStartDate() { |
| 102 | + return startDate; |
| 103 | + } |
| 104 | + |
| 105 | + public Date getEndDate() { |
| 106 | + return endDate; |
| 107 | + } |
| 108 | + |
| 109 | + public Boolean getEnabled() { |
| 110 | + if (enabled == null) { |
| 111 | + enabled = true; |
| 112 | + } |
| 113 | + return enabled; |
| 114 | + } |
| 115 | + |
| 116 | + public Map<String, String> getDetails() { |
| 117 | + return convertDetailsToMap(details); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void execute() { |
| 122 | + ResourceScheduleResponse response = resourceScheduleManager.createSchedule(getResourceType(), getResourceId(), |
| 123 | + getDescription(), getSchedule(), getTimeZone(), getAction(), getStartDate(), getEndDate(), getEnabled(), getDetails()); |
| 124 | + response.setResponseName(getCommandName()); |
| 125 | + setResponseObject(response); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public long getEntityOwnerId() { |
| 130 | + return CallContext.current().getCallingAccount().getAccountId(); |
| 131 | + } |
| 132 | +} |
0 commit comments