mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Add support to create, delete internet services
This commit is contained in:
parent
07f4267d8e
commit
6c538a0a85
15 changed files with 158 additions and 60 deletions
|
@ -9,30 +9,62 @@ module Fog
|
|||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :image
|
||||
attribute :sshkeyFingerPrint
|
||||
attribute :status
|
||||
attribute :OperatingSystem
|
||||
attribute :VirtualHardware
|
||||
attribute :IpAddress
|
||||
attribute :PublicIpAddress
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
data = connection.power_off(id).body
|
||||
task = connection.tasks.new(data)
|
||||
task.wait_for { ready? }
|
||||
connection.delete_vapp(id)
|
||||
case self.status
|
||||
when "0"
|
||||
return false
|
||||
when "4"
|
||||
data = connection.power_off(self.id).body
|
||||
wait_for { off? }
|
||||
end
|
||||
connection.delete_vapp(self.id)
|
||||
true
|
||||
end
|
||||
|
||||
def delete_internet_services
|
||||
|
||||
#Find the internet service
|
||||
services = connection.get_internet_services(connection.default_vdc_id)
|
||||
internet_info = services.body["InternetServices"].find {|item| item["Name"] == self.name}
|
||||
|
||||
#Delete all the associated nodes
|
||||
if internet_info
|
||||
nodes = connection.get_node_services(internet_info["Id"]).body["NodeServices"]
|
||||
nodes.select { |item| connection.delete_node_service(item["Id"]) }
|
||||
|
||||
#Clear out the services
|
||||
connection.delete_internet_service(internet_info["Id"])
|
||||
|
||||
#Release IP Address
|
||||
connection.delete_public_ip(internet_info["PublicIpAddress"]["Id"])
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# { '0' => 'Being created', '2' => 'Powered Off', '4' => 'Powered On'}
|
||||
def ready?
|
||||
status == '2'
|
||||
state = connection.get_vapp(id).body["status"]
|
||||
puts " id : #{id}, state : #{state}"
|
||||
state == '2'
|
||||
end
|
||||
|
||||
def on?
|
||||
status == '4'
|
||||
state = connection.get_vapp(id).body["status"]
|
||||
puts " id : #{id}, state : #{state}"
|
||||
state == '4'
|
||||
end
|
||||
|
||||
def off?
|
||||
status == '2'
|
||||
state = connection.get_vapp(id).body["status"]
|
||||
state == '2'
|
||||
end
|
||||
|
||||
def power_on(options = {})
|
||||
|
@ -81,14 +113,34 @@ module Fog
|
|||
power_on
|
||||
end
|
||||
|
||||
def create_internet_service(protocol="TCP", port="22")
|
||||
data = connection.create_internet_service(
|
||||
vdc = connection.default_vdc_id,
|
||||
name = self.name,
|
||||
protocol = protocol,
|
||||
port = port,
|
||||
options = {'Enabled' => 'true',
|
||||
"Description" => :name
|
||||
}
|
||||
)
|
||||
merge_attributes(data.body)
|
||||
ssh_service = data.body["Id"]
|
||||
data = connection.add_node_service(
|
||||
service_id = ssh_service,
|
||||
ip = self.IpAddress,
|
||||
name = self.name,
|
||||
port = port,
|
||||
options = {'Enabled' => 'true',
|
||||
"Description" => :name
|
||||
}
|
||||
)
|
||||
true
|
||||
end
|
||||
|
||||
def save
|
||||
requires :name
|
||||
data = connection.instantiate_vapp(name)
|
||||
data = connection.instantiate_vapp_template(server_name=name, vapp_template=image, options={'ssh_key_fingerprint'=>sshkeyFingerPrint})
|
||||
merge_attributes(data.body)
|
||||
task = connection.deploy_vapp(id)
|
||||
task.wait_for { ready? }
|
||||
task = connection.power_on(id)
|
||||
task.wait_for { ready? }
|
||||
true
|
||||
end
|
||||
|
||||
|
@ -100,7 +152,6 @@ module Fog
|
|||
|
||||
def type=(new_type); @type = new_type; end
|
||||
def size=(new_size); @size = new_size; end
|
||||
def IpAddress=(new_ipaddress); @IpAddress = new_ipaddress; end
|
||||
def Links=(new_links); @Links = new_links; end
|
||||
|
||||
end
|
||||
|
|
|
@ -22,15 +22,30 @@ module Fog
|
|||
model Fog::Terremark::Shared::Server
|
||||
|
||||
def all
|
||||
data = connection.get_vdc(vdc_id).body['ResourceEntities'].select do |entity|
|
||||
entity['type'] == 'application/vnd.vmware.vcloud.vApp+xml'
|
||||
data = []
|
||||
connection.get_vdc(vdc_id).body['ResourceEntities'].select do |entity|
|
||||
data << connection.servers.get(entity["href"].split('/').last)
|
||||
end
|
||||
load(data)
|
||||
data
|
||||
end
|
||||
|
||||
def get(server_id)
|
||||
if server_id && server = connection.get_vapp(server_id).body
|
||||
new(server)
|
||||
server = new(server)
|
||||
|
||||
#Find the Public IP Address
|
||||
#Identify Public IP address by matching Internet Service name with Server Name
|
||||
services = connection.get_internet_services(connection.default_vdc_id)
|
||||
internet_info = services.body["InternetServices"].find {|item| item["Name"] == server.name}
|
||||
|
||||
if internet_info
|
||||
nodes = connection.get_node_services(internet_info["Id"])
|
||||
if nodes.body["NodeServices"].find{|item| item["IpAddress"] == server.IpAddress }
|
||||
server.PublicIpAddress = internet_info["PublicIpAddress"]["Name"]
|
||||
end
|
||||
end
|
||||
|
||||
server
|
||||
elsif !server_id
|
||||
nil
|
||||
end
|
||||
|
|
|
@ -9,7 +9,9 @@ module Fog
|
|||
identity :id
|
||||
|
||||
attribute :name
|
||||
|
||||
attribute :ResourceEntities
|
||||
attribute :AvailableNetworks
|
||||
attribute :links
|
||||
def networks
|
||||
connection.networks(:vdc_id => id)
|
||||
end
|
||||
|
|
|
@ -22,9 +22,6 @@ module Fog
|
|||
end
|
||||
catalog_item["id"] = catalog_item["href"].split('/').last
|
||||
end
|
||||
# until attributes.empty?
|
||||
# catalog_item[attributes.shift] = attributes.shift
|
||||
# end
|
||||
@response['CatalogItems'] << catalog_item
|
||||
when 'Catalog'
|
||||
catalog = {}
|
||||
|
|
|
@ -30,23 +30,21 @@ module Fog
|
|||
unless options.has_key?('Enabled')
|
||||
options['Enabled'] = true
|
||||
end
|
||||
|
||||
data = <<-DATA
|
||||
<NodeService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:tmrk:vCloudExpress-1.0:request:createNodeService">
|
||||
<IpAddress>#{ip}</IpAddress>
|
||||
<Name>#{name}</Name>
|
||||
<Port>#{port}</Port>
|
||||
<Enabled>#{options['Enabled']}</Enabled>
|
||||
<Description>#{options['Description']}</Description>
|
||||
</NodeService>
|
||||
DATA
|
||||
request(
|
||||
<CreateNodeServiceRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:tmrk:vCloudExpressExtensions-1.6"><IpAddress>#{ip}</IpAddress><Name>#{name}</Name><Port>#{port}</Port><Enabled>#{options['Enabled']}</Enabled><Description>#{options['Description']}</Description></CreateNodeServiceRequest>
|
||||
DATA
|
||||
response = request(
|
||||
:body => data,
|
||||
:expects => 200,
|
||||
:headers => {'Content-Type' => 'application/xml'},
|
||||
:headers => {'Content-Type' => 'application/vnd.tmrk.vCloud.nodeService+xml'},
|
||||
:method => 'POST',
|
||||
:parser => Fog::Parsers::Terremark::Shared::InternetService.new,
|
||||
:path => "internetServices/#{service_id}/nodes"
|
||||
:parser => Fog::Parsers::Terremark::Shared::NodeService.new,
|
||||
:path => "api/extensions/v1.6/internetService/#{service_id}/nodeServices",
|
||||
:override_path => true
|
||||
)
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,6 +2,7 @@ module Fog
|
|||
module Terremark
|
||||
module Shared
|
||||
module Real
|
||||
include Common
|
||||
|
||||
# Reserve requested resources and deploy vApp
|
||||
#
|
||||
|
@ -30,23 +31,30 @@ module Fog
|
|||
unless options.has_key?('Enabled')
|
||||
options['Enabled'] = true
|
||||
end
|
||||
org_path = @path
|
||||
#Sample: "https://services.vcloudexpress.terremark.com/api/extensions/v1.6/vdc/3142/internetServices"
|
||||
path = vdcs.get(vdc_id).links.find { |item| item['name'] == 'Internet Services'}['href'].split(@host)[1]
|
||||
@path = path
|
||||
puts "PATH =#{path}"
|
||||
puts "PATHA = #{@path}"
|
||||
data = <<-DATA
|
||||
<InternetService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:tmrk:vCloudExpress-1.0:request:createInternetService">
|
||||
<Name>#{name}</Name>
|
||||
<Protocol>#{protocol.upcase}</Protocol>
|
||||
<Port>#{port}</Port>
|
||||
<Enabled>#{options['Enabled']}</Enabled>
|
||||
<Description>#{options['Description']}</Description>
|
||||
</InternetService>
|
||||
DATA
|
||||
request(
|
||||
<CreateInternetServiceRequest xml:lang="en" xmlns="urn:tmrk:vCloudExpressExtensions-1.6" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<Name>#{name}</Name>
|
||||
<Protocol>#{protocol.upcase}</Protocol>
|
||||
<Port>#{port}</Port>
|
||||
<Enabled>#{options['Enabled']}</Enabled>
|
||||
<Description>#{options['Description']}</Description>
|
||||
</CreateInternetServiceRequest>
|
||||
DATA
|
||||
response = request(
|
||||
:body => data,
|
||||
:expects => 200,
|
||||
:headers => {'Content-Type' => 'application/xml'},
|
||||
:headers => {'Content-Type' => 'application/vnd.tmrk.vCloud.internetService+xml'},
|
||||
:method => 'POST',
|
||||
:parser => Fog::Parsers::Terremark::Shared::InternetService.new,
|
||||
:path => "vdc/#{vdc_id}/internetServices"
|
||||
:parser => Fog::Parsers::Terremark::Shared::InternetService.new
|
||||
)
|
||||
@path = org_path
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -12,7 +12,8 @@ module Fog
|
|||
request(
|
||||
:expects => 200,
|
||||
:method => 'DELETE',
|
||||
:path => "InternetServices/#{internet_service_id}"
|
||||
:path => "api/extensions/v1.6/internetService/#{internet_service_id}",
|
||||
:override_path => true
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -12,7 +12,8 @@ module Fog
|
|||
request(
|
||||
:expects => 200,
|
||||
:method => 'DELETE',
|
||||
:path => "nodeServices/#{node_service_id}"
|
||||
:path => "api/extensions/v1.6/nodeService/#{node_service_id}",
|
||||
:override_path => true
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -12,7 +12,8 @@ module Fog
|
|||
request(
|
||||
:expects => 200,
|
||||
:method => 'DELETE',
|
||||
:path => "publicIps/#{public_ip_id}"
|
||||
:path => "api/extensions/v1.6/publicIp/#{public_ip_id}",
|
||||
:override_path => true
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -25,7 +25,8 @@ module Fog
|
|||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Terremark::Shared::GetInternetServices.new,
|
||||
:path => "vdc/#{vdc_id}/internetServices"
|
||||
:path => "api/extensions/v1.6/vdc/#{vdc_id}/internetServices",
|
||||
:override_path => true
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -19,16 +19,13 @@ module Fog
|
|||
# * 'rel'<~String> - action to perform
|
||||
# * 'type'<~String> - type of link
|
||||
def get_keys_list(organization_id)
|
||||
org_path = @path
|
||||
@path="/api/extensions/v1.6/"
|
||||
response = request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Terremark::Shared::GetKeysList.new,
|
||||
:path => "org/#{organization_id}/keys"
|
||||
:path => "api/extensions/v1.6/org/#{organization_id}/keys",
|
||||
:override_path => true
|
||||
)
|
||||
#Restore original path
|
||||
@path = org_path
|
||||
response
|
||||
end
|
||||
|
||||
|
|
|
@ -20,7 +20,8 @@ module Fog
|
|||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Terremark::Shared::GetNodeServices.new,
|
||||
:path => "InternetServices/#{service_id}/nodes"
|
||||
:path => "api/extensions/v1.6/internetService/#{service_id}/nodeServices",
|
||||
:override_path => true
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -23,21 +23,27 @@ module Fog
|
|||
unless name.length < 15
|
||||
raise ArgumentError.new('Name must be fewer than 15 characters')
|
||||
end
|
||||
unless options['ssh_key_fingerprint']
|
||||
raise ArgumentError.new("SSH Key Fingerprint is a compulsary parameter")
|
||||
unless vapp_template
|
||||
raise ArgumentError.new("vApp Image Template is a compulsary parameter")
|
||||
end
|
||||
options['ssh_key_fingerprint'] ||= default_ssh_key["FingerPrint"]
|
||||
options['cpus'] ||= 1
|
||||
options['memory'] ||= 512
|
||||
options['network_id'] ||= default_network_id
|
||||
options['vdc_id'] ||= default_vdc_id
|
||||
options['primary_dns'] ||= '208.67.222.222'
|
||||
options['secondary_dns'] ||= '208.67.220.220'
|
||||
|
||||
data = <<-DATA
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<InstantiateVAppTemplateParams name="#{name}" xmlns="http://www.vmware.com/vcloud/v0.8" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v0.8 http://services.vcloudexpress.terremark.com/api/v0.8/ns/vcloud.xsd">
|
||||
<VAppTemplate href="#{@scheme}://#{@host}/#{@path}/vAppTemplate/#{vapp_template}" />
|
||||
<InstantiationParams xmlns:vmw="http://www.vmware.com/schema/ovf">
|
||||
<ProductSection xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:q1="http://www.vmware.com/vcloud/v0.8"/>
|
||||
<ProductSection xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:q1="http://www.vmware.com/vcloud/v0.8">
|
||||
<Property xmlns="http://schemas.dmtf.org/ovf/envelope/1" ovf:key="primaryDNS" ovf:value="#{options['primary_dns']}" />
|
||||
<Property xmlns="http://schemas.dmtf.org/ovf/envelope/1" ovf:key="secondaryDNS" ovf:value="#{options['secondary_dns']}" />
|
||||
<Property xmlns="http://schemas.dmtf.org/ovf/envelope/1" ovf:key="sshKeyFingerprint" ovf:value="#{options['ssh_key_fingerprint']}" />
|
||||
</ProductSection>
|
||||
<VirtualHardwareSection xmlns:q1="http://www.vmware.com/vcloud/v0.8">
|
||||
<Item xmlns="http://schemas.dmtf.org/ovf/envelope/1">
|
||||
<InstanceID xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">1</InstanceID>
|
||||
|
|
|
@ -72,6 +72,17 @@ module Fog
|
|||
if @cookie
|
||||
headers.merge!('Cookie' => @cookie)
|
||||
end
|
||||
if params[:path]
|
||||
if params[:override_path] == true
|
||||
path = params[:path]
|
||||
else
|
||||
path = "#{@path}/#{params[:path]}"
|
||||
end
|
||||
else
|
||||
path = "#{@path}"
|
||||
end
|
||||
puts "PATH in do_request: #{path}"
|
||||
puts "HOST in do_request: #{@host}"
|
||||
@connection.request({
|
||||
:body => params[:body],
|
||||
:expects => params[:expects],
|
||||
|
@ -79,7 +90,7 @@ module Fog
|
|||
:host => @host,
|
||||
:method => params[:method],
|
||||
:parser => params[:parser],
|
||||
:path => "#{@path}/#{params[:path]}"
|
||||
:path => path
|
||||
})
|
||||
end
|
||||
|
||||
|
|
|
@ -97,6 +97,14 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def default_ssh_key
|
||||
if default_ssh_key
|
||||
@default_ssh_key ||= begin
|
||||
keys = get_keys_list(default_organization_id).body["Keys"]
|
||||
keys.find { |item| item["IsDefault"] == "true" }
|
||||
end
|
||||
end
|
||||
end
|
||||
class Mock
|
||||
include Fog::Terremark::Shared::Mock
|
||||
include Fog::Terremark::Shared::Parser
|
||||
|
|
Loading…
Add table
Reference in a new issue