Create a policy action script
Use policy action scripts to get, update, or set values in cloud requests.
Avant de commencer
Pourquoi et quand exécuter cette tâche
For more information on using scripts, see Cloud scripts and cloud script templates.
Procédure
Exemple
customScript : function(formData){
var groups = this.parameters.userData.groups;
if( groups.length > 0){
formData.App_Server_NodeName = "MyNodeName";
}
return formData;
},
This example returns a random number:
customScript : function(formData){
var min = 10000;
var max = 99999;
var num = Math.floor(Math.random() * (max - min + 1)) + min;
formData.randomNumber = num + "";
return formData;
},
This example script adds three hours to the lease date using the base-system GlideDateTime class.
var SetLeaseShort = Class.create();
SetLeaseShort.prototype = Object.extendsObject(sn_cmp.PolicyExecutionBase, {
customScript : function(formData){
// Manipulation of form parameters are supported. Changes in any other attributes
// will be ignored.
// The data available for manipulation is as follows:
// Form Data - for example: StackName can be accessed through formData.StackName
// formData.StackName = "MyStack";
// User Data - for example: User Id can be accessed through this.parameters.userData
// if(this.parameter.userData.userId == 'servicenowuserId')
// this.info("id = " + formData.StackName);
// var leaseValue = now();
// leaseValue = leaseValue.setHours(leaseValue.getHours()+4);
// formData.LeaseEndDate = leaseValue;
if (formData.hasOwnProperty("LeaseEndDate")) {
gdt = new GlideDateTime();
// 10800 = 3 hours
gdt.addSeconds(10800);
formData.LeaseEndDate = gdt.getDisplayValue();
}
return formData;
},
execute: function() {
if(this.parameters != null && this.parameters.formData != null){
var inputData = JSON.parse(JSON.stringify(this.parameters.formData));
var outputFormData = this.customScript(inputData);
if( outputFormData != null){
this.parameters.formData = outputFormData;
}
}
var output = {};
output.answer = this.parameters;
return JSON.stringify(output);
},
type : 'SetLeaseShort'
});
var SetLeaseShortObj = new SetLeaseShort(inputAttributes);
var outputParams = SetLeaseShortObj.execute();
gs.info( "Output of script is = " + outputParams);
This example script sets the user-specified Azure Wordpress DNS name field from the form to the same name as the stack name:
var WordpressFill = Class.create();
WordpressFill.prototype = Object.extendsObject(sn_cmp.PolicyExecutionBase, {
customScript : function(formData){
// Manipulation of form parameters are supported. Changes in any other attributes
// will be ignored.
// The data available for manipulation is as follows:
// Form Data - for example: StackName can be accessed through formData.StackName
// formData.StackName = "MyStack";
// User Data - for example: User Id can be accessed through this.parameters.userData
// if(this.parameter.userData.userId == 'servicenowuserId')
//this.info("id = " + formData.StackName);
formData.Azure_Wordpress_v6_vmDnsName = formData.StackName;
return formData;
},
execute: function() {
if(this.parameters != null && this.parameters.formData != null){
var inputData = JSON.parse(JSON.stringify(this.parameters.formData));
var outputFormData = this.customScript(inputData);
if( outputFormData != null){
this.parameters.formData = outputFormData;
}
}
var output = {};
output.answer = this.parameters;
return JSON.stringify(output);
},
type : 'WordpressFill'
});
var WordpressFillObj = new WordpressFill(inputAttributes);
var outputParams = WordpressFillObj.execute();
gs.info( "Output of script is = " + outputParams);