Update attributes of an environment
Script example to get an Environment and update its attributes.
Script
package com.apwide.golive.example
import com.apwide.env.environment.Environment
import com.apwide.golive.helper.EnvironmentHelper
EnvironmentHelper helper = new EnvironmentHelper()
Environment environment = helper.getEnvironment("eCommerce", "Staging")
String owner = "Fred"
String team = "Red Team"
String teamUrl = "https://red-team.example.com"
environment = helper.setAttributes(environment, ["Team": """<a href="${teamUrl}">${team}</a>""", "Owner": owner])
return """
Updated environment : ${environment},
Team=${helper.getAttributeValue(environment, "Team")},
Owner=${helper.getAttributeValue(environment, "Owner")}
"""
GROOVY
This script depends on :
com.apwide.golive.helper.EnvironmentHelper
com.apwide.golive.accessor.EnvironmentServicesAccessor
groovy classes (you can copy code below). Files must be structured that way in your script root:
EnvironmentHelper
package com.apwide.golive.helper
import com.apwide.env.application.ApplicationSearcher
import com.apwide.env.attribute.Attribute
import com.apwide.env.attribute.value.AttributeValue
import com.apwide.env.common.BaseResult
import com.apwide.env.common.ErrorCollectionHelper
import com.apwide.env.envcategory.EnvironmentCategory
import com.apwide.env.envcategory.EnvironmentCategorySearcher
import com.apwide.env.environment.Environment
import com.apwide.env.environment.admin.EnvironmentInputParameters
import com.apwide.env.environment.admin.EnvironmentManager
import com.apwide.env.environment.search.EnvironmentSearcher
import com.apwide.env.service.EnvironmentServices
import com.apwide.golive.accessor.EnvironmentServicesAccessor
import com.atlassian.crowd.model.application.Application
import groovy.util.logging.Log4j
@Log4j
class EnvironmentHelper {
EnvironmentServices environmentServices = EnvironmentServicesAccessor.get()
EnvironmentSearcher environmentSearcher = environmentServices.getEnvSearcher()
EnvironmentManager environmentManager = environmentServices.getEnvMgr()
EnvironmentCategorySearcher categorySearcher = environmentServices.getEnvCatSearcher()
ApplicationSearcher applicationSearcher = environmentServices.getAppSearcher()
Environment getEnvironment(String applicationName, String categoryName) {
BaseResult<Environment> result = environmentSearcher.getByUnique(getApplication(applicationName), getEnvironmentCategory(categoryName))
if (result.hasAnyErrors()) {
log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
return null
}
return result.getEntity()
}
Environment setAttributes(Environment environment, Map<String, String> attributes) {
List<AttributeValue> attributeValues = attributes.entrySet().stream().map({ entry ->
Attribute attribute = Attribute.fromName(entry.key)
return new AttributeValue.AttributeValueBuilder().attribute(attribute).value(entry.value).build()
}).toArray()
EnvironmentInputParameters params = new EnvironmentInputParameters()
params.setAttributes(attributeValues)
BaseResult<Environment> result = environmentManager.update(environment.getID(), params)
if (result.hasAnyErrors()) {
log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
return null
}
return result.getEntity()
}
String getAttributeValue(Environment environment, String attributeName){
if (!environment.getAttributes())
return null
AttributeValue attributeValue = environment.getAttributes().find({AttributeValue attributeValue -> attributeValue.attribute.name == attributeName})
return attributeValue?.value
}
Application getApplication(String applicationName) {
BaseResult<Application> result = applicationSearcher.getByName(applicationName)
if (result.hasAnyErrors()) {
log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
return null
}
return result.getEntity()
}
EnvironmentCategory getEnvironmentCategory(String categoryName) {
BaseResult<EnvironmentCategory> result = categorySearcher.getByName(categoryName)
if (result.hasAnyErrors()) {
log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
return null
}
return result.getEntity()
}
}
GROOVY
EnvironmentServicesAccessor
package com.apwide.golive.accessor
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.holydev.env.plugin.jira-holydev-env-plugin")
import com.atlassian.jira.component.ComponentAccessor
import com.apwide.env.service.GoliveEnvironmentServices
class EnvironmentServicesAccessor {
static GoliveEnvironmentServices get(){
final Class serviceClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.apwide.env.service.GoliveEnvironmentServices")
return ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass)
}
}
GROOVY