Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Programmatically create and deploy a virtual asset using the REST API

[Deleted User]
[Deleted User] Posts: 0 admin
edited December 2018 in Virtualize

This script will enable you to create and deploy an asset dynamically. This is useful when you want to create an asset on the fly. This uses the SOAVirt API.

#Server Settings
VIRT_USER=admin
VIRT_PASS=admin
VIRT_HOST=localhost
VIRT_PORT=9080

#Workspace Settings
VA_SUBFOLDER=scratchpad
ASSET_NAME=parabankRESTapi
RESPONDER_NAME="Responder1"

#Asset Settings
ASSET_PAYLOAD='{\"account\":{\"balance\":5022.93,\"customerId\":12212,\"id\":13344,\"type\":\"CHECKING\"}}'

MIME="application/json"
REQUEST_TEMPLATE="http://localhost:8080/parabank/services/bank/accounts/13344"

#Correlation Settings
COR_PATH_ON=true
COR_PATH="/services/bank/accounts/13344"
COR_METHOD_ON=true
COR_METHOD="GET"

#Deployment Settings
DEPLOYMENT_PATH="/parabank"

#===================================================================================
#Ignore This
ASSET_ID="/VirtualAssets/$VA_SUBFOLDER/$ASSET_NAME.pva"


#Create the PVA file

curl -v \
  --user $VIRT_USER:$VIRT_PASS \
  --header "Content-Type: application/json" \
  --header "ParasoftRefresh: true" \
  --request POST \
  --data '{
    "name" : "'$ASSET_NAME'",
    "parent" : {
        "id" : "/VirtualAssets/'$VA_SUBFOLDER'"
    }
}' \
  http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v5/files/pvas
echo
echo "==================================================================="
echo "ASSET PAYLOAD IS ----->" $ASSET_PAYLOAD

#Create a responder

curl -v \
  --user $VIRT_USER:$VIRT_PASS \
  --header "Content-Type: application/json" \
  --request POST \
  --data '{
    "type" : "messageResponder",
    "name" : "'$RESPONDER_NAME'",
    "parent" : {
        "id" : "'$ASSET_ID'/Responder Suite"
    },
    "response" : {
        "inputMode" : "literal",
        "literal" : {
            "text" : "'$ASSET_PAYLOAD'",
            "type" : "text",
            "mimeType" : "'$MIME'"
        }
    },
    "transportHeader" : {
        "httpTransportHeaders" : {
            "type" : "table",
            "httpHeadersTable" : {
                "rows" : [
                ]
            }
        },
        "type" : "table"
    },
    "options" : {
        "requestTemplate" : {
            "requestUrlTemplate" : "'$REQUEST_TEMPLATE'",
            "requestMessageTemplate" : ""
        },
        "requestHandling" : {
            "convertIncomingRequestToXmlBeforeResponderCorrelations" : true
        }
    },
    "dataSource" : null,
    "assetDisabled" : false,
    "responderCorrelation" : {
        "transport" : {
            "enabled" : false,
            "headers" : [
                {
                    "name" : "SOAPAction",
                    "value" : "SOAPAction"
                }
            ]
        },
        "xmlMessage" : {
            "enabled" : false,
            "xpaths" : [
            ]
        },
        "urlParameters" : {
            "enabled" : false,
            "parameters" : [
            ]
        },
        "urlPath" : {
            "path" : "'$COR_PATH'",
            "enabled" : '$COR_PATH_ON'
        },
        "httpMethods" : {
            "methods" : [
                {
                    "value" : "'$COR_METHOD'"
                }
            ],
            "enabled" : '$COR_METHOD_ON'
        }
    },
    "dataSourceCorrelation" : {
        "enabled" : false,
        "failoverOptions" : {
            "continueSearchingIfCorrelationFails" : true
        },
        "urlParameters" : [
        ],
        "urlPaths" : [
        ],
        "xmlMessage" : [
        ]
    }
}' \
  http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v5/tools/messageResponders

echo
echo "==================================================================="

#Get the Deployment ID
response=$(curl -vs \
  --user $VIRT_USER:$VIRT_PASS \
  -X GET \
  -H  "Accept: application/json" \
    "http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v6/virtualAssets?fields=id&pvaLocation=%2FVirtualAssets%2F$VA_SUBFOLDER%2F$ASSET_NAME.pva")

DEP_ID=${response:25:50}
echo $DEP_ID
echo "==================================================================="

#Set the deployment
curl -v \
  --user $VIRT_USER:$VIRT_PASS \
  --header "Content-Type: application/json" \
  --request PUT \
  --data '{
  "name": "'$ASSET_NAME'",
  "pvaLocation": {
    "id": "'$ASSET_ID'"
  },
  "enabled": true,
  "description": "",
  "transports": {
    "http": {
      "path": "'$DEPLOYMENT_PATH'"
    }
  }
}' \
  http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v5/virtualAssets/$DEP_ID
echo
echo "==================================================================="
Tagged:

Comments