Skip to main content

Library for working with the PuppetDB REST API.

Project description

pypuppetdb

https://api.travis-ci.org/nedap/pypuppetdb.png

pypuppetdtb is a library to work with PuppetDB’s REST API. It is implemented using the requests library.

This library is a thin wrapper around the REST API providing some convinience functions and objects to request and hold data from PuppetDB.

To use this library you will need:
  • Python 2.6 or 2.7

  • Python 3.3

Installation

You can install this package from source or from PyPi.

$ pip install pypuppetdb
$ git clone https://github.com/nedap/pypuppetdb
$ python setup.py install

If you wish to hack on it clone the repository but after that run:

$ pip install -r requirements.txt

This will install all the runtime requirements of pypuppetdb and the dependencies for the test suite and generation of documentation.

Usage

Once you have pypuppetdb installed you can configure it to connect to PuppetDB and take it from there.

Connecting

The first thing you need to do is to connect with PuppetDB:

>>> from pypuppetdb import connect
>>> db = connect()

Nodes

The following will return a generator object yielding Node objects for every returned node from PuppetDB.

>>> nodes = db.nodes()
>>> for node in nodes:
>>>   print(node)
host1
host2
...

To query a single node the singular node() can be used:

>>> node = db.node('hostname')
>>> print(node)
hostname
Node scope

The Node objects are a bit more special in that they can query for facts and resources themselves. Using those methods from a node object will automatically add a query to the request scoping the request to the node.

>>> node = db.node('hostname')
>>> print(node.fact('osfamily'))
osfamily/hostname

Facts

>>> facts = db.facts('osfamily')
>>> for fact in facts:
>>>   print(fact)
osfamily/host1
osfamily/host2

That queries PuppetDB for the ‘osfamily’ fact and will yield Fact objects, one per node this fact is known for.

Resources

>>> resources = db.resources('file')

Will return a generator object containing all file resources you’re managing across your infrastructure. This is probably a bad idea if you have a big number of nodes as the response will be huge.

Getting Help

This project is still very new so it’s not inconceivable you’ll run into issues.

For bug reports you can file an issue. If you need help with something feel free to hit up @daenney by e-mail or find him on IRC. He can usually be found on IRCnet and Freenode and idles in #puppet.

Documentation

API documentation is automatically generated from the docstrings using Sphinx’s autodoc feature.

Documentation will automatically be rebuilt on every push thanks to the Read The Docs webhook. You can find it here.

You can build the documentation manually by doing:

$ cd docs
$ make html

Doing so will only work if you have Sphinx installed, which you can acheive through:

$ pip install -r requirements.txt

Contributing

We welcome contributions to this library. However, there are a few ground rules contributors should be aware of.

License

This project is licensed under the Apache v2.0 License. As such, your contributions, once accepted, are automatically covered by this license.

Commit messages

Write decent commit messages. Don’t use swear words and refrain from uninformative commit messages as ‘fixed typo’.

The preferred format of a commit message:

docs/quickstart: Fixed a typo in the Nodes section.

If needed, elaborate further on this commit. Feel free to write a
complete blog post here if that helps us understand what this is
all about.

Fixes #4 and resolves #2.

If you’d like a more elaborate guide on how to write and format your commit messages have a look at this post by Tim Pope.

Tests

Commits are expected to contain tests or updates to tests if they add to or modify the current behaviour.

The test suite is powered by pytest and requires pytest, pytest-pep8, httpretty and pytest-httpretty which will be installed for you if you run:

$ pip install -r requirements/tests.txt

To run the unit tests (the ones that don’t require a live PuppetDB):

$ py.test -v -m unit

If the tests pass, you’re golden. If not we’ll have to figure out why and fix that. Feel free to ask for help on this.

Changelog

0.0.2

  • Fix a bug in setup.py preventing successful installation.

0.0.1

Initial release. Implements most of the v2 API.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypuppetdb-0.0.2.tar.gz (22.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page