Class: Saklient::Cloud::Resources::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/saklient/cloud/resources/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Resource

Returns a new instance of Resource.

Parameters:



91
92
93
94
95
# File 'lib/saklient/cloud/resources/resource.rb', line 91

def initialize(client)
  Saklient::Util::validate_type(client, 'Saklient::Cloud::Client')
  @_client = client
  @_query = {}
end

Instance Attribute Details

#clientSaklient::Cloud::Client (readonly)



32
33
34
# File 'lib/saklient/cloud/resources/resource.rb', line 32

def client
  @client
end

Class Method Details

.create_with(className, client, obj, wrapped = false) ⇒ Resource

Parameters:

Returns:



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/saklient/cloud/resources/resource.rb', line 296

def self.create_with(className, client, obj, wrapped = false)
  Saklient::Util::validate_type(className, 'String')
  Saklient::Util::validate_type(client, 'Saklient::Cloud::Client')
  Saklient::Util::validate_type(wrapped, 'bool')
  a = [
    client,
    obj,
    wrapped
  ]
  return Saklient::Util::create_class_instance('saklient.cloud.resources.' + className, a)
end

Instance Method Details

#_class_nameString

Returns:

  • (String)


79
80
81
# File 'lib/saklient/cloud/resources/resource.rb', line 79

def _class_name
  return nil
end

#_idString

Returns:

  • (String)


85
86
87
# File 'lib/saklient/cloud/resources/resource.rb', line 85

def _id
  return nil
end

#api_deserialize(obj, wrapped = false) ⇒ void

This method returns an undefined value.

Parameters:

  • obj (any)
  • wrapped (bool) (defaults to: false)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/saklient/cloud/resources/resource.rb', line 154

def api_deserialize(obj, wrapped = false)
  Saklient::Util::validate_type(wrapped, 'bool')
  root = nil
  record = nil
  rkey = _root_key
  if !(obj).nil?
    if !wrapped
      if !(rkey).nil?
        root = {}
        root[rkey.to_sym] = obj
      end
      record = obj
    else
      root = obj
      record = obj[rkey.to_sym]
    end
  end
  _on_before_api_deserialize(record, root)
  api_deserialize_impl(record)
  _on_after_api_deserialize(record, root)
end

#api_serialize(withClean = false) ⇒ any

Parameters:

  • withClean (bool) (defaults to: false)

Returns:

  • (any)


191
192
193
194
195
196
197
# File 'lib/saklient/cloud/resources/resource.rb', line 191

def api_serialize(withClean = false)
  Saklient::Util::validate_type(withClean, 'bool')
  _on_before_api_serialize(withClean)
  ret = api_serialize_impl(withClean)
  _on_after_api_serialize(ret, withClean)
  return ret
end

#destroyvoid

This method returns an undefined value.

このローカルオブジェクトのIDと一致するリソースの削除リクエストをAPIに送信します.



249
250
251
252
253
# File 'lib/saklient/cloud/resources/resource.rb', line 249

def destroy
  return nil if @is_new
  path = _api_path + '/' + Saklient::Util::url_encode(_id)
  request_retry('DELETE', path)
end

#dumpany

Returns:

  • (any)


286
287
288
# File 'lib/saklient/cloud/resources/resource.rb', line 286

def dump
  return api_serialize(true)
end

#existsbool

このリソースが存在するかを調べます.

Returns:

  • (bool)


275
276
277
278
279
280
281
282
# File 'lib/saklient/cloud/resources/resource.rb', line 275

def exists
  query = {}
  Saklient::Util::set_by_path(query, 'Filter.ID', [_id])
  Saklient::Util::set_by_path(query, 'Include', ['ID'])
  result = request_retry('GET', _api_path, query)
  cnt = result[:Count]
  return cnt == 1
end

#request_retry(method, path, query = nil, retryCount = 5, retrySleep = 5) ⇒ any

Parameters:

  • method (String)
  • path (String)
  • query (any) (defaults to: nil)
  • retryCount (Fixnum) (defaults to: 5)
  • retrySleep (Fixnum) (defaults to: 5)

Returns:

  • (any)


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/saklient/cloud/resources/resource.rb', line 314

def request_retry(method, path, query = nil, retryCount = 5, retrySleep = 5)
  Saklient::Util::validate_type(method, 'String')
  Saklient::Util::validate_type(path, 'String')
  Saklient::Util::validate_type(retryCount, 'Fixnum')
  Saklient::Util::validate_type(retrySleep, 'Fixnum')
  ret = nil
  while 1 < retryCount do
    isOk = false
    begin
      ret = @_client.request(method, path, query)
      isOk = true
    rescue Saklient::Errors::HttpException
      isOk = false
    end
    if isOk
      retryCount = -1
    else
      retryCount -= 1
      sleep(retrySleep)
    end
  end
  ret = @_client.request(method, path, query) if retryCount == 0
  return ret
end

#set_param(key, value) ⇒ void

This method returns an undefined value.

Parameters:

  • key (String)
  • value (any)


50
51
52
53
# File 'lib/saklient/cloud/resources/resource.rb', line 50

def set_param(key, value)
  Saklient::Util::validate_type(key, 'String')
  @_query[key.to_sym] = value
end