UrbanCode Deploy

UrbanCode Deploy: 10 Minute Tip – Some Property Magic …

There have been many times when creating new components in IBM UrbanCode Deploy when I’ve had to create a lot of component properties from an existing source document that needs some fudging to get it into a form suitable for UCD and then we’ve called the API to add each of the new properties to the component.  However, there are problems with this approach:

  1. It can take quite a long time to add a lot of properties to a component this way
  2. It creates a huge number of property sheet versions for the component which is why it takes a long time.

Of course in the UCD GUI, I can use the batch mode and just cut and paste them and that is much quicker, but if I have a lot of components to do then I still have a good amount of work to do and there is always the danger of mangling the property values, especially those that have values that span multiple lines.

Wouldn’t it be nice if there was an easier way ??

Well, as it turns out, there is.  The UCDRestClient libraries have a class called the PropertyClient which provides some interesting interfaces.

This example code shows how to use the PropertyClient class to update the properties of a component much the same as the batch edit mode, but programmatically.  The example shows how to retrieve the properties currently set and also how to replace that set with whatever you want.

It’s left to the reader to work out how to potentially merge what is present with what is desired.  But for my use-case of the initial creation of properties it ticks most of the boxes.

import java.io.IOException;
import com.urbancode.ud.client.PropertyClient;
import com.urbancode.ud.client.ComponentClient;

import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONException;
import groovy.json.JsonSlurper;
import java.net.URI;

String url=https://localhost:8443
String userName = "admin"
String password = "admin"
String compName = "provider"

// Get the component client to get some info about the component.  Specifically we need the property sheet
// path and the most recent version number.  The path is an internal UCD thing we need to identify what to
// update.  The version we need to tell UCD what version to update, this I believe has to be the latest version.
myComp = new ComponentClient( new URI(url), userName, password, true)
JSONObject comp = myComp.getComponent(compName)

// Now get the info we need, the propertysheet ID and the current version number
propSheetPath = comp.propSheet.path
propSheetVer = comp.propSheet.version

Map <String, String> origProps= myComp.getComponentProperties(compName)
println "Original component properties: ${origProps}"

// Now create an instance of the property client
propClient = new PropertyClient( new URI(url), userName, password, true)

// Now we need to create our property values - so this would be ALL the properties you want.
// Each property value pair is expressed as name=val\n.  \n Is the separator.  If you want newlines
// in the value put \\n
// For secure properties, just put the value you want in clear it will be encrypted on save
// To leave a secure property unchanged just put the value of ****
// Note there isn't a way to create secure properties through this mechanism (that I know of) so 
// you either have to pre-create them as secure or go tick the secure box later
// The addition of properties allows the use of single quote to preserve the newline escape
JSONObject props = new JsonSlurper().parseText("{}")
props.put ("properties", 'a=9\nb=8\\nA\nc=7\nsec=****\n')

// Now do the update.  Although this method is named updateResourcePropValues, it also works for component
// properties as the context is in the propSheetPath which we got from our component
JSONObject c = propClient.updateResourcePropValues(propSheetPath, propSheetVer.toString(), props)

 


Alan Murphy is an IBM services consultant who has worked with clients to help them adopt new tools and processes for the last 20 years. UrbanCode Deploy and DevOps has been his focus for the last 5 years. He also develops tools to assist clients in tool adoption and blogs on an occasional basis.

Tags: