diff options
-rw-r--r-- | lib/chef/provider/support/zypper_repo.erb | 6 | ||||
-rw-r--r-- | lib/chef/provider/zypper_repository.rb | 58 | ||||
-rw-r--r-- | lib/chef/resource/zypper_repository.rb | 32 | ||||
-rw-r--r-- | spec/unit/provider/zypper_repository_spec.rb | 13 | ||||
-rw-r--r-- | spec/unit/resource/zypper_repository_spec.rb | 2 |
5 files changed, 61 insertions, 50 deletions
diff --git a/lib/chef/provider/support/zypper_repo.erb b/lib/chef/provider/support/zypper_repo.erb index 6d508fa77f..23e871e406 100644 --- a/lib/chef/provider/support/zypper_repo.erb +++ b/lib/chef/provider/support/zypper_repo.erb @@ -1,15 +1,17 @@ -# This file was generated by Chef +# This file was generated by Chef Infra # Do NOT modify this file by hand. [<%= @config.repo_name %>] <% %w{ type enabled autorefresh gpgcheck gpgkey baseurl mirrorlist path priority keeppackages mode refresh_cache }.each do |prop| -%> -<% next if @config.send(prop.to_sym).nil? -%> +<% next if @config.send(prop.to_sym).nil? || (@config.send(prop.to_sym).is_a?(Array) && @config.send(prop.to_sym).empty?) -%> <%= prop %>=<%= case @config.send(prop.to_sym) when TrueClass '1' when FalseClass '0' + when Array + @config.send(prop.to_sym).join("\n ") else @config.send(prop.to_sym) end %> diff --git a/lib/chef/provider/zypper_repository.rb b/lib/chef/provider/zypper_repository.rb index bbb25ee6cd..c1c863e20e 100644 --- a/lib/chef/provider/zypper_repository.rb +++ b/lib/chef/provider/zypper_repository.rb @@ -31,12 +31,12 @@ class Chef action :create do if new_resource.gpgautoimportkeys - install_gpg_key(new_resource.gpgkey) + install_gpg_keys(new_resource.gpgkey) else logger.debug("'gpgautoimportkeys' property is set to false. Skipping key import.") end - declare_resource(:template, "/etc/zypp/repos.d/#{escaped_repo_name}.repo") do + template "/etc/zypp/repos.d/#{escaped_repo_name}.repo" do if template_available?(new_resource.source) source new_resource.source else @@ -51,13 +51,13 @@ class Chef end action :delete do - declare_resource(:execute, "zypper --quiet --non-interactive removerepo #{escaped_repo_name}") do + execute "zypper --quiet --non-interactive removerepo #{escaped_repo_name}" do only_if "zypper --quiet lr #{escaped_repo_name}" end end action :refresh do - declare_resource(:execute, "zypper --quiet --non-interactive refresh --force #{escaped_repo_name}") do + execute "zypper --quiet --non-interactive refresh --force #{escaped_repo_name}" do only_if "zypper --quiet lr #{escaped_repo_name}" end end @@ -68,15 +68,7 @@ class Chef # zypper repos are allowed to have spaces in the names # @return [String] escaped repo string def escaped_repo_name - Shellwords.escape(new_resource.repo_name) - end - - # return the specified cookbook name or the cookbook containing the - # resource. - # - # @return [String] name of the cookbook - def cookbook_name - new_resource.cookbook || new_resource.cookbook_name + @escaped_repo_name ||= Shellwords.escape(new_resource.repo_name) end # determine if a template file is available in the current run @@ -84,7 +76,7 @@ class Chef # # @return [Boolean] template file exists or doesn't def template_available?(path) - !path.nil? && run_context.has_template_in_cookbook?(cookbook_name, path) + !path.nil? && run_context.has_template_in_cookbook?(new_resource.cookbook, path) end # determine if a cookbook file is available in the run @@ -92,7 +84,7 @@ class Chef # # @return [Boolean] cookbook file exists or doesn't def has_cookbook_file?(fn) - run_context.has_cookbook_file_in_cookbook?(cookbook_name, fn) + run_context.has_cookbook_file_in_cookbook?(new_resource.cookbook, fn) end # Given the provided key URI determine what kind of chef resource we need @@ -158,27 +150,31 @@ class Chef short_key_id end - # install the provided gpg key - # @param [String] uri the uri of the local or remote gpg key - def install_gpg_key(uri) - unless uri - logger.debug("'gpgkey' property not provided or set to nil. Skipping key import.") + # install the provided gpg keys + # @param [String] uris the uri of the local or remote gpg key + def install_gpg_keys(uris) + if uris.empty? + logger.debug("'gpgkey' property not provided or set. Skipping gpg key import.") return end - cached_keyfile = ::File.join(Chef::Config[:file_cache_path], uri.split("/")[-1]) + # fetch each key to the cache dir either from the cookbook or by downloading it + # and then import the key + uris.each do |uri| + cached_keyfile = ::File.join(Chef::Config[:file_cache_path], uri.split("/")[-1]) - declare_resource(key_type(new_resource.gpgkey), cached_keyfile) do - source uri - mode "0644" - sensitive new_resource.sensitive - action :create - end + declare_resource(key_type(uri), cached_keyfile) do + source uri + mode "0644" + sensitive new_resource.sensitive + action :create + end - declare_resource(:execute, "import gpg key from #{new_resource.gpgkey}") do - command "/bin/rpm --import #{cached_keyfile}" - not_if { key_installed?(cached_keyfile) } - action :run + execute "import gpg key from #{uri}" do + command "/bin/rpm --import #{cached_keyfile}" + not_if { key_installed?(cached_keyfile) } + action :run + end end end end diff --git a/lib/chef/resource/zypper_repository.rb b/lib/chef/resource/zypper_repository.rb index 89ed7f1aef..228329d2ea 100644 --- a/lib/chef/resource/zypper_repository.rb +++ b/lib/chef/resource/zypper_repository.rb @@ -24,7 +24,7 @@ class Chef unified_mode true provides(:zypper_repository) { true } - provides(:zypper_repo) { true } + provides(:zypper_repo) { true } # legacy cookbook compatibility description "Use the **zypper_repository** resource to create Zypper package repositories on SUSE Enterprise Linux and openSUSE systems. This resource maintains full compatibility with the **zypper_repository** resource in the existing **zypper** cookbook." introduced "13.3" @@ -34,11 +34,27 @@ class Chef ```ruby zypper_repository 'apache' do baseurl 'http://download.opensuse.org/repositories/Apache' - path '/openSUSE_Leap_15.0' + path '/openSUSE_Leap_15.2' type 'rpm-md' priority '100' end ``` + + **Remove the repo named 'apache'**: + + ```ruby + zypper_repository 'apache' do + action :delete + end + ``` + + **Refresh the repo named 'apache'**: + + ```ruby + zypper_repository 'apache' do + action :refresh + end + ``` DOC property :repo_name, String, @@ -66,8 +82,10 @@ class Chef description: "Determines whether or not to perform a GPG signature check on the repository.", default: true - property :gpgkey, String, - description: "The location of the repository key to be imported." + property :gpgkey, [String, Array], + description: "The location of the repository key(s) to be imported.", + coerce: proc { |v| Array(v) }, + default: [] property :baseurl, String, description: "The base URL for the Zypper repository, such as `http://download.opensuse.org`." @@ -95,10 +113,12 @@ class Chef default: true property :source, String, - description: "The name of the template for the repository file. Only necessary if you're not using the built in template." + description: "The name of the template for the repository file. Only necessary if you're using a custom template for the repository file." property :cookbook, String, - description: "The cookbook to source the repository template file from. Only necessary if you're not using the built in template.", + description: "The cookbook to source the repository template file from. Only necessary if you're using a custom template for the repository file.", + default: lazy { cookbook_name }, + default_description: "The cookbook containing the resource", desired_state: false property :gpgautoimportkeys, [TrueClass, FalseClass], diff --git a/spec/unit/provider/zypper_repository_spec.rb b/spec/unit/provider/zypper_repository_spec.rb index 950022080a..2cf4ed99c8 100644 --- a/spec/unit/provider/zypper_repository_spec.rb +++ b/spec/unit/provider/zypper_repository_spec.rb @@ -107,13 +107,6 @@ describe Chef::Provider::ZypperRepository do end end - describe "#cookbook_name" do - it "returns 'test' when the cookbook property is set" do - new_resource.cookbook("test") - expect(provider.cookbook_name).to eq("test") - end - end - describe "#key_type" do it "returns :remote_file with an http URL" do expect(provider.key_type("https://www.chef.io/key")).to eq(:remote_file) @@ -167,10 +160,10 @@ describe Chef::Provider::ZypperRepository do end end - describe "#install_gpg_key" do - it "skips installing the key if a nil value for key is passed" do + describe "#install_gpg_keys" do + it "skips installing the key if an empty array for key URL is passed" do expect(logger).to receive(:debug) - provider.install_gpg_key(nil) + provider.install_gpg_keys([]) end end end diff --git a/spec/unit/resource/zypper_repository_spec.rb b/spec/unit/resource/zypper_repository_spec.rb index b1b9cdf874..18ce0e47b3 100644 --- a/spec/unit/resource/zypper_repository_spec.rb +++ b/spec/unit/resource/zypper_repository_spec.rb @@ -85,7 +85,7 @@ describe Chef::Resource::ZypperRepository do it "accepts the legacy 'key' property" do resource.key "foo" - expect(resource.gpgkey).to eql("foo") + expect(resource.gpgkey).to eql(["foo"]) end it "accepts the legacy 'uri' property" do |