/
GroupApi.java
271 lines (251 loc) · 10 KB
/
GroupApi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/
package org.hyperic.hq.hqapi1;
import org.hyperic.hq.hqapi1.types.EscalationResponse;
import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.GroupResponse;
import org.hyperic.hq.hqapi1.types.GroupsRequest;
import org.hyperic.hq.hqapi1.types.GroupsResponse;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ResponseStatus;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The Hyperic HQ Group API.
* <br><br>
* This class provides access to the {@link org.hyperic.hq.hqapi1.types.Group}s
* within the HQ system. Each of the methods in this class return
* {@link org.hyperic.hq.hqapi1.types.Response} objects that wrap the result
* of the method with a
* {@link org.hyperic.hq.hqapi1.types.ResponseStatus} and a
* {@link org.hyperic.hq.hqapi1.types.ServiceError} that indicates the error
* if the response status is {@link org.hyperic.hq.hqapi1.types.ResponseStatus#FAILURE}.
*/
public class GroupApi extends BaseApi {
GroupApi(HQConnection conn) {
super(conn);
}
/**
* Get a {@link org.hyperic.hq.hqapi1.types.Group} by name.
*
* @param name The group name to search for.
*
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* the User by the given name is returned via
* {@link org.hyperic.hq.hqapi1.types.GroupResponse#getGroup()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupResponse getGroup(String name)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("name", new String[] { name });
return doGet("group/get.hqu", params,
new XmlResponseHandler<GroupResponse>(GroupResponse.class));
}
/**
* Get a {@link org.hyperic.hq.hqapi1.types.Group} by id.
*
* @param id The group id to look up.
*
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* the User by the given id is returned via
* {@link org.hyperic.hq.hqapi1.types.GroupResponse#getGroup()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupResponse getGroup(int id)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("id", new String[] { Integer.toString(id) });
return doGet("group/get.hqu", params,
new XmlResponseHandler<GroupResponse>(GroupResponse.class));
}
private GroupResponse syncSingleGroup(Group group)
throws IOException
{
List<Group> groups = new ArrayList<Group>();
groups.add(group);
GroupsResponse syncResponse = syncGroups(groups);
GroupResponse groupResponse = new GroupResponse();
groupResponse.setStatus(syncResponse.getStatus());
if (syncResponse.getStatus().equals(ResponseStatus.SUCCESS)) {
groupResponse.setGroup(syncResponse.getGroup().get(0));
} else {
groupResponse.setError(syncResponse.getError());
}
return groupResponse;
}
/**
* Create a {@link org.hyperic.hq.hqapi1.types.Group}.
*
* @param group The group to create.
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* group was created successfully.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupResponse createGroup(Group group)
throws IOException
{
return syncSingleGroup(group);
}
/**
* Update a {@link org.hyperic.hq.hqapi1.types.Group}.
*
* @param group The group to create.
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* group was created successfully.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupResponse updateGroup(Group group)
throws IOException
{
return syncSingleGroup(group);
}
/**
* Delete a {@link Group}.
*
* @param id The {@link org.hyperic.hq.hqapi1.types.Group} id to delete.
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* group was deleted successfully.
*
* @throws IOException If a network error occurs while making the request.
*/
public StatusResponse deleteGroup(int id)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("id", new String[] { Integer.toString(id) });
return doGet("group/delete.hqu", params,
new XmlResponseHandler<StatusResponse>(StatusResponse.class));
}
/**
* List all {@link org.hyperic.hq.hqapi1.types.Group}s.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if
* all the groups were successfully retrieved from the server.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupsResponse getGroups()
throws IOException
{
return doGet("group/list.hqu", new HashMap<String,String[]>(),
new XmlResponseHandler<GroupsResponse>(GroupsResponse.class));
}
/**
* List all compatible {@link org.hyperic.hq.hqapi1.types.Group}s. A
* compatible group is a group where all members of the group have the
* same {@link org.hyperic.hq.hqapi1.types.ResourcePrototype}.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if
* all the groups were successfully retrieved from the server.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupsResponse getCompatibleGroups()
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("compatible", new String[] { Boolean.toString(true) });
return doGet("group/list.hqu", params,
new XmlResponseHandler<GroupsResponse>(GroupsResponse.class));
}
/**
* List all mixed {@link org.hyperic.hq.hqapi1.types.Group}s. A
* mixed group is a group where the members will have different
* {@link org.hyperic.hq.hqapi1.types.ResourcePrototype}s.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if
* all the groups were successfully retrieved from the server.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupsResponse getMixedGroups()
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("compatible", new String[] { Boolean.toString(false) });
return doGet("group/list.hqu", params,
new XmlResponseHandler<GroupsResponse>(GroupsResponse.class));
}
/**
* List all {@link org.hyperic.hq.hqapi1.types.Group}s containing
* the input resource.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupsResponse getGroupsContaining(Resource r)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("containing", new String[] { Boolean.toString(true) });
params.put("resourceId", new String[] { Integer.toString(r.getId()) });
return doGet("group/list.hqu", params,
new XmlResponseHandler<GroupsResponse>(GroupsResponse.class));
}
/**
* List all {@link org.hyperic.hq.hqapi1.types.Group}s not containing
* the input resource.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupsResponse getGroupsNotContaining(Resource r)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("containing", new String[] { Boolean.toString(false) });
params.put("resourceId", new String[] { Integer.toString(r.getId()) });
return doGet("group/list.hqu", params,
new XmlResponseHandler<GroupsResponse>(GroupsResponse.class));
}
/**
* Sync a list of {@link org.hyperic.hq.hqapi1.types.Group}s.
*
* @param groups The list of groups to sync.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if
* all the groups were successfully syced.
*
* @throws IOException If a network error occurs while making the request.
*/
public GroupsResponse syncGroups(List<Group> groups)
throws IOException {
GroupsRequest groupRequest = new GroupsRequest();
groupRequest.getGroup().addAll(groups);
return doPost("group/sync.hqu", groupRequest,
new XmlResponseHandler<GroupsResponse>(GroupsResponse.class));
}
}