Class: Ovirt::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/ovirt/service.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{}
REQUIRED_OPTIONS =
[:server, :username, :password]
DEFAULT_PORT_3_0 =
8443
DEFAULT_PORT_3_1 =
443
DEFAULT_PORT =
DEFAULT_PORT_3_1
DEFAULT_SCHEME =
'https'.freeze
SESSION_ID_KEY =
'JSESSIONID'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Service

Returns a new instance of Service.



23
24
25
26
27
28
29
# File 'lib/ovirt/service.rb', line 23

def initialize(options={})
  @options = DEFAULT_OPTIONS.merge(options)
  parse_domain_name
  REQUIRED_OPTIONS.each { |key| raise "No #{key.to_s} specified" unless @options.has_key?(key) }
  @password = @options.delete(:password)
  @session_id = @options[:session_id]
end

Instance Attribute Details

#session_idObject

Returns the value of attribute session_id.



13
14
15
# File 'lib/ovirt/service.rb', line 13

def session_id
  @session_id
end

Class Method Details

.name_to_class(name) ⇒ Object



15
16
17
# File 'lib/ovirt/service.rb', line 15

def self.name_to_class(name)
  Ovirt.const_get(name.camelize)
end

Instance Method Details

#api(reload = false) ⇒ Object



35
36
37
38
# File 'lib/ovirt/service.rb', line 35

def api(reload = false)
  @api   = nil if reload
  @api ||= xml_to_object(Api, resource_get)
end

#api_uri(path = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/ovirt/service.rb', line 127

def api_uri(path = nil)
  uri = "#{base_uri}/api"
  unless path.nil?
    parts = path.to_s.split('/')
    parts.shift if parts.first == ''    # Remove leading slash
    parts.shift if parts.first == 'api' # We already have /api in our URI
    uri += "/#{parts.join('/')}" unless parts.empty?
  end
  uri
end

#blank_templateObject



72
73
74
75
76
77
# File 'lib/ovirt/service.rb', line 72

def blank_template
  @blank_template ||= begin
    href = special_objects[:"templates/blank"]
    href.blank? ? nil : Template.find_by_href(self, href)
  end
end

#create_resource(path = nil) ⇒ Object



175
176
177
178
# File 'lib/ovirt/service.rb', line 175

def create_resource(path = nil)
  require "rest-client"
  RestClient::Resource.new(api_uri(path), resource_options)
end

#disconnectObject



94
95
# File 'lib/ovirt/service.rb', line 94

def disconnect
end

#get_resource_by_ems_ref(uri_suffix, element_name = nil) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/ovirt/service.rb', line 97

def get_resource_by_ems_ref(uri_suffix, element_name = nil)
  xml     = resource_get(uri_suffix)
  doc     = Nokogiri::XML(xml)
  element_name ||= doc.root.name
  klass   = self.class.name_to_class(element_name)
  xml_to_object(klass, doc.root)
end

#inspectObject

just like the default inspect, but WITHOUT @password



31
32
33
# File 'lib/ovirt/service.rb', line 31

def inspect # just like the default inspect, but WITHOUT @password
  "#<#{self.class.name}:0x#{(self.object_id << 1).to_s(16).rjust(14,'0')} @options=#{@options.inspect}>"
end

#iso_imagesObject



90
91
92
# File 'lib/ovirt/service.rb', line 90

def iso_images
  iso_storage_domain.nil? ? [] : iso_storage_domain.iso_images
end

#iso_storage_domainObject



86
87
88
# File 'lib/ovirt/service.rb', line 86

def iso_storage_domain
  @iso_storage_domain ||= StorageDomain.iso_storage_domain(self)
end

#nameObject



44
45
46
# File 'lib/ovirt/service.rb', line 44

def name
  @name ||= product_info[:name]
end

#paginate_resource_get(path = nil, sort_by = :name, direction = :asc) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ovirt/service.rb', line 138

def paginate_resource_get(path = nil, sort_by=:name, direction=:asc)
  log_header = "#{self.class.name}#paginate_resource_get"
  page = 1
  full_xml = nil
  loop do
    uri = "#{path}?search=sortby%20#{sort_by}%20#{direction}%20page%20#{page}"
    partial_xml_str = self.resource_get(uri)
    if full_xml.nil?
      full_xml = Nokogiri::XML(partial_xml_str)
    else
      partial_xml = Nokogiri::XML(partial_xml_str)
      break if partial_xml.root.children.count == 0
      $rhevm_log.debug "#{log_header}: Combining resource elements for <#{path}> from page:<#{page}>" if $rhevm_log && $rhevm_log.debug?
      full_xml.root << partial_xml.root.children
    end
    page += 1
  end
  $rhevm_log.debug "#{log_header}: Combined elements for <#{path}>.  Total elements:<#{full_xml.root.children.count}>" if $rhevm_log && $rhevm_log.debug?
  return full_xml
end

#product_infoObject



40
41
42
# File 'lib/ovirt/service.rb', line 40

def product_info
  @product_info ||= api[:product_info]
end

#resource_delete(path) ⇒ Object



171
172
173
# File 'lib/ovirt/service.rb', line 171

def resource_delete(path)
  resource_verb(path, :delete)
end

#resource_get(path = nil) ⇒ Object



159
160
161
# File 'lib/ovirt/service.rb', line 159

def resource_get(path = nil)
  resource_verb(path, :get)
end

#resource_post(path, payload, additional_headers = {:content_type => :xml, :accept => :xml}) ⇒ Object



167
168
169
# File 'lib/ovirt/service.rb', line 167

def resource_post(path, payload, additional_headers={:content_type => :xml, :accept => :xml})
  resource_verb(path, :post, payload, additional_headers)
end

#resource_put(path, payload, additional_headers = {:content_type => :xml, :accept => :xml}) ⇒ Object



163
164
165
# File 'lib/ovirt/service.rb', line 163

def resource_put(path, payload, additional_headers={:content_type => :xml, :accept => :xml})
  resource_verb(path, :put, payload, additional_headers)
end

#root_tagObject



79
80
81
82
83
84
# File 'lib/ovirt/service.rb', line 79

def root_tag
  @root_tag ||= begin
    href = special_objects[:"tags/root"]
    href.blank? ? nil : Tag.find_by_href(self, href)
  end
end

#special_objectsObject



68
69
70
# File 'lib/ovirt/service.rb', line 68

def special_objects
  @special_objects ||= api[:special_objects]
end

#standard_collection(uri_suffix, element_name = nil, paginate = false, sort_by = :name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ovirt/service.rb', line 105

def standard_collection(uri_suffix, element_name = nil, paginate=false, sort_by=:name)
  if paginate
    doc = paginate_resource_get(uri_suffix, sort_by)
  else
    xml = resource_get(uri_suffix)
    doc = Nokogiri::XML(xml)
  end
  element_name ||= uri_suffix.singularize
  klass   = self.class.name_to_class(element_name)

  xml_path = uri_suffix == 'api' ? element_name : "#{element_name.pluralize}/#{element_name}"
  objects = doc.xpath("//#{xml_path}")
  objects.collect { |obj| xml_to_object(klass, obj) }
end

#status(link) ⇒ Object



120
121
122
123
124
125
# File 'lib/ovirt/service.rb', line 120

def status(link)
  response = resource_get(link)

  node = Base.xml_to_nokogiri(response)
  node.xpath('status/state').text
end

#summaryObject



64
65
66
# File 'lib/ovirt/service.rb', line 64

def summary
  api(true)[:summary] # This is volatile information
end

#vendorObject



48
49
50
# File 'lib/ovirt/service.rb', line 48

def vendor
  @vendor ||= product_info[:vendor]
end

#versionObject



52
53
54
# File 'lib/ovirt/service.rb', line 52

def version
  @version ||= product_info[:version]
end

#version_3_0?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ovirt/service.rb', line 60

def version_3_0?
  version_string.starts_with?("3.0")
end

#version_stringObject



56
57
58
# File 'lib/ovirt/service.rb', line 56

def version_string
  @version_string ||= "#{version[:major]}.#{version[:minor]}.#{version[:revision]}.#{version[:build]}"
end

#xml_to_object(klass, xml) ⇒ Object



19
20
21
# File 'lib/ovirt/service.rb', line 19

def xml_to_object(klass, xml)
  klass.create_from_xml(self, xml)
end