From 56f17833199b2c8457b7c23331754dc79c6bbf8b Mon Sep 17 00:00:00 2001 From: Lincoln Stoll Date: Wed, 11 May 2011 19:47:30 -0700 Subject: [PATCH] [ninefold|compute] Public IPs and Rules. --- lib/fog/compute/models/ninefold/address.rb | 102 ++++++++++++++++++ lib/fog/compute/models/ninefold/addresses.rb | 31 ++++++ .../models/ninefold/ip_forwarding_rule.rb | 78 ++++++++++++++ .../models/ninefold/ip_forwarding_rules.rb | 31 ++++++ lib/fog/compute/models/ninefold/server.rb | 1 + lib/fog/compute/ninefold.rb | 12 +-- 6 files changed, 247 insertions(+), 8 deletions(-) create mode 100644 lib/fog/compute/models/ninefold/address.rb create mode 100644 lib/fog/compute/models/ninefold/addresses.rb create mode 100644 lib/fog/compute/models/ninefold/ip_forwarding_rule.rb create mode 100644 lib/fog/compute/models/ninefold/ip_forwarding_rules.rb diff --git a/lib/fog/compute/models/ninefold/address.rb b/lib/fog/compute/models/ninefold/address.rb new file mode 100644 index 000000000..51d5f69da --- /dev/null +++ b/lib/fog/compute/models/ninefold/address.rb @@ -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 diff --git a/lib/fog/compute/models/ninefold/addresses.rb b/lib/fog/compute/models/ninefold/addresses.rb new file mode 100644 index 000000000..9e23ca816 --- /dev/null +++ b/lib/fog/compute/models/ninefold/addresses.rb @@ -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 diff --git a/lib/fog/compute/models/ninefold/ip_forwarding_rule.rb b/lib/fog/compute/models/ninefold/ip_forwarding_rule.rb new file mode 100644 index 000000000..b6f47f86d --- /dev/null +++ b/lib/fog/compute/models/ninefold/ip_forwarding_rule.rb @@ -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 diff --git a/lib/fog/compute/models/ninefold/ip_forwarding_rules.rb b/lib/fog/compute/models/ninefold/ip_forwarding_rules.rb new file mode 100644 index 000000000..8746f8ac6 --- /dev/null +++ b/lib/fog/compute/models/ninefold/ip_forwarding_rules.rb @@ -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 diff --git a/lib/fog/compute/models/ninefold/server.rb b/lib/fog/compute/models/ninefold/server.rb index 9bf945dd4..e999b6064 100644 --- a/lib/fog/compute/models/ninefold/server.rb +++ b/lib/fog/compute/models/ninefold/server.rb @@ -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 diff --git a/lib/fog/compute/ninefold.rb b/lib/fog/compute/ninefold.rb index a08acb89d..077fa5d40 100644 --- a/lib/fog/compute/ninefold.rb +++ b/lib/fog/compute/ninefold.rb @@ -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