|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +Puppet::Type.type(:foreman_hostgroup).provide( |
| 4 | + :rest_v3, |
| 5 | + parent: Puppet::Type.type(:foreman_resource).provider(:rest_v3) |
| 6 | +) do |
| 7 | + confine feature: %i[json oauth] |
| 8 | + |
| 9 | + def initialize(value = {}) |
| 10 | + super |
| 11 | + @property_flush = {} |
| 12 | + end |
| 13 | + |
| 14 | + def exists? |
| 15 | + !id.nil? |
| 16 | + end |
| 17 | + |
| 18 | + def create |
| 19 | + Puppet.debug("Creating Foreman Hostgroup #{resource[:name]} with parent #{resource[:parent_hostgroup]}") |
| 20 | + |
| 21 | + if resource[:parent_hostgroup] && parent_hostgroup_id.nil? |
| 22 | + raise Puppet::Error, |
| 23 | + "Parent hostgroup #{resource[:parent_hostgroup]} for #{resource[:name]} not found" |
| 24 | + end |
| 25 | + |
| 26 | + organization_ids = resource[:organizations]&.map { |org| organization_id(org) } |
| 27 | + location_ids = resource[:locations]&.map { |loc| location_id(loc) } |
| 28 | + |
| 29 | + post_data = { |
| 30 | + hostgroup: { |
| 31 | + name: resource[:name], |
| 32 | + parent_id: parent_hostgroup_id, |
| 33 | + description: resource[:description], |
| 34 | + organization_ids: organization_ids, |
| 35 | + location_ids: location_ids |
| 36 | + } |
| 37 | + }.to_json |
| 38 | + path = 'api/v2/hostgroups' |
| 39 | + r = request(:post, path, {}, post_data) |
| 40 | + |
| 41 | + return if success?(r) |
| 42 | + |
| 43 | + raise Puppet::Error, "Error making POST request to Foreman at #{request_uri(path)}: #{error_message(r)}" |
| 44 | + end |
| 45 | + |
| 46 | + def destroy |
| 47 | + Puppet.debug("Destroying Foreman Hostgroup #{resource[:name]} with parent #{resource[:parent_hostgroup]}") |
| 48 | + path = "api/v2/hostgroups/#{id}" |
| 49 | + r = request(:delete, path) |
| 50 | + |
| 51 | + unless success?(r) |
| 52 | + error_string = "Error making DELETE request to Foreman at #{request_uri(path)}: #{error_message(r)}" |
| 53 | + raise Puppet::Error, error_string |
| 54 | + end |
| 55 | + |
| 56 | + @hostgroup = nil |
| 57 | + end |
| 58 | + |
| 59 | + def flush |
| 60 | + return if @property_flush.empty? |
| 61 | + |
| 62 | + Puppet.debug "Calling API to update properties for #{resource[:name]}" |
| 63 | + |
| 64 | + path = "api/v2/hostgroups/#{id}" |
| 65 | + r = request(:put, path, {}, { hostgroup: @property_flush }.to_json) |
| 66 | + |
| 67 | + return if success?(r) |
| 68 | + |
| 69 | + raise Puppet::Error, "Error making PUT request to Foreman at #{request_uri(path)}: #{error_message(r)}" |
| 70 | + end |
| 71 | + |
| 72 | + # Property getters |
| 73 | + def description |
| 74 | + hostgroup ? hostgroup['description'] : nil |
| 75 | + end |
| 76 | + |
| 77 | + def organizations |
| 78 | + hostgroup ? hostgroup['organizations'].map { |org| org['title'] } : nil |
| 79 | + end |
| 80 | + |
| 81 | + def locations |
| 82 | + hostgroup ? hostgroup['locations'].map { |org| org['title'] } : nil |
| 83 | + end |
| 84 | + |
| 85 | + # Property setters |
| 86 | + # If one of more properties is being modified then group all of these updates in @property_flush so that we can update them in a single API call in `flush()` |
| 87 | + def description=(value) |
| 88 | + @property_flush[:description] = value |
| 89 | + end |
| 90 | + |
| 91 | + def organizations=(value) |
| 92 | + @property_flush[:organization_ids] = value.map { |org| organization_id(org) } |
| 93 | + end |
| 94 | + |
| 95 | + def locations=(value) |
| 96 | + @property_flush[:location_ids] = value.map { |loc| location_id(loc) } |
| 97 | + end |
| 98 | + |
| 99 | + private |
| 100 | + |
| 101 | + def hostgroup |
| 102 | + @hostgroup ||= begin |
| 103 | + path = 'api/v2/hostgroups' |
| 104 | + search_name = resource[:name] |
| 105 | + search_title = if resource[:parent_hostgroup] |
| 106 | + "#{resource[:parent_hostgroup]}/#{resource[:name]}" |
| 107 | + else |
| 108 | + search_name |
| 109 | + end |
| 110 | + Puppet.debug("Searching for hostgroup with name #{search_name} and title #{search_title}") |
| 111 | + r = request(:get, path, search: %(title="#{search_title}" and name="#{search_name}")) |
| 112 | + |
| 113 | + raise Puppet::Error, "Error making GET request to Foreman at #{request_uri(path)}: #{error_message(r)}" unless success?(r) |
| 114 | + |
| 115 | + results = JSON.parse(r.body)['results'] |
| 116 | + unless results.empty? |
| 117 | + raise Puppet::Error, "Too many hostgroups found when looking for hostgroup with name #{search_name} and title #{search_title}" if results.size > 1 |
| 118 | + |
| 119 | + get_hostgroup_by_id(results[0]['id']) |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + def get_hostgroup_by_id(id) |
| 125 | + path = "api/v2/hostgroups/#{id}" |
| 126 | + r = request(:get, path) |
| 127 | + |
| 128 | + raise Puppet::Error, "Error making GET request to Foreman at #{request_uri(path)}: #{error_message(r)}" unless success?(r) |
| 129 | + |
| 130 | + JSON.parse(r.body) |
| 131 | + end |
| 132 | + |
| 133 | + def id |
| 134 | + hostgroup ? hostgroup['id'] : nil |
| 135 | + end |
| 136 | + |
| 137 | + def parent_hostgroup |
| 138 | + return nil unless resource[:parent_hostgroup] |
| 139 | + |
| 140 | + @parent_hostgroup ||= begin |
| 141 | + path = 'api/v2/hostgroups' |
| 142 | + search_title = resource[:parent_hostgroup] |
| 143 | + search_name = resource[:parent_hostgroup_name] || search_title.split('/').last |
| 144 | + Puppet.debug("Searching for parent hostgroup with name #{search_name} and title #{search_title}") |
| 145 | + r = request(:get, path, search: %(title="#{search_title}" and name="#{search_name}")) |
| 146 | + |
| 147 | + raise Puppet::Error, "Error making GET request to Foreman at #{request_uri(path)}: #{error_message(r)}" unless success?(r) |
| 148 | + |
| 149 | + results = JSON.parse(r.body)['results'] |
| 150 | + raise Puppet::Error, "Parent hostgroup #{resource[:parent_hostgroup]} for #{resource[:name]} not found" if results.empty? |
| 151 | + raise Puppet::Error, "Too many hostgroups found when looking for parent hostgroup with name #{search_name} and title #{search_title}" if results.size > 1 |
| 152 | + |
| 153 | + get_hostgroup_by_id(results[0]['id']) |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + def parent_hostgroup_id |
| 158 | + parent_hostgroup ? parent_hostgroup['id'] : nil |
| 159 | + end |
| 160 | + |
| 161 | + def organization_id(organization_title) |
| 162 | + title_to_id('organizations', organization_title) |
| 163 | + end |
| 164 | + |
| 165 | + def location_id(location_title) |
| 166 | + title_to_id('locations', location_title) |
| 167 | + end |
| 168 | + |
| 169 | + # Returns the id of a location/organization based on its title |
| 170 | + def title_to_id(type, title) |
| 171 | + path = "api/v2/#{type}" |
| 172 | + r = request(:get, path, search: %(title="#{title}")) |
| 173 | + unless success?(r) |
| 174 | + raise Puppet::Error, |
| 175 | + "Error making GET request to Foreman at #{request_uri(path)}: #{error_message(r)}" |
| 176 | + end |
| 177 | + results = JSON.parse(r.body)['results'] |
| 178 | + raise Puppet::Error, "#{title} not found in #{type}" unless results.size == 1 |
| 179 | + |
| 180 | + results[0]['id'] |
| 181 | + end |
| 182 | +end |
0 commit comments