Assets - Groovy script - Set object attribute value using objects derived from an AQL query result in JSM DC
Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.
Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
This article provides a groovy script example to update an object attribute value using objects derived from an AQL query result within Assets.
Other examples can be found in Groovy script examples. The groovy script could be executed in an Assets Automation rule
Solution
Example
The following information is provided as-is. Atlassian Support cannot provide further assistance with the Groovy script described below.
Script was tested against Jira Service Management 5.12.x.
Groovy script has code to execute an AQL Query to obtain the objects that then will be added to an Object attribute. This can be used in combination with an Assets Automation rule and the action would be Execute Groovy script.
As an example, the script below modifies an attribute named 'AWS instances' within an object named 'Jira', using the results obtained from an AQL query. This serves as a basic example that can be built upon as needed.
Update object attribute value using objects derived from an AQL query result
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
import com.atlassian.jira.component.ComponentAccessor
try {
// Load Insight classes and get OSGi component instances in one step
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(
ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
)
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(
ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade")
)
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(
ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory")
)
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(
ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade")
)
// Define the AQL queries
def ec2Query = "objectType = EC2"
def systemQuery = 'objectType = Systems AND Name = "Jira"'
// Execute the AQL queries
def ec2Objects = iqlFacade.findObjectsByIQLAndSchema(1, ec2Query) // Replace it for the Schema ID
def systemObjects = iqlFacade.findObjectsByIQLAndSchema(1, systemQuery) // Replace it for the Schema ID
log.info("EC2 Objects found: ${ec2Objects.size()}")
log.info("System Objects found: ${systemObjects.size()}")
if (systemObjects.size() > 0) {
def systemObject = systemObjects[0] // Assuming there's only one "Jira" system object
// Load the object type attribute bean using the ID of the attribute
def attributeId = 3016 // Replace with the actual attribute ID for "AWS Instances"
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(attributeId).createMutable()
// Load existing attribute values as object IDs
def objectAttributeBean = objectFacade.loadObjectAttributeBean(systemObject.getId(), objectTypeAttributeBean.getId())
def existingObjectIds = []
if (objectAttributeBean != null) {
existingObjectIds = objectAttributeBean.getObjectAttributeValueBeans().collect { it.getReferencedObjectBeanId().toString() }
}
// Add new references, convert to string IDs
ec2Objects.each { ec2Object ->
if (!existingObjectIds.contains(ec2Object.getId().toString())) {
existingObjectIds.add(ec2Object.getId().toString())
}
}
// Create new attribute bean with combined references
def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(systemObject, objectTypeAttributeBean, existingObjectIds as String[])
if (objectAttributeBean != null) {
newObjectAttributeBean.setId(objectAttributeBean.getId())
}
// Store the updated object attribute into Insight
try {
objectFacade.storeObjectAttributeBean(newObjectAttributeBean)
log.info("Updated attribute with combined values.")
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception: " + vie.getMessage())
}
log.info("All attributes updated successfully.")
} else {
log.warn("No system object found with Name 'Jira'")
}
} catch (ClassNotFoundException e) {
log.error("Class not found: " + e.message)
} catch (Exception e) {
log.error("An error occurred: " + e.message)
}
Was this helpful?