Name
sn_agent.AccPolicyAPI
Description
No description available
Script
var AccPolicyAPI = Class.create();
AccPolicyAPI.prototype = {
initialize: function() {
},
NO_RECORD_FOUND_MSG: "No record found, operation aborted",
getPoliciesByQuery: function(encodedQuery, withAgentAndChecks, withCheckParams) {
var policies = [];
// query the agent ci table to enable encoded query on that table
var policiesGr = new GlideRecord("sn_agent_policy");
if (!gs.nil(encodedQuery))
policiesGr.addEncodedQuery(encodedQuery);
policiesGr.query();
while (policiesGr.next()) {
var policyId = policiesGr.getValue("sys_id");
var policy = {
name: policiesGr.getValue("name"),
sys_id: policyId,
active: policiesGr.getDisplayValue("active"),
interval: policiesGr.getValue("interval"),
sys_updated_on: policiesGr.getValue("sys_updated_on"),
monitored_ci_type_filter: policiesGr.getDisplayValue("monitored_ci_type_filter"),
filter: policiesGr.getValue("filter"),
table: policiesGr.getValue("table"),
monitored_ci_type_script: policiesGr.getDisplayValue("monitored_ci_type_script"),
monitored_ci_script: policiesGr.getValue("monitored_ci_script"),
monitored_ci_type_group: policiesGr.getDisplayValue("monitored_ci_type_group"),
monitored_ci_group: policiesGr.getValue("monitored_ci_group") +"// group name as seen in cmdb_group table",
cred_alias: policiesGr.getValue("cred_alias") +"// credential name as seen in discovery_credentials table",
credential_alias: policiesGr.getValue("credential_alias") + "// credential alias sys id as seen in sys_alias table",
publish_status : policiesGr.getDisplayValue("publish_status")
};
if (withAgentAndChecks || withCheckParams) {
policy.checks = this.getChecks("monitoring_policy="+policyId, withCheckParams);
//if not active or draft, no agents are running the policy
if(policiesGr.active && !policiesGr.is_draft) {
var agentIds = [];
var gr = new GlideRecord('sn_agent_policy_monitored_cis');
gr.addQuery('policy', policyId);
gr.addNotNullQuery("agent_id");
gr.query();
//can be multiple in case of domain seperation
while (gr.next()) {
agentIds.push(gr.getValue('agent_id'));
}
policy.agent_ids = agentIds.join(',');
}
}
policies.push(policy);
}
return policies;
},
getChecks: function(encodedQuery, withParams) {
var checks = [];
var checksRecord = new GlideRecord("sn_agent_check");
if (!gs.nil(encodedQuery))
checksRecord.addEncodedQuery(encodedQuery);
checksRecord.query();
while (checksRecord.next()) {
var check = {
name: checksRecord.getValue("name"),
sys_id: checksRecord.getValue("sys_id"),
active: checksRecord.getDisplayValue("active"),
command: checksRecord.getValue("command"),
command_prefix: checksRecord.getValue("command_prefix"),
auto_generate: checksRecord.getDisplayValue("auto_generate"),
timeout: checksRecord.getValue("timeout"),
interval: checksRecord.getValue("interval"),
event_status_change_threshold: checksRecord.getValue("event_status_change_threshold"),
event_status_repair_threshold: checksRecord.getValue("event_status_repair_threshold"),
check_type: ""+checksRecord.check_def.check_script_type.name
};
if (withParams) {
var params = [];
var paramsRecord = new GlideRecord("sn_agent_check_param");
paramsRecord.addQuery("check", checksRecord.getValue("sys_id"));
paramsRecord.query();
while(paramsRecord.next()) {
params.push({
name: paramsRecord.getValue("name"),
sys_id: paramsRecord.getValue("sys_id"),
value: paramsRecord.getValue("value"),
active: paramsRecord.getDisplayValue("active"),
mandatory: paramsRecord.getDisplayValue("mandatory"),
value_required: paramsRecord.getDisplayValue("value_required"),
flag: paramsRecord.getValue("flag")
});
}
check.params = params;
var secureParams = [];
var secureParamsRecord = new GlideRecord("sn_agent_check_secure_param");
secureParamsRecord.addQuery("check", checksRecord.getValue("sys_id"));
secureParamsRecord.query();
while(secureParamsRecord.next()) {
secureParams.push({
name: secureParamsRecord.getValue("name"),
sys_id: secureParamsRecord.getValue("sys_id"),
active: secureParamsRecord.getDisplayValue("active"),
order: secureParamsRecord.getValue("order")
});
}
check.secure_params = secureParams;
}
checks.push(check);
}
return checks;
},
updatePolicy: function(policyId, keyValueMap) {
return this.updateTableFromKeyValueMap("sn_agent_policy", policyId, keyValueMap);
},
updateCheckInstance: function(checkId, keyValueMap) {
return this.updateTableFromKeyValueMap("sn_agent_check", checkId, keyValueMap);
},
updateCheckInstanceParam: function(paramId, keyValueMap) {
return this.updateTableFromKeyValueMap("sn_agent_check_param", paramId, keyValueMap);
},
updateCheckInstanceSecureParam: function(paramId, keyValueMap) {
return this.updateTableFromKeyValueMap("sn_agent_check_secure_param", paramId, keyValueMap);
},
updateTableFromKeyValueMap: function(table, entityId, keyValueMap) {
var entityDraftGr = new GlideRecord(table);
if(entityDraftGr.get(entityId)) {
if (entityDraftGr.getDisplayValue("is_draft") == "false") {
return "Update is not allowed directly on 'Published' policy, use getSandboxPolicy, update it and publish";
}
for(var key in keyValueMap) {
entityDraftGr.setValue(key, keyValueMap[key]);
}
var success = entityDraftGr.update();
if(!success)
return entityDraftGr.getLastErrorMessage();
}
else {
return this.NO_RECORD_FOUND_MSG;
}
},
getSandboxPolicy: function (policyId) {
var publishedPolicyGr = new GlideRecord("sn_agent_policy");
if(!publishedPolicyGr.get(policyId))
return this.NO_RECORD_FOUND_MSG;
if(publishedPolicyGr.getDisplayValue("is_draft")=="true") {
return "Input policy must be published policy Id";
}
var draftSysId = (new PolicyDraftUtils()).getSandboxPolicyId(publishedPolicyGr);
var policiesWrapper = this.getPoliciesByQuery("sys_id="+draftSysId, true, true);
if(!policiesWrapper || policiesWrapper.length != 1)
return "Failed getting sandbox policy";
return policiesWrapper[0];
},
getPoliciesListView: function (encodedQuery, withAgentAndChecks, withCheckParams) {
var query = "is_draft=false^ORpublish_status=0";
if(encodedQuery)
query=query+"^"+encodedQuery;
return this.getPoliciesByQuery(query, withAgentAndChecks, withCheckParams);
},
publishSandboxPolicy: function(draftSysId) {
var draftRecord = new GlideRecord("sn_agent_policy");
if(!draftRecord.get(draftSysId))
return this.NO_RECORD_FOUND_MSG;
if(draftRecord.getDisplayValue("is_draft") == "false")
return "Published policy can not be published. Only Sandbox policy can be publsihed";
//if its Published status, it has no changes no need to do anything, but its not error, so simply return
if(draftRecord.getDisplayValue("publish_status") == "Published")
return;
var draftUtils = new PolicyDraftUtils();
draftUtils.publishDraft(draftRecord);
},
changeActiveValueForPolicy: function(publishedSysId, activeValue) {
var publishedRecord = new GlideRecord("sn_agent_policy");
if(!publishedRecord.get(publishedSysId))
return this.NO_RECORD_FOUND_MSG;
if(publishedRecord.getDisplayValue("is_draft") == "true")
return "Sandbox policy can not be activated/deactivated, only published policy can be activated/deactivated";
//change activation value, only if its different from the current
if (publishedRecord.getDisplayValue("active") != ""+activeValue) {
var draftUtils = new PolicyDraftUtils();
draftUtils.changeActiveValueForPolicy(publishedRecord, activeValue);
}
},
prepareResponse: function(errMsg, response) {
if(!errMsg) {
response.setContentType("application/json");
response.setStatus(200);
response.getStreamWriter().writeString(JSON.stringify({
message: "Operation was successful"
}));
return response;
} else {
var requestErr = new sn_ws_err.ServiceError();
if(errMsg == this.NO_RECORD_FOUND_MSG)
requestErr.setStatus(404);
else
requestErr.setStatus(500);
requestErr.setMessage("Error performing operation");
requestErr.setDetail(errMsg);
return requestErr;
}
},
type: 'AccPolicyAPI'
};
Sys ID
35e3ba66eb112010ff78ba27065228b3