Class: Dcmgr::Endpoints::CoreAPI

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Logger
Defined in:
lib/dcmgr/endpoints/core_api.rb,
lib/dcmgr/endpoints/core_api_mock.rb

Instance Method Summary collapse

Methods included from Logger

create, default_logdev, included

Instance Method Details

#create_volume_from_snapshot(account_id, snapshot_id) ⇒ Object

Raises:

  • (UnknownVolumeSnapshot)


110
111
112
113
114
115
# File 'lib/dcmgr/endpoints/core_api.rb', line 110

def create_volume_from_snapshot(, snapshot_id)
  vs = find_by_uuid(:VolumeSnapshot, snapshot_id)
  raise UnknownVolumeSnapshot if vs.nil?
  raise InvalidRequestCredentials unless vs.state.to_s == 'available'
  vs.create_volume()
end

#examine_owner(account_resource) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/dcmgr/endpoints/core_api.rb', line 117

def examine_owner()
  if @account.canonical_uuid == . ||
      @account.canonical_uuid == 'a-00000000'
    return true
  else
    return false
  end
end

#find_account(account_uuid) ⇒ Object



49
50
51
# File 'lib/dcmgr/endpoints/core_api.rb', line 49

def ()
  find_by_uuid(:Account, )
end

#find_by_uuid(model_class, uuid) ⇒ Object



42
43
44
45
46
47
# File 'lib/dcmgr/endpoints/core_api.rb', line 42

def find_by_uuid(model_class, uuid)
  if model_class.is_a?(Symbol)
    model_class = Models.const_get(model_class)
  end
  model_class[uuid] || raise(UnknownUUIDResource, uuid.to_s)
end

#find_user(user_uuid) ⇒ Object



59
60
61
# File 'lib/dcmgr/endpoints/core_api_mock.rb', line 59

def find_user(user_uuid)
  find_by_uuid(:User, user_uuid)
end

#handle_exception!(boom) ⇒ Object

I am not going to use error(ex, &blk) hook since it works only when matches the Exception class exactly. I expect to match whole subclasses of APIError so that override handle_exception!().



100
101
102
103
104
105
106
107
108
# File 'lib/dcmgr/endpoints/core_api.rb', line 100

def handle_exception!(boom)
  logger.error(boom)
  if boom.kind_of?(APIError)
    @env['sinatra.error'] = boom
    error(boom.status_code, boom.class.to_s)
  else
    super
  end
end

#pagenate(data, start, limit) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dcmgr/endpoints/core_api_mock.rb', line 105

def pagenate(data,start,limit) 
  return data unless data.kind_of?(Array)
  if !start.nil? && !limit.nil?
    start = start.to_i
    limit = limit.to_i
    from = start
    to = (from + limit -1)
    data = data[from..to]         
  end
  data
end

#parsed_request_bodyObject

Returns deserialized hash from HTTP body. Serialization fromat is guessed from content type header. The query string params is returned if none of content type header is in HTTP headers. This method is called only when the request method is POST.



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
# File 'lib/dcmgr/endpoints/core_api.rb', line 57

def parsed_request_body
  # @mime_types should be defined by sinatra/respond_to.rb plugin.
  if @mime_types.nil?
    # use query string as requested params if Content-Type
    # header was not sent.
    # ActiveResource library tells the one level nested hash which has
    # {'something key'=>real_params} so that dummy key is assinged here.
    hash = {:dummy=>@params}
  else
    mime = @mime_types.first
    case mime.to_s
    when 'application/json', 'text/json'
      require 'json'
      hash = JSON.load(request.body)
      hash = hash.to_mash
    when 'application/yaml', 'text/yaml'
      require 'yaml'
      hash = YAML.load(request.body)
      hash = hash.to_mash
    else
      raise "Unsupported body document type: #{mime.to_s}"
    end
  end
  return hash.values.first
end

#response_to(res) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dcmgr/endpoints/core_api.rb', line 83

def response_to(res)
  mime = @mime_types.first unless @mime_types.nil?
  case mime.to_s
  when 'application/yaml', 'text/yaml'
    content_type 'yaml'
    res.to_yaml
  when 'application/xml', 'text/xml'
    raise NotImplementedError
  else
    content_type 'json'
    res.to_json
  end
end