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:

  • built_jobs (Array<JobBuilder>)

    Built job templates

  • manifest (Hash)

    Deployment manifest



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
53
# 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 jobs format in deployment " +
      "manifest, Array expected, #{@manifest["jobs"].class} given")
  end

  @manifest["jobs"].each do |job|
    unless job.is_a?(Hash)
      bad_manifest("Invalid job 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 job without name")
    end

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

  @template_errors = []
  # TODO: track missing props and show the list to user (super helpful!)
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)

TODO: tests



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

def template_errors
  @template_errors
end

Instance Method Details

#validateObject



55
56
57
58
59
# File 'lib/cli/job_property_validator.rb', line 55

def validate
  @manifest["jobs"].each do |job_spec|
    validate_templates(job_spec)
  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



63
64
65
66
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 63

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

  if built_job.nil?
    raise CliError, "Job `#{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.
  # TODO: provide all keys in the spec?
  spec = {
    "job" => {
      "name" => job_spec["name"]
    },
    "networks" => {
      "default" => {"ip" => "10.0.0.1"}
    },
    "properties" => collection.to_hash,
    "index" => 0
  }

  built_job.all_templates.each do |template_path|
    # TODO: add progress bar?
    evaluate_template(built_job, template_path, spec)
  end
end