bosh-template 2.0.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +10 -8
- data/lib/bosh/template/evaluation_context.rb +59 -33
- data/lib/bosh/template/evaluation_link.rb +23 -8
- data/lib/bosh/template/evaluation_link_instance.rb +7 -5
- data/lib/bosh/template/manual_link_dns_encoder.rb +1 -1
- data/lib/bosh/template/renderer.rb +1 -1
- data/lib/bosh/template/test/job.rb +3 -1
- data/lib/bosh/template/test/link.rb +4 -2
- data/lib/bosh/template/test/template.rb +2 -2
- data/lib/bosh/template/version.rb +1 -1
- metadata +6 -7
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 | 
            -
             | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 01d5f8a545ae5989e1e3aaf8f44100cb936157c1aab72a852b71d2033ab034ad
         | 
| 4 | 
            +
              data.tar.gz: 88ced16b9df4bdadd6e355778ef69128911d9a63c50bf67a2c7abe4ccc3de93e
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 924d88d24c9ddf8966abed228557e520835872f2064eb3ce15be7445624a626a09593e97fb45969a94f7e13a08a6d448add538dcdaa889d7f373b6152f05df3c
         | 
| 7 | 
            +
              data.tar.gz: 92b8857af18937f7c93928c0a70a5c010603800d3cd4d463f8b363b6de1f9b08215cc55e352fc5e711b32af75aa4033375158c319923b94905c8ae9a7f8afc70
         | 
    
        data/README.md
    CHANGED
    
    | @@ -21,19 +21,21 @@ When you create your own release, you can likewise create a tests folder in your | |
| 21 21 | 
             
            ```ruby
         | 
| 22 22 | 
             
            let(:job) {release.job('JOB-NAME')}  # e.g. 'web-server;
         | 
| 23 23 | 
             
            let(:template) {job.template('PATH-TO-TEMPLATE')}  # e.g. 'config/config-with-nested'
         | 
| 24 | 
            -
            let(:manifest)  | 
| 25 | 
            -
               | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 24 | 
            +
            let(:manifest) do
         | 
| 25 | 
            +
              {
         | 
| 26 | 
            +
                'cert' => '----- BEGIN ... -----',
         | 
| 27 | 
            +
                'port' => 42,
         | 
| 28 | 
            +
              }
         | 
| 29 | 
            +
            end
         | 
| 28 30 | 
             
            let(:instance) { InstanceSpec.new(name:'instance-name', az: 'az1', bootstrap: true) }
         | 
| 29 31 | 
             
            let(:link_instance) { InstanceSpec.new(name:'link-instance-name', az: 'az2') }
         | 
| 30 | 
            -
            let(:link_properties) | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 32 | 
            +
            let(:link_properties) do
         | 
| 33 | 
            +
              { 'link-key' => 'link-value' }
         | 
| 34 | 
            +
            end
         | 
| 33 35 | 
             
            let(:link) { Link.new(name:'link-name', instances:[link_instance], properties: link_properties)}
         | 
| 34 36 |  | 
| 35 37 |  | 
| 36 38 | 
             
            rendered_template = JSON.parse(template.render(manifest, spec: instance, consumes: [link]))
         | 
| 37 39 | 
             
            ```
         | 
| 38 40 |  | 
| 39 | 
            -
            And then check that their template rendered as they expected.
         | 
| 41 | 
            +
            And then check that their template rendered as they expected.
         | 
| @@ -51,6 +51,13 @@ module Bosh | |
| 51 51 | 
             
                    @links = spec['links'] || {}
         | 
| 52 52 | 
             
                  end
         | 
| 53 53 |  | 
| 54 | 
            +
                  def ==(other)
         | 
| 55 | 
            +
                    public_members = %w[spec raw_properties name index properties]
         | 
| 56 | 
            +
                    public_members.all? do |member|
         | 
| 57 | 
            +
                      other.respond_to?(member) && send(member) == other.send(member)
         | 
| 58 | 
            +
                    end
         | 
| 59 | 
            +
                  end
         | 
| 60 | 
            +
             | 
| 54 61 | 
             
                  # @return [Binding] Template binding
         | 
| 55 62 | 
             
                  def get_binding
         | 
| 56 63 | 
             
                    binding.taint
         | 
| @@ -95,34 +102,6 @@ module Bosh | |
| 95 102 | 
             
                    raise UnknownProperty.new(names)
         | 
| 96 103 | 
             
                  end
         | 
| 97 104 |  | 
| 98 | 
            -
                  def link(name)
         | 
