Skip to content

Commit

Permalink
Compute support new model
Browse files Browse the repository at this point in the history
  • Loading branch information
dengqinsi committed Oct 22, 2015
1 parent efc98f4 commit 7b46930
Show file tree
Hide file tree
Showing 27 changed files with 808 additions and 25 deletions.
2 changes: 1 addition & 1 deletion fog-aliyun.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |spec|

spec.summary = %q{Fog provider for Aliyun Web Services.}
spec.description = %q{As a FOG provider, fog-aliyun support aliyun OSS/ECS. It will support more aliyun services later.}
spec.homepage = "https://github.com/dtdream/fog-aliyun.git"
spec.homepage = "https://github.com/fog/fog-aliyun.git"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
Expand Down
20 changes: 20 additions & 0 deletions lib/fog/aliyun/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ class Aliyun < Fog::Service
## MODELS
#
model_path 'fog/aliyun/models/compute'
model :server
collection :servers
model :image
collection :images
model :eip_address
collection :eip_addresses
model :security_group
collection :security_groups
model :security_group_rule
collection :security_group_rules
model :volume
collection :volumes
model :snapshot
Expand All @@ -20,6 +28,12 @@ class Aliyun < Fog::Service
collection :vpcs
model :vswitch
collection :vswitches
model :vrouter
collection :vrouters
model :route_table
collection :route_tables
model :route_entry
collection :route_entrys

## REQUESTS
#
Expand Down Expand Up @@ -86,6 +100,12 @@ class Aliyun < Fog::Service
request :modify_vpc
request :modify_vswitch

#VRouter
request :list_vrouters

#RouteTable
request :list_route_tables


#clouddisk
request :list_disks
Expand Down
54 changes: 54 additions & 0 deletions lib/fog/aliyun/models/compute/eip_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'fog/core/model'
module Fog
module Compute
class Aliyun
class EipAddress < Fog::Model

identity :id, :aliases => 'AllocationId'

attribute :allocated_at, :aliases => 'AllocationTime'
attribute :bandwidth, :aliases => 'Bandwidth'
attribute :server_id, :aliases => 'InstanceId'
attribute :charge_type, :aliases => 'InternetChargeType'
attribute :ip_address, :aliases => ['IpAddress','EipAddress']
attribute :opertion_locks,:aliases => 'OperationLocks'
attribute :region_id, :aliases => 'RegionId'
attribute :state, :aliases => 'Status'

def destroy
requires :id
service.release_eip_address(id)
true
end

def ready?
requires :state
state == 'Available'
end

def save(options={})
# raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
# requires :availability_zone
options[:bandwidth] = bandwidth if bandwidth
options[:internet_charge_type]=charge_type if charge_type

data = Fog::JSON.decode(service.allocate_eip_address(options).body)
merge_attributes(data)
true
end


# def associate(new_server)
# unless persisted?
# @server = new_server
# else
# @server = nil
# self.server_id = new_server.id
# service.associate_address(server_id, public_ip, network_interface_id, allocation_id)
# end
# end

end
end
end
end
30 changes: 30 additions & 0 deletions lib/fog/aliyun/models/compute/eip_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'fog/core/collection'
require 'fog/aliyun/models/compute/eip_address'

module Fog
module Compute
class Aliyun
class EipAddresses < Fog::Collection

model Fog::Compute::Aliyun::EipAddress

def all(filters_arg = {})
data = Fog::JSON.decode(service.list_eip_addresses(filters_arg).body)['EipAddresses']['EipAddress']
load(data)
# load(data['volumeSet'])
# if server
# self.replace(self.select {|volume| volume.server_id == server.id})
# end
# self
end

def get(allocation_id)
if allocation_id
self.class.new(:service => service).all(:allocation_id => allocation_)[0]
end
end

end
end
end
end
28 changes: 28 additions & 0 deletions lib/fog/aliyun/models/compute/route_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'fog/core/model'
module Fog
module Compute
class Aliyun
class RouteEntry < Fog::Model
# "RouteTables"=>{"RouteTable"=>[
# {"CreationTime"=>"2015-08-03T11:23:35Z", "RouteEntrys"=>{"RouteEntry"=>[
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"172.16.0.0/24"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"172.16.1.0/24"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"172.16.2.0/24"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"100.64.0.0/10"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"10.0.0.0/8"}]},
# "RouteTableId"=>"vtb-2504onoxh", "RouteTableType"=>"System", "VRouterId"=>"vrt-25azmd2wm"}]}
identity :cidr_block, :aliases => 'DestinationCidrBlock'
attribute :state, :aliases => 'Status'
attribute :server_id, :aliases => 'InstanceId'
attribute :type, :aliases => 'Type'
attribute :route_table_id, :aliases => 'RouteTableId'

# def save
# requires :cidr_block,:route_table_id
# if(cidr_block)


end
end
end
end
41 changes: 41 additions & 0 deletions lib/fog/aliyun/models/compute/route_entrys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'fog/core/collection'
require 'fog/aliyun/models/compute/route_entry'

module Fog
module Compute
class Aliyun
class RouteEntrys < Fog::Collection
attribute :route_table

model Fog::Compute::Aliyun::RouteEntry

def all(options={})
requires :route_table
options[:routeTableId]=route_table.id
data = Fog::JSON.decode(service.list_route_tables(route_table.v_router_id, options).body)['RouteTables']['RouteTable'][0]["RouteEntrys"]["RouteEntry"]
load(data)
end

