While simple at first, webhooks can become a hassle if you have products that use a good number of them and you have to keep them up to date. Here at Dojo Five, we found that having a YAML file with the needed information was a great way to give a little automation to this process.

Your WebHooks Always Match Current Code

We developed a way to take a config.yml file and use that to update and manage webhooks on the Particle Console. Each webhook looked something like this:

webhooks:
  - name: test_webhook
    url: <https://samplesite.com>
    requestType: POST

Pretty easy! If you wanted to edit the url or name, delete a webhook, or add a webhook, it can all be done in the same place!

https://images.unsplash.com/photo-1456428746267-a1756408f782?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb
All of the functionality for your webhooks could be connected by one file in your repo.

You Don’t Have to Configure by Hand

What it normally looks like

The process of adding webhooks in itself isn’t too bad, but when you have a fleet of devices or just a lot of webhooks for one device, it can become a pain to manage and keep adding the hooks.

First you’ll need to navigate to the website and add the webhook.
Next, you’ll need to enter in some data. Not bad. But not fun.
You’ll have some options to run through, like integrations or other things.
Finish it up and create it!

 

With only four steps, things might not look so bad, but those steps can add up quickly on bigger projects. The UI also lacks a way to check that your webhooks are actually what you think they are and it can be easy to miss a PUT that should have been a GET or some other small user error.

So the info is in the yaml. Now what?

Once we have our configuration yaml ready, we have a little more work to do. We used python and its requests and pyyaml libraries for the heavy lifting.

To give a brief overview of the code (Don’t worry. I’ll have a link to the code later!), we will setup a few key functions, such as create_webhook, delete_webhook, manage_webhooks and parse_yaml_webhook.

  • parse_yaml_webhook – We want this function to grab our webhooks from config.yml.
  • manage_webhooks – We will want this to compare the webhooks in our yaml file with the ones on Particle’s Console. If it finds a webhook that’s in the yaml but not on Particle Console, it will add that webhook to the Particle Console. If it finds a webhook that’s on Particle Console but not in the yaml, it will delete the webhook on Particle Console.
  • create_webhook and delete_webhook will do as they sound. These two will use python requests to execute their functions. That request may look something like this:
    params = (
        ('access_token', API_TOKEN),
    )
    data = {
        'integration_type': 'Webhook',
        'event': webhook_info.name,
        'url': webhook_info.url,
        'requestType': webhook_info.requestType
    }
    response = requests.post('<https://api.particle.io/v1/integrations>', params=params, data=data)
    if(response.status_code == 201):
        return True
    else:
        return False

If you think it sounds interesting, here is the promised code!

NOTE: If you do take a look at the code, it is specific to the Particle Console API. Some extra features you will see will be a pretend feature that you can use to see what would happen with a change before you implement it and the use of environment variables to grab an API token. Check out the README for more info on how to run our specific example!

Infrastructure as Code

The overarching topic we’ve been discussing here is also known as Infrastructure as Code (IaC).

What is IaC?

IaC is a way to manage your hardware, infrastructure or software using configuration files. Before IaC, management of IT infrastructure had to be done manually or through tools. If you want to read a bit more about IaC and how cloud computing has spurred its advancement, you should check out this article at Stackify.

How does it relate?

It relates because we are taking the management of webhooks, which formerly needed to be done manually, and automating it using a configuration (YAML) file. With the ever increasing number of tools, programs and security surrounding software and embedded development, advancing IaC helps by saving developers tons of time. What used to take 10-15 minutes, now eats up only a minute at best!

Summary

Some things may seem trivial, but adding automation to these small tasks may prove to save time in the long run. Hopefully, this example here can help speed you up a bit with your own webhooks or get you thinking about how to automate your own processes!

And if you have questions about an embedded project you’re working on, Dojo Five can help you with not only webhooks but all aspects of your devops for embedded journey! We are always happy to hear about cool projects or interesting problems to solve, so don’t hesitate to reach out and chat with us on LinkedIn or through  email!

cole.spear@dojofive.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.