| 99 | 
            -
                    link_spec = lookup_property(@links, name)
         | 
| 100 | 
            -
                    raise UnknownLink.new(name) if link_spec.nil?
         | 
| 101 | 
            -
             | 
| 102 | 
            -
                    if link_spec.has_key?('instances')
         | 
| 103 | 
            -
                      link_instances = link_spec['instances'].map do |instance_link_spec|
         | 
| 104 | 
            -
                        EvaluationLinkInstance.new(instance_link_spec['name'], instance_link_spec['index'], instance_link_spec['id'], instance_link_spec['az'], instance_link_spec['address'], instance_link_spec['properties'], instance_link_spec['bootstrap'])
         | 
| 105 | 
            -
                      end
         | 
| 106 | 
            -
             | 
| 107 | 
            -
                      if link_spec.has_key?('address')
         | 
| 108 | 
            -
                        encoder_to_inject = ManualLinkDnsEncoder.new(link_spec['address'])
         | 
| 109 | 
            -
                      else
         | 
| 110 | 
            -
                        encoder_to_inject = @dns_encoder
         | 
| 111 | 
            -
                      end
         | 
| 112 | 
            -
             | 
| 113 | 
            -
                      return EvaluationLink.new(
         | 
| 114 | 
            -
                        link_instances,
         | 
| 115 | 
            -
                        link_spec['properties'],
         | 
| 116 | 
            -
                        link_spec['instance_group'],
         | 
| 117 | 
            -
                        link_spec['default_network'],
         | 
| 118 | 
            -
                        link_spec['deployment_name'],
         | 
| 119 | 
            -
                        link_spec['domain'],
         | 
| 120 | 
            -
                        encoder_to_inject,
         | 
| 121 | 
            -
                      )
         | 
| 122 | 
            -
                    end
         | 
| 123 | 
            -
                    raise UnknownLink.new(name)
         | 
| 124 | 
            -
                  end
         | 
| 125 | 
            -
             | 
| 126 105 | 
             
                  # Run a block of code if all given properties are defined
         | 
| 127 106 | 
             
                  # @param [Array<String>] names Property names
         | 
| 128 107 | 
             
                  # @yield [Object] property values
         | 
| @@ -137,6 +116,17 @@ module Bosh | |
| 137 116 | 
             
                    InactiveElseBlock.new
         | 
| 138 117 | 
             
                  end
         | 
| 139 118 |  | 
| 119 | 
            +
                  def link(name)
         | 
| 120 | 
            +
                    link_spec = lookup_property(@links, name)
         | 
| 121 | 
            +
                    raise UnknownLink.new(name) if link_spec.nil?
         | 
| 122 | 
            +
             | 
| 123 | 
            +
                    if link_spec.has_key?('instances')
         | 
| 124 | 
            +
                      return create_evaluation_link(link_spec)
         | 
| 125 | 
            +
                    end
         | 
| 126 | 
            +
             | 
| 127 | 
            +
                    raise UnknownLink.new(name)
         | 
| 128 | 
            +
                  end
         | 
| 129 | 
            +
             | 
| 140 130 | 
             
                  # Run a block of code if the link given exists
         | 
| 141 131 | 
             
                  # @param [String] name of the link
         | 
| 142 132 | 
             
                  # @yield [Object] link, which is an array of instances
         | 
| @@ -145,17 +135,53 @@ module Bosh | |
| 145 135 | 
             
                    if link_spec.nil? || !link_spec.has_key?('instances')
         | 
| 146 136 | 
             
                      return ActiveElseBlock.new(self)
         | 
| 147 137 | 
             
                    else
         | 
| 148 | 
            -
                       | 
| 149 | 
            -
                        EvaluationLinkInstance.new(instance_link_spec['name'], instance_link_spec['index'], instance_link_spec['id'], instance_link_spec['az'], instance_link_spec['address'], instance_link_spec['properties'], instance_link_spec['bootstrap'])
         | 
| 150 | 
            -
                      end
         | 
| 151 | 
            -
             | 
| 152 | 
            -
                      yield EvaluationLink.new(link_instances, link_spec['properties'], link_spec['instance_group'], link_spec['default_network'], link_spec['deployment_name'], link_spec['root_domain'], @dns_encoder)
         | 
| 138 | 
            +
                      yield create_evaluation_link(link_spec)
         | 
| 153 139 | 
             
                      InactiveElseBlock.new
         | 
| 154 140 | 
             
                    end
         | 
| 155 141 | 
             
                  end
         | 
| 156 142 |  | 
| 157 143 | 
             
                  private
         | 
