Skip to content

Commit b315ca1

Browse files
author
Alena Prokharchyk
committed
CLOUDSTACK-5261: support for alert publishing via Root admin web api
listAlerts: introduced new parameter "name" to the alertResponse Added new Admin API - generateAlert. Available to ROOT admin only listAlerts: implemented search by alert name
1 parent 8367a8f commit b315ca1

36 files changed

Lines changed: 450 additions & 243 deletions

File tree

‎api/src/com/cloud/alert/Alert.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ public interface Alert extends Identity, InternalIdentity {
3131
Date getLastSent();
3232
Date getResolved();
3333
boolean getArchived();
34+
String getName();
3435
}

‎api/src/com/cloud/event/EventTypes.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ public class EventTypes {
456456
// Object store migration
457457
public static final String EVENT_MIGRATE_PREPARE_SECONDARY_STORAGE = "MIGRATE.PREPARE.SS";
458458

459+
//Alert generation
460+
public static final String ALERT_GENERATE = "ALERT.GENERATE";
461+
462+
459463
static {
460464

461465
// TODO: need a way to force author adding event types to declare the entity details as well, with out braking
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.alert;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import com.cloud.capacity.Capacity;
23+
import com.cloud.exception.InvalidParameterValueException;
24+
25+
public interface AlertService {
26+
public static class AlertType {
27+
private static Set<AlertType> defaultAlertTypes = new HashSet<AlertType>();
28+
private final String name;
29+
private final short type;
30+
31+
private AlertType(short type, String name, boolean isDefault) {
32+
this.name = name;
33+
this.type = type;
34+
if (isDefault) {
35+
defaultAlertTypes.add(this);
36+
}
37+
}
38+
39+
public static final AlertType ALERT_TYPE_MEMORY = new AlertType(Capacity.CAPACITY_TYPE_MEMORY, "ALERT.MEMORY", true);
40+
public static final AlertType ALERT_TYPE_CPU = new AlertType(Capacity.CAPACITY_TYPE_CPU, "ALERT.CPU", true);
41+
public static final AlertType ALERT_TYPE_STORAGE = new AlertType(Capacity.CAPACITY_TYPE_STORAGE, "ALERT.STORAGE", true);
42+
public static final AlertType ALERT_TYPE_STORAGE_ALLOCATED = new AlertType(Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, "ALERT.STORAGE.ALLOCATED", true);
43+
public static final AlertType ALERT_TYPE_VIRTUAL_NETWORK_PUBLIC_IP = new AlertType(Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP, "ALERT.NETWORK.PUBLICIP", true);
44+
public static final AlertType ALERT_TYPE_PRIVATE_IP = new AlertType(Capacity.CAPACITY_TYPE_PRIVATE_IP, "ALERT.NETWORK.PRIVATEIP", true);
45+
public static final AlertType ALERT_TYPE_SECONDARY_STORAGE = new AlertType(Capacity.CAPACITY_TYPE_SECONDARY_STORAGE, "ALERT.STORAGE.SECONDARY", true);
46+
public static final AlertType ALERT_TYPE_HOST = new AlertType((short)7, "ALERT.COMPUTE.HOST", true);
47+
public static final AlertType ALERT_TYPE_USERVM = new AlertType((short)8, "ALERT.USERVM", true);
48+
public static final AlertType ALERT_TYPE_DOMAIN_ROUTER = new AlertType((short)9, "ALERT.SERVICE.DOMAINROUTER", true);
49+
public static final AlertType ALERT_TYPE_CONSOLE_PROXY = new AlertType((short)10, "ALERT.SERVICE.CONSOLEPROXY", true);
50+
public static final AlertType ALERT_TYPE_ROUTING = new AlertType((short)11, "ALERT.NETWORK.ROUTING", true);
51+
public static final AlertType ALERT_TYPE_STORAGE_MISC = new AlertType((short)12, "ALERT.STORAGE.MISC", true);
52+
public static final AlertType ALERT_TYPE_USAGE_SERVER = new AlertType((short)13, "ALERT.USAGE", true);
53+
public static final AlertType ALERT_TYPE_MANAGMENT_NODE = new AlertType((short)14, "ALERT.MANAGEMENT", true);
54+
public static final AlertType ALERT_TYPE_DOMAIN_ROUTER_MIGRATE = new AlertType((short)15, "ALERT.NETWORK.DOMAINROUTERMIGRATE", true);
55+
public static final AlertType ALERT_TYPE_CONSOLE_PROXY_MIGRATE = new AlertType((short)16, "ALERT.SERVICE.CONSOLEPROXYMIGRATE", true);
56+
public static final AlertType ALERT_TYPE_USERVM_MIGRATE = new AlertType((short)17, "ALERT.USERVM.MIGRATE", true);
57+
public static final AlertType ALERT_TYPE_VLAN = new AlertType((short)18, "ALERT.NETWORK.VLAN", true);
58+
public static final AlertType ALERT_TYPE_SSVM = new AlertType((short)19, "ALERT.SERVICE.SSVM", true);
59+
public static final AlertType ALERT_TYPE_USAGE_SERVER_RESULT = new AlertType((short)20, "ALERT.USAGE.RESULT", true);
60+
public static final AlertType ALERT_TYPE_STORAGE_DELETE = new AlertType((short)21, "ALERT.STORAGE.DELETE", true);
61+
public static final AlertType ALERT_TYPE_UPDATE_RESOURCE_COUNT = new AlertType((short)22, "ALERT.RESOURCE.COUNT", true);
62+
public static final AlertType ALERT_TYPE_USAGE_SANITY_RESULT = new AlertType((short)23, "ALERT.USAGE.SANITY", true);
63+
public static final AlertType ALERT_TYPE_DIRECT_ATTACHED_PUBLIC_IP = new AlertType((short)24, "ALERT.NETWORK.DIRECTPUBLICIP", true);
64+
public static final AlertType ALERT_TYPE_LOCAL_STORAGE = new AlertType((short)25, "ALERT.STORAGE.LOCAL", true);
65+
public static final AlertType ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED = new AlertType((short)26, "ALERT.RESOURCE.EXCEED", true);
66+
public static final AlertType ALERT_TYPE_SYNC = new AlertType((short)27, "ALERT.TYPE.SYNC", true);
67+
68+
public short getType() {
69+
return type;
70+
}
71+
72+
public String getName() {
73+
return name;
74+
}
75+
76+
77+
private static AlertType getAlertType(short type) {
78+
for (AlertType alertType : defaultAlertTypes) {
79+
if (alertType.getType() == type) {
80+
return alertType;
81+
}
82+
}
83+
return null;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return String.valueOf(this.getType());
89+
}
90+
91+
public static AlertType generateAlert(short type, String name) {
92+
AlertType defaultAlert = getAlertType(type);
93+
if (defaultAlert != null && !defaultAlert.getName().equalsIgnoreCase(name)) {
94+
throw new InvalidParameterValueException("There is a default alert having type " + type + " and name " + defaultAlert.getName());
95+
} else {
96+
return new AlertType(type, name, false);
97+
}
98+
}
99+
}
100+
101+
boolean generateAlert(AlertType alertType, long dataCenterId, Long podId, String msg);
102+
103+
}

‎api/src/org/apache/cloudstack/api/BaseCmd.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@
2727
import javax.inject.Inject;
2828

2929
import org.apache.cloudstack.affinity.AffinityGroupService;
30-
31-
import com.cloud.server.ResourceMetaDataService;
32-
30+
import org.apache.cloudstack.alert.AlertService;
3331
import org.apache.cloudstack.network.element.InternalLoadBalancerElementService;
3432
import org.apache.cloudstack.network.lb.ApplicationLoadBalancerService;
3533
import org.apache.cloudstack.network.lb.InternalLoadBalancerVMService;
3634
import org.apache.cloudstack.query.QueryService;
3735
import org.apache.cloudstack.usage.UsageService;
38-
3936
import org.apache.log4j.Logger;
4037

4138
import com.cloud.configuration.ConfigurationService;
@@ -54,10 +51,10 @@
5451
import com.cloud.network.VpcVirtualNetworkApplianceService;
5552
import com.cloud.network.as.AutoScaleService;
5653
import com.cloud.network.firewall.FirewallService;
57-
import com.cloud.network.vpc.NetworkACLService;
5854
import com.cloud.network.lb.LoadBalancingRulesService;
5955
import com.cloud.network.rules.RulesService;
6056
import com.cloud.network.security.SecurityGroupService;
57+
import com.cloud.network.vpc.NetworkACLService;
6158
import com.cloud.network.vpc.VpcProvisioningService;
6259
import com.cloud.network.vpc.VpcService;
6360
import com.cloud.network.vpn.RemoteAccessVpnService;
@@ -66,6 +63,7 @@
6663
import com.cloud.projects.ProjectService;
6764
import com.cloud.resource.ResourceService;
6865
import com.cloud.server.ManagementService;
66+
import com.cloud.server.ResourceMetaDataService;
6967
import com.cloud.server.TaggedResourceService;
7068
import com.cloud.storage.DataStoreProviderApiService;
7169
import com.cloud.storage.StorageService;
@@ -151,6 +149,7 @@ public enum HTTPMethod {
151149
@Inject public InternalLoadBalancerElementService _internalLbElementSvc;
152150
@Inject public InternalLoadBalancerVMService _internalLbSvc;
153151
@Inject public NetworkModel _ntwkModel;
152+
@Inject public AlertService _alertSvc;
154153

155154
public abstract void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException;
156155

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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.admin.alert;
18+
19+
import org.apache.cloudstack.alert.AlertService;
20+
import org.apache.cloudstack.alert.AlertService.AlertType;
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.BaseAsyncCmd;
25+
import org.apache.cloudstack.api.Parameter;
26+
import org.apache.cloudstack.api.ServerApiException;
27+
import org.apache.cloudstack.api.response.PodResponse;
28+
import org.apache.cloudstack.api.response.SuccessResponse;
29+
import org.apache.cloudstack.api.response.ZoneResponse;
30+
import org.apache.log4j.Logger;
31+
32+
import com.cloud.event.EventTypes;
33+
34+
@APICommand(name = "generateAlert", description = "Generates an alert", responseObject = SuccessResponse.class, since="4.3")
35+
public class GenerateAlertCmd extends BaseAsyncCmd {
36+
37+
public static final Logger s_logger = Logger.getLogger(GenerateAlertCmd.class.getName());
38+
39+
private static final String s_name = "generatealertresponse";
40+
41+
// ///////////////////////////////////////////////////
42+
// ////////////// API parameters /////////////////////
43+
// ///////////////////////////////////////////////////
44+
45+
@Parameter(name = ApiConstants.TYPE, type = CommandType.SHORT, description = "Type of the alert", required=true)
46+
private Short type;
47+
48+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "Name of the alert", required=true)
49+
private String name;
50+
51+
@Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "Alert description", required=true)
52+
private String description;
53+
54+
@Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType=ZoneResponse.class, description="Zone id for which alert is generated")
55+
private Long zoneId;
56+
57+
@Parameter(name=ApiConstants.POD_ID, type=CommandType.UUID, entityType=PodResponse.class, description="Pod id for which alert is generated")
58+
private Long podId;
59+
60+
// ///////////////////////////////////////////////////
61+
// ///////////////// Accessors ///////////////////////
62+
// ///////////////////////////////////////////////////
63+
@Override
64+
public String getCommandName() {
65+
return s_name;
66+
}
67+
68+
public Short getType() {
69+
return type;
70+
}
71+
72+
public String getName() {
73+
return name;
74+
}
75+
76+
public String getDescription() {
77+
return description;
78+
}
79+
80+
public long getZoneId() {
81+
if (zoneId == null) {
82+
return 0L;
83+
}
84+
return zoneId;
85+
}
86+
87+
public Long getPodId() {
88+
return podId;
89+
}
90+
91+
92+
// ///////////////////////////////////////////////////
93+
// ///////////// API Implementation///////////////////
94+
// ///////////////////////////////////////////////////
95+
96+
97+
98+
@Override
99+
public void execute() {
100+
AlertType alertType = AlertService.AlertType.generateAlert(getType(), getName());
101+
if (_alertSvc.generateAlert(alertType, getZoneId(), getPodId(), getDescription())) {
102+
SuccessResponse response = new SuccessResponse(getCommandName());
103+
this.setResponseObject(response);
104+
} else {
105+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate an alert");
106+
}
107+
}
108+
109+
@Override
110+
public String getEventType() {
111+
return EventTypes.ALERT_GENERATE;
112+
}
113+
114+
@Override
115+
public String getEventDescription() {
116+
return "Generating alert of type " + type + "; name " + name;
117+
}
118+
119+
@Override
120+
public long getEntityOwnerId() {
121+
return 0;
122+
}
123+
}

