Search code examples
testingpactpact-broker

Get dependency map API [pact-broker]


Is there a way to get full dependency map of all contracts from the Pact Broker (preferably in json format)?

There is an API call used in the graph: https://<broker-url>/groups/<service>.csv to get data to draw the graph, but that is not great for parsing and requires a call to find all services and then a call for each service to get the dependencies.

It would be nice to have one call with a full dependency map in json format.


Solution

  • Yes! There is a HAL browser built in to broker, which enables you to follow the graph programmatically.

    For example, you could run a query like this and filter with jq on the subset of properties you need, and re-order the output:

    curl -v -u 'dXfltyFMgNOFZAxr8io9wJ37iUpY42M:O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1' https://test.pact.dius.com.au/pacts/latest | jq '.pacts[]._embedded | select(.consumer.name | contains("AWSSummiteer")) | .consumer.name + "->" + .provider.name'
    

    Which produces something like:

    "AWSSummiteerSentimentSNSProvider->AWSSummiteerTwitterSNSProvider"
    "AWSSummiteerTwitterSNSConsumer->AWSSummiteerTwitterSNSProvider"
    "AWSSummiteerTwitterSNSProvider->Twitter"
    "AWSSummiteerWeb->AWSSummiteerIoT"
    "AWSSummiteerWeb->AWSSummiteerIoTPresignedUrl"
    "AWSSummiteerWeb->AWSSummiteerSentimentSNSProvider"
    "AWSSummiteerWeb->AWSSummiteerTwitterSNSConsumer"
    "AWSSummiteerWeb->AWSSummiteerWeb"
    

    which you could pipe into graphviz to create pretty charts but of course you could translate this into any format you like.

    Here is the full graphviz visualisation:

    echo "digraph { ranksep=3; ratio=auto; overlap=false; node [  shape = plaintext, fontname = "Helvetica" ];" > latest.dot ; curl -v -u 'dXfltyFMgNOFZAxr8io9wJ37iUpY42M:O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1' https://test.pact.dius.com.au/pacts/latest | jq '.pacts[]._embedded | select(.consumer.name | contains("AWSSummiteer")) | .consumer.name + "->" + .provider.name' | tr -d '"' |  sed 's/-/_/g' | sed 's/_>/->/g' >> latest.dot; echo "}" >> latest.dot
    dot latest.dot -otest.png -Tpng
    

    which creates this pretty picture:

    Dummy architecture