def get(cidr_block)
requires :route_table
data=self.class.new(:service=>service,:route_table=>route_table).all()
result=nil
data.each do |i|
if i.cidr_block==cidr_block
result=i
break
end
end
result
end

# def get(routeTableId)
# requires :v_router
# if routeTableId
# self.class.new(:service => service,:v_router=>v_router).all(:routeTableId=>routeTableId)[0]
# end
# end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/fog/aliyun/models/compute/route_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'fog/core/model'
module Fog
module Compute
class Aliyun
class RouteTable < Fog::Model
# "RouteTables"=>{"RouteTable"=>[
# {"CreationTime"=>"2015-08-03T11:23:35Z", "RouteEntrys"=>{"RouteEntry"=>[
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"172.16.0.0/24"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"172.16.1.0/24"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"172.16.2.0/24"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"100.64.0.0/10"},
# {"Status"=>"Available", "Type"=>"System", "InstanceId"=>"", "RouteTableId"=>"vtb-2504onoxh", "DestinationCidrBlock"=>"10.0.0.0/8"}]},
# "RouteTableId"=>"vtb-2504onoxh", "RouteTableType"=>"System", "VRouterId"=>"vrt-25azmd2wm"}]}
identity :id, :aliases => 'RouteTableId'
attribute :created_at, :aliases => 'CreationTime'
attribute :type, :aliases => 'RouteTableType'
attribute :v_router_id, :aliases => 'VRouterId'
# collection Fog::Compute::Aliyun::RouteEntrys
def route_entrys
@route_entrys ||= begin
Fog::Compute::Aliyun::RouteEntrys.new(
:route_table => self,
:service => service
)
end
end


end
end
end
end
43 changes: 43 additions & 0 deletions lib/fog/aliyun/models/compute/route_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'fog/core/collection'
require 'fog/aliyun/models/compute/route_table'

module Fog
module Compute
class Aliyun
class RouteTables < Fog::Collection
attribute :v_router

model Fog::Compute::Aliyun::RouteTable


def all(options={})
requires :v_router
data = Fog::JSON.decode(service.list_route_tables(v_router.id, options).body)['RouteTables']['RouteTable']
load(data)
end

# Used to retrieve a VPC
# vpc_id is required to get the associated VPC information.
#
# You can run the following command to get the details:
# Aliyun.vpcs.get("vpc-12345678")
#
# ==== Returns
#
#>> Aliyun.vpcs.get("vpc-12345678")
# <Fog::Aliyun::Compute::VPC
# id="vpc-12345678",
# TODO
# >
#

def get(routeTableId)
requires :v_router
if routeTableId
self.class.new(:service => service,:v_router=>v_router).all(:routeTableId=>routeTableId)[0]
end
end
end
end
end
end
86 changes: 86 additions & 0 deletions lib/fog/aliyun/models/compute/security_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require 'fog/core/model'

module Fog
module Compute
class Aliyun
class SecurityGroup < Fog::Model
# {"SecurityGroup"=>[{"CreationTime"=>"2015-08-10T03:21:02Z",
# "SecurityGroupId"=>"sg-25mexinlu", "Description"=>"", "SecurityGroupName"=>"bosh",
# "VpcId"=>"vpc-25mj6mguq"}}

identity :id, :aliases =>"SecurityGroupId"

attribute :name, :aliases => "SecurityGroupName"
attribute :description, :aliases => "Description"
attribute :created_at, :aliases => "CreationTime"
attribute :vpc_id, :aliases => "VpcId"

def vpc
requires :vpc_id
$vpc=Fog::Compute::Aliyun::Vpcs.new(:service=>service).all('vpcId'=>vpc_id)[0]
end

def security_group_rules
requires :id
Fog::Compute::Aliyun::SecurityGroupRules.new(:service=>service).get(id)
end

def save(options={})
options[:vpcId] = vpc_id if vpc_id
options[:name] = name if name
options[:description] = description if description
data = Fog::JSON.decode(service.create_security_group(options).body)
true
end


def destroy
requires :id
service.delete_security_group(id)
true
end

# def security_group_rules
# Fog::Compute::OpenStack::SecurityGroupRules.new(:service => service).load(attributes[:security_group_rules])
# end

# def rules
# Fog::Logger.deprecation('#rules is deprecated. Use #security_group_rules instead')
# attributes[:security_group_rules]
# end

# # no one should be calling this because it doesn't do anything
# # useful but we deprecated the rules attribute and need to maintain the API
# def rules=(new_rules)
# Fog::Logger.deprecation('#rules= is deprecated. Use the Fog::Compute::Openstack::SecurityGroupRules collection to create new rules.')
# attributes[:security_group_rules] = new_rules
# end

# def save
# requires :name, :description
# data = service.create_security_group(name, description)
# merge_attributes(data.body['security_group'])
# true
# end

# def destroy
# requires :id
# service.delete_security_group(id)
# true
# end

# def create_security_group_rule(min, max, ip_protocol = "tcp", cidr = "0.0.0.0/0", group_id = nil)
# Fog::Logger.deprecation('#create_security_group_rule is deprecated. Use the Fog::Compute::Openstack::SecurityGroupRules collection to create new rules.')
# requires :id
# service.create_security_group_rule(id, ip_protocol, min, max, cidr, group_id)
# end

# def delete_security_group_rule(rule_id)
# Fog::Logger.deprecation('#create_security_group_rule is deprecated. Use the Fog::Compute::Openstack::SecurityGroupRule objects to destroy rules.')
# service.delete_security_group_rule(rule_id)
# true
# end
end
end
end
end
Loading

0 comments on commit 7b46930

Please sign in to comment.