Skip to content

Commit

Permalink
Merge pull request #3 from janehyt/master
Browse files Browse the repository at this point in the history
complete eip_address model
  • Loading branch information
dengqinsi committed Oct 23, 2015
2 parents 7b46930 + 833fb48 commit c7732a0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
27 changes: 18 additions & 9 deletions lib/fog/aliyun/models/compute/eip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@ def save(options={})
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
def associate(new_server,options={})
unless persisted?
@server = new_server
else
@server = nil
self.server_id = new_server.id
service.associate_eip_address(server_id,allocation_id,options)
end
end

def disassociate(new_server,options={})
@server = nil
self.server_id = new_server.id
if persisted?
service.disassociate_address(server_id,allocation_id,options)
end

end

end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/fog/aliyun/requests/compute/associate_eip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class Real
# * 'RequestId'<~String> - Id of the request
#
# {Aliyun API Reference}[https://docs.aliyun.com/?spm=5176.100054.201.106.DGkmH7#/pub/ecs/open-api/network&associateeipaddresss]
def associate_eip_address(server_id, allocationId)
def associate_eip_address(server_id, allocationId,options={})

_action = 'AssociateEipAddress'
_sigNonce = randonStr()
_time = Time.new.utc

type=options['instance_type']

_parameters = defalutParameters(_action, _sigNonce, _time)
_pathURL = defaultAliyunUri(_action, _sigNonce, _time)

Expand All @@ -27,6 +29,11 @@ def associate_eip_address(server_id, allocationId)

_parameters['AllocationId'] = allocationId
_pathURL += '&AllocationId='+allocationId

if type
_parameters['InstanceType'] = type
_pathURL += 'InstanceType='+type
end

_signature = sign(@aliyun_accesskey_secret, _parameters)
_pathURL += '&Signature='+_signature
Expand Down
74 changes: 74 additions & 0 deletions lib/fog/aliyun/requests/compute/disassociate_eip_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module Fog
module Compute
class Aliyun
class Real
# Disassociate an avalable eip IP address to the given instance.
#
# ==== Parameters
# * server_id<~String> - id of the instance
# * allocationId<~String> - id of the EIP
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'RequestId'<~String> - Id of the request
#
# {Aliyun API Reference}[https://docs.aliyun.com/?spm=5176.100054.201.106.DGkmH7#/pub/ecs/open-api/network&associateeipaddresss]
def associate_eip_address(server_id, allocationId,options={})

_action = 'UnassociateEipAddress'
_sigNonce = randonStr()
_time = Time.new.utc

type=options['instance_type']

_parameters = defalutParameters(_action, _sigNonce, _time)
_pathURL = defaultAliyunUri(_action, _sigNonce, _time)

_parameters['InstanceId'] = server_id
_pathURL += '&InstanceId='+server_id

_parameters['AllocationId'] = allocationId
_pathURL += '&AllocationId='+allocationId

if type
_parameters['InstanceType'] = type
_pathURL += 'InstanceType='+type
end

_signature = sign(@aliyun_accesskey_secret, _parameters)
_pathURL += '&Signature='+_signature

request(
:expects => [200, 204],
:method => 'GET',
:path => _pathURL
)
end
end

class Mock
def allocate_address(pool = nil)
response = Excon::Response.new
response.status = 200
response.headers = {
"X-Compute-Request-Id" => "req-d4a21158-a86c-44a6-983a-e25645907f26",
"Content-Type" => "application/json",
"Content-Length" => "105",
"Date"=> Date.new
}
response.body = {
"floating_ip" => {
"instance_id" => nil,
"ip" => "192.168.27.132",
"fixed_ip" => nil,
"id" => 4,
"pool"=>"nova"
}
}
response
end
end # mock
end # aliyun
end #compute
end

0 comments on commit c7732a0

Please sign in to comment.