{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":29262,"path":"it-operations-management/cloud-configuration-governance/response-processor-example.md","title":"Virtual server response processor example","bundle":null,"doc_type":null,"release":"australia","body":"\n# Virtual server response processor example\n\nThe **Create\\_Virtual\\_Server\\_Response\\_Processor** script, which is available by default in Cloud Provisioning and Governance, is the response processor that handles the creation of AWS virtual machine CIs.\n\n## Create Virtual Server\n\nThe Create\\_Virtual\\_Server\\_Response\\_Processor resource processor script is available by default on the Virtual Server resource block. Its job is to create a virtual server record in the Virtual Machine Instance \\[cmdb\\_ci\\_vm\\_instance\\] table when a new virtual server is provisioned.\n\nAll response processors have this first line with these common parameters:\n\n```\nfunction processResponse(response, cloudServiceAccountId, ldc, correlationId,step, requestorContext) {\n\n```\n\nThis brings in the response from the cloud provider and the important information, such as the account ID, that is required for the new CI that the system can create. All of these parameters are required for all response processors.\n\nLine 10 parses the response into JSON so that the system can process it. The information is held in the vmResponse variable:\n\n```\n\nvar vmResponse = global.JSON.parse(response);\n\n```\n\nWhenever you create or edit a response processor, you must know which inputs are required for the CI type. Line 11 handles one of the necessary inputs, the hardware ID, that the CMDB record requires:\n\n```\n\nvar hardwareId = vmResponse.hardwareId;\n\n```\n\nLine 39 shows the information that is required for the system to identify the new virtual server and related CIs, so the information can be put into the CMDB. In this case, the service account object ID identifies the account associated with the virtual server, the datacenter object ID identifies the datacenter in which the virtual server lives, and the virtual machine instance object ID identifies the virtual server itself. This identification code block prevents the creation of duplicate CIs.\n\n```\n\nvar vmInfo = {\n    \"cmdb_ci_vm_instance\": {\n      \"validator\": \"virtual_machine_create_update_validator\",\n      \"validator_overrides\": {},\n      \"identification\": {\n        \"cmdb_ci_cloud_service_account\": {\n          \"criterion\": {\n            \"object_id\": cloudServiceAccountId\n          }\n        },\n        \"cmdb_ci_aws_datacenter\": {\n          \"criterion\": {\n            \"object_id\": ldc\n          }\n        },\n        \"cmdb_ci_vm_instance\": {\n          \"criterion\": {\n            \"object_id\": vmResponse.nodeId\n          }\n        }\n      },\n```\n\nAttributes are populated into the fields on in the cmdb\\_ci\\_vm\\_instance table. These attributes are defined in line 61:\n\n```\n\n\"attributes\": {\n  \"name\": vmResponse.nodeName,\n  \"object_id\": vmResponse.nodeId,\n  \"state\": status_map[vmResponse.state],\n  \"dns_suffix\": vmResponse.dnsSuffix,\n  \"cpus\": vmCPUs,\n  \"memory\": vmMemory,\n  \"disks\": vmResponse.volumes.length,\n  \"disks_size\": \"\",\n  \"nics\": vmResponse.networkInterfaces.length,\n  \"terminated_on\": \"\",\n  \"termination_protection\": \"\",\n  \"ip_address\": vmPubIPAddr,\n  \"assigned_to\": reqContext.userId,\n  \"assignment_group\": reqContext.groupId\n},\n\n```\n\nReferences to other CIs can also be included in the response processor. In this case, the OS template that the virtual server is based on is identified by first identifying the object ID of the service account and the datacenter along with the actual OS template.\n\n```\n\n\"references\": {\n  \"cmdb_ci_os_template\": {\n    \"identification\": {\n      \"cmdb_ci_cloud_service_account\": {\n        \"criterion\": {\n          \"object_id\": cloudServiceAccountId\n        }\n      },\n      \"cmdb_ci_aws_datacenter\": {\n        \"criterion\": {\n          \"object_id\": ldc\n        }\n      },\n      \"cmdb_ci_os_template\": {\n        \"criterion\": {\n          \"object_id\": imageIdTrim\n        }\n      }\n    },\n```\n\nThe following code block adds the object ID of the OS image to the attributes list so that this information can be populated into the virtual server CMDB record.\n\n```\n\n\"attributes\": {\n  \"object_id\": imageIdTrim\n}\n\n```\n\nThis code block performs additional identification on the Compute template \\(the hardware type\\) and then add it to the attributes:\n\n```\n\n\"cmdb_ci_compute_template\": {\n  \"identification\": {\n    \"cmdb_ci_cloud_service_account\": {\n      \"criterion\": {\n        \"object_id\": cloudServiceAccountId\n      }\n    },\n    \"cmdb_ci_aws_datacenter\": {\n      \"criterion\": {\n        \"object_id\": ldc\n      }\n    },\n    \"cmdb_ci_compute_template\": {\n      \"criterion\": {\n        \"object_id\": vmResponse.hardwareId\n      }\n    }\n  },\n  \"attributes\": {\n    \"object_id\": vmResponse.hardwareId,\n    \"name\": vmResponse.hardwareId\n  }\n}\n```\n\nAdditional code sections make the relationship with network interfaces and identify any storage attached to the virtual server.\n\nThis mandatory code block pushes the data to the CMDB and returns the JSON string:\n\n```\n\ncloudModelString.push(vmInfo);\nreturn global.JSON.stringify(cloudModelString);\n\n```\n\n**Parent Topic:**[Configure a response processor](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/it-operations-management/cloud-configuration-governance/configure-response-processor.md)\n\n"}}