‎api/src/org/apache/cloudstack/api/command/admin/resource/ListAlertsCmd.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public class ListAlertsCmd extends BaseListCmd {
4747

4848
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, description = "list by alert type")
4949
private String type;
50+
51+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "list by alert name", since="4.3")
52+
private String name;
5053

5154
// ///////////////////////////////////////////////////
5255
// ///////////////// Accessors ///////////////////////
@@ -59,6 +62,10 @@ public Long getId() {
5962
public String getType() {
6063
return type;
6164
}
65+
66+
public String getName() {
67+
return name;
68+
}
6269

6370
// ///////////////////////////////////////////////////
6471
// ///////////// API Implementation///////////////////
@@ -80,6 +87,7 @@ public void execute() {
8087
alertResponse.setAlertType(alert.getType());
8188
alertResponse.setDescription(alert.getSubject());
8289
alertResponse.setLastSent(alert.getLastSent());
90+
alertResponse.setName(alert.getName());
8391

8492
alertResponse.setObjectName("alert");
8593
alertResponseList.add(alertResponse);

‎api/src/org/apache/cloudstack/api/response/AlertResponse.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class AlertResponse extends BaseResponse {
4141
"DOMAIN_ROUTER_MIGRATE = 14, CONSOLE_PROXY_MIGRATE = 15, USERVM_MIGRATE = 16, VLAN = 17, SSVM = 18, " +
4242
"USAGE_SERVER_RESULT = 19")
4343
private Short alertType;
44+
45+
@SerializedName(ApiConstants.NAME) @Param(description="the name of the alert", since="4.3")
46+
private String alertName;
4447

4548
@SerializedName(ApiConstants.DESCRIPTION) @Param(description="description of the alert")
4649
private String description;
@@ -63,4 +66,8 @@ public void setDescription(String description) {
6366
public void setLastSent(Date lastSent) {
6467
this.lastSent = lastSent;
6568
}
69+
70+
public void setName(String name) {
71+
this.alertName = name;
72+
}
6673
}

‎client/tomcatconf/commands.properties.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ deleteEvents=15
246246
listAlerts=3
247247
archiveAlerts=1
248248
deleteAlerts=1
249+
generateAlert=1
249250

250251
#### system capacity commands
251252
listCapacity=3

0 commit comments

Comments
 (0)