Custom Object :: Launch a Bomgar Session from Axeda

    /**

    * Launch a Bomgar Session from Axeda

    * (1) get Bomgar ID & check logged in

    * (2) generate session key and external key

    * (3) create / update software package with explorer execute instruction

    * (4) deploy package against supplied asset

     

    Add the following Paramters to the Action Custom Object

    user

    pwd

    asset

     

    */

     

    import net.sf.json.JSONObject

    import groovyx.net.http.HTTPBuilder

    import static groovyx.net.http.ContentType.*

     

    import com.axeda.drm.sdk.scripto.Request

    import com.axeda.services.v2.AssetReference

    import com.axeda.services.v2.Asset

    import com.axeda.services.v2.Model

    import com.axeda.services.v2.ExecuteApplicationInstruction

    import com.axeda.services.v2.SoftwarePackage

    import com.axeda.services.v2.DeploymentType

    import com.axeda.services.v2.Deployment

    import com.axeda.services.v2.SoftwarePackageCriteria

     

    String VERSION = "2.0.0.0"

    String NAME = "LaunchBomgar"

    String BOMGAR_HOST = "https://your.bomgar.box.com/"

    String BOMGAR_PATH = "api/command.ns"

    String BOMGAR_ID = "get_api_info"

    String BOMGAR_SESS = "generate_session_key"

    String BOMGAR_LOGG = "get_logged_in_reps"

     

     

    def toJSON(msg){

        msg = [ response: msg ]

        toJSON = ['Content-Type': 'application/json', 'Content':JSONObject.fromObject(msg).toString(2)]

    }

     

    try

    {

        String sBomgarID, sShortKey, sURL, sModel, sSerial, sExtKey, sError

        Boolean bLoggedIn=false

     

        def params = Request.parameters?.size() >2 ? Request.parameters : parameters

        String sUser = params.user

        String sPwd = params.pwd

        String sAssID = params.asset

     

        // find asset

        Asset myAsset = bridges.assetBridge.findById(sAssID)

        if (myAsset == null){

            logger.info "Asset ID not found " + sAssID

            return toJSON("Asset ID not found " + sAssID)

        }

     

        // find Model

        Model myModel = bridges.modelBridge.findById(myAsset.getModel().getSystemId())

        if (myModel == null){

            logger.info "Model not found"

            return toJSON("Model not found")

        }

     

        sSerial = myAsset.serialNumber

        sModel = myModel.modelNumber

        sExtKey  = sModel + "|" + sSerial

     

        // get my Bomgar rep id

        def Query = [username:sUser, password:sPwd, action:BOMGAR_ID]

        def bomgar = new HTTPBuilder(BOMGAR_HOST)

        bomgar.get ( path: BOMGAR_PATH, query: Query, contentType : XML ) { resp, xml ->

            sBomgarID = xml.user_id.text()

            sError = xml.text()

        }

        if (sBomgarID==""){

            logger.info "Bomgar (" + sUser + ") " + sError

            return toJSON("Bomgar (" + sUser + ") " + sError)

        }

     

        // check that I'm logged in

        Query = [username:sUser, password:sPwd, action:BOMGAR_LOGG]

        bomgar = new HTTPBuilder(BOMGAR_HOST)

        bomgar.get ( path: BOMGAR_PATH, query: Query, contentType : XML ) { resp, xml ->

            if (xml.rep.size() > 0) {

                xml.rep.each {

                    if ( sBomgarID == it.@id.text() ) bLoggedIn = true

                }

            }

        }

        if (!bLoggedIn){

            logger.info "Bomgar (" + sUser + ") Not Logged In"

            return toJSON("Bomgar (" + sUser + ") Not Logged In")

        }

     

     

        // create a Bomgar session key

        Query = [username:sUser, password:sPwd, action:BOMGAR_SESS, type:'support', queue_id:'rep:' + sBomgarID, external_key:sExtKey]

        bomgar.get ( path: BOMGAR_PATH, query: Query, contentType : XML ) { resp, xml ->

            sURL = xml.key_url

            sShortKey = xml.short_key

        }

        if (sURL.size()<5){

            logger.info "Bomgar Session not generated"

            return toJSON("Bomgar Session not generated")

        }

     

     

        // search for existing package

        SoftwarePackageCriteria softCrit = new SoftwarePackageCriteria()

        softCrit.setName(NAME+sSerial)

        softCrit.setVersion(VERSION)

        packages = bridges.softwarePackageBridge.find(softCrit)

        // delete old packages

        bridges.softwarePackageBridge.delete(packages.getSoftwarePackages())

     

        // create execute instruction which runs explorer on asset

        ExecuteApplicationInstruction execIns = new ExecuteApplicationInstruction()

        execIns.setPathToExecutable("explorer")

        execIns.setAsyncExecution(true)

        execIns.setArguments("\"" + sURL + "\"")

     

     

        // create simple software package

        SoftwarePackage softPack = new SoftwarePackage()

        softPack.setName(NAME+sSerial)

        softPack.setModel(myAsset.model)

        softPack.setVersion(VERSION)

        softPack.instructions.add(execIns)

        result = bridges.softwarePackageBridge.create(softPack)

        if ( ! result.isSuccessful() ) {

            logger.info "Package error " + result.toString()

            return toJSON("Package error " + result.toString())

        }

     

        // create software deployment

        Deployment deploy = new Deployment()

        deploy.setSoftwarePackage(softPack)

        deploy.setType(DeploymentType.MANUAL)

        AssetReference assRef = myAsset

        deploy.assets.add(assRef)

        result = bridges.deploymentBridge.create(deploy)

        if ( ! result.isSuccessful() ) {

            logger.info "Deployment Error" + result

            return toJSON("Deployment error " + result)

        }

        else

        {

            logger.info "Session Deployed " + sShortKey

            return toJSON("Session Deployed " + sShortKey)

        }

    }

    catch (Exception e){

        logger.error(e)

        return toJSON(e.toString())

    }