class Saklient::Cloud::Resources::Resource

@private

Attributes

_client[RW]

@private @return [Saklient::Cloud::Client]

_query[RW]

@private @return [any]

client[R]

@private @return [Saklient::Cloud::Client]

is_incomplete[RW]

@private @return [bool]

is_new[RW]

@private @return [bool]

Public Class Methods

create_with(className, client, obj, wrapped = false) click to toggle source

@private @param [String] className @param [Saklient::Cloud::Client] client @param [any] obj @param [bool] wrapped @return [Resource]

# File lib/saklient/cloud/resources/resource.rb, line 295
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
new(client) click to toggle source

@private @param [Saklient::Cloud::Client] client

# File lib/saklient/cloud/resources/resource.rb, line 90
def initialize(client)
  Saklient::Util::validate_type(client, 'Saklient::Cloud::Client')
  @_client = client
  @_query = {}
end

Public Instance Methods

_class_name() click to toggle source

@private @return [String]

# File lib/saklient/cloud/resources/resource.rb, line 78
def _class_name
  return nil
end
_id() click to toggle source

@private @return [String]

# File lib/saklient/cloud/resources/resource.rb, line 84
def _id
  return nil
end
api_deserialize(obj, wrapped = false) click to toggle source

@private @param [any] obj @param [bool] wrapped @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 153
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) click to toggle source

@private @param [bool] withClean @return [any]

# File lib/saklient/cloud/resources/resource.rb, line 190
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
destroy() click to toggle source

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

@return [void]

# File lib/saklient/cloud/resources/resource.rb, line 248
def destroy
  return nil if @is_new
  path = _api_path + '/' + Saklient::Util::url_encode(_id)
  request_retry('DELETE', path)
end
dump() click to toggle source

@private @return [any]

# File lib/saklient/cloud/resources/resource.rb, line 285
def dump
  return api_serialize(true)
end
exists() click to toggle source

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

@return [bool]

# File lib/saklient/cloud/resources/resource.rb, line 274
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) click to toggle source

@param [String] method @param [String] path @param [any] query @param [Fixnum] retryCount @param [Fixnum] retrySleep @return [any]

# File lib/saklient/cloud/resources/resource.rb, line 313
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) click to toggle source

@private @param [String] key @param [any] value @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 49
def set_param(key, value)
  Saklient::Util::validate_type(key, 'String')
  @_query[key.to_sym] = value
end

Protected Instance Methods

_api_path() click to toggle source

@private @return [String]

# File lib/saklient/cloud/resources/resource.rb, line 58
def _api_path
  return nil
end
_on_after_api_deserialize(r, root) click to toggle source

@private @param [any] r @param [any] root @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 123
def _on_after_api_deserialize(r, root)
end
_on_after_api_serialize(r, withClean) click to toggle source

@private @param [any] r @param [bool] withClean @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 137
def _on_after_api_serialize(r, withClean)
  Saklient::Util::validate_type(withClean, 'bool')
end
_on_before_api_deserialize(r, root) click to toggle source

@private @param [any] r @param [any] root @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 116
def _on_before_api_deserialize(r, root)
end
_on_before_api_serialize(withClean) click to toggle source

@private @param [bool] withClean @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 129
def _on_before_api_serialize(withClean)
  Saklient::Util::validate_type(withClean, 'bool')
end
_on_before_save(query) click to toggle source

@private @param [any] query @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 109
def _on_before_save(query)
end
_reload() click to toggle source

最新のリソース情報を再取得します.

@private @return [Resource] this

# File lib/saklient/cloud/resources/resource.rb, line 260
def _reload
  id = _id
  if !(id).nil?
    result = request_retry('GET', _api_path + '/' + Saklient::Util::url_encode(id))
    api_deserialize(result, true)
  end
  return self
end
_root_key() click to toggle source

@private @return [String]

# File lib/saklient/cloud/resources/resource.rb, line 64
def _root_key
  return nil
end
_root_key_m() click to toggle source

@private @return [String]

# File lib/saklient/cloud/resources/resource.rb, line 70
def _root_key_m
  return nil
end
_save() click to toggle source

このローカルオブジェクトに現在設定されているリソース情報をAPIに送信し, 新規作成または上書き保存します.

@private @return [Resource] this

# File lib/saklient/cloud/resources/resource.rb, line 223
def _save
  r = api_serialize
  query = @_query
  @_query = {}
  keys = query.keys.map{|k| k.to_s}
  for k in keys
    v = query[k.to_sym]
    r[k.to_sym] = v
  end
  method = @is_new ? 'POST' : 'PUT'
  path = _api_path
  path += '/' + Saklient::Util::url_encode(_id) if !@is_new
  q = {}
  q[_root_key.to_sym] = r
  _on_before_save(q)
  result = @_client.request(method, path, q)
  api_deserialize(result, true)
  return self
end
api_deserialize_impl(r) click to toggle source

@private @param [any] r @return [void]

# File lib/saklient/cloud/resources/resource.rb, line 144
def api_deserialize_impl(r)
end
api_serialize_id() click to toggle source

@private @return [any]

# File lib/saklient/cloud/resources/resource.rb, line 202
def api_serialize_id
  id = _id
  return nil if (id).nil?
  r = {}
  r[:ID] = id
  return r
end
api_serialize_impl(withClean = false) click to toggle source

@private @param [bool] withClean @return [any]

# File lib/saklient/cloud/resources/resource.rb, line 180
def api_serialize_impl(withClean = false)
  Saklient::Util::validate_type(withClean, 'bool')
  return nil
end
get_client() click to toggle source

@private @return [Saklient::Cloud::Client]

# File lib/saklient/cloud/resources/resource.rb, line 23
def get_client
  return @_client
end
normalize_field_name(name) click to toggle source

@private @param [String] name @return [String]

# File lib/saklient/cloud/resources/resource.rb, line 213
def normalize_field_name(name)
  Saklient::Util::validate_type(name, 'String')
  name.gsub!(/[A-Z]/) {|s| '_'+s.downcase}
  return name
end