1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[ninefold|compute] Public IPs and Rules.

This commit is contained in:
Lincoln Stoll 2011-05-11 19:47:30 -07:00
parent c42a272fc0
commit 56f1783319
6 changed files with 247 additions and 8 deletions

View file

@ -0,0 +1,102 @@
require 'fog/core/model'
module Fog
module Ninefold
class Compute
class Address < Fog::Model
identity :id
attribute :account
attribute :allocated
attribute :associatednetworkid
attribute :domain
attribute :domainid
attribute :forvirtualnetwork
attribute :ipaddress
attribute :issourcenat
attribute :isstaticnat
attribute :jobid
attribute :jobstatus
attribute :networkid
attribute :state
attribute :virtualmachinedisplayname
attribute :virtualmachineid
attribute :virtualmachinename
attribute :vlanid
attribute :vlanname
attribute :zoneid
attribute :zonename
def initialize(attributes={})
super
end
def destroy
requires :identity
self.jobid = extract_job_id(connection.disassociate_ip_address(:id => identity))
true
end
def enable_static_nat(server)
server.kind_of?(Integer) ? serverid = server : serverid = server.identity
res = connection.enable_static_nat(:virtualmachineid => serverid, :ipaddressid => identity)
reload
to_boolean(res['success'])
end
def disable_static_nat()
self.jobid = extract_job_id(connection.disable_static_nat(:ipaddressid => identity))
true
end
def reload
self.virtualmachinedisplayname = nil
self.virtualmachineid = nil
self.virtualmachinename = nil
super
end
def ready?
if jobid && connection.query_async_job_result(:jobid => jobid)['jobstatus'] == 0
false
else # No running job, we are ready. Refresh data.
reload
true
end
end
def save
requires :zoneid
options = {
:zoneid => zoneid,
:networkid => networkid,
:account => account,
:domainid => domainid
}.delete_if {|k,v| v.nil? || v == "" }
data = connection.associate_ip_address(options)
merge_attributes(data)
true
end
private
def extract_job_id(job)
if job.kind_of? Integer
job
else
job['jobid'] || job['id']
end
end
# needed to hack around API inconsistencies
def to_boolean(val)
val && (val.to_s.match(/(true|t|yes|y|1)$/i) != nil)
end
end
end
end
end

View file

@ -0,0 +1,31 @@
require 'fog/core/collection'
require 'fog/compute/models/ninefold/address'
module Fog
module Ninefold
class Compute
class Addresses < Fog::Collection
model Fog::Ninefold::Compute::Address
def all
data = connection.list_public_ip_addresses
load(data)
end
def get(identifier)
return nil if identifier.nil? || identifier == ""
data = connection.list_public_ip_addresses(:id => identifier)
if data.empty?
nil
else
new(data[0])
end
end
end
end
end
end

View file

@ -0,0 +1,78 @@
require 'fog/core/model'
module Fog
module Ninefold
class Compute
class IpForwardingRule < Fog::Model
identity :id
attribute :protocol
attribute :virtualmachineid
attribute :virtualmachinename
attribute :ipaddressid
attribute :ipaddress
attribute :startport
attribute :endport
attribute :state
attribute :jobid
def initialize(attributes={})
super
end
def destroy
requires :identity
self.jobid = extract_job_id(connection.delete_ip_forwarding_rule(:id => identity))
true
end
def ready?
if jobid && connection.query_async_job_result(:jobid => jobid)['jobstatus'] == 0
false
else # No running job, we are ready. Refresh data.
reload
true
end
end
def address
Ninefold.address.get(ipaddressid)
end
def address=(addr)
self.ipaddressid = addr.identity
end
def save
requires :ipaddressid
requires :protocol
requires :startport
options = {
:ipaddressid => ipaddressid,
:protocol => protocol,
:startport => startport,
:endport => endport
}.delete_if {|k,v| v.nil? || v == "" }
data = connection.create_ip_forwarding_rule(options)
merge_attributes(data)
true
end
private
def extract_job_id(job)
if job.kind_of? Integer
job
else
job['jobid'] || job['id']
end
end
end
end
end
end

View file

@ -0,0 +1,31 @@
require 'fog/core/collection'
require 'fog/compute/models/ninefold/ip_forwarding_rule'
module Fog
module Ninefold
class Compute
class IpForwardingRules < Fog::Collection
model Fog::Ninefold::Compute::IpForwardingRule
def all
data = connection.list_ip_forwarding_rules
load(data)
end
def get(identifier)
return nil if identifier.nil? || identifier == ""
data = connection.list_ip_forwarding_rules(:id => identifier)
if data.empty?
nil
else
new(data[0])
end
end
end
end
end
end

View file

@ -117,6 +117,7 @@ module Fog
else
# update with new values.
merge_attributes(res['jobresult']['virtualmachine'])
true
end
else # No running job, we are ready. Refresh data.
reload

View file

@ -14,14 +14,10 @@ module Fog
collection :flavors
model :image
collection :images
#collection :load_balancers
#model :load_balancer
#collection :zones
#model :zone
#collection :cloud_ips
#model :cloud_ip
#collection :users
#model :user
model :address
collection :addresses
model :ip_forwarding_rule
collection :ip_forwarding_rules
request_path 'fog/compute/requests/ninefold'
# General list-only stuff