diff --git a/lib/puppet/provider/liu_pulpcore_deb_verbatim_publication/liu_pulpcore_deb_verbatim_publication.rb b/lib/puppet/provider/liu_pulpcore_deb_verbatim_publication/liu_pulpcore_deb_verbatim_publication.rb new file mode 100644 index 0000000000000000000000000000000000000000..de70effe710ad9543a71e21e24d9794e7a4a8748 --- /dev/null +++ b/lib/puppet/provider/liu_pulpcore_deb_verbatim_publication/liu_pulpcore_deb_verbatim_publication.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'puppet/resource_api/simple_provider' +require 'puppet_x/liu_pulpcore/connection' +require 'puppet_x/liu_pulpcore/resource_cache' + +# Implementation for the liu_pulpcore_deb_verbatim_publication type using Net::HTTP +class Puppet::Provider::LiuPulpcoreDebVerbatimPublication::LiuPulpcoreDebVerbatimPublication < Puppet::ResourceApi::SimpleProvider + include PuppetX::LiuPulpcore::Connection + include PuppetX::LiuPulpcore::ResourceCache + + resource_type :liu_pulpcore_deb_verbatim_publication + + def uncached_get(_context) + paginated(per_page: 1000) { request('/pulp/api/v3/publications/deb/verbatim/', 'get') }['results'].map do |pub| + next if pub['repository'].nil? + + result = pub.transform_keys(&:to_sym).slice(*attribute_keys).compact + result[:ensure] = 'present' + result[:name] = find_cached(:liu_pulpcore_deb_repository, pub['repository'])[:name] + result[:pulp_href] = pub['pulp_href'] + result + end + end + + def create(_context, name, should) + payload = should.slice(*attribute_keys) + payload.delete :name + payload[:repository] = find_cached(:liu_pulpcore_deb_repository, name)[:pulp_href] + request('/pulp/api/v3/publications/deb/verbatim/', 'post', body: payload.compact) + cache << should.merge(name: name) + end + + def update(_context, name, should) + pulp_href = should[:pulp_href] + pulp_href ||= find_cached(:liu_pulpcore_deb_publication, name)[:pulp_href] + request(pulp_href, 'delete') + + payload = should.slice(*attribute_keys) + payload[:repository] = find_cached(:liu_pulpcore_deb_repository, name)[:pulp_href] + request('/pulp/api/v3/publications/deb/verbatim/', 'post', body: payload.compact) + + cache.find { |pub| pub[:name] == name }&.merge!(should) + end + + def delete(_context, name) + pulp_href = find_cached(:liu_pulpcore_deb_verbatim_publication, name)[:pulp_href] + request(pulp_href, 'delete') + # cache.delete_if { |pub| pub[:pulp_href] == pulp_href } + end +end diff --git a/lib/puppet/type/liu_pulpcore_deb_verbatim_publication.rb b/lib/puppet/type/liu_pulpcore_deb_verbatim_publication.rb new file mode 100644 index 0000000000000000000000000000000000000000..8d05cc2ea918c6ea27ef20468e9d85af5a7173ed --- /dev/null +++ b/lib/puppet/type/liu_pulpcore_deb_verbatim_publication.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'puppet/resource_api' + +Puppet::ResourceApi.register_type( + name: 'liu_pulpcore_deb_verbatim_publication', + docs: <<-EOS, +@summary Pulp DEB Verbatim Publication +EOS + features: [], + attributes: { + ensure: { + type: 'Enum[present, absent]', + default: 'present', + desc: 'Whether this resource should be present or absent on the target system.', + }, + name: { + type: 'String', + behaviour: :namevar, + desc: 'Repository name.', + }, + + pulp_href: { + type: 'Optional[String]', + behaviour: :read_only, + desc: 'The internal pulp href for the object', + }, + }, + autorequire: { + liu_pulpcore_deb_repository: [ + '$name', + ], + }, +) diff --git a/spec/fixtures/pulp_data/get_deb_verbatim_publications.json b/spec/fixtures/pulp_data/get_deb_verbatim_publications.json new file mode 100644 index 0000000000000000000000000000000000000000..71037eb88c5329822913a7727ca97d66da209ef7 --- /dev/null +++ b/spec/fixtures/pulp_data/get_deb_verbatim_publications.json @@ -0,0 +1,15 @@ +{ + "count": 3, + "next": null, + "previous": null, + "results": [ + { + "pulp_href": "/pulp/api/v3/publications/deb/verbatim/0196cde8-5262-71a1-9ee9-f857da7d5f2d/", + "prn": "prn:deb.verbatimpublication:0196cde8-5262-71a1-9ee9-f857da7d5f2d", + "pulp_created": "2025-05-14T08:27:56.899878Z", + "pulp_last_updated": "2025-05-14T08:27:56.960171Z", + "repository_version": "/pulp/api/v3/repositories/deb/apt/018eadfc-4c94-72d6-8e16-bfa0bba968d1/versions/0/", + "repository": "/pulp/api/v3/repositories/deb/apt/018eadfc-4c94-72d6-8e16-bfa0bba968d1/" + } + ] +} diff --git a/spec/integration/liu_pulpcore/catalog_spec.rb b/spec/integration/liu_pulpcore/catalog_spec.rb index be69025c13666bdade287019cb99078f1f000750..98597f94224dc00e5cfd05a8c5a72e60d690c812 100644 --- a/spec/integration/liu_pulpcore/catalog_spec.rb +++ b/spec/integration/liu_pulpcore/catalog_spec.rb @@ -11,6 +11,7 @@ RSpec.describe 'LiU_pulpcore integration test' do let(:deb_remote) { Puppet::Type.type(:liu_pulpcore_deb_remote).new(name: 'testing', url: 'https://') } let(:deb_repository) { Puppet::Type.type(:liu_pulpcore_deb_repository).new(name: 'testing', remote: 'testing') } let(:deb_publication) { Puppet::Type.type(:liu_pulpcore_deb_publication).new(name: 'testing') } + let(:deb_verbatim_publication) { Puppet::Type.type(:liu_pulpcore_deb_verbatim_publication).new(name: 'testing') } let(:deb_distribution) { Puppet::Type.type(:liu_pulpcore_deb_distribution).new(name: 'testing', base_path: 'foo', repository: 'testing') } let(:python_remote) { Puppet::Type.type(:liu_pulpcore_python_remote).new(name: 'testing', url: 'https://') } let(:python_repository) { Puppet::Type.type(:liu_pulpcore_python_repository).new(name: 'testing', remote: 'testing') } @@ -28,7 +29,7 @@ RSpec.describe 'LiU_pulpcore integration test' do # Shuffle order to ensure autorequire does what it should [ rpm_remote, rpm_repository, rpm_publication, rpm_distribution, - deb_remote, deb_repository, deb_publication, deb_distribution, + deb_remote, deb_repository, deb_publication, deb_verbatim_publication, deb_distribution, python_remote, python_repository, python_publication, python_distribution, role, group, user, group_role, user_role @@ -44,6 +45,7 @@ RSpec.describe 'LiU_pulpcore integration test' do # Add some role references [user_role, group_role].each do |holder| holder[:object] << deb_publication + holder[:object] << deb_verbatim_publication holder[:object] << rpm_publication holder[:object] << python_publication holder[:object] << deb_remote @@ -199,6 +201,19 @@ RSpec.describe 'LiU_pulpcore integration test' do }.to_json, ) + debv_pub = stub_request(:post, 'http://pulpcore-api/pulp/api/v3/publications/deb/verbatim/') + .with( + body: { + repository: '/pulp/api/v3/repositories/deb/apt/def456/', + }, + ) + .to_return( + status: 202, + body: { + task: 'http://example.com', + }.to_json, + ) + python_pub = stub_request(:post, 'http://pulpcore-api/pulp/api/v3/publications/python/pypi/') .with( body: { @@ -348,6 +363,7 @@ RSpec.describe 'LiU_pulpcore integration test' do assert_requested python_repo assert_requested rpm_pub assert_requested deb_pub + assert_requested debv_pub assert_requested python_pub assert_requested rpm_dist assert_requested deb_dist @@ -365,6 +381,7 @@ RSpec.describe 'LiU_pulpcore integration test' do assert_requested :get, 'http://pulpcore-api/pulp/api/v3/distributions/python/pypi/?limit=100&offset=0', times: 1 assert_requested :get, 'http://pulpcore-api/pulp/api/v3/publications/rpm/rpm/?limit=1000&offset=0', times: 1 assert_requested :get, 'http://pulpcore-api/pulp/api/v3/publications/deb/apt/?limit=1000&offset=0', times: 1 + assert_requested :get, 'http://pulpcore-api/pulp/api/v3/publications/deb/verbatim/?limit=1000&offset=0', times: 1 assert_requested :get, 'http://pulpcore-api/pulp/api/v3/publications/python/pypi/?limit=1000&offset=0', times: 1 assert_requested :get, 'http://pulpcore-api/pulp/api/v3/repositories/rpm/rpm/?limit=100&offset=0', times: 1 assert_requested :get, 'http://pulpcore-api/pulp/api/v3/repositories/deb/apt/?limit=100&offset=0', times: 1 diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb index 299e8c21f9325428305c8ce85564d04a802669d3..89945812c86e0e752388063a0d41668250ee7275 100644 --- a/spec/spec_helper_local.rb +++ b/spec/spec_helper_local.rb @@ -63,6 +63,11 @@ def allow_searching_full_cache status: 200, body: File.read('spec/fixtures/pulp_data/get_deb_publications.json'), ) + stub_request(:get, 'http://pulpcore-api/pulp/api/v3/publications/deb/verbatim/?limit=1000&offset=0') + .to_return( + status: 200, + body: File.read('spec/fixtures/pulp_data/get_deb_verbatim_publications.json'), + ) stub_request(:get, 'http://pulpcore-api/pulp/api/v3/publications/python/pypi/?limit=1000&offset=0') .to_return( status: 200, diff --git a/spec/unit/puppet/provider/liu_pulpcore_deb_verbatim_publication/liu_pulpcore_deb_verbatim_publication_spec.rb b/spec/unit/puppet/provider/liu_pulpcore_deb_verbatim_publication/liu_pulpcore_deb_verbatim_publication_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..0b399163488daadf5953e4479563c424a59fb762 --- /dev/null +++ b/spec/unit/puppet/provider/liu_pulpcore_deb_verbatim_publication/liu_pulpcore_deb_verbatim_publication_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'spec_helper' + +ensure_module_defined('Puppet::Provider::LiuPulpcoreDebVerbatimPublication') +require 'puppet/provider/liu_pulpcore_deb_verbatim_publication/liu_pulpcore_deb_verbatim_publication' + +RSpec.describe Puppet::Provider::LiuPulpcoreDebVerbatimPublication::LiuPulpcoreDebVerbatimPublication do + subject(:provider) { described_class.new } + + let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') } + + let(:test_data) do + { + pulp_href: '/pulp/api/v3/publications/deb/verbatim/0196cde8-5262-71a1-9ee9-f857da7d5f2d/', + ensure: 'present', + name: 'internal_jammy-x86_64', + } + end + + before(:each) do + # Allows retrieving the publications + stub_request(:get, 'http://pulpcore-api/pulp/api/v3/publications/deb/verbatim/?limit=1000&offset=0') + .to_return( + status: 200, + body: File.read('spec/fixtures/pulp_data/get_deb_verbatim_publications.json'), + ) + + # Allow looking up repository names + stub_request(:get, 'http://pulpcore-api/pulp/api/v3/repositories/deb/apt/?limit=100&offset=0') + .to_return( + status: 200, + body: File.read('spec/fixtures/pulp_data/get_deb_repositories-trimmed.json'), + ) + end + + describe 'create(context, name, should)' do + it 'creates the resource' do + req = stub_request(:post, 'http://pulpcore-api/pulp/api/v3/publications/deb/verbatim/') + .with( + body: { + repository: '/pulp/api/v3/repositories/deb/apt/018eadfc-4c94-72d6-8e16-bfa0bba968d1/', + }, + ) + .to_return( + status: 200, + body: '{}', + ) + + provider.create(context, test_data[:name], test_data) + + assert_requested req + end + end + + describe '#get' do + it 'processes resources' do + data = provider.get(context) + + expect(data.size).to eq 1 + expect(data.first).to eq test_data + end + end + + describe 'delete(context, name)' do + it 'deletes the resource' do + req = stub_request(:delete, 'http://pulpcore-api/pulp/api/v3/publications/deb/verbatim/0196cde8-5262-71a1-9ee9-f857da7d5f2d/') + .to_return( + status: 200, + body: '{}', + ) + + provider.delete(context, test_data[:name]) + + assert_requested req + end + end +end