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.

Dynamically create proxies, record traffic, and create assets using the REST API

Options

I was recently at a customer engagement and they had the following requirement:

We would like to create proxies on the fly, record traffic, create the asset, then delete the proxy.

Parasoft has a powerful REST API that can easily facilitate this. This script does the following
1. Creates a new Proxy in record mode
2. Grabs the proxy ID
3. Executes a test (Through the proxy)
4. Turns off the proxy
5. creates a virtual service from traffic
6. (Optional) Deletes the proxy

This script could be used to create multiple recordings and assets by looping through the v6/messageProxies PUT

Enjoy!
#Dynamic Proxy and Asset creator script
#Written 11/7/2019 by Chris Colosimo

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

#Proxy Settings
PROXY_NAME=dynamicProxy
PROXY_LISTEN_PATH="/dynamicproxy"
PROXY_SERVICE_HOST="deckofcardsapi.com"
PROXY_SERVICE_PORT=443
PROXY_SERVICE_FORWARD_PATH="/api/"
TRAFFIC_FILE_NAME=cardTraffic.txt

#Asset Settings
VA_SUBFOLDER=scratchpad
ASSET_NAME=dynamicAsset



#===================================================================================

#Create the proxy in record mode
CREATE_PROXY=$(curl -v \
  --user $VIRT_USER:$VIRT_PASS \
  --header "Content-Type: application/json" \
  --header "ParasoftRefresh: true" \
  --request POST \
  --data '{
     "name": "'$PROXY_NAME'",
      "enabled": true,
      "recording": true,
      "description": "",
      "connections": [
        {
          "type": "HTTP",
          "httpProxy": {
            "connection": {
              "proxySettings": {
                "proxyListenPath": "'$PROXY_LISTEN_PATH'"
              },
              "primaryConnection": {
                "serviceHost": "'$PROXY_SERVICE_HOST'",
                "servicePort": '$PROXY_SERVICE_PORT',
                "serviceForwardPath": "'$PROXY_SERVICE_FORWARD_PATH'"
              },
              "secondaryConnection": {
                "useSecondaryConnectionIfPrimaryFails": {
                  "enabled": false,
                  "value": {
                    "serviceHost": "",
                    "servicePort": 80,
                    "serviceForwardPath": "",
                    "recordingOptions": "BOTH_CONNECTIONS"
                  }
                }
              }
            },
            "security": {
              "sslSettings": {
                "useSsl": {
                  "enabled": true,
                  "value": {
                    "trustAllCertificates": false,
                    "acceptSelfSignedCertificates": false,
                    "keyStore": {
                      "keyStorePassword": "AgAAAAA=",
                      "keyStoreType": "PKCS12",
                      "certificate": ""
                    },
                    "trustStore": {
                      "keyStorePassword": "AgAAAAA=",
                      "keyStoreType": "PKCS12"
                    }
                  }
                }
              }
            }
          },
          "recording": {
            "targetFile": {
              "parent": {
                "id": "/VirtualAssets/recorded_traffic"
              },
              "name": "'$TRAFFIC_FILE_NAME'"
            },
            "appendTrafficData": true,
            "newFilePerTransaction": false
          }
        }


  ]
}' \
http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v6/messageProxies)
echo "==================================================================="

#Get the Proxy ID.
PROXY_ID=$(echo $CREATE_PROXY | awk -F "{\"id\":\"" '{print $2}' | awk -F "\",\"url\"" '{print $1}')
echo 'The Proxy ID is: ' $PROXY_ID

#Run Your tests Here
echo "==================================================================="
echo                          Run Your tests Here
echo http://$VIRT_HOST:$VIRT_PORT$PROXY_LISTEN_PATH/deck/new/draw/?count=2
curl http://$VIRT_HOST:$VIRT_PORT$PROXY_LISTEN_PATH/deck/new/draw/?count=2
echo
echo "==================================================================="

#Optional turn off recording and disable the proxy
#You can also use this PUT to enable/disable and change other fields of an existing Proxy
curl -v \
  --user $VIRT_USER:$VIRT_PASS \
  --header "Content-Type: application/json" \
  --header "ParasoftRefresh: true" \
  --request PUT \
  --data '{
     "name": "'$PROXY_NAME'",
      "enabled": false,
      "recording": false,
      "description": "",
      "connections": [
        {
          "type": "HTTP",
          "httpProxy": {
            "connection": {
              "proxySettings": {
                "proxyListenPath": "'$PROXY_LISTEN_PATH'"
              },
              "primaryConnection": {
                "serviceHost": "'$PROXY_SERVICE_HOST'",
                "servicePort": '$PROXY_SERVICE_PORT',
                "serviceForwardPath": "'$PROXY_SERVICE_FORWARD_PATH'"
              },
              "secondaryConnection": {
                "useSecondaryConnectionIfPrimaryFails": {
                  "enabled": false,
                  "value": {
                    "serviceHost": "",
                    "servicePort": 80,
                    "serviceForwardPath": "",
                    "recordingOptions": "BOTH_CONNECTIONS"
                  }
                }
              }
            },
            "security": {
              "sslSettings": {
                "useSsl": {
                  "enabled": true,
                  "value": {
                    "trustAllCertificates": false,
                    "acceptSelfSignedCertificates": false,
                    "keyStore": {
                      "keyStorePassword": "AgAAAAA=",
                      "keyStoreType": "PKCS12",
                      "certificate": ""
                    },
                    "trustStore": {
                      "keyStorePassword": "AgAAAAA=",
                      "keyStoreType": "PKCS12"
                    }
                  }
                }
              }
            }
          },
          "recording": {
            "targetFile": {
              "parent": {
                "id": "/VirtualAssets/recorded_traffic"
              },
              "name": "'$TRAFFIC_FILE_NAME'"
            },
            "appendTrafficData": true,
            "newFilePerTransaction": false
          }
        }


  ]
}' \
http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v6/messageProxies/$PROXY_ID
echo
echo "==================================================================="

#Create the PVA file

curl -v \
  --user $VIRT_USER:$VIRT_PASS \
  --header "Content-Type: application/json" \
  --header "ParasoftRefresh: true" \
  --request POST \
  --data '{
  "inferConstraints": "",
  "parent": {
    "id": "/VirtualAssets/'$VA_SUBFOLDER'/"
  },
  "name": "'$ASSET_NAME'",
  "trafficFile": {
    "id": "VirtualAssets/recorded_traffic/'$TRAFFIC_FILE_NAME'"
  },
  "repositoryConnectionSettings": {
    "host": "localhost",
    "port": 2424,
    "repositoryName": "'$ASSET_NAME'",
    "user": "admin",
    "password": "admin"
  },
  "dataReuse": {
    "dataSetImport": "update"
  },
  "characterEncoding": ""
}' \
http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v6/files/pvas/traffic
echo
echo "==================================================================="

#Optional delete the proxy
curl -X DELETE \
  --user $VIRT_USER:$VIRT_PASS \
  --header "Content-Type: application/json" \
  --header "ParasoftRefresh: true" \
http://$VIRT_HOST:$VIRT_PORT/soavirt/api/v6/messageProxies/$PROXY_ID
echo
echo "==================================================================="

#Finalize 
echo "Your asset is ready, You can access it at the following endpoint" 
echo "http://$VIRT_HOST:$VIRT_PORT/$ASSET_NAME$PROXY_LISTEN_PATH/deck/new/draw"
echo
echo "==================================================================="

Comments