Class: OneFlowTemplateHelper

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

Overview

Oneflow Template command helper

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenNebulaHelper::OneHelper

#check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, #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, #show_resource, #start_pager, #stop_pager, table_conf, #to_id, to_id_desc, #user_name

Constructor Details

This class inherits a constructor from OpenNebulaHelper::OneHelper

Class Method Details

.conf_fileObject

Configuration file



23
24
25
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 23

def self.conf_file
    'oneflowtemplate.yaml'
end

Instance Method Details

#client(options) ⇒ Object

Get client to make request



30
31
32
33
34
35
36
37
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 30

def client(options)
    Service::Client.new(
        :username => options[:username],
        :password => options[:password],
        :url => options[:server],
        :user_agent => USER_AGENT
    )
end

#custom_attrs(custom_attrs) ⇒ Hash

Get custom attributes values from user

Parameters:

  • custom_attrs (Hash)

    Custom attributes from template

Returns:

  • (Hash)

    Custom attributes values



185
186
187
188
189
190
191
192
193
194
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 185

def custom_attrs(custom_attrs)
    # rubocop:disable Layout/LineLength
    return if custom_attrs.nil? || custom_attrs.empty?

    ret = {}
    ret['custom_attrs_values'] = OpenNebulaHelper.parse_user_inputs(custom_attrs)

    # rubocop:enable Layout/LineLength
    ret
end

#factory(id = nil) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 336

def factory(id = nil)
    if id
        OpenNebula::ServiceTemplate.new_with_id(id, @client)
    else
        xml = OpenNebula::ServiceTemplate.build_xml
        OpenNebula::ServiceTemplate.new(xml, @client)
    end
end

#format_resource(client, service_template, options) ⇒ Object

Show service template detailed information

Parameters:

  • client (Service::Client)

    Petition client

  • service_template (Integer)

    Service template ID

  • options (Hash)

    CLI options



126
127
128
129
130
131
132
133
134
135
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 126

def format_resource(client, service_template, options)
    response = client.get("#{RESOURCE_PATH}/#{service_template}")

    if CloudClient.is_error?(response)
        [response.code.to_i, response.to_s]
    else
        if options[:json]
            [0, response.body]
        elsif options[:yaml]
            [0, JSON.parse(response.body).to_yaml(:indent => 4)]
        else
            str    = '%-20s: %-20s'
            str_h1 = '%-80s'

            document = JSON.parse(response.body)['DOCUMENT']
            template = document['TEMPLATE']['BODY']
            reg_time = OpenNebulaHelper.time_to_str(
                template['registration_time']
            )

            CLIHelper.print_header(
                str_h1 % "SERVICE TEMPLATE #{document['ID']} INFORMATION"
            )

            puts Kernel.format str, 'ID',   document['ID']
            puts Kernel.format str, 'NAME', document['NAME']
            puts Kernel.format str, 'USER', document['UNAME']
            puts Kernel.format str, 'GROUP', document['GNAME']
            puts Kernel.format str, 'REGISTRATION TIME', reg_time

            puts

            CLIHelper.print_header(str_h1 % 'PERMISSIONS', false)

            %w[OWNER GROUP OTHER].each do |e|
                mask = '---'
                permissions_hash = document['PERMISSIONS']
                mask[0] = 'u' if permissions_hash["#{e}_U"] == '1'
                mask[1] = 'm' if permissions_hash["#{e}_M"] == '1'
                mask[2] = 'a' if permissions_hash["#{e}_A"] == '1'

                puts Kernel.format str, e, mask
            end

            puts

            CLIHelper.print_header(str_h1 % 'TEMPLATE CONTENTS', false)
            puts JSON.pretty_generate(template)

            0
        end
    end
end

#format_service_template_poolObject

Get service template pool



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 40

def format_service_template_pool
    config_file = self.class.table_conf

    CLIHelper::ShowTable.new(config_file, self) do
        column :ID, 'ID', :size => 10 do |d|
            d['ID']
        end

        column :USER, 'Username', :left, :size => 15 do |d|
            d['UNAME']
        end

        column :GROUP, 'Group', :left, :size => 15 do |d|
            d['GNAME']
        end

        column :NAME, 'Name', :left, :expand => true do |d|
            d['NAME']
        end

        column :REGTIME,
               'Registration time of the Service Template',
               :size => 15 do |d|
            d.extend(CLIHelper::HashWithSearch)
            d = d.dsearch('TEMPLATE/BODY')

            OpenNebulaHelper.time_to_str(d['registration_time'])
        end

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

