From c5fff682c44c1e21c26cc6af9f1396484a9d652c Mon Sep 17 00:00:00 2001 From: Ricky Smith Date: Tue, 26 Feb 2019 15:39:59 -0500 Subject: [PATCH 1/2] Correct bug detecting content type and parsing json responses This is likely a breaking bug fix, since requests will now _correctly_ parse the response body as json and return a hash, instead of returning the body as a string. Fixes #34 --- lib/fog/aliyun/compute.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/fog/aliyun/compute.rb b/lib/fog/aliyun/compute.rb index 3fe11d9..3a92f40 100644 --- a/lib/fog/aliyun/compute.rb +++ b/lib/fog/aliyun/compute.rb @@ -300,13 +300,18 @@ def reload def request(params) begin - response = @connection.request(params.merge(headers: { + headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Auth-Token' => @auth_token - }.merge!(params[:headers] || {}), - path: "#{@path}/#{params[:path]}", - query: params[:query])) + }.merge!(params[:headers] || {}) + + request_params = params.merge( + headers: headers, + path: "#{@path}/#{params[:path]}", + query: params[:query]) + + response = @connection.request(request_params) rescue Excon::Errors::HTTPStatusError => error raise case error when Excon::Errors::NotFound @@ -316,7 +321,7 @@ def request(params) end end - response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type') == 'application/json' + response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type').start_with?('application/json') response end @@ -339,7 +344,7 @@ def VPCrequest(params) end end - response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type') == 'application/json' + response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type').start_with?('application/json') response end From 3acf21541a70e5bd1dd277423f7823a6c525dac2 Mon Sep 17 00:00:00 2001 From: Ricky Smith Date: Tue, 26 Feb 2019 15:53:34 -0500 Subject: [PATCH 2/2] Remove redundant JSON decode calls in model classes --- lib/fog/aliyun/models/compute/eip_address.rb | 2 +- lib/fog/aliyun/models/compute/eip_addresses.rb | 2 +- lib/fog/aliyun/models/compute/image.rb | 2 +- lib/fog/aliyun/models/compute/images.rb | 2 +- lib/fog/aliyun/models/compute/route_entrys.rb | 2 +- lib/fog/aliyun/models/compute/route_tables.rb | 2 +- lib/fog/aliyun/models/compute/security_group.rb | 2 +- lib/fog/aliyun/models/compute/security_group_rules.rb | 2 +- lib/fog/aliyun/models/compute/security_groups.rb | 2 +- lib/fog/aliyun/models/compute/snapshot.rb | 2 +- lib/fog/aliyun/models/compute/snapshots.rb | 2 +- lib/fog/aliyun/models/compute/volume.rb | 4 ++-- lib/fog/aliyun/models/compute/volumes.rb | 2 +- lib/fog/aliyun/models/compute/vpc.rb | 2 +- lib/fog/aliyun/models/compute/vpcs.rb | 2 +- lib/fog/aliyun/models/compute/vrouters.rb | 2 +- lib/fog/aliyun/models/compute/vswitch.rb | 4 ++-- lib/fog/aliyun/models/compute/vswitches.rb | 2 +- 18 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/fog/aliyun/models/compute/eip_address.rb b/lib/fog/aliyun/models/compute/eip_address.rb index a47fe82..999e333 100644 --- a/lib/fog/aliyun/models/compute/eip_address.rb +++ b/lib/fog/aliyun/models/compute/eip_address.rb @@ -33,7 +33,7 @@ def save(options = {}) options[:bandwidth] = bandwidth if bandwidth options[:internet_charge_type] = charge_type if charge_type - data = Fog::JSON.decode(service.allocate_eip_address(options).body) + data = service.allocate_eip_address(options).body merge_attributes(data) true end diff --git a/lib/fog/aliyun/models/compute/eip_addresses.rb b/lib/fog/aliyun/models/compute/eip_addresses.rb index 21cabea..86e52cc 100644 --- a/lib/fog/aliyun/models/compute/eip_addresses.rb +++ b/lib/fog/aliyun/models/compute/eip_addresses.rb @@ -10,7 +10,7 @@ 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'] + data = service.list_eip_addresses(filters_arg).body['EipAddresses']['EipAddress'] load(data) # load(data['volumeSet']) # if server diff --git a/lib/fog/aliyun/models/compute/image.rb b/lib/fog/aliyun/models/compute/image.rb index 7e753c9..cf899d3 100644 --- a/lib/fog/aliyun/models/compute/image.rb +++ b/lib/fog/aliyun/models/compute/image.rb @@ -39,7 +39,7 @@ def save(options = {}) requires :snapshot_id options[:name] = name if name options[:description] = description if description - data = Fog::JSON.decode(service.create_image(snapshot_id, options).body) + data = service.create_image(snapshot_id, options).body merge_attributes(data) true end diff --git a/lib/fog/aliyun/models/compute/images.rb b/lib/fog/aliyun/models/compute/images.rb index 4f065e5..ad4a9fd 100644 --- a/lib/fog/aliyun/models/compute/images.rb +++ b/lib/fog/aliyun/models/compute/images.rb @@ -14,7 +14,7 @@ def all(filters_arg = {}) Fog::Logger.deprecation("all with #{filters_arg.class} param is deprecated, use all('diskIds' => []) instead [light_black](#{caller.first})[/]") filters_arg = { imageId: filters_arg } end - data = Fog::JSON.decode(service.list_images(filters_arg).body)['Images']['Image'] + data = service.list_images(filters_arg).body['Images']['Image'] load(data) end diff --git a/lib/fog/aliyun/models/compute/route_entrys.rb b/lib/fog/aliyun/models/compute/route_entrys.rb index 6f23f6e..a58cdb5 100644 --- a/lib/fog/aliyun/models/compute/route_entrys.rb +++ b/lib/fog/aliyun/models/compute/route_entrys.rb @@ -14,7 +14,7 @@ class RouteEntrys < Fog::Collection 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'] + data = service.list_route_tables(route_table.v_router_id, options).body['RouteTables']['RouteTable'][0]['RouteEntrys']['RouteEntry'] load(data) end diff --git a/lib/fog/aliyun/models/compute/route_tables.rb b/lib/fog/aliyun/models/compute/route_tables.rb index 0ff6783..317f304 100644 --- a/lib/fog/aliyun/models/compute/route_tables.rb +++ b/lib/fog/aliyun/models/compute/route_tables.rb @@ -13,7 +13,7 @@ class RouteTables < Fog::Collection def all(options = {}) requires :v_router - data = Fog::JSON.decode(service.list_route_tables(v_router.id, options).body)['RouteTables']['RouteTable'] + data = service.list_route_tables(v_router.id, options).body['RouteTables']['RouteTable'] load(data) end diff --git a/lib/fog/aliyun/models/compute/security_group.rb b/lib/fog/aliyun/models/compute/security_group.rb index 8e08d26..b1fc1f0 100644 --- a/lib/fog/aliyun/models/compute/security_group.rb +++ b/lib/fog/aliyun/models/compute/security_group.rb @@ -26,7 +26,7 @@ def save(options = {}) options[:vpcId] = vpc_id if vpc_id options[:name] = name if name options[:description] = description if description - Fog::JSON.decode(service.create_security_group(options).body) + service.create_security_group(options).body true end diff --git a/lib/fog/aliyun/models/compute/security_group_rules.rb b/lib/fog/aliyun/models/compute/security_group_rules.rb index f4f7e2a..0d155cb 100644 --- a/lib/fog/aliyun/models/compute/security_group_rules.rb +++ b/lib/fog/aliyun/models/compute/security_group_rules.rb @@ -11,7 +11,7 @@ class SecurityGroupRules < Fog::Collection attribute :security_group_id def get(security_group_id, options = {}) - data = Fog::JSON.decode(service.list_security_group_rules(security_group_id, options).body) + data = service.list_security_group_rules(security_group_id, options).body self.security_group_id = data['SecurityGroupId'] permissions = data['Permissions']['Permission'] permissions.each do |permission| diff --git a/lib/fog/aliyun/models/compute/security_groups.rb b/lib/fog/aliyun/models/compute/security_groups.rb index 2b7db84..8d7f853 100644 --- a/lib/fog/aliyun/models/compute/security_groups.rb +++ b/lib/fog/aliyun/models/compute/security_groups.rb @@ -10,7 +10,7 @@ class SecurityGroups < Fog::Collection model Fog::Compute::Aliyun::SecurityGroup def all(options = {}) - data = Fog::JSON.decode(service.list_security_groups(options).body)['SecurityGroups']['SecurityGroup'] + data = service.list_security_groups(options).body['SecurityGroups']['SecurityGroup'] load(data) # ['Images']['Image'] end diff --git a/lib/fog/aliyun/models/compute/snapshot.rb b/lib/fog/aliyun/models/compute/snapshot.rb index 0be1f57..0772d29 100644 --- a/lib/fog/aliyun/models/compute/snapshot.rb +++ b/lib/fog/aliyun/models/compute/snapshot.rb @@ -33,7 +33,7 @@ def save(options = {}) requires :volume_id options[:name] = name if name options[:description] = description if description - data = Fog::JSON.decode(service.create_snapshot(volume_id, options).body) + data = service.create_snapshot(volume_id, options).body merge_attributes(data) true end diff --git a/lib/fog/aliyun/models/compute/snapshots.rb b/lib/fog/aliyun/models/compute/snapshots.rb index 33d5b37..09ee64c 100644 --- a/lib/fog/aliyun/models/compute/snapshots.rb +++ b/lib/fog/aliyun/models/compute/snapshots.rb @@ -24,7 +24,7 @@ def all(filters_arg = {}) volume_type = filters_arg[:volume_type] filters_arg[:diskId] = volume_id if volume_id filters_arg[:sourseDiskType] = volume_type if volume_type - data = Fog::JSON.decode(service.list_snapshots(filters_arg).body)['Snapshots']['Snapshot'] + data = service.list_snapshots(filters_arg).body['Snapshots']['Snapshot'] load(data) end diff --git a/lib/fog/aliyun/models/compute/volume.rb b/lib/fog/aliyun/models/compute/volume.rb index 3006e50..9d28447 100644 --- a/lib/fog/aliyun/models/compute/volume.rb +++ b/lib/fog/aliyun/models/compute/volume.rb @@ -63,10 +63,10 @@ def save(options = {}) options[:name] = name if name options[:description] = description if description if snapshot_id - data = Fog::JSON.decode(service.create_disk_by_snapshot(snapshot_id, options).body) + data = service.create_disk_by_snapshot(snapshot_id, options).body merge_attributes(data) elsif size - data = Fog::JSON.decode(service.create_disk(size, options).body) + data = service.create_disk(size, options).body merge_attributes(data) end diff --git a/lib/fog/aliyun/models/compute/volumes.rb b/lib/fog/aliyun/models/compute/volumes.rb index 468fac5..9c9d97e 100644 --- a/lib/fog/aliyun/models/compute/volumes.rb +++ b/lib/fog/aliyun/models/compute/volumes.rb @@ -78,7 +78,7 @@ def all(filters_arg = {}) Fog::Logger.deprecation("all with #{filters_arg.class} param is deprecated, use all('diskIds' => []) instead [light_black](#{caller.first})[/]") filters_arg = { 'diskIds' => [*filters_arg] } end - data = Fog::JSON.decode(service.list_disks(filters_arg).body)['Disks']['Disk'] + data = service.list_disks(filters_arg).body['Disks']['Disk'] load(data) # load(data['volumeSet']) # if server diff --git a/lib/fog/aliyun/models/compute/vpc.rb b/lib/fog/aliyun/models/compute/vpc.rb index 0d28589..9af5f8f 100644 --- a/lib/fog/aliyun/models/compute/vpc.rb +++ b/lib/fog/aliyun/models/compute/vpc.rb @@ -70,7 +70,7 @@ def save(options = {}) requires :cidr_block options[:name] = name if name options[:description] = description if description - Fog::JSON.decode(service.create_vpc(cidr_block, options).body) + service.create_vpc(cidr_block, options).body true end end diff --git a/lib/fog/aliyun/models/compute/vpcs.rb b/lib/fog/aliyun/models/compute/vpcs.rb index bd9afdd..3164668 100644 --- a/lib/fog/aliyun/models/compute/vpcs.rb +++ b/lib/fog/aliyun/models/compute/vpcs.rb @@ -52,7 +52,7 @@ def all(filters_arg = {}) Fog::Logger.warning("all with #{filters_arg.class} param is deprecated, use all('vpcId' => []) instead [light_black](#{caller.first})[/]") filters_arg = { 'vpcId' => [*filters_arg] } end - data = Fog::JSON.decode(service.list_vpcs(filters_arg).body)['Vpcs']['Vpc'] + data = service.list_vpcs(filters_arg).body['Vpcs']['Vpc'] load(data) end diff --git a/lib/fog/aliyun/models/compute/vrouters.rb b/lib/fog/aliyun/models/compute/vrouters.rb index c3e3ee5..fa9a958 100644 --- a/lib/fog/aliyun/models/compute/vrouters.rb +++ b/lib/fog/aliyun/models/compute/vrouters.rb @@ -33,7 +33,7 @@ def all(filters_arg = {}) Fog::Logger.warning("all with #{filters_arg.class} param is deprecated, use all('vRouterId' => "") instead [light_black](#{caller.first})[/]") filters_arg = { 'vRouterId' => filters_arg } end - data = Fog::JSON.decode(service.list_vrouters(filters_arg).body)['VRouters']['VRouter'] + data = service.list_vrouters(filters_arg).body['VRouters']['VRouter'] load(data) end diff --git a/lib/fog/aliyun/models/compute/vswitch.rb b/lib/fog/aliyun/models/compute/vswitch.rb index 1bce7a4..811480d 100644 --- a/lib/fog/aliyun/models/compute/vswitch.rb +++ b/lib/fog/aliyun/models/compute/vswitch.rb @@ -55,7 +55,7 @@ def save(options = {}) requires :vpc, :cidr_block options[:name] = name if name options[:description] = description if description - Fog::JSON.decode(service.create_vswitch(vpc.id, cidr_block, options).body) + service.create_vswitch(vpc.id, cidr_block, options).body true end @@ -64,7 +64,7 @@ def vpc end def all - Fog::JSON.decode(service.list_vswitchs(vpc_id).body)['VSwitches']['VSwitch'] + service.list_vswitchs(vpc_id).body['VSwitches']['VSwitch'] end end end diff --git a/lib/fog/aliyun/models/compute/vswitches.rb b/lib/fog/aliyun/models/compute/vswitches.rb index 14434dc..84ccf90 100644 --- a/lib/fog/aliyun/models/compute/vswitches.rb +++ b/lib/fog/aliyun/models/compute/vswitches.rb @@ -51,7 +51,7 @@ class Vswitches < Fog::Collection def all(options = {}) requires :vpc - data = Fog::JSON.decode(service.list_vswitchs(vpc.id, options).body)['VSwitches']['VSwitch'] + data = service.list_vswitchs(vpc.id, options).body['VSwitches']['VSwitch'] load(data) end