diff options
author | Thayne McCombs <thayne@lucidchart.com> | 2023-05-03 12:34:20 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-03 14:34:20 -0400 |
commit | b731df4d74cc510d16d5db15ba54ab13fbf14373 (patch) | |
tree | 25642a52682c51f0c1a7ca618c52dbdcb3b52cbe | |
parent | ab8dbddf34f10409ef5398bdf597599f31707b09 (diff) | |
download | chef-b731df4d74cc510d16d5db15ba54ab13fbf14373.tar.gz |
feat(apt_repository): Allow specifying arbitrary options (#13728)
This allows specifying additional options to apt repositories, in addition to `trusted` and `arch`.
By using an array of strings we also allow using multivalue operators like -= and +=
Fixes: #13727
Signed-off-by: Thayne McCombs <thayne@lucid.co>
-rw-r--r-- | cspell.json | 1 | ||||
-rw-r--r-- | lib/chef/resource/apt_repository.rb | 31 | ||||
-rw-r--r-- | spec/unit/provider/apt_repository_spec.rb | 24 | ||||
-rw-r--r-- | spec/unit/resource/apt_repository_spec.rb | 5 |
4 files changed, 48 insertions, 13 deletions
diff --git a/cspell.json b/cspell.json index d833183917..9e8b47308a 100644 --- a/cspell.json +++ b/cspell.json @@ -218,6 +218,7 @@ "cookbookname", "copypasta", "coreutils", + "corretto", "cpio", "Crae", "CREAT", diff --git a/lib/chef/resource/apt_repository.rb b/lib/chef/resource/apt_repository.rb index 7e2ced5c92..5ea3b31a75 100644 --- a/lib/chef/resource/apt_repository.rb +++ b/lib/chef/resource/apt_repository.rb @@ -98,6 +98,18 @@ class Chef end ``` + **Add repository that needs custom options**: + ```ruby + apt_repository 'corretto' do + uri 'https://apt.corretto.aws' + arch 'amd64' + distribution 'stable' + components ['main'] + options ['target-=Contents-deb'] + key 'https://apt.corretto.aws/corretto.key' + end + ``` + **Remove a repository from the list**: ```ruby @@ -159,6 +171,10 @@ class Chef description: "Determines whether to rebuild the APT package cache.", default: true, desired_state: false + property :options, [String, Array], + description: "Additional options to set for the repository", + default: [], coerce: proc { |x| Array(x) } + default_action :add allowed_actions :add, :remove @@ -388,19 +404,21 @@ class Chef # @param [Array] components # @param [Boolean] trusted # @param [String] arch + # @param [Array] options # @param [Boolean] add_src # # @return [String] complete repo config text - def build_repo(uri, distribution, components, trusted, arch, add_src = false) + def build_repo(uri, distribution, components, trusted, arch, options, add_src = false) uri = make_ppa_url(uri) if is_ppa_url?(uri) uri = Addressable::URI.parse(uri) components = Array(components).join(" ") - options = [] - options << "arch=#{arch}" if arch - options << "trusted=yes" if trusted - optstr = unless options.empty? - "[" + options.join(" ") + "]" + options_list = [] + options_list << "arch=#{arch}" if arch + options_list << "trusted=yes" if trusted + options_list += options + optstr = unless options_list.empty? + "[" + options_list.join(" ") + "]" end info = [ optstr, uri.normalize.to_s, distribution, components ].compact.join(" ") repo = "deb #{info}\n" @@ -461,6 +479,7 @@ class Chef repo_components, new_resource.trusted, new_resource.arch, + new_resource.options, new_resource.deb_src ) diff --git a/spec/unit/provider/apt_repository_spec.rb b/spec/unit/provider/apt_repository_spec.rb index 0f5421abc2..979119ed11 100644 --- a/spec/unit/provider/apt_repository_spec.rb +++ b/spec/unit/provider/apt_repository_spec.rb @@ -246,33 +246,43 @@ C5986B4F1257FFA86632CBA746181433FBB75451 describe "#build_repo" do it "creates a repository string" do target = "deb http://test/uri unstable main\n" - expect(provider.build_repo("http://test/uri", "unstable", "main", false, nil)).to eql(target) + expect(provider.build_repo("http://test/uri", "unstable", "main", false, nil, [])).to eql(target) end it "creates a repository string with spaces" do target = "deb http://test/uri%20with%20spaces unstable main\n" - expect(provider.build_repo("http://test/uri with spaces", "unstable", "main", false, nil)).to eql(target) + expect(provider.build_repo("http://test/uri with spaces", "unstable", "main", false, nil, [])).to eql(target) end it "creates a repository string with no distribution" do target = "deb http://test/uri main\n" - expect(provider.build_repo("http://test/uri", nil, "main", false, nil)).to eql(target) + expect(provider.build_repo("http://test/uri", nil, "main", false, nil, [])).to eql(target) end it "creates a repository string with source" do target = "deb http://test/uri unstable main\ndeb-src http://test/uri unstable main\n" - expect(provider.build_repo("http://test/uri", "unstable", "main", false, nil, true)).to eql(target) + expect(provider.build_repo("http://test/uri", "unstable", "main", false, nil, [], true)).to eql(target) end - it "creates a repository string with options" do + it "creates a repository string with trusted" do target = "deb [trusted=yes] http://test/uri unstable main\n" - expect(provider.build_repo("http://test/uri", "unstable", "main", true, nil)).to eql(target) + expect(provider.build_repo("http://test/uri", "unstable", "main", true, nil, [])).to eql(target) + end + + it "creates a repository string with custom options" do + target = "deb [by-hash=no] http://test/uri unstable main\n" + expect(provider.build_repo("http://test/uri", "unstable", "main", false, nil, ["by-hash=no"])).to eql(target) + end + + it "creates a repository string with trusted, arch, and custom options" do + target = "deb [arch=amd64 trusted=yes by-hash=no] http://test/uri unstable main\n" + expect(provider.build_repo("http://test/uri", "unstable", "main", true, "amd64", ["by-hash=no"])).to eql(target) end it "handles a ppa repo" do target = "deb http://ppa.launchpad.net/chef/main/ubuntu unstable main\n" expect(provider).to receive(:make_ppa_url).with("ppa:chef/main").and_return("http://ppa.launchpad.net/chef/main/ubuntu") - expect(provider.build_repo("ppa:chef/main", "unstable", "main", false, nil)).to eql(target) + expect(provider.build_repo("ppa:chef/main", "unstable", "main", false, nil, [])).to eql(target) end end end diff --git a/spec/unit/resource/apt_repository_spec.rb b/spec/unit/resource/apt_repository_spec.rb index 23016c9abf..d97d746364 100644 --- a/spec/unit/resource/apt_repository_spec.rb +++ b/spec/unit/resource/apt_repository_spec.rb @@ -68,6 +68,11 @@ describe Chef::Resource::AptRepository do expect(resource.key).to eql(["key1"]) end + it "allows setting options to a String and coerces it to an Array" do + resource.options = "by-hash=no" + expect(resource.options).to eql(["by-hash=no"]) + end + it "fails if the user provides a repo_name with a forward slash" do expect { resource.repo_name "foo/bar" }.to raise_error(ArgumentError) end |