Class: OneTemplateHelper

Inherits:
OpenNebulaHelper::OneHelper show all
Defined in:
lib/one_helper/onetemplate_helper.rb

Overview

Helper class for Template commands

Direct Known Subclasses

OneVNTemplateHelper

Constant Summary collapse

VM_NAME =
{
    :name  => 'name',
    :large => '--name name',
    :format => String,
    :description =>  <<~EOT.strip
        Name of the new VM or TEMPLATE. When instantiating
                                       multiple VMs you can use the \"%i\" wildcard to produce
                                       different names such as vm-0, vm-1...
    EOT
}
MULTIPLE =
{
    :name  => 'multiple',
    :short => '-m x',
    :large => '--multiple x',
    :format => Integer,
    :description => 'Instance multiple VMs'
}
EXTENDED =
{
    :name => 'extended',
    :large => '--extended',
    :description => 'Process the template and included extended '+
                    'information, such as the SIZE for each DISK'
}
RECURSIVE =
{
    :name => 'recursive',
    :short => '-R',
    :large => '--recursive',
    :description => 'Applies the action to the template plus any '+
    'image defined in DISK'
}
PERSISTENT =
{
    :name => 'persistent',
    :large => '--persistent',
    :description => 'Creates a private persistent copy of the template '+
    'plus any image defined in DISK, and instantiates that copy'
}
INT_EXP =
/^-?\d+$/
FLOAT_EXP =
/^-?\d+(\.\d+)?$/

Instance Attribute Summary

Attributes inherited from OpenNebulaHelper::OneHelper

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenNebulaHelper::OneHelper

#backup_mode_valid?, #check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, list_layout_help, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #start_pager, #stop_pager, table_conf, template_input_help, #to_id, to_id_desc, #user_name

Constructor Details

This class inherits a constructor from OpenNebulaHelper::OneHelper

Class Method Details

.conf_fileObject



69
70
71
# File 'lib/one_helper/onetemplate_helper.rb', line 69

def self.conf_file
    'onetemplate.yaml'
end

.get_user_inputs(template, keys = []) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/one_helper/onetemplate_helper.rb', line 136

def self.get_user_inputs(template, keys = [])
    user_inputs = template['VMTEMPLATE']['TEMPLATE']['USER_INPUTS']

    return '' unless user_inputs

    if (inputs_order = template['VMTEMPLATE']['TEMPLATE']['INPUTS_ORDER'])
        ordered_inputs = inputs_order.split(',').map(&:strip)
        user_inputs    = user_inputs.slice(*ordered_inputs).merge(user_inputs)
    end

    answers = OpenNebulaHelper.parse_user_inputs(user_inputs, keys)
    answers_s = ''
    answers.each do |key, val|
        next unless val

        # Do not replace values that are equal to the ones already in the
        # template. Useful for cpu, mem, vcpu
        if key != template['VMTEMPLATE']['TEMPLATE'][key]
            answers_s << "#{key} = \""
            answers_s << val.gsub('"', '\"') << "\"\n"
        end
    end

    answers_s
end

.rnameObject



65
66
67
# File 'lib/one_helper/onetemplate_helper.rb', line 65

def self.rname
    'VMTEMPLATE'
end

Instance Method Details

#format_pool(options) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/one_helper/onetemplate_helper.rb', line 103

def format_pool(options)
    config_file = self.class.table_conf

    CLIHelper::ShowTable.new(config_file, self) do
        column :ID, 'ONE identifier for the Template', :size=>4 do |d|
            d['ID']
        end

        column :NAME, 'Name of the Template', :left, :size=>27 do |d|
            d['NAME']
        end

        column :USER, 'Username of the Template owner', :left,
               :size=>15 do |d|
            helper.user_name(d, options)
        end

        column :GROUP, 'Group of the Template', :left, :size=>15 do |d|
            helper.group_name(d, options)
        end

        column :REGTIME, 'Registration time of the Template',
               :size=>15 do |d|
            OpenNebulaHelper.time_to_str(d['REGTIME'])
        end

        default :ID, :USER, :GROUP, :NAME, :REGTIME
    end
end

#show_resource(id, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/one_helper/onetemplate_helper.rb', line 73

def show_resource(id, options)
    resource = retrieve_resource(id)

    if !options[:extended].nil?
        rc = resource.info(options[:extended])
    else
        rc = resource.info
    end

    return -1, rc.message if OpenNebula.is_error?(rc)

    if options[:xml]
        return 0, resource.to_xml(true)
    elsif options[:json]
        # If body is set, the resource contains a JSON inside
        if options[:body]
            return 0, check_resource_xsd(resource)
        else
            return 0, ::JSON.pretty_generate(
                check_resource_xsd(resource)
            )
        end
    elsif options[:yaml]
        return 0, check_resource_xsd(resource).to_yaml(:indent => 4)
    else
        format_resource(resource, options)
        return 0
    end
end