Sha256: f9db7228920d725683689868a4bfa11db802ed7b89f11faac850f5c4bab8143f

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Utils
  module LitaPuppet
    # Utility methods for working with PuppetDB
    module PuppetDB
      def class_nodes(url, classname)
        client = ::PuppetDB::Client.new(server: url)
        q = client.request(
          'resources',
          [
            :and,
            [:'=', 'type', 'Class'],
            [:'=', 'title', classname.to_s]
          ]
        )

        q.data.map { |node| node['certname'] }
      end

      def node_roles_and_profiles(url, what, nodename)
        # TODO: validate url and nodename
        ::PuppetDB::Client.new(server: url) # this is weird but required
        d = ::PuppetDB::Client.get("/catalogs/#{nodename}")
        return d['error'] if d['error']

        tags = []
        d['data']['resources'].each { |r| tags.concat(r['tags']) }

        # return all the tags related to profile:: or role::
        case what
        when 'profiles'
          tags.sort.uniq.select { |t| t.match(/^profile::/) }
        when 'roles'
          tags.sort.uniq.select { |t| t.match(/^role::/) }
        when 'r&p', 'p&r', 'roles and profiles'
          tags.sort.uniq.select { |t| t.match(/^(profile|role)::/) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-puppet-0.7.1 lib/utils/lita_puppet/puppetdb.rb