mirror of
https://github.com/OpenNebula/one.git
synced 2025-02-08 05:57:23 +03:00
parent
9ed5c84373
commit
d6ec6fad07
@ -193,6 +193,35 @@ class OneFlowTemplateHelper < OpenNebulaHelper::OneHelper
|
|||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Get custom role attributes values from user
|
||||||
|
#
|
||||||
|
# @param role [Hash] Service role with custom attributes
|
||||||
|
#
|
||||||
|
# @return [Hash] Role with custom attributes values
|
||||||
|
def custom_role_attrs(roles)
|
||||||
|
return if roles.nil? || roles.empty?
|
||||||
|
|
||||||
|
ret = {}
|
||||||
|
role_with_custom_attrs = false
|
||||||
|
|
||||||
|
roles.each do |role|
|
||||||
|
next unless role.key?('custom_attrs')
|
||||||
|
|
||||||
|
####################################################################
|
||||||
|
# Display Role Information
|
||||||
|
####################################################################
|
||||||
|
header = "> Please insert the user inputs for the role \"#{role['name']}\""
|
||||||
|
puts header
|
||||||
|
|
||||||
|
role.merge!(custom_attrs(role['custom_attrs']))
|
||||||
|
role_with_custom_attrs = true
|
||||||
|
end
|
||||||
|
|
||||||
|
ret['roles'] = roles if role_with_custom_attrs
|
||||||
|
|
||||||
|
ret
|
||||||
|
end
|
||||||
|
|
||||||
def networks(vnets)
|
def networks(vnets)
|
||||||
return unless vnets
|
return unless vnets
|
||||||
|
|
||||||
|
@ -61,6 +61,7 @@ require 'command_parser'
|
|||||||
require 'opennebula/oneflow_client'
|
require 'opennebula/oneflow_client'
|
||||||
require 'cli_helper'
|
require 'cli_helper'
|
||||||
require 'one_helper/oneflowtemplate_helper'
|
require 'one_helper/oneflowtemplate_helper'
|
||||||
|
require 'one_helper/onetemplate_helper'
|
||||||
|
|
||||||
USER_AGENT = 'CLI'
|
USER_AGENT = 'CLI'
|
||||||
|
|
||||||
@ -253,14 +254,18 @@ CommandParser::CmdParser.new(ARGV) do
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
|
params['merge_template'] = {}
|
||||||
body = JSON.parse(response.body)['DOCUMENT']['TEMPLATE']['BODY']
|
body = JSON.parse(response.body)['DOCUMENT']['TEMPLATE']['BODY']
|
||||||
|
|
||||||
params['merge_template'] = helper.custom_attrs(
|
# Check global custom attributes
|
||||||
body['custom_attrs']
|
custom_attrs = helper.custom_attrs(body['custom_attrs'])
|
||||||
)
|
params['merge_template'].merge!(custom_attrs) unless custom_attrs.nil?
|
||||||
|
|
||||||
params['merge_template'] = {} unless params['merge_template']
|
# Check role level custom attributes
|
||||||
|
custom_role_attrs = helper.custom_role_attrs(body['roles'])
|
||||||
|
params['merge_template'].merge!(custom_role_attrs) unless custom_role_attrs.nil?
|
||||||
|
|
||||||
|
# Check vnets attributes
|
||||||
vnets = helper.networks(body['networks'])
|
vnets = helper.networks(body['networks'])
|
||||||
params['merge_template'].merge!(vnets) unless vnets.nil?
|
params['merge_template'].merge!(vnets) unless vnets.nil?
|
||||||
end
|
end
|
||||||
|
@ -323,6 +323,9 @@ module OpenNebula
|
|||||||
|
|
||||||
template['start_time'] = Integer(Time.now)
|
template['start_time'] = Integer(Time.now)
|
||||||
|
|
||||||
|
# Replace $attibute by the corresponding value
|
||||||
|
resolve_attributes(template)
|
||||||
|
|
||||||
super(template.to_json, template['name'])
|
super(template.to_json, template['name'])
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -660,7 +663,7 @@ module OpenNebula
|
|||||||
end if deploy
|
end if deploy
|
||||||
|
|
||||||
# Replace $attibute by the corresponding value
|
# Replace $attibute by the corresponding value
|
||||||
resolve_attributes(body)
|
resolve_networks(body)
|
||||||
|
|
||||||
# @body = template.to_hash
|
# @body = template.to_hash
|
||||||
|
|
||||||
@ -784,31 +787,49 @@ module OpenNebula
|
|||||||
end
|
end
|
||||||
|
|
||||||
# rubocop:disable Layout/LineLength
|
# rubocop:disable Layout/LineLength
|
||||||
|
def resolve_networks(template)
|
||||||
|
template['roles'].each do |role|
|
||||||
|
next unless role['vm_template_contents']
|
||||||
|
|
||||||
|
# $CUSTOM1_VAR Any word character
|
||||||
|
# (letter, number, underscore)
|
||||||
|
role['vm_template_contents'].scan(/\$(\w+)/).each do |key|
|
||||||
|
net = template['networks_values'].find {|att| att.key? key[0] }
|
||||||
|
|
||||||
|
next if net.nil?
|
||||||
|
|
||||||
|
role['vm_template_contents'].gsub!(
|
||||||
|
'$'+key[0],
|
||||||
|
net[net.keys[0]]['id'].to_s
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def resolve_attributes(template)
|
def resolve_attributes(template)
|
||||||
template['roles'].each do |role|
|
template['roles'].each do |role|
|
||||||
if role['vm_template_contents']
|
if role['vm_template_contents']
|
||||||
# $CUSTOM1_VAR Any word character
|
# $CUSTOM1_VAR Any word character
|
||||||
# (letter, number, underscore)
|
# (letter, number, underscore)
|
||||||
role['vm_template_contents'].scan(/\$(\w+)/).each do |key|
|
role['vm_template_contents'].scan(/\$(\w+)/).each do |key|
|
||||||
# Check if $ var value is in custom_attrs_values
|
# Check if $ var value is in custom_attrs_values within the role
|
||||||
if !template['custom_attrs_values'].nil? &&
|
if !role['custom_attrs_values'].nil? && \
|
||||||
template['custom_attrs_values'].key?(key[0])
|
role['custom_attrs_values'].key?(key[0])
|
||||||
role['vm_template_contents'].gsub!(
|
role['vm_template_contents'].gsub!(
|
||||||
'$'+key[0],
|
'$'+key[0],
|
||||||
template['custom_attrs_values'][key[0]]
|
role['custom_attrs_values'][key[0]]
|
||||||
)
|
)
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check if $ var value is in networks
|
# Check if $ var value is in custom_attrs_values
|
||||||
net = template['networks_values']
|
|
||||||
.find {|att| att.key? key[0] }
|
|
||||||
|
|
||||||
next if net.nil?
|
next unless !template['custom_attrs_values'].nil? && \
|
||||||
|
template['custom_attrs_values'].key?(key[0])
|
||||||
|
|
||||||
role['vm_template_contents'].gsub!(
|
role['vm_template_contents'].gsub!(
|
||||||
'$'+key[0],
|
'$'+key[0],
|
||||||
net[net.keys[0]]['id'].to_s
|
template['custom_attrs_values'][key[0]]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -186,6 +186,22 @@ def one_error_to_http(error)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Check if the custom_attrs and their respective values are correct
|
||||||
|
#
|
||||||
|
# @param custom_attrs [Hash] Custom attrs of the service/role
|
||||||
|
# @param custom_attrs_values [Hash] Custom attrs values to check
|
||||||
|
def check_custom_attrs(custom_attrs, custom_attrs_values)
|
||||||
|
return if custom_attrs.nil? || custom_attrs.empty?
|
||||||
|
|
||||||
|
if !custom_attrs.is_a?(Hash) || !custom_attrs_values.is_a?(Hash)
|
||||||
|
raise 'Wrong custom_attrs or custom_attrs_values format'
|
||||||
|
end
|
||||||
|
|
||||||
|
return if (custom_attrs.keys - custom_attrs_values.keys).empty?
|
||||||
|
|
||||||
|
raise 'Verify that every custom attribute have its corresponding value defined'
|
||||||
|
end
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
# Defaults
|
# Defaults
|
||||||
##############################################################################
|
##############################################################################
|
||||||
@ -629,36 +645,40 @@ post '/service_template/:id/action' do
|
|||||||
merge_template = opts['merge_template']
|
merge_template = opts['merge_template']
|
||||||
service_json = JSON.parse(service_template.to_json)
|
service_json = JSON.parse(service_template.to_json)
|
||||||
|
|
||||||
# Check custom_attrs
|
body = service_json['DOCUMENT']['TEMPLATE']['BODY']
|
||||||
body = service_json['DOCUMENT']['TEMPLATE']['BODY']
|
|
||||||
custom_attrs = body['custom_attrs']
|
|
||||||
|
|
||||||
if merge_template
|
begin
|
||||||
|
# Check service custom_attrs
|
||||||
|
custom_attrs = body['custom_attrs']
|
||||||
custom_attrs_values = merge_template['custom_attrs_values']
|
custom_attrs_values = merge_template['custom_attrs_values']
|
||||||
end
|
check_custom_attrs(custom_attrs, custom_attrs_values)
|
||||||
|
|
||||||
if custom_attrs && !(custom_attrs.is_a? Hash)
|
# Check custom attrs in each role
|
||||||
return internal_error('Wrong custom_attrs format',
|
body['roles'].each do |role|
|
||||||
VALIDATION_EC)
|
next if role['custom_attrs'].nil?
|
||||||
end
|
|
||||||
|
|
||||||
if custom_attrs_values && !(custom_attrs_values.is_a? Hash)
|
roles_merge_template = merge_template['roles']
|
||||||
return internal_error('Wrong custom_attrs_values format',
|
|
||||||
VALIDATION_EC)
|
|
||||||
end
|
|
||||||
|
|
||||||
if custom_attrs && !custom_attrs.empty? && !custom_attrs_values
|
# merge_template must have 'role' key if role has custom attributes
|
||||||
return internal_error('No custom_attrs_values found',
|
if roles_merge_template.nil?
|
||||||
VALIDATION_EC)
|
raise 'The Service template specifies custom attributes for roles ' \
|
||||||
end
|
'but no values have been found'
|
||||||
|
end
|
||||||
|
|
||||||
if custom_attrs &&
|
if !roles_merge_template.is_a?(Array) || roles_merge_template.empty?
|
||||||
!custom_attrs.empty? &&
|
raise 'The role custom attributes are empty or do not have a valid format'
|
||||||
custom_attrs_values &&
|
end
|
||||||
!(custom_attrs.keys - custom_attrs_values.keys).empty?
|
|
||||||
return internal_error('Every custom_attrs key must have its ' \
|
# Select role from merge_template by role name
|
||||||
'value defined at custom_attrs_value',
|
merge_role = roles_merge_template.find {|item| item['name'] == role['name'] }
|
||||||
VALIDATION_EC)
|
|
||||||
|
role_custom_attrs = role['custom_attrs']
|
||||||
|
role_custom_attrs_values = merge_role['custom_attrs_values']
|
||||||
|
|
||||||
|
check_custom_attrs(role_custom_attrs, role_custom_attrs_values)
|
||||||
|
end
|
||||||
|
rescue StandardError => e
|
||||||
|
return internal_error(e.message, VALIDATION_EC)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check networks
|
# Check networks
|
||||||
|
@ -42,6 +42,16 @@ module OpenNebula
|
|||||||
:type => :string,
|
:type => :string,
|
||||||
:required => false
|
:required => false
|
||||||
},
|
},
|
||||||
|
'custom_attrs' => {
|
||||||
|
:type => :object,
|
||||||
|
:properties => {},
|
||||||
|
:required => false
|
||||||
|
},
|
||||||
|
'custom_attrs_values' => {
|
||||||
|
:type => :object,
|
||||||
|
:properties => {},
|
||||||
|
:required => false
|
||||||
|
},
|
||||||
'parents' => {
|
'parents' => {
|
||||||
:type => :array,
|
:type => :array,
|
||||||
:items => {
|
:items => {
|
||||||
@ -487,7 +497,7 @@ module OpenNebula
|
|||||||
instantiate_template = JSON.parse(@body.to_json)
|
instantiate_template = JSON.parse(@body.to_json)
|
||||||
else
|
else
|
||||||
instantiate_template = JSON.parse(@body.to_json)
|
instantiate_template = JSON.parse(@body.to_json)
|
||||||
.merge(merge_template)
|
.deep_merge(merge_template)
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
Loading…
x
Reference in New Issue
Block a user