Class: Azure::Roles

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AzureUtility

#xml_content

Constructor Details

#initialize(connection) ⇒ Roles

Returns a new instance of Roles.



23
24
25
26
# File 'lib/azure/role.rb', line 23

def initialize(connection)
  @connection = connection
  @roles = nil
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#rolesObject

Returns the value of attribute roles.



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

def roles
  @roles
end

Instance Method Details

#allObject

do not use this unless you want a list of all roles(vms) in your subscription



28
29
30
31
32
33
34
35
36
# File 'lib/azure/role.rb', line 28

def all
  @roles = Array.new
  @connection.deploys.all.each do |deploy|
    deploy.roles.each do |role|
      @roles << role
    end
  end
  @roles
end

#alone_on_hostedservice(found_role) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/azure/role.rb', line 65

def alone_on_hostedservice(found_role)
  roles = find_roles_within_hostedservice(found_role.hostedservicename)
  if roles && roles.length > 1
    return false
  end
  return true
end

#delete(name, params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/azure/role.rb', line 77

def delete(name, params)
  role = find(name)
  if role != nil
    if alone_on_hostedservice(role)
      servicecall = "hostedservices/#{role.hostedservicename}/deployments" +
      "/#{role.deployname}"
    else
      servicecall = "hostedservices/#{role.hostedservicename}/deployments" +
      "/#{role.deployname}/roles/#{role.name}"
    end

    roleXML = nil

    unless params[:preserve_azure_os_disk]
        roleXML = @connection.query_azure(servicecall, "get")
    end

    @connection.query_azure(servicecall, "delete")
    # delete role from local cache as well.
    @connection.hosts.find(role.hostedservicename).delete_role(role)
    @roles.delete(role) if @roles

    unless params[:preserve_azure_dns_name]
      unless params[:azure_dns_name].nil?
        roles_using_same_service = find_roles_within_hostedservice(params[:azure_dns_name])
        if roles_using_same_service.size <= 1
          servicecall = "hostedservices/" + params[:azure_dns_name]
          @connection.query_azure(servicecall, "delete")
        end
      end
    end

    unless params[:preserve_azure_os_disk]
      osdisk = roleXML.css(roleXML, 'OSVirtualHardDisk')
      disk_name = xml_content(osdisk, 'DiskName')
      servicecall = "disks/#{disk_name}"
       = @connection.query_azure(servicecall, "get")

      # OS Disk can only be deleted if it is detached from the VM.
      # So Iteratively check for disk detachment from the VM while waiting for 5 minutes ,
      # exit otherwise after 12 attempts.
      for attempt in 0..12
         break if @connection.query_azure(servicecall, "get").search("AttachedTo").text == ""
         if attempt == 12 then puts "The associated disk could not be deleted due to time out." else sleep 25 end
      end
      @connection.query_azure(servicecall, "delete")

      if params[:delete_azure_storage_account]
         = xml_content(, "MediaLink")
         = .gsub("http://", "").gsub(/.blob(.*)$/, "")

        begin
          @connection.query_azure("storageservices/#{}", "delete")
        rescue Exception => ex
          ui.warn("#{ex.message}")
          ui.warn("#{ex.backtrace.join("\n")}")
        end

      end

    end


  end
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/azure/role.rb', line 73

def exists?(name)
  find(name) != nil
end

#find(role_name, params = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/azure/role.rb', line 49

def find(role_name, params= nil)
  if params && params[:azure_dns_name]
    return find_in_hosted_service(role_name, params[:azure_dns_name])
  end

  all if @roles == nil

  # TODO - optimize this lookup
  @roles.each do |role|
    if(role.name == role_name)
      return role
    end
  end
  nil
end

#find_in_hosted_service(role_name, hostedservicename) ⇒ Object



43
44
45
46
47
# File 'lib/azure/role.rb', line 43

def find_in_hosted_service(role_name, hostedservicename)
  host = @connection.hosts.find(hostedservicename)
  return nil if host.nil?
  host.find_role(role_name)
end

#find_roles_within_hostedservice(hostedservicename) ⇒ Object



38
39
40
41
# File 'lib/azure/role.rb', line 38

def find_roles_within_hostedservice(hostedservicename)
  host = @connection.hosts.find(hostedservicename)
  (host) ? host.roles : nil # nil says invalid hosted service
end