File: compute.rb

package info (click to toggle)
ruby-fog-aliyun 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 720 kB
  • sloc: ruby: 5,804; makefile: 6; sh: 3
file content (448 lines) | stat: -rw-r--r-- 16,350 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# frozen_string_literal: true

require 'addressable'

module Fog
  module Compute
    class Aliyun < Fog::Service
      recognizes :aliyun_url,
                 :aliyun_accesskey_id,
                 :aliyun_accesskey_secret,
                 :aliyun_region_id,
                 :aliyun_zone_id

      ## 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
      collection :snapshots
      model :vpc
      collection :vpcs
      model :vswitch
      collection :vswitches
      model :vrouter
      collection :vrouters
      model :route_table
      collection :route_tables
      model :route_entry
      collection :route_entrys
      model :flavor
      collection :flavors
      ## REQUESTS
      #
      request_path 'fog/aliyun/requests/compute'

      # Server CRUD
      request :list_servers
      request :list_server_types
      request :create_server
      request :delete_server

      # Server Actions
      request :reboot_server
      request :start_server
      request :stop_server

      # SnapShoot CRUD
      request :list_snapshots
      request :create_snapshot
      request :delete_snapshot

      # Image CRUD
      request :list_images
      request :create_image
      request :delete_image

      # Eip
      request :list_eip_addresses
      request :allocate_eip_address
      request :release_eip_address
      request :associate_eip_address
      request :unassociate_eip_address

      # Security Group
      request :list_security_groups
      request :list_security_group_rules
      request :create_security_group
      request :create_security_group_ip_rule
      request :create_security_group_sg_rule
      request :create_security_group_egress_ip_rule
      request :create_security_group_egress_sg_rule
      request :delete_security_group
      request :delete_security_group_ip_rule
      request :delete_security_group_sg_rule
      request :delete_security_group_egress_ip_rule
      request :delete_security_group_egress_sg_rule
      request :join_security_group
      request :leave_security_group

      # Zones
      request :list_zones

      # VPC
      request :create_vpc
      request :delete_vpc
      request :list_vpcs
      request :create_vswitch
      request :delete_vswitch
      request :list_vswitchs
      request :modify_vpc
      request :modify_vswitch
      request :list_vpn_gateways
      request :list_vpn_customergateways
      request :list_vpn_connections
      request :create_vpn_customergateway
      request :create_vpn_connection
      request :delete_vpn_customergateway
      request :delete_vpn_connection
      # VRouter
      request :list_vrouters

      # RouteTable
      request :list_route_tables

      # clouddisk
      request :list_disks
      request :create_disk
      request :delete_disk
      request :attach_disk
      request :detach_disk

      # PublicIpAddress
      request :allocate_public_ip_address

      class Mock
        attr_reader :auth_token
        attr_reader :auth_token_expiration
        attr_reader :current_user
        attr_reader :current_tenant

        def self.data
          @data ||= Hash.new do |hash, key|
            hash[key] = {
              last_modified: {
                images: {},
                servers: {},
                key_pairs: {},
                security_groups: {},
                addresses: {}
              },
              images: {
                '0e09fbd6-43c5-448a-83e9-0d3d05f9747e' => {
                  'id' => '0e09fbd6-43c5-448a-83e9-0d3d05f9747e',
                  'name' => 'cirros-0.3.0-x86_64-blank',
                  'progress'  => 100,
                  'status'    => 'ACTIVE',
                  'updated'   => '',
                  'minRam'    => 0,
                  'minDisk'   => 0,
                  'metadata'  => {},
                  'links'     => [{ 'href' => 'http://nova1:8774/v1.1/admin/images/1', 'rel' => 'self' }, { 'href' => 'http://nova1:8774/admin/images/2', 'rel' => 'bookmark' }]
                }
              },
              servers: {},
              key_pairs: {},
              security_groups: {
                '0' => {
                  'id'          => 0,
                  'tenant_id'   => Fog::Mock.random_hex(8),
                  'name'        => 'default',
                  'description' => 'default',
                  'rules'       => [
                    { 'id'              => 0,
                      'parent_group_id' => 0,
                      'from_port'       => 68,
                      'to_port'         => 68,
                      'ip_protocol'     => 'udp',
                      'ip_range'        => { 'cidr' => '0.0.0.0/0' },
                      'group'           => {} }
                  ]
                }
              },
              server_security_group_map: {},
              addresses: {},
              quota: {
                'security_group_rules' => 20,
                'security_groups' => 10,
                'injected_file_content_bytes' => 10_240,
                'injected_file_path_bytes' => 256,
                'injected_files' => 5,
                'metadata_items' => 128,
                'floating_ips'   => 10,
                'instances'      => 10,
                'key_pairs'      => 10,
                'gigabytes'      => 5000,
                'volumes'        => 10,
                'cores'          => 20,
                'ram'            => 51_200
              },
              volumes: {}
            }
          end
        end

        def self.reset
          @data = nil
        end

        def initialize(options = {})
          @aliyun_username = options[:aliyun_username]
          @aliyun_user_domain = options[:aliyun_user_domain] || options[:aliyun_domain]
          @aliyun_project_domain = options[:aliyun_project_domain] || options[:aliyun_domain] || 'Default'

          raise ArgumentError, 'Missing required arguments: :aliyun_auth_url' unless options.key?[:aliyun_auth_url]
          @aliyun_auth_uri = URI.parse(options[:aliyun_auth_url])

          @current_tenant = options[:aliyun_tenant]
          @current_tenant_id = options[:aliyun_tenant_id]

          @auth_token = Fog::Mock.random_base64(64)
          @auth_token_expiration = (Time.now.utc + 86_400).iso8601

          management_url = URI.parse(@aliyun_auth_url)
          management_url.port = 8774
          management_url.path = '/v1.1/1'
          @aliyun_management_url = management_url.to_s

          identity_public_endpoint = URI.parse(@aliyun_auth_url)
          identity_public_endpoint.port = 5000
          @aliyun_identity_public_endpoint = identity_public_endpoint.to_s
        end

        def data
          self.class.data["#{@aliyun_username}-#{@current_tenant}"]
        end

        def reset_data
          self.class.data.delete("#{@aliyun_username}-#{@current_tenant}")
        end

        def credentials
          { provider: 'aliyun',
            aliyun_auth_url: @aliyun_auth_uri.to_s,
            aliyun_auth_token: @auth_token,
            aliyun_management_url: @aliyun_management_url,
            aliyun_identity_endpoint: @aliyun_identity_public_endpoint }
        end
      end

      class Real
        # Initialize connection to ECS
        #
        # ==== Notes
        # options parameter must include values for :aliyun_url, :aliyun_accesskey_id,
        # :aliyun_secret_access_key, :aliyun_region_id and :aliyun_zone_id in order to create a connection.
        # if you haven't set these values in the configuration file.
        #
        # ==== Examples
        #   sdb = Fog::Compute.new(:provider=>'aliyun',
        #    :aliyun_accesskey_id => your_:aliyun_accesskey_id,
        #    :aliyun_secret_access_key => your_aliyun_secret_access_key
        #   )
        #
        # ==== Parameters
        # * options<~Hash> - config arguments for connection.  Defaults to {}.
        #
        # ==== Returns
        # * ECS object with connection to aliyun.
        attr_reader :aliyun_accesskey_id
        attr_reader :aliyun_accesskey_secret
        attr_reader :aliyun_region_id
        attr_reader :aliyun_url
        attr_reader :aliyun_zone_id

        def initialize(options = {})
          # initialize the parameters
          @aliyun_url = options[:aliyun_url]
          @aliyun_accesskey_id = options[:aliyun_accesskey_id]
          @aliyun_accesskey_secret = options[:aliyun_accesskey_secret]
          @aliyun_region_id = options[:aliyun_region_id]
          @aliyun_zone_id = options[:aliyun_zone_id]

          # check for the parameters
          missing_credentials = []
          missing_credentials << :aliyun_accesskey_id unless @aliyun_accesskey_id
          missing_credentials << :aliyun_accesskey_secret unless @aliyun_accesskey_secret
          missing_credentials << :aliyun_region_id unless @aliyun_region_id
          missing_credentials << :aliyun_url unless @aliyun_url
          raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?

          @connection_options = options[:connection_options] || {}

          uri = URI.parse(@aliyun_url)
          @host = uri.host
          @path = uri.path
          @port = uri.port
          @scheme = uri.scheme

          vpcuri = URI.parse('https://vpc.aliyuncs.com')
          @vpchost = vpcuri.host
          @vpcpath = vpcuri.path
          @vpcport = vpcuri.port
          @vpcscheme = vpcuri.scheme

          @persistent = options[:persistent] || false
          @connection = Fog::Core::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
          @VPCconnection = Fog::Core::Connection.new("#{@vpcscheme}://#{@vpchost}:#{@vpcport}", @persistent, @connection_options)
        end

        def reload
          @connection.reset
          @VPCconnection.reset
        end

        def request(params)
          begin
            response = @connection.request(params.merge(headers: {
              'Content-Type' => 'application/json',
              'Accept' => 'application/json',
              'X-Auth-Token' => @auth_token
            }.merge!(params[:headers] || {}),
                                                        path: "#{@path}/#{params[:path]}",
                                                        query: params[:query]))
          rescue Excon::Errors::HTTPStatusError => error
            raise case error
                  when Excon::Errors::NotFound
                    Fog::Compute::Aliyun::NotFound.slurp(error)
                  else
                    error
                  end
          end

          response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type') == 'application/json'

          response
        end

        def VPCrequest(params)
          begin
            response = @VPCconnection.request(params.merge(headers: {
              'Content-Type' => 'application/json',
              'Accept' => 'application/json',
              'X-Auth-Token' => @auth_token
            }.merge!(params[:headers] || {}),
                                                           path: "#{@path}/#{params[:path]}",
                                                           query: params[:query]))
          rescue Excon::Errors::HTTPStatusError => error
            raise case error
                  when Excon::Errors::NotFound
                    Fog::Compute::Aliyun::NotFound.slurp(error)
                  else
                    error
                  end
          end

          response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type') == 'application/json'

          response
        end

        # operation compute-- default URL
        def defaultAliyunUri(action, sigNonce, time)
          parTimeFormat = time.strftime('%Y-%m-%dT%H:%M:%SZ')
          urlTimeFormat = Addressable::URI.encode_component(parTimeFormat, Addressable::URI::CharacterClasses::UNRESERVED + '|')
          '?Format=JSON&AccessKeyId=' + @aliyun_accesskey_id + '&Action=' + action + '&SignatureMethod=HMAC-SHA1&RegionId=' + @aliyun_region_id + '&SignatureNonce=' + sigNonce + '&SignatureVersion=1.0&Version=2014-05-26&Timestamp=' + urlTimeFormat
        end

        def defaultAliyunQueryParameters(action, sigNonce, time)
          {
            Format: 'JSON',
            AccessKeyId: @aliyun_accesskey_id,
            Action: action,
            SignatureMethod: 'HMAC-SHA1',
            RegionId: @aliyun_region_id,
            SignatureNonce: sigNonce,
            SignatureVersion: '1.0',
            Version: '2014-05-26',
            Timestamp: time.strftime('%Y-%m-%dT%H:%M:%SZ')
          }
        end

        def defaultAliyunVPCUri(action, sigNonce, time)
          parTimeFormat = time.strftime('%Y-%m-%dT%H:%M:%SZ')
          urlTimeFormat = Addressable::URI.encode_component(parTimeFormat, Addressable::URI::CharacterClasses::UNRESERVED + '|')
          '?Format=JSON&AccessKeyId=' + @aliyun_accesskey_id + '&Action=' + action + '&SignatureMethod=HMAC-SHA1&RegionId=' + @aliyun_region_id + '&SignatureNonce=' + sigNonce + '&SignatureVersion=1.0&Version=2016-04-28&Timestamp=' + urlTimeFormat
        end

        # generate random num
        def randonStr
          numStr = rand(100_000).to_s
          timeStr = Time.now.to_f.to_s
          ranStr = timeStr + '-' + numStr
          ranStr
        end

        # operation compute--collection of default parameters
        def defaultParameters(action, sigNonce, time)
          parTimeFormat = time.strftime('%Y-%m-%dT%H:%M:%SZ')
          para = {
            'Format' => 'JSON',
            'Version' => '2014-05-26',
            'Action' => action,
            'AccessKeyId' => @aliyun_accesskey_id,
            'SignatureVersion' => '1.0',
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => sigNonce,
            'RegionId' => @aliyun_region_id,
            'Timestamp' => parTimeFormat
          }
          para
        end

        def defalutVPCParameters(action, sigNonce, time)
          parTimeFormat = time.strftime('%Y-%m-%dT%H:%M:%SZ')
          para = {
            'Format' => 'JSON',
            'Version' => '2016-04-28',
            'Action' => action,
            'AccessKeyId' => @aliyun_accesskey_id,
            'SignatureVersion' => '1.0',
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => sigNonce,
            'RegionId' => @aliyun_region_id,
            'Timestamp' => parTimeFormat
          }
          para
        end

        # compute signature
        # This method should be considered deprecated and replaced with sign_without_encoding, which is better for using querystring hashes and not
        # building querystrings with string concatination.
        def sign(accessKeySecret, parameters)
          signature = sign_without_encoding(accessKeySecret, parameters)
          Addressable::URI.encode_component(signature, Addressable::URI::CharacterClasses::UNRESERVED + '|')
        end

        def sign_without_encoding(accessKeySecret, parameters)
          sortedParameters = parameters.sort
          canonicalizedQueryString = ''
          sortedParameters.each do |k, v|
            canonicalizedQueryString += '&' + Addressable::URI.encode_component(k, Addressable::URI::CharacterClasses::UNRESERVED + '|') + '=' + Addressable::URI.encode_component(v, Addressable::URI::CharacterClasses::UNRESERVED + '|')
          end

          canonicalizedQueryString[0] = ''
          stringToSign = 'GET&%2F&' + Addressable::URI.encode_component(canonicalizedQueryString, Addressable::URI::CharacterClasses::UNRESERVED + '|')
          key = accessKeySecret + '&'

          digVer = OpenSSL::Digest.new('sha1')
          digest = OpenSSL::HMAC.digest(digVer, key, stringToSign)
          signature = Base64.encode64(digest)
          signature[-1] = ''
          signature
        end
      end
    end
  end
end