Skip to content

Instantly share code, notes, and snippets.

@fanf
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanf/193ca80959dde12bc4d3 to your computer and use it in GitHub Desktop.
Save fanf/193ca80959dde12bc4d3 to your computer and use it in GitHub Desktop.
Getting list of directive id/technique name, version/name in Rudder API with jq

jq (http://stedolan.github.io/jq/ ) is a fabulous command line tools to post process JSON API response.

Here how you can easely get the list of Rudder API Directive (http://www.rudder-project.org/rudder-api-doc/#api-Directives-listDirectives), whith their id, name, parent Technique and Version:

% curl -k -H "X-API-Token: D6NckZMXawkQAsWTqUJHjsfUnVCY15da" -X GET https://192.168.41.2/rudder/api/latest/directives \
| jq '.data.directives[] | "\(.id) \(.techniqueName)[\(.techniqueVersion)] \(.displayName)"'

returns:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9418  100  9418    0     0   112k      0 --:--:-- --:--:-- --:--:--  110k

"82ead089-b393-4ef3-9219-ada3a872d301 checkGenericFileContent[6.0] agent1_check_v6"
"e08d19ad-e9c2-439d-8bc6-3994de649252 userManagement[6.0] Test User Directive"
"239b3b6e-4f64-41ef-b4e6-3e037db03270 test_for_good[1.0] test for good version 1.0"
"672c49e9-caff-4026-94af-5ffee819f971 test1[1.0] test1"
"216fe8d6-c160-4bf5-866c-aff4e713ef02 Install_apache[1.0] apache"
"14621111-bf61-4203-b715-acaa036474ce motdConfiguration[3.1] MOTD configuration version 3.1"
"51ea8689-aa51-4e84-8ca4-fcf472e9469f checkGenericFileContent[7.0] agent2_check_v7"

You can also easely filter by technique name:

% curl -k -H "X-API-Token: D6NckZMXawkQAsWTqUJHjsfUnVCY15da" -X GET https://192.168.41.2/rudder/api/latest/directives \
| jq '.data.directives[]| select(.techniqueName=="checkGenericFileContent") \
| "\(.id) \(.techniqueName)[\(.techniqueVersion)] \(.displayName)"'

returns:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9418  100  9418    0     0   153k      0 --:--:-- --:--:-- --:--:--  155k
"82ead089-b393-4ef3-9219-ada3a872d301 checkGenericFileContent[6.0] agent1_check_v6"
"51ea8689-aa51-4e84-8ca4-fcf472e9469f checkGenericFileContent[7.0] agent2_check_v7"

Then, it's far more easy (and lead to much less click) to migrate Directive's Technique version:

% curl -k -H "X-API-Token: D6NckZMXawkQAsWTqUJHjsfUnVCY15da" -X POST https://192.168.41.2/rudder/api/latest/directives/14621111-bf61-4203-b715-acaa036474ce -d "techniqueVersion=3.0" | jq .result

returns:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   713  100   693  100    20   9882    285 --:--:-- --:--:-- --:--:--  9900

"success"

Of course, you could even script the call to change version with a for loop:

% for i in $(curl -k -H "X-API-Token: D6NckZMXawkQAsWTqUJHjsfUnVCY15da" -X GET https://192.168.41.2/rudder/api/latest/directives \
| jq '.data.directives[]| select(.techniqueName=="checkGenericFileContent") | .id'); 
do curl -k -H "X-API-Token: D6NckZMXawkQAsWTqUJHjsfUnVCY15da" -X POST https://192.168.41.2/rudder/api/latest/directives/"${i//\"}" -d "techniqueVersion=7.0" | jq .result ;
done

returns:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9418  100  9418    0     0   196k      0 --:--:-- --:--:-- --:--:--  199k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2815  100  2795  100    20  13348     95 --:--:-- --:--:-- --:--:-- 13373
"success"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2813  100  2793  100    20  23551    168 --:--:-- --:--:-- --:--:-- 23669
"success"
@peckpeck
Copy link

curl can be easily replaced by rudder-cli.
Then your for loop becomes :

% for i in $(rudder-cli directives list | jq '.data.directives[]| select(.techniqueName=="checkGenericFileContent") | .id');
do rudder-cli directives update "${i//"}" --techniqueVersion=7.0 | jq .result ;
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment