Class: VCloudApiHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/help/v_cloud_api_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, url_endpoint, logger = Logger.new(STDOUT)) ⇒ VCloudApiHandler

Returns a new instance of VCloudApiHandler.



5
6
7
8
9
10
# File 'lib/help/v_cloud_api_handler.rb', line 5

def initialize(username, password, url_endpoint, logger = Logger.new(STDOUT))
  @username = username
  @password = password
  @url_endpoint = url_endpoint
  @logger = logger
end

Instance Method Details

#firewall_rulesObject



112
113
114
# File 'lib/help/v_cloud_api_handler.rb', line 112

def firewall_rules
  
end

#generic_get_request(full_url) ⇒ Object

private

Raises:

  • (Exception)


118
119
120
121
122
123
124
125
# File 'lib/help/v_cloud_api_handler.rb', line 118

def generic_get_request(full_url)
  raise Exception.new("no url") if full_url == nil
  @logger.debug "generic request: url = #{full_url.inspect}"
  url = URI.parse(full_url)
  req = Net::HTTP::Get.new(url.path)
  prepare_request(req)
  return http_request(url, req)
end

#http_request(url, request) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/help/v_cloud_api_handler.rb', line 139

def http_request(url, request)
  http_session = Net::HTTP.new(url.host, url.port)
  http_session.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http_session.use_ssl = true
  res = http_session.start {|http|
    http.request(request)
  }

  @logger.debug "--- Response-Header:"
  res.each_header do |key, name|
    @logger.debug "#{key}: #{name}"
  end
  xmlbody = XmlSimple.xml_in(res.body)
  @logger.debug "response-body: #{xmlbody.inspect}"
  return res
end

#internet_servicesObject



94
95
96
97
98
99
100
# File 'lib/help/v_cloud_api_handler.rb', line 94

def internet_services
  @internet_services_link = "#{@vdc_link}/internetServices"
  @internet_services_link.gsub!("api/v0.8a-ext1.6","api/extensions/v1.6")
  res = generic_get_request(@internet_services_link)
  xmlbody = XmlSimple.xml_in(res.body)
  return xmlbody
end

#loginObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/help/v_cloud_api_handler.rb', line 17

def 
  url = URI.parse("https://services.vcloudexpress.terremark.com/api/v0.8a-ext1.6/login")    
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth("#{@username}", "#{@password}")
  req.add_field('Content-Length','0')
  req.add_field('Content-Type', 'application/vdn.vmware.vCloud.orgList+xml')
  @logger.debug "--- Request-Header:"
  req.each_header do |key, name|
    @logger.debug "#{key}: #{name}"
  end
  res = http_request(url, req)
  @vcloud_token = res.get_fields("Set-Cookie")
  xmlbody = XmlSimple.xml_in(res.body)
  @organization_link = xmlbody["Org"].first["href"]
  return xmlbody
end

#networksObject



88
89
90
91
92
# File 'lib/help/v_cloud_api_handler.rb', line 88

def networks
  @network_links.each() {|network_link|
    generic_get_request(network_link)
  }
end

#orgObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/help/v_cloud_api_handler.rb', line 34

def org
  res = generic_get_request(@organization_link)
  xmlbody = XmlSimple.xml_in(res.body)
  xmlbody["Link"].each() {|info|
    @logger.info "org: found info on #{info.inspect}"
    case info['type']
      when "application/vnd.vmware.vcloud.vdc+xml"
        @vdc_link = info['href']
      when "application/vnd.vmware.vcloud.catalog+xml"
        @catalog_link = info['href']
      when "application/vnd.vmware.vcloud.tasksList+xml"
        @taskslist_link = info['href']
      when "application/vnd.tmrk.vcloudExpress.keysList+xml"
        @keyslist_link = info['href']
      else
        @logger.info "could not identify #{info['type']} for org"
    end
  }
  return xmlbody
end

#prepare_request(request) ⇒ Object

Raises:

  • (Exception)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/help/v_cloud_api_handler.rb', line 127

def prepare_request(request)
  raise Exception.new("no vcloud-token") if @vcloud_token == nil
  request.add_field('Content-Length','0')
  request.add_field('Content-Type', 'application/vdn.vmware.vCloud.orgList+xml')
  request.add_field('Cookie', @vcloud_token)
  
  @logger.debug "--- Request-Header:"
  request.each_header do |key, name|
    @logger.debug "#{key}: #{name}"
  end
end

#server_ip_address(server_ip) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/help/v_cloud_api_handler.rb', line 102

def server_ip_address(server_ip)
  server_link = "https://services.vcloudexpress.terremark.com/api/v0.8a-ext1.6/vapp/#{server_ip}"
  res = generic_get_request(server_link)
  xmlbody = XmlSimple.xml_in(res.body)
  ip_address = xmlbody["NetworkConnectionSection"].first["NetworkConnection"].first["IpAddress"]
  @logger.debug "Ip: #{ip_address}"
  #"NetworkConnectionSection"=>[{"NetworkConnection"=>[{"IpAddress"=>["10.114.117.11"],
  ip_address
end

#v_appsObject



82
83
84
85
86
# File 'lib/help/v_cloud_api_handler.rb', line 82

def v_apps
  @server_links.each() {|server_link|
    generic_get_request(server_link)
  }
end

#vdcObject



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
# File 'lib/help/v_cloud_api_handler.rb', line 55

def vdc
  @server_links = []
  @network_links = []    
  res = generic_get_request(@vdc_link)
  xmlbody = XmlSimple.xml_in(res.body)    
  xmlbody['ResourceEntities'].first['ResourceEntity'].each() do |info|
    @logger.info "vdc: found info on #{info.inspect}"
    case info['type']      
      when "application/vnd.vmware.vcloud.vApp+xml"
        @server_links << info['href']
    else
      @logger.info "could not identify #{info['type']} for vdc"
    end
  end
  @logger.debug "@server_links = #{@server_links.inspect}"
  xmlbody['AvailableNetworks'].first['Network'].each() do |info|
    @logger.debug "vdc: found info on #{info.inspect}"
    case info['type']
      when "application/vnd.vmware.vcloud.network+xml"
        @network_links << info['href']
    else
      @logger.info "could not identify #{info['type']} for vdc"
    end
  end
  @logger.debug "@network_links = #{@network_links.inspect}"
end

#versionsObject



12
13
14
15
# File 'lib/help/v_cloud_api_handler.rb', line 12

def versions
  res = Net::HTTP.get "#{@url_endpoint}", '/api/versions'
  return XmlSimple.xml_in(res)
end