|
| 1 | +Puppet::Type.type(:foreman_subnet).provide(:rest_v3, :parent => Puppet::Type.type(:foreman_resource).provider(:rest_v3)) do |
| 2 | + confine :feature => [:json, :oauth] |
| 3 | + |
| 4 | + def exists? |
| 5 | + !id.nil? |
| 6 | + end |
| 7 | + |
| 8 | + def create |
| 9 | + path = "api/v2/subnets" |
| 10 | + payload = { |
| 11 | + :subnet => { |
| 12 | + :name => resource[:name], |
| 13 | + :description => resource[:description], |
| 14 | + :network_type => resource[:network_type], |
| 15 | + :network => resource[:network], |
| 16 | + :cidr => resource[:cidr], |
| 17 | + :mask => resource[:mask], |
| 18 | + :gateway => resource[:gateway], |
| 19 | + :dns_primary => resource[:dns_primary], |
| 20 | + :dns_secondary => resource[:dns_secondary], |
| 21 | + :ipam => !resource[:ipam].nil? ? resource[:ipam] : 'None', |
| 22 | + :from => resource[:from], |
| 23 | + :to => resource[:to], |
| 24 | + :vlanid => resource[:vlanid], |
| 25 | + :domain_ids => resource[:domain_ids].map {|s| search('domains', s)}, |
| 26 | + :dhcp_id => search('smart_proxies', resource[:dhcp_id]), |
| 27 | + :tftp_id => search('smart_proxies', resource[:tftp_id]), |
| 28 | + :httpboot_id => search('smart_proxies', resource[:httpboot_id]), |
| 29 | + :dns_id => search('smart_proxies', resource[:dns_id]), |
| 30 | + :template_id => search('smart_proxies', resource[:template_id]), |
| 31 | + :bmc_id => search('smart_proxies', resource[:bmc_id]), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + req = request(:post, path, {}, payload.to_json) |
| 36 | + |
| 37 | + unless success?(req) |
| 38 | + error_string = "Error making POST request to Foreman at #{request_uri(path)}: #{error_message(req)}" |
| 39 | + raise Puppet::Error.new(error_string) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def destroy |
| 44 | + req = request(:delete, destroy_path, {}) |
| 45 | + |
| 46 | + unless success?(req) |
| 47 | + error_string = "Error making DELETE request to Foreman at #{request_uri(path)}: #{error_message(req)}" |
| 48 | + raise Puppet::Error.new(error_string) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def id |
| 53 | + subnet['id'] if subnet |
| 54 | + end |
| 55 | + |
| 56 | + def subnet |
| 57 | + @subnet ||= begin |
| 58 | + path = 'api/v2/subnets' |
| 59 | + req = request(:get, path, :search => %{name="#{resource[:name]}"}) |
| 60 | + |
| 61 | + unless success?(req) |
| 62 | + error_string = "Error making GET request to Foreman at #{request_uri(path)}: #{error_message(req)}" |
| 63 | + raise Puppet::Error.new(error_string) |
| 64 | + end |
| 65 | + |
| 66 | + JSON.load(req.body)['results'].first |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + private |
| 71 | + |
| 72 | + def destroy_path |
| 73 | + "api/v2/subnets/#{id}" |
| 74 | + end |
| 75 | +end |
0 commit comments