Skip to content

Instantly share code, notes, and snippets.

@mikekistler
Last active June 24, 2022 12:31
Show Gist options
  • Save mikekistler/20147caa9a5e094c8c06455ebcd19c18 to your computer and use it in GitHub Desktop.
Save mikekistler/20147caa9a5e094c8c06455ebcd19c18 to your computer and use it in GitHub Desktop.
How to create Restler config for an Azure service

How to Create the Restler Config for an Azure Service

Let me start with the tip that Marina gave me in email:

My usual way of creating config.json is to compile one spec via “--api_spec”, then copying the generated config out of the ‘Compile’ folder and modifying the paths.

There are two different ways to run the Compile step of Restler:

restler compile --api_spec <path to OpenAPI definition>

or

restler compile <compiler config file>

When you use the first way, passing the OpenAPI definition, Restler will create a Compile directory with a config.json and dict.json tailored to that specific OpenAPI defintion.

spec=~/Projects/Azure/azure-rest-api-specs/specification/appconfiguration/resource-manager/Microsoft.AppConfiguration/stable/2022-05-01/appconfiguration.json
dotnet $restler_bin/restler/Restler.dll compile --api_spec $spec                                                                                             
Starting task Compile...
Task Compile succeeded.
Collecting logs...
ls -1 Compile
StdErr.txt
StdOut.txt
config.json
custom_value_gen_template.py
defaultDict.json
dependencies.json
dependencies_debug.json
dict.json
engine_settings.json
grammar.json
grammar.py
preprocessed
restler-20220623-071647.log
unresolved_dependencies.json

We need to modify the dict.json to add specific values for certain parameters to be used in the Test phase of Restler. In the case of the Azure App Configuration service, the values needed are:

  • subscriptionId
  • resourceGroupName
  • location

Note: We do not need to supply a value for api-version (for App Configuration) because every operation that requires it has an example in the API spec that specifies the correct value.

This same set of values is probably all that is needed for most if not all management plane services. For data plane there may be some special values and we'll need a little experimentation on that part.

Updating dict.json with these values is quite easy with jq:

vars='{
   "subscriptionId": ["<my-subscription-id>"],
   "resourceGroupName": ["<my-resourcegroup-name>"],
   "location": ["<my-location>"]
 }'
jq ".restler_custom_payload = $vars" Compile/dict.json > dict.json

Next we need to make a small change to config.json to make CustomDictionaryFilePath to point to our modified dict.json.

This is also easily done with jq.

jq ".CustomDictionaryFilePath = \"$PWD/Compile/dict.json\"" Compile/config.json > config.json

Now we need to run the compile step again to pick up these configuration changes.

dotnet $restler_bin/restler/Restler.dll compile config.json

Now you are ready to run the Test phase of Restler.

dotnet $restler_bin/restler/Restler.dll test --dictionary_file dict.json --grammar_file Compile/grammar.py --settings Compile/engine_settings.json --token_refresh_command "bash $PWD/getToken.sh" --token_refresh_interval 60

Restler needs a way to obtain authentication tokens to pass on service requests. Use the --token_refresh_command argument to specify a script that will produce authentication tokens. The getToken.sh script does this using the Azure CLI:

#!/bin/bash

find . -name 'token.json' -depth 1 -mtime -1h | grep . &> /dev/null || az account get-access-token > token.json

token=$(jq -r '.accessToken' token.json)

echo "{'user1':{}, 'user2':{}}"
echo "Authorization: bearer ${token}"
echo "Authorization: shadow_unit_test_token"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment