Class: Azure::Hosts

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/host.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Hosts

Returns a new instance of Hosts.



21
22
23
# File 'lib/azure/host.rb', line 21

def initialize(connection)
  @connection=connection
end

Instance Method Details

#allObject



43
44
45
# File 'lib/azure/host.rb', line 43

def all
  self.load.values
end

#create(params) ⇒ Object



81
82
83
84
# File 'lib/azure/host.rb', line 81

def create(params)
  host = Host.new(@connection)
  host.create(params)
end

#delete(name) ⇒ Object



85
86
87
88
89
90
# File 'lib/azure/host.rb', line 85

def delete(name)
  if self.exists?(name)
      servicecall = "hostedservices/" + name
    @connection.query_azure(servicecall, "delete") 
  end
end

#exists?(name) ⇒ Boolean

first look up local cache if we have already loaded list.

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/azure/host.rb', line 48

def exists?(name)
  return @hosted_services.key?(name) if @hosted_services
  self.exists_on_cloud?(name)
end

#exists_on_cloud?(name) ⇒ Boolean

Look up on cloud and not local cache

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'lib/azure/host.rb', line 54

def exists_on_cloud?(name)
  ret_val = @connection.query_azure("hostedservices/#{name}")
  if ret_val.nil? || ret_val.css('Error Code').length > 0
    Chef::Log.warn 'Unable to find hosted(cloud) service:' + ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content if ret_val
    false
  else
    true
  end
end

#fetch_from_cloud(name) ⇒ Object

Look up hosted service on cloud and not local cache



71
72
73
74
75
76
77
78
79
# File 'lib/azure/host.rb', line 71

def fetch_from_cloud(name)
  ret_val = @connection.query_azure("hostedservices/#{name}")
  if ret_val.nil? || ret_val.css('Error Code').length > 0
    Chef::Log.warn 'Unable to find hosted(cloud) service:' + ret_val.at_css('Error Code').content + ' : ' + ret_val.at_css('Error Message').content if ret_val
    nil
  else
    Host.new(@connection).parse(ret_val)
  end
end

#find(name) ⇒ Object

first look up local cache if we have already loaded list.



65
66
67
68
# File 'lib/azure/host.rb', line 65

def find(name)
  return @hosted_services[name] if @hosted_services && @hosted_services.key?(name)
  self.fetch_from_cloud(name)
end

#load(force_load = false) ⇒ Object

force_load should be true when there is something in local cache and we want to reload first call is always load.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/azure/host.rb', line 27

def load(force_load = false)
  if not @hosted_services || force_load
    @hosted_services = begin
      hosted_services = Hash.new
      responseXML = @connection.query_azure('hostedservices')
      servicesXML = responseXML.css('HostedServices HostedService')
      servicesXML.each do |serviceXML|
        host = Host.new(@connection).parse(serviceXML)
        hosted_services[host.name] = host
      end
      hosted_services
    end
  end
  @hosted_services
end