| 158 144 |  | 
| 145 | 
            +
                  def create_evaluation_link(link_spec)
         | 
| 146 | 
            +
                    link_instances = link_spec['instances'].map do |instance_link_spec|
         | 
| 147 | 
            +
                      EvaluationLinkInstance.new(
         | 
| 148 | 
            +
                        instance_link_spec['name'],
         | 
| 149 | 
            +
                        instance_link_spec['index'],
         | 
| 150 | 
            +
                        instance_link_spec['id'],
         | 
| 151 | 
            +
                        instance_link_spec['az'],
         | 
| 152 | 
            +
                        instance_link_spec['address'],
         | 
| 153 | 
            +
                        instance_link_spec['properties'],
         | 
| 154 | 
            +
                        instance_link_spec['bootstrap'],
         | 
| 155 | 
            +
                      )
         | 
| 156 | 
            +
                    end
         | 
| 157 | 
            +
             | 
| 158 | 
            +
                    if link_spec.has_key?('address')
         | 
| 159 | 
            +
                      encoder_to_inject = ManualLinkDnsEncoder.new(link_spec['address'])
         | 
| 160 | 
            +
                    else
         | 
| 161 | 
            +
                      encoder_to_inject = @dns_encoder
         | 
| 162 | 
            +
                    end
         | 
| 163 | 
            +
             | 
| 164 | 
            +
                    group_name = link_spec['instance_group']
         | 
| 165 | 
            +
                    group_type = 'instance-group'
         | 
| 166 | 
            +
             | 
| 167 | 
            +
                    if link_spec.fetch('use_link_dns_names', false)
         | 
| 168 | 
            +
                      group_name = link_spec['group_name']
         | 
| 169 | 
            +
                      group_type = 'link'
         | 
| 170 | 
            +
                    end
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                    EvaluationLink.new(
         | 
| 173 | 
            +
                      link_instances,
         | 
| 174 | 
            +
                      link_spec['properties'],
         | 
| 175 | 
            +
                      group_name,
         | 
| 176 | 
            +
                      group_type,
         | 
| 177 | 
            +
                      link_spec['default_network'],
         | 
| 178 | 
            +
                      link_spec['deployment_name'],
         | 
| 179 | 
            +
                      link_spec['domain'],
         | 
| 180 | 
            +
                      encoder_to_inject,
         | 
| 181 | 
            +
                      link_spec.fetch('use_short_dns_addresses', false),
         | 
| 182 | 
            +
                    )
         | 
| 183 | 
            +
                  end
         | 
| 184 | 
            +
             | 
| 159 185 | 
             
                  # @return [Object] Object representation where all hashes are unrolled
         | 
| 160 186 | 
             
                  #   into OpenStruct objects. This exists mostly for backward
         | 
| 161 187 | 
             
                  #   compatibility, as it doesn't provide good error reporting.
         | 
| @@ -8,14 +8,26 @@ module Bosh | |
| 8 8 | 
             
                  attr_reader :instances
         | 
| 9 9 | 
             
                  attr_reader :properties
         | 