#list_service_template_pool(client, options) ⇒ Object

List service template pool

Parameters:

  • client (Service::Client)

    Petition client

  • options (Hash)

    CLI options



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 77

def list_service_template_pool(client, options)
    response = client.get(RESOURCE_PATH)

    if CloudClient.is_error?(response)
        [response.code.to_i, response.to_s]
    else
        if options[:json]
            [0, response.body]
        elsif options[:yaml]
            [0, JSON.parse(response.body).to_yaml(:indent => 4)]
        else
            documents = JSON.parse(response.body)['DOCUMENT_POOL']
            format_service_template_pool.show(documents['DOCUMENT'])

            0
        end
    end
end

#networks(vnets) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 196

def networks(vnets)
    return unless vnets

    ret = {}
    ret['networks_values'] = parse_networks(vnets)

    ret
end

#parse_networks(vnets, get_defaults = false) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 205

def parse_networks(vnets, get_defaults = false)
    unless get_defaults
        puts 'There are some networks that require user input. ' \
             'Use the string <<EDITOR>> to launch an editor ' \
             '(e.g. for multi-line inputs)'
    end

    answers = []

    vnets.each do |key, val|
        input_cfg = val.split('|', -1)

        if input_cfg.length != 5
            STDERR.puts 'Malformed user input. It should have 5'\
                        "parts separated by '|':"
            STDERR.puts "  #{key}: #{val}"
            exit(-1)
        end

        vnet                                  = {}
        mandatory, _, description, _, initial = input_cfg

        if initial && !initial.empty?
            type, resource_id, extra = initial.split(':', -1)
        end

        if (!type || !resource_id) && (initial && !initial.empty?)
            STDERR.puts 'Wrong type for user input default value:'
            STDERR.puts "  #{key}: #{val}"
            exit(-1)
        end

        vnet[key] = {}

        if get_defaults
            vnet[key][type]    = resource_id
            vnet[key]['extra'] = extra

            answers << vnet unless mandatory == 'M'
            next
        end

        puts "  * (#{key}) #{description}"

        ####################################################################
        # Ask for type
        ####################################################################
        header  = '    '
        header += 'TYPE Existing(1), Create(2), Reserve(3). '
        header += 'Press enter for default. ' if type && !type.empty?

        print header

        answer = STDIN.readline.chop

        if type && !type.empty? && ![1, 2, 3].include?(answer.to_i)
            type_a = type
        else
            until [1, 2, 3].include?(answer.to_i)
                print header
                answer = STDIN.readline.chop
            end
        end

        case answer.to_i
        when 1
            type_a = 'id'
        when 2
            type_a = 'template_id'
        when 3
            type_a = 'reserve_from'
        end

        ####################################################################
        # Ask for resource id
        ####################################################################
        header = '    '

        if type_a == 'template_id'
            header += 'VN Template ID. '
        else
            header += 'VN ID. '
        end

        if resource_id && !resource_id.empty?
            header += "Press enter for default (#{resource_id}). "
        end

        print header

        resource_id_a = STDIN.readline.chop

        if resource_id && !resource_id.empty?
            resource_id_a = resource_id
        else
            while resource_id_a.empty?
                print header
                resource_id_a = STDIN.readline.chop
            end
        end

        ####################################################################
        # Asks for extra
        ####################################################################
        if type_a != 'id'
            header  = '    '
            header += 'EXTRA (Type EMPTY for leaving empty). '

            if extra && !extra.empty?
                header += " Press enter for default (#{extra}). "
            end

            print header

            extra_a = STDIN.readline.chop

            if extra_a.empty?
                vnet[key]['extra'] = extra
            else
                vnet[key]['extra'] = extra_a
            end
        end

        vnet[key][type_a] = resource_id_a

        answers << vnet
    end

    answers
end

#top_service_template_pool(client, options) ⇒ Object

List service template pool continiously

Parameters:

  • client (Service::Client)

    Petition client

  • options (Hash)

    CLI options



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/one_helper/oneflowtemplate_helper.rb', line 100

def top_service_template_pool(client, options)
    # TODO: make default delay configurable
    options[:delay] ? delay = options[:delay] : delay = 4

    begin
        loop do
            CLIHelper.scr_cls
            CLIHelper.scr_move(0, 0)

            list_service_template_pool(client, options)

            sleep delay
        end
    rescue StandardError => e
        STDERR.puts e.message
        exit(-1)
    end

    0
end