From 2fc97b86dbd377177221481329f8052fc6f86d68 Mon Sep 17 00:00:00 2001 From: sx-h9030 Date: Fri, 23 Oct 2015 16:04:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?eip=5Faddress.rb=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/fog/aliyun/models/compute/eip_address.rb | 27 ++++--- .../requests/compute/associate_eip_address.rb | 9 ++- .../compute/disassociate_eip_address.rb | 74 +++++++++++++++++++ 3 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 lib/fog/aliyun/requests/compute/disassociate_eip_address.rb diff --git a/lib/fog/aliyun/models/compute/eip_address.rb b/lib/fog/aliyun/models/compute/eip_address.rb index 00f2672..d603b07 100644 --- a/lib/fog/aliyun/models/compute/eip_address.rb +++ b/lib/fog/aliyun/models/compute/eip_address.rb @@ -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 diff --git a/lib/fog/aliyun/requests/compute/associate_eip_address.rb b/lib/fog/aliyun/requests/compute/associate_eip_address.rb index 4eaaec6..5075a1c 100644 --- a/lib/fog/aliyun/requests/compute/associate_eip_address.rb +++ b/lib/fog/aliyun/requests/compute/associate_eip_address.rb @@ -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) @@ -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 diff --git a/lib/fog/aliyun/requests/compute/disassociate_eip_address.rb b/lib/fog/aliyun/requests/compute/disassociate_eip_address.rb new file mode 100644 index 0000000..75e770e --- /dev/null +++ b/lib/fog/aliyun/requests/compute/disassociate_eip_address.rb @@ -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 + From 80132d6e732bb1a56e0c1710319842f116f6819f Mon Sep 17 00:00:00 2001 From: sx-h9030 Date: Fri, 23 Oct 2015 16:16:12 +0800 Subject: [PATCH 2/2] complete eip_address.rb --- lib/fog/aliyun/models/compute/eip_address.rb | 27 ++++--- .../requests/compute/associate_eip_address.rb | 9 ++- .../compute/disassociate_eip_address.rb | 74 +++++++++++++++++++ 3 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 lib/fog/aliyun/requests/compute/disassociate_eip_address.rb diff --git a/lib/fog/aliyun/models/compute/eip_address.rb b/lib/fog/aliyun/models/compute/eip_address.rb index 00f2672..d603b07 100644 --- a/lib/fog/aliyun/models/compute/eip_address.rb +++ b/lib/fog/aliyun/models/compute/eip_address.rb @@ -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 diff --git a/lib/fog/aliyun/requests/compute/associate_eip_address.rb b/lib/fog/aliyun/requests/compute/associate_eip_address.rb index 4eaaec6..5075a1c 100644 --- a/lib/fog/aliyun/requests/compute/associate_eip_address.rb +++ b/lib/fog/aliyun/requests/compute/associate_eip_address.rb @@ -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) @@ -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 diff --git a/lib/fog/aliyun/requests/compute/disassociate_eip_address.rb b/lib/fog/aliyun/requests/compute/disassociate_eip_address.rb new file mode 100644 index 0000000..75e770e --- /dev/null +++ b/lib/fog/aliyun/requests/compute/disassociate_eip_address.rb @@ -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 +