| 10 10 |  | 
| 11 | 
            -
                  def initialize( | 
| 11 | 
            +
                  def initialize(
         | 
| 12 | 
            +
                    instances,
         | 
| 13 | 
            +
                    properties,
         | 
| 14 | 
            +
                    group_name,
         | 
| 15 | 
            +
                    group_type,
         | 
| 16 | 
            +
                    default_network,
         | 
| 17 | 
            +
                    deployment_name,
         | 
| 18 | 
            +
                    root_domain,
         | 
| 19 | 
            +
                    dns_encoder,
         | 
| 20 | 
            +
                    use_short_dns
         | 
| 21 | 
            +
                  )
         | 
| 12 22 | 
             
                    @instances = instances
         | 
| 13 23 | 
             
                    @properties = properties
         | 
| 14 | 
            -
                    @ | 
| 24 | 
            +
                    @group_name = group_name
         | 
| 25 | 
            +
                    @group_type = group_type
         | 
| 15 26 | 
             
                    @default_network = default_network
         | 
| 16 27 | 
             
                    @deployment_name = deployment_name
         | 
| 17 28 | 
             
                    @root_domain = root_domain
         | 
| 18 29 | 
             
                    @dns_encoder = dns_encoder
         | 
| 30 | 
            +
                    @use_short_dns = use_short_dns
         | 
| 19 31 | 
             
                  end
         | 
| 20 32 |  | 
| 21 33 | 
             
                  def p(*args)
         | 
| @@ -27,31 +39,34 @@ module Bosh | |
| 27 39 | 
             
                    end
         | 
| 28 40 |  | 
| 29 41 | 
             
                    return args[1] if args.length == 2
         | 
| 30 | 
            -
             | 
| 42 | 
            +
             | 
| 43 | 
            +
                    raise UnknownProperty, names
         | 
| 31 44 | 
             
                  end
         | 
| 32 45 |  | 
| 33 46 | 
             
                  def if_p(*names)
         | 
| 34 47 | 
             
                    values = names.map do |name|
         | 
| 35 48 | 
             
                      value = lookup_property(@properties, name)
         | 
| 36 49 | 
             
                      return Bosh::Template::EvaluationContext::ActiveElseBlock.new(self) if value.nil?
         | 
| 50 | 
            +
             | 
| 37 51 | 
             
                      value
         | 
| 38 52 | 
             
                    end
         | 
| 39 53 |  | 
| 40 | 
            -
                    yield | 
| 54 | 
            +
                    yield(*values)
         | 
| 55 | 
            +
             | 
| 41 56 | 
             
                    Bosh::Template::EvaluationContext::InactiveElseBlock.new
         | 
| 42 57 | 
             
                  end
         | 
| 43 58 |  | 
| 44 59 | 
             
                  def address(criteria = {})
         | 
| 45 | 
            -
                    raise NotImplementedError | 
| 60 | 
            +
                    raise NotImplementedError, 'link.address requires bosh director' if @dns_encoder.nil?
         | 
| 46 61 |  | 
| 47 62 | 
             
                    full_criteria = criteria.merge(
         | 
| 48 | 
            -
                       | 
| 63 | 
            +
                      group_name: @group_name,
         | 
| 64 | 
            +
                      group_type: @group_type,
         | 
| 49 65 | 
             
                      default_network: @default_network,
         | 
| 50 66 | 
             
                      deployment_name: @deployment_name,
         | 
| 51 67 | 
             
                      root_domain: @root_domain,
         | 
| 52 68 | 
             
                    )
         | 
| 53 | 
            -
             | 
| 54 | 
            -
                    @dns_encoder.encode_query(full_criteria)
         | 
| 69 | 
            +
                    @dns_encoder.encode_query(full_criteria, @use_short_dns)
         | 
| 55 70 | 
             
                  end
         | 
| 56 71 | 
             
                end
         | 
| 57 72 | 
             
              end
         | 
| @@ -32,19 +32,21 @@ module Bosh | |
| 32 32 | 
             
                    end
         | 
| 33 33 |  | 
| 34 34 | 
             
                    return args[1] if args.length == 2
         | 
| 35 | 
            -
             | 
| 35 | 
            +
             | 
| 36 | 
            +
                    raise UnknownProperty, names
         | 
| 36 37 | 
             
                  end
         | 
| 37 38 |  | 
| 38 39 | 
             
                  def if_p(*names)
         | 
| 39 40 | 
             
                    values = names.map do |name|
         | 
| 40 41 | 
             
                      value = lookup_property(@properties, name)
         | 
| 41 | 
            -
                      return ActiveElseBlock.new(self) if value.nil?
         | 
| 42 | 
            +
                      return Bosh::Template::EvaluationContext::ActiveElseBlock.new(self) if value.nil?
         | 
| 43 | 
            +
             | 
| 42 44 | 
             
                      value
         | 
| 43 45 | 
             
                    end
         | 
| 44 46 |  | 
| 45 | 
            -
                    yield | 
| 46 | 
            -
                    InactiveElseBlock.new
         | 
| 47 | 
            +
                    yield(*values)
         | 
| 48 | 
            +
                    Bosh::Template::EvaluationContext::InactiveElseBlock.new
         | 
| 47 49 | 
             
                  end
         | 
| 48 50 | 
             
                end
         | 
| 49 51 | 
             
              end
         | 
| 50 | 
            -
            end
         | 
| 52 | 
            +
            end
         | 
| @@ -12,7 +12,7 @@ module Bosh | |
| 12 12 | 
             
                  def render(template_name)
         | 
| 13 13 | 
             
                    spec = JSON.parse(@context)
         | 
| 14 14 | 
             
                    evaluation_context = EvaluationContext.new(spec, nil)
         | 
| 15 | 
            -
                    template = ERB.new(File.read(template_name),  | 
| 15 | 
            +
                    template = ERB.new(File.read(template_name), trim_mode: "-")
         | 
| 16 16 | 
             
                    template.result(evaluation_context.get_binding)
         | 
| 17 17 | 
             
                  end
         | 
| 18 18 | 
             
                end
         | 
| @@ -1,3 +1,5 @@ | |
| 1 | 
            +
            require 'yaml'
         | 
| 2 | 
            +
             | 
| 1 3 | 
             
            module Bosh::Template::Test
         | 
| 2 4 | 
             
              class Job
         | 
| 3 5 | 
             
                def initialize(release_path, name)
         | 
| @@ -17,4 +19,4 @@ module Bosh::Template::Test | |
| 17 19 | 
             
                  raise "Template for rendered path filename not found: #{rendered_file_name}. Possible values are: [#{@templates.values.join(', ')}]"
         | 
| 18 20 | 
             
                end
         | 
| 19 21 | 
             
              end
         | 
| 20 | 
            -
            end
         | 
| 22 | 
            +
            end
         | 
| @@ -1,11 +1,12 @@ | |
| 1 1 | 
             
            module Bosh::Template::Test
         | 
| 2 2 | 
             
              class Link
         | 
| 3 | 
            -
                attr_reader :instances, :name, :properties
         | 
| 3 | 
            +
                attr_reader :instances, :name, :properties, :address
         | 
| 4 4 |  | 
| 5 | 
            -
                def initialize(name:, instances: [], properties: {})
         | 
| 5 | 
            +
                def initialize(name:, instances: [], properties: {}, address: nil)
         | 
| 6 6 | 
             
                  @instances = instances
         | 
| 7 7 | 
             
                  @name = name
         | 
| 8 8 | 
             
                  @properties = properties
         | 
| 9 | 
            +
                  @address = address
         | 
| 9 10 | 
             
                end
         | 
| 10 11 |  | 
| 11 12 | 
             
                def to_h
         | 
| @@ -13,6 +14,7 @@ module Bosh::Template::Test | |
| 13 14 | 
             
                    'instances' => instances.map(&:to_h),
         | 
| 14 15 | 
             
                    'name' => name,
         | 
| 15 16 | 
             
                    'properties' => properties,
         | 
| 17 | 
            +
                    'address' => address,
         | 
| 16 18 | 
             
                  }
         | 
| 17 19 | 
             
                end
         | 
| 18 20 | 
             
              end
         | 
| @@ -19,7 +19,7 @@ module Bosh::Template | |
| 19 19 |  | 
| 20 20 | 
             
                    binding = Bosh::Template::EvaluationContext.new(sanitized_hash_with_spec, nil).get_binding
         | 
| 21 21 | 
             
                    raise "No such file at #{@template_path}" unless File.exist?(@template_path)
         | 
| 22 | 
            -
                    ERB.new(File.read(@template_path)).result(binding)
         | 
| 22 | 
            +
                    ERB.new(File.read(@template_path), trim_mode: '-').result(binding)
         | 
| 23 23 | 
             
                  end
         | 
| 24 24 |  | 
| 25 25 | 
             
                  private
         | 
| @@ -74,4 +74,4 @@ module Bosh::Template | |
| 74 74 | 
             
                  end
         | 
| 75 75 | 
             
                end
         | 
| 76 76 | 
             
              end
         | 
| 77 | 
            -
            end
         | 
| 77 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: bosh-template
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 2. | 
| 4 | 
            +
              version: 2.3.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Pivotal
         | 
| 8 | 
            -
            autorequire: | 
| 8 | 
            +
            autorequire:
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date:  | 
| 11 | 
            +
            date: 1980-01-01 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: semi_semantic
         | 
| @@ -56,7 +56,7 @@ homepage: https://github.com/cloudfoundry/bosh | |
| 56 56 | 
             
            licenses:
         | 
| 57 57 | 
             
            - Apache-2.0
         | 
| 58 58 | 
             
            metadata: {}
         | 
| 59 | 
            -
            post_install_message: | 
| 59 | 
            +
            post_install_message:
         | 
| 60 60 | 
             
            rdoc_options: []
         | 
| 61 61 | 
             
            require_paths:
         | 
| 62 62 | 
             
            - lib
         | 
| @@ -71,9 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 71 71 | 
             
                - !ruby/object:Gem::Version
         | 
| 72 72 | 
             
                  version: '0'
         | 
| 73 73 | 
             
            requirements: []
         | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
            signing_key: 
         | 
| 74 | 
            +
            rubygems_version: 3.2.26
         | 
| 75 | 
            +
            signing_key:
         | 
| 77 76 | 
             
            specification_version: 4
         | 
| 78 77 | 
             
            summary: Renders bosh templates
         | 
| 79 78 | 
             
            test_files: []
         |