Skip to content

Instantly share code, notes, and snippets.

@jcpowermac
Created October 13, 2014 13:59
Show Gist options
  • Select an option

  • Save jcpowermac/775c06b6a19f93df09e9 to your computer and use it in GitHub Desktop.

Select an option

Save jcpowermac/775c06b6a19f93df09e9 to your computer and use it in GitHub Desktop.
diff --git a/core/model/base.rb b/core/model/base.rb
index 09bf228..a5a25df 100644
--- a/core/model/base.rb
+++ b/core/model/base.rb
@@ -21,6 +21,7 @@ module ProjectHanlon
attr_accessor :counter
attr_accessor :log
attr_accessor :req_metadata_hash
+attr_accessor :opt_metadata_hash
# init
# @param hash [Hash]
@@ -32,6 +33,7 @@ module ProjectHanlon
@noun = "model"
@description = "Base model template"
@req_metadata_hash = {}
+@opt_metadata_hash = {}
@callback = {}
@current_state = :init
@node = nil
@@ -227,6 +229,7 @@ module ProjectHanlon
end
def web_create_metadata(provided_metadata)
+puts "DEBUG: web_create_metadata"
missing_metadata = []
rmd = req_metadata_hash
rmd.each_key do
@@ -249,9 +252,49 @@ module ProjectHanlon
end
end
- def cli_create_metadata
+def yaml_read_metadata(yaml_metadata_hash)
+
+req_meta_vals = yaml_metadata_hash.select{ |key| req_metadata_hash.keys.include?(key) }
+
+req_meta_vals.each{ |key,value|
+ metadata = map_keys_to_symbols(req_metadata_hash[key])
+ key = key.to_sym if !key.is_a?(Symbol)
+#puts ">>> key = #{key}, value = #{value}"
+#puts ">>> validation = #{metadata[:validation]}"
+
+flag = set_metadata_value(key, value, metadata[:validation])
+if !flag
+ raise ProjectHanlon::Error::Slice::InvalidModelMetadata, "Invalid Metadata [#{key}:#{value}]"
+end
+}
+
+#puts ">>> req_meta_vals #{req_meta_vals.inspect}"
+
+optional_vals = yaml_metadata_hash.reject{ |key| req_metadata_hash.keys.include?(key) }
+
+#puts "#{optional_vals.inspect}"
+
+optional_vals.each{ |key,value|
+#puts "opt_metadata_hash[#{key}] == #{opt_metadata_hash[key]}"
+ metadata = map_keys_to_symbols( opt_metadata_hash[key] )
+ key = key.to_sym if !key.is_a?(Symbol)
+ set_metadata_value(key, value, metadata[:validation])
+}
+
+#puts " >>> optional_vals #{optional_vals.inspect}"
+
+(req_metadata_hash.keys - yaml_metadata_hash.keys)
+
+end
+
+ def cli_create_metadata(yaml_metadata_hash)
puts "--- Building Model (#{name}): #{label}\n".yellow
- req_metadata_hash.each_key { |key|
+remaining_keys = yaml_read_metadata(yaml_metadata_hash)
+#puts ">>> result_metadata_hash #{remaining_keys.inspect}"
+
+# req_metadata_hash.each_key { |key|
+remaining_keys.each { |key|
+
metadata = map_keys_to_symbols(req_metadata_hash[key])
key = key.to_sym if !key.is_a?(Symbol)
flag = false
@@ -296,15 +339,21 @@ module ProjectHanlon
end
def set_metadata_value(key, value, validation)
- regex = Regexp.new(validation)
- if regex =~ value
- self.instance_variable_set(key.to_sym, value)
- true
- else
- false
- end
+puts "DEBUG(#{self.name}): self.instance_variable_set(#{key.to_sym}, #{value})"
+ if value.is_a?(Array)
+ self.instance_variable_set(key.to_sym, value)
+ true
+ else
+ regex = Regexp.new(validation)
+ if regex =~ value
+ self.instance_variable_set(key.to_sym, value)
+ true
+ else
+ false
+ end
+ end
end
-
+
def skip_quit_option
"(" + "SKIP".white + " to skip, " + "QUIT".red + " to cancel)"
end
diff --git a/core/model/vmware_esxi.rb b/core/model/vmware_esxi.rb
index d99fe8d..f16042c 100644
--- a/core/model/vmware_esxi.rb
+++ b/core/model/vmware_esxi.rb
@@ -47,6 +47,8 @@ module ProjectHanlon
@vcenter_name = nil
@vcenter_datacenter_path = nil
@vcenter_cluster_path = nil
+
+ @packages = nil
# Metadata
@req_metadata_hash = {
"@esx_license" => { :default => "",
@@ -99,7 +101,9 @@ module ProjectHanlon
:example => "ntp.hanlon.example.local",
:validation => '^[\w.]{3,}$',
:required => true,
- :description => "NTP server for node" },
+ :description => "NTP server for node" }
+ }
+@opt_metadata_hash = {
"@vcenter_name" => { :default => "",
:example => "vcenter01",
:validation => '^[\w.-]{3,}$',
@@ -114,7 +118,13 @@ module ProjectHanlon
:example => "Cluster01",
:validation => '^[a-zA-Z\d-]{3,}$',
:required => false,
- :description => "Optional for broker use: the vCenter Cluster to place ESXi node in" }
+ :description => "Optional for broker use: the vCenter Cluster to place ESXi node in" },
+"@packages" => { :default => "",
+ :example => "",
+ :validation => '',
+ :required => false,
+ :description => "Optional for broker use: the vCenter Cluster to place ESXi node in" }
+
}
diff --git a/core/slice/model.rb b/core/slice/model.rb
index daf30f5..42cd8b4 100644
--- a/core/slice/model.rb
+++ b/core/slice/model.rb
@@ -65,7 +65,16 @@ module ProjectHanlon
:description => 'The image UUID to use for the new model.',
:uuid_is => 'not_allowed',
:required => true
+ },
+ { :name => :optional_yaml,
+ :default => false,
+ :short_form => '-o',
+ :long_form => '--option YAML_FILE',
+ :description => 'Use optional yaml file to create model',
+ :uuid_is => 'not_allowed',
+ :required => false
}
+
],
:update => [
{ :name => :label,
@@ -161,6 +170,7 @@ module ProjectHanlon
# load the appropriate option items for the subcommand we are handling
option_items = command_option_data(:add)
command_hash = Hash[*@command_array]
+#puts ">>> command_hash - #{command_hash.inspect}"
template_name = command_hash["-t"] || command_hash["--template"]
option_items = option_items.map { |option|
option[:name] == :image_uuid ? (option[:required] = false; option) : option
@@ -169,18 +179,42 @@ module ProjectHanlon
# subcommand (this method will return a UUID value, if present, and the
# options map constructed from the @commmand_array)
tmp, options = parse_and_validate_options(option_items, "hanlon model add (options...)", :require_all)
+#puts ">>> tmp - #{tmp.inspect}"
+#puts ">>> options - #{options.inspect}"
+
includes_uuid = true if tmp && tmp != "add"
# check for usage errors (the boolean value at the end of this method
# call is used to indicate whether the choice of options from the
# option_items hash must be an exclusive choice)
check_option_usage(option_items, options, includes_uuid, false)
+
+optional_yaml_file = options[:optional_yaml]
+#puts ">>> options_yaml_file #{optional_yaml_file}"
+
template = options[:template]
label = options[:label]
image_uuid = options[:image_uuid]
# use the arguments passed in to create a new model
model = get_model_using_template_name(options[:template])
raise ProjectHanlon::Error::Slice::InputError, "Invalid model template [#{options[:template]}] " unless model
- model.cli_create_metadata
+if optional_yaml_file
+metadata_hash = YAML.load(File.read(optional_yaml_file))
+#puts ">>> metadata_hash = #{metadata_hash.inspect}"
+
+
+# we need to modify the hash map from yaml to include the @ symbol to match req_metadata_hash keys
+
+metadata_hash.keys.each{|k| metadata_hash.store("@#{k}", metadata_hash.delete(k))}
+
+
+model.cli_create_metadata(metadata_hash)
+#metadata_hash.each { |key,value|
+#puts ">>> key = #{key}"
+#puts ">>> val = #{value}"
+#}
+else
+ model.cli_create_metadata({})
+end
# setup the POST (to create the requested policy) and return the results
uri = URI.parse @uri_string
body_hash = {
@@ -190,9 +224,20 @@ module ProjectHanlon
"req_metadata_hash" => model.req_metadata_hash
}
model.req_metadata_hash.each { |key, md_hash_value|
+puts "DEBUG(): req_metadata_has key == #{key}"
value = model.instance_variable_get(key)
body_hash[key] = value
}
+
+model.opt_metadata_hash.each { |key, md_hash_value|
+ value = model.instance_variable_get(key)
+
+puts "DEBUG(): opt_metadata_hash key == #{key}, value == #{value}"
+#puts "DEBUG: opt_metadata_hash key == #{key}"
+ body_hash[key] = value
+
+}
+
json_data = body_hash.to_json
result, response = hnl_http_post_json_data(uri, json_data, true)
if response.instance_of?(Net::HTTPBadRequest)
diff --git a/web/api/api_model_v1.rb b/web/api/api_model_v1.rb
index b9fa85a..82631f3 100644
--- a/web/api/api_model_v1.rb
+++ b/web/api/api_model_v1.rb
@@ -105,12 +105,16 @@ module Hanlon
requires "label", type: String, desc: "The new model's label"
optional "image_uuid", type: String, default: "false", desc: "The UUID of the image to use"
requires "req_metadata_hash", type: Hash, desc: "The (JSON) metadata hash"
+ optional "opt_metadata_hash", type: Hash, desc: "The (JSON) optional metadata hash"
end
post do
template = params["template"]
label = params["label"]
image_uuid = params["image_uuid"] unless params["image_uuid"] == "false"
req_metadata_hash = params["req_metadata_hash"]
+
+opt_metadata_hash = params["opt_metadata_hash"]
+
# check the values that were passed in
model = SLICE_REF.get_model_using_template_name(template)
is_noop_template = ["boot_local", "discover_only"].include?(template)
@@ -123,11 +127,19 @@ module Hanlon
model.label = label
model.image_uuid = image.uuid if image
model.is_template = false
+puts "!!! in api/model.rb !!!"
model.req_metadata_hash.each { |key, md_hash_value|
value = params[key]
model.set_metadata_value(key, value, md_hash_value[:validation])
}
+model.opt_metadata_hash.each { |key, md_hash_value|
+ value = params[key]
+ model.set_metadata_value(key, value, md_hash_value[:validation])
+}
+
model.req_metadata_hash = req_metadata_hash
+#model.opt_metadata_hash = opt_metadata_hash
+
get_data_ref.persist_object(model)
raise(ProjectHanlon::Error::Slice::CouldNotCreate, "Could not create Model") unless model
slice_success_object(SLICE_REF, :create_model, model, :success_type => :created)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment