Class: Bosh::Cli::JobPropertyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/job_property_validator.rb

Defined Under Namespace

Classes: TemplateError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(built_jobs, manifest) ⇒ JobPropertyValidator

Returns a new instance of JobPropertyValidator.

Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cli/job_property_validator.rb', line 12

def initialize(built_jobs, manifest)
  @built_jobs = {}
  @manifest = manifest

  @jobs_without_properties = []

  built_jobs.each do |job|
    @built_jobs[job.name] = job
    if job.properties.empty?
      @jobs_without_properties << job
    end
  end

  unless @manifest["properties"].is_a?(Hash)
    bad_manifest("Invalid properties format in deployment " +
      "manifest, Hash expected, #{@manifest["properties"].class} given")
  end

  unless @manifest["jobs"].is_a?(Array)
    bad_manifest("Invalid instance groups format in deployment " +
      "manifest, Array expected, #{@manifest["jobs"].class} given")
  end

  @manifest["jobs"].each do |job|
    unless job.is_a?(Hash)
      bad_manifest("Invalid instance group spec in the manifest " +
                   "Hash expected, #{job.class} given")
    end

    job_name = job["name"]
    if job_name.nil?
      bad_manifest("Manifest contains at least one instance group without name")
    end

    if job["template"].nil?
      bad_manifest("Instance group '#{job_name}' doesn't have a job")
    end
  end

  @template_errors = []
end

Instance Attribute Details

#jobs_without_propertiesObject (readonly)

Returns the value of attribute jobs_without_properties.



8
9
10
# File 'lib/cli/job_property_validator.rb', line 8

def jobs_without_properties
  @jobs_without_properties
end

#template_errorsObject (readonly)

Returns the value of attribute template_errors.



7
8
9
# File 'lib/cli/job_property_validator.rb', line 7

def template_errors
  @template_errors
end

Instance Method Details

#job_network_spec(job_spec) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/cli/job_property_validator.rb', line 94

def job_network_spec(job_spec)
  job_spec['networks'].reduce({}) do |networks, network|
    networks[network['name']] = {
        'ip' => '127.0.0.1', # faking the IP since it shouldn't affect logic
        'netmask' => '255.255.255.0',
        'gateway' => '127.0.0.2'
    }
    networks
  end
end

#validateObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/cli/job_property_validator.rb', line 54

def validate
  @manifest["jobs"].each do |job_spec|
    job_templates = Array(job_spec['template'])
    job_templates.each do |job_template|
      job_spec_for_template = job_spec.dup
      job_spec_for_template['template'] = job_template
      validate_templates(job_spec_for_template)
    end
  end
end

#validate_templates(job_spec) ⇒ Object

Tries to fill in each job template with job properties, collects errors

Parameters:

  • job_spec (Hash)

    Job spec from the manifest



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cli/job_property_validator.rb', line 67

def validate_templates(job_spec)
  built_job = @built_jobs[job_spec["template"]]

  if built_job.nil?
    raise CliError, "Instance group '#{job_spec["template"]}' has not been built"
  end

  collection = JobPropertyCollection.new(
    built_job, @manifest["properties"],
    job_spec["properties"], job_spec["property_mappings"])

  # Spec is usually more than that but jobs rarely use anything but
  # networks and properties.
  spec = {
    'job' => {
        'name' => job_spec['name']
    },
    'index' => 0,
    'networks' => job_network_spec(job_spec),
    'properties' => collection.to_hash
  }

  built_job.files.each do |file_tuple|
    evaluate_template(built_job, file_tuple.first, spec)
  end
end