Class: Azure::Hosts

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

Instance Method Summary collapse

Methods included from AzureUtility

#error_from_response_xml, #xml_content

Constructor Details

#initialize(connection) ⇒ Hosts

Returns a new instance of Hosts.



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

def initialize(connection)
  @connection = connection
end

Instance Method Details

#allObject



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

def all
  load.values
end

#create(params) ⇒ Object



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

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

#delete(name) ⇒ Object



91
92
93
94
95
96
# File 'lib/azure/service_management/host.rb', line 91

def delete(name)
  if 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)


49
50
51
52
53
# File 'lib/azure/service_management/host.rb', line 49

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

  exists_on_cloud?(name)
end

#exists_on_cloud?(name) ⇒ Boolean

Look up on cloud and not local cache

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
# File 'lib/azure/service_management/host.rb', line 56

def exists_on_cloud?(name)
  ret_val = @connection.query_azure("hostedservices/#{name}")
  error_code, error_message = error_from_response_xml(ret_val) if ret_val
  if ret_val.nil? || error_code.length > 0
    Chef::Log.debug("Unable to find hosted(cloud) service:" + error_code + " : " + error_message) if ret_val
    false
  else
    true
  end
end

#fetch_from_cloud(name) ⇒ Object

Look up hosted service on cloud and not local cache



75
76
77
78
79
80
81
82
83
84
# File 'lib/azure/service_management/host.rb', line 75

def fetch_from_cloud(name)
  ret_val = @connection.query_azure("hostedservices/#{name}")
  error_code, error_message = error_from_response_xml(ret_val) if ret_val
  if ret_val.nil? || error_code.length > 0
    Chef::Log.warn("Unable to find hosted(cloud) service:" + error_code + " : " + error_message) 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.



68
69
70
71
72
# File 'lib/azure/service_management/host.rb', line 68

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

  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.



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

def load(force_load = false)
  unless @hosted_services || force_load
    @hosted_services = begin
      hosted_services = {}
      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