Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 列出资源的属性_Ruby_Chef Infra - Fatal编程技术网

Ruby 列出资源的属性

Ruby 列出资源的属性,ruby,chef-infra,Ruby,Chef Infra,我正在实现一个定制资源,它基本上是一个现有资源的门面(在下面的示例中是它的资源) 使用现有资源,此代码有效: certificate = vault_certificate 'a common name' do combine_certificate_and_chain true output_certificates false # Just to decrease the chef-client run output vault_path "pki/issue/#{no

我正在实现一个定制资源,它基本上是一个现有资源的门面(在下面的示例中是它的资源)

使用现有资源,此代码有效:

certificate = vault_certificate 'a common name' do
  combine_certificate_and_chain true
  output_certificates false # Just to decrease the chef-client run output
  vault_path "pki/issue/#{node['deployment']}"
end

template "a path" do
  source 'nginx/dummy.conf.erb'
  variables(
    certificate: certificate.certificate_filename
    key: certificate.key_filename
  )
end
请注意,我可以调用
certificate.certificate\u filename
certificate.key\u filename
。或者更一般地说,我可以读取vault_证书资源定义的任何属性

现在有了新的资源(有点像是vault_证书的门面)

如果我现在使用此资源并尝试调用
.certificate\u filename
.key\u filename

certificate = vault_certificate_handle_exceptions 'a common name' do
  action :create
end
template "a path" do
  source 'nginx/dummy.conf.erb'
  variables(
    certificate: certificate.certificate_filename
    key: certificate.key_filename
  )
end
我收到一个错误消息,表示未为
保险库\u证书\u句柄\u异常定义方法
证书\u文件名
(或
密钥\u文件名
)。为了解决这个问题,我采用了以下方法:

provides :vault_certificate_handle_exceptions

unified_mode true

property :common_name, String, name_property: true
property :max_retries, Integer, default: 5

action :create do
  require 'retries'
  # new_resource.max_retries is being used inside the retry_options. I omitted that part as its not relevant for the question
  with_retries(retry_options) do
    begin
      cert = vault_certificate new_resource.common_name do
        combine_certificate_and_chain true
        output_certificates false # Just to decrease the chef-client run output
        vault_path "pki/issue/#{node['deployment']}"
        ignore_failure :quiet
      end
      # These lines ensure we can read the vault_certificate properties as if they were properties of this resource (vault_certificate_handle_exceptions)
      Chef::ResourceResolver.resolve(cert.resource_name).properties.keys.each do |name|
        new_resource.send(:define_singleton_method, name.to_sym) do
          cert.send(name.to_sym)
        end
      end
    rescue Vault::HTTPClientError => e
      data = JSON.parse(e.errors)['data']
      if data['error'] == 'Certificate not found locally'
        # This error is one we can recover from (actually we are expecting it). This raise with VaultCertificateError will trigger the with_retries.
        raise VaultCertificateError.new("Waiting for the certificate to appear in the store (because I'm not the leader)", data)
      else
        # Any other error means something really went wrong.
        raise e
      end
    end
  end
end
有没有更干净的方法来实现这一点?如果没有,是否有更直接的方法列出资源的所有属性?我原以为
cert.properties
会管用,但运气不好

provides :vault_certificate_handle_exceptions

unified_mode true

property :common_name, String, name_property: true
property :max_retries, Integer, default: 5

action :create do
  require 'retries'
  # new_resource.max_retries is being used inside the retry_options. I omitted that part as its not relevant for the question
  with_retries(retry_options) do
    begin
      cert = vault_certificate new_resource.common_name do
        combine_certificate_and_chain true
        output_certificates false # Just to decrease the chef-client run output
        vault_path "pki/issue/#{node['deployment']}"
        ignore_failure :quiet
      end
      # These lines ensure we can read the vault_certificate properties as if they were properties of this resource (vault_certificate_handle_exceptions)
      Chef::ResourceResolver.resolve(cert.resource_name).properties.keys.each do |name|
        new_resource.send(:define_singleton_method, name.to_sym) do
          cert.send(name.to_sym)
        end
      end
    rescue Vault::HTTPClientError => e
      data = JSON.parse(e.errors)['data']
      if data['error'] == 'Certificate not found locally'
        # This error is one we can recover from (actually we are expecting it). This raise with VaultCertificateError will trigger the with_retries.
        raise VaultCertificateError.new("Waiting for the certificate to appear in the store (because I'm not the leader)", data)
      else
        # Any other error means something really went wrong.
        raise e
      end
    end
  end
end