Module: OpenNebula::ServiceTemplateExt

Defined in:
lib/opennebula/flow/service_template_ext.rb

Overview

Module to decorate ServiceTemplate class with additional helpers not directly exposed through the OpenNebula XMLRPC API. The extensions include

- mp_import helper that imports a template into a marketplace

rubocop:disable Style/ClassAndModuleChildren

Class Method Summary collapse

Class Method Details

.extend_object(obj) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/opennebula/flow/service_template_ext.rb', line 24

def self.extend_object(obj)
    if !obj.is_a?(OpenNebula::ServiceTemplate)
        raise StandardError, "Cannot extended #{obj.class} " \
                             'with MarketPlaceAppExt'
    end

    class << obj

        ####################################################################
        # Public extended interface
        ####################################################################
        # Imports service template into marketplace
        #
        # @param templates [Hash]    Service roles templates information
        # @param market_id [Integer] Marketplace ID to import app
        # @param name      [String]  Service Template App name
        def mp_import(templates, market_id, name)
            template = ''
            name   ||= "#{@body['name']}-#{SecureRandom.hex[0..9]}"

            template = <<-EOT
            NAME      = "#{name}"
            ORIGIN_ID = "-1"
            TYPE      = "SERVICE_TEMPLATE"
            APPTEMPLATE64 = "#{Base64.strict_encode64(@body.to_json)}"
            EOT

            # Add VM template name into roles information
            @body['roles'].each do |role|
                # Find role template into templates to get the name to use
                t = templates.find do |_, v|
                    v[:template]['ID'].to_i == role['vm_template']
                end

                next if t.nil? || t[1].nil? || t[1][:name].nil?

                app_name = t[1][:name]

                template << <<-EOT
                ROLE = [ NAME="#{role['name']}", APP="#{app_name}"]
                EOT

                role.delete('vm_template')
            end

            xml = MarketPlaceApp.build_xml
            app = MarketPlaceApp.new(xml, @client)
            rc  = app.allocate(template, market_id)

            if OpenNebula.is_error?(rc)
                [-1, rc]
            else
                [0, app.id]
            end
        end

    end
end