private void get(String[] args) throws Exception { String[] ONE_REQUIRED = {OPT_RESOURCEID, OPT_GROUPID, OPT_ALL}; final DateFormat df = SimpleDateFormat.getInstance(); OptionParser p = getOptionParser(); p.accepts(OPT_RESOURCEID, "The id of the resource to query for maintenance") .withRequiredArg() .ofType(Integer.class); p.accepts(OPT_GROUPID, "The id of the group to query for maintenance") .withRequiredArg() .ofType(Integer.class); p.accepts(OPT_ALL, "Get all maintenance schedules"); OptionSet options = getOptions(p, args); int criteria = 0; for (String opt : ONE_REQUIRED) { if (options.has(opt)) { criteria++; } } if (criteria == 0) { System.err.println("One of " + Arrays.toString(ONE_REQUIRED) + " is required."); System.exit(-1); } else if (criteria > 1) { System.err.println("Only one of " + Arrays.toString(ONE_REQUIRED) + " may be specified"); System.exit(-1); } HQApi api = getApi(options); MaintenanceApi maintenanceApi = api.getMaintenanceApi(); if (options.has(OPT_ALL)) { MaintenancesResponse schedules = maintenanceApi.getAll(null); checkSuccess(schedules); XmlUtil.serialize(schedules, System.out, Boolean.TRUE); } else { MaintenanceResponse response = null; Integer id = null; if (options.has(OPT_GROUPID)) { id = (Integer) getRequired(options, OPT_GROUPID); response = maintenanceApi.get(id); } else if (options.has(OPT_RESOURCEID)) { id = (Integer) getRequired(options, OPT_RESOURCEID); ResourceResponse resourceResponse = api.getResourceApi().getResource(id, false, false); checkSuccess(resourceResponse); response = maintenanceApi.get(resourceResponse.getResource()); } checkSuccess(response); MaintenanceEvent event = response.getMaintenanceEvent(); if (event != null && event.getState() != null && event.getStartTime() != 0 && event.getEndTime() != 0) { if (event.getGroupId() > 0) { System.out.println("Maintenance schedule for group " + event.getGroupId()); } else { System.out.println("Maintenance schedule for resource " + event.getResourceId()); } System.out.println("State: " + event.getState().value()); System.out.println("Start Time: " + df.format(event.getStartTime())); System.out.println("End Time: " + df.format(event.getEndTime())); } else { if (options.has(OPT_GROUPID)) { System.out.println("No maintenance events found for group " + id); } else if (options.has(OPT_RESOURCEID)) { System.out.println("No maintenance events found for resource " + id); } } } }
private void schedule(String[] args) throws Exception { String[] ONE_REQUIRED = {OPT_RESOURCEID, OPT_GROUPID}; OptionParser p = getOptionParser(); p.accepts(OPT_RESOURCEID, "The id of the resource to schedule for maintenance") .withRequiredArg() .ofType(Integer.class); p.accepts(OPT_GROUPID, "The id of the group to schedule for maintenance") .withRequiredArg() .ofType(Integer.class); p.accepts(OPT_START, "Start time for maintenance.").withRequiredArg(); p.accepts(OPT_END, "End time for maintenance.").withRequiredArg(); OptionSet options = getOptions(p, args); int criteria = 0; for (String opt : ONE_REQUIRED) { if (options.has(opt)) { criteria++; } } if (criteria == 0) { System.err.println("One of " + Arrays.toString(ONE_REQUIRED) + " is required."); System.exit(-1); } else if (criteria > 1) { System.err.println("Only one of " + Arrays.toString(ONE_REQUIRED) + " may be specified"); System.exit(-1); } HQApi api = getApi(options); MaintenanceApi maintenanceApi = api.getMaintenanceApi(); MaintenanceResponse response = null; String startStr = (String) getRequired(options, OPT_START); String endStr = (String) getRequired(options, OPT_END); Date startDate = parseDateString(startStr); if (startDate == null) { System.err.println("Unable to parse date/time string: '" + startStr + "'"); System.exit(-1); } Date endDate = parseDateString(endStr); if (endDate == null) { System.err.println("Unable to parse date/time string: '" + endStr + "'"); System.exit(-1); } if (options.has(OPT_GROUPID)) { Integer groupId = (Integer) getRequired(options, OPT_GROUPID); response = maintenanceApi.schedule(groupId, startDate.getTime(), endDate.getTime()); } else { Integer resourceId = (Integer) getRequired(options, OPT_RESOURCEID); ResourceResponse resourceResponse = api.getResourceApi().getResource(resourceId, false, false); checkSuccess(resourceResponse); response = maintenanceApi.schedule( resourceResponse.getResource(), startDate.getTime(), endDate.getTime()); } checkSuccess(response); MaintenanceEvent event = response.getMaintenanceEvent(); if (event.getGroupId() > 0) { System.out.println("Maintenance scheduled for group " + event.getGroupId()); } else { System.out.println("Maintenance scheduled for resource " + event.getResourceId()); } }