Wednesday 5 October 2016

Extending the UDRestClient class in a plugin step


// --------------------------------------------------
// Absorb.groovy
// Author: Bob Clarke
// Date: 15/08/2016
// --------------------------------------------------

import com.urbancode.air.AirPluginTool
import com.urbancode.ud.client.*
import org.apache.http.HttpResponse
import org.apache.http.client.methods.*
import groovy.json.JsonSlurper

def apTool = new AirPluginTool(this.args[0], this.args[1])
def props = apTool.getStepProperties()
applicationName = props['application']
environmentName = props['environment']
snapshotName = props['snapshot']
weburl = System.getenv("AH_WEB_URL")
udUser = apTool.getAuthTokenUsername()
udPass = apTool.getAuthToken()
pluginDev = System.getenv("PLUGIN_DEV")

// Check if we're being invoked from UrbanCode or via a command line test rig
if (pluginDev.equals("yes")){
 println "MANUAL CREDS"
 udUser = props['udUser']
 udPass = props['udPass']
}

 
// --------------------------------------------------
// Main
// --------------------------------------------------
BOBUCRestClient restClient = new BOBUCRestClient(new URI(weburl), udUser, udPass)
def applicationId = restClient.getApplicationId(applicationName)
def snapshotId = restClient.getSnapshotId(applicationName, snapshotName)
def environmentId = restClient.getEnvironmentId(applicationName, environmentName)
restClient.getEnvPropsForSnapshot(applicationId, environmentId, snapshotId)
 
// --------------------------------------------------
// Classes and Functions
// --------------------------------------------------
 
public class BOBUCRestClient extends UDRestClient {
     public BOBUCRestClient(URI url, String clientUser, String clientPassword) {
  super(url, clientUser, clientPassword)
     }

 public  getApplicationId(String applicationName){
  String endpoint = "${url}/cli/application/info?application=${applicationName}"
  println "\nGetting ID of application: ${applicationName}"
  println "REST call is: ${endpoint}"
  HttpGet method = new HttpGet(endpoint)
  HttpResponse response = invokeMethod(method)
  String body = getBody(response)
  def slurper = new JsonSlurper()
  def json = slurper.parseText(body.toString())
  println "ID is ${json.id}"
  return(json.id)
 }

 public  getSnapshotId(String applicationName, String snapshotName){
  String endpoint = "${url}/cli/snapshot/getSnapshot?application=${applicationName}&snapshot=${snapshotName}"
  println "\nGetting ID of snapshot: ${snapshotName}"
  println "REST call is: ${endpoint}"
  HttpGet method = new HttpGet(endpoint)
  HttpResponse response = invokeMethod(method)
  String body = getBody(response)
  def slurper = new JsonSlurper()
  def json = slurper.parseText(body.toString())
  println "ID is ${json.id}"
  return(json.id)
 }

 public  getEnvironmentId(String applicationName, String environmentName){
  String endpoint = "${url}/cli/environment/info?application=${applicationName}&environment=${environmentName}"
  println "\nGetting ID of environment: ${environmentName}"
  println "REST call is: ${endpoint}"
  HttpGet method = new HttpGet(endpoint)
  HttpResponse response = invokeMethod(method)
  String body = getBody(response)
  def slurper = new JsonSlurper()
  def json = slurper.parseText(body.toString())
  println "ID is ${json.id}"
  return(json.id)
 }

 public  getEnvPropsForSnapshot(String applicationId, String environmentId, String snapshotId){
  String endpoint = "${url}/rest/deploy/snapshot/${snapshotId}/configurationByPath/applications/${applicationId}/environments/${environmentId}/properties"
  println "\nGetting name, path, version and versionCount"
  println "REST call is: ${endpoint}\n"
  HttpGet method = new HttpGet(endpoint)
  HttpResponse response = invokeMethod(method)
  String body = getBody(response)
  def slurper = new JsonSlurper()
  def json = slurper.parseText(body.toString())
  json.each{ 
   println "Component ${it.name}"
   println " Version is ${it.version}"
   println " VersionCount is ${it.versionCount}"
   def propsId = it.path.split("/").last()
   println " propsId is ${propsId}"
   if(it.version != it.versionCount){
    println "\t${it.name} env props version is at ${it.version} however there are ${it.versionCount} versions available"
    println "\tSetting ${it.name} env props to version ${it.versionCount}"
    def endpoint2 = "${url}/rest/deploy/snapshot/${snapshotId}/configuration/applications%26${applicationId}%26environments%26${environmentId}%26properties%26${propsId}/${it.versionCount}"
    println "REST call is: ${endpoint2} -X PUT\n"
    HttpPut method2 = new HttpPut(endpoint2)
                  HttpResponse response2 = invokeMethod(method2)
   }
  }
 }
}

No comments:

Post a Comment