Class: Dcmgr::Endpoints::CoreAPI
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Dcmgr::Endpoints::CoreAPI
show all
- Includes:
- Logger
- Defined in:
- lib/dcmgr/endpoints/core_api.rb
Instance Method Summary
collapse
Methods included from Logger
create, default_logdev, included
Instance Method Details
#examine_owner(account_resource) ⇒ Object
146
147
148
149
150
151
152
153
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 146
def examine_owner(account_resource)
if @account.canonical_uuid == account_resource.account_id ||
@account.canonical_uuid == 'a-00000000'
return true
else
return false
end
end
|
#find_account(account_uuid) ⇒ Object
64
65
66
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 64
def find_account(account_uuid)
find_by_uuid(:Account, account_uuid)
end
|
#find_by_uuid(model_class, uuid) ⇒ Object
57
58
59
60
61
62
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 57
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_volume_snapshot(snapshot_id) ⇒ Object
139
140
141
142
143
144
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 139
def find_volume_snapshot(snapshot_id)
vs = Models::VolumeSnapshot[snapshot_id]
raise UnknownVolumeSnapshot if vs.nil?
raise InvalidVolumeState unless vs.state.to_s == 'available'
vs
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!().
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 120
def handle_exception!(boom)
boom = case boom
when Sequel::DatabaseError
DatabaseError.new
else
boom
end
if boom.kind_of?(APIError)
@env['sinatra.error'] = boom
Dcmgr::Logger.create('API Error').error("#{request.path_info} -> #{boom.class.to_s}: #{boom.message} (#{boom.backtrace.first})")
error(boom.status_code, response_to({:error=>boom.class.to_s, :message=>boom.message, :code=>boom.error_code}))
else
logger.error(boom)
super
end
end
|
#parsed_request_body ⇒ Object
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.
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
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 72
def parsed_request_body
if @mime_types.nil?
hash = {:dummy=>@params}
else
mime = @mime_types.first
begin
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
rescue => e
hash = {:dummy=>@params}
end
end
return hash.values.first
end
|
#response_to(res) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 103
def response_to(res)
mime = @mime_types.first unless @mime_types.nil?
case mime.to_s
when 'application/yaml', 'text/yaml'
content_type 'yaml'
body res.to_yaml
when 'application/xml', 'text/xml'
raise NotImplementedError
else
content_type 'json'
body res.to_json(JSON::PRETTY_STATE_PROTOTYPE)
end
end
|
#select_index(model_class, data) ⇒ Object
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
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 155
def select_index(model_class, data)
if model_class.is_a?(Symbol)
model_class = Models.const_get(model_class)
end
start = data[:start].to_i
start = start < 1 ? 0 : start
limit = data[:limit].to_i
limit = limit < 1 ? nil : limit
if %w(Dcmgr::Models::InstanceSpec).member?(model_class.to_s)
total_ds = model_class.where(:account_id=>[@account.canonical_uuid,
Models::Account::SystemAccount::SharedPoolAccount.uuid,
])
else
total_ds = model_class.where(:account_id=>@account.canonical_uuid)
end
if %w(Dcmgr::Models::Instance Dcmgr::Models::Volume Dcmgr::Models::VolumeSnapshot).member?(model_class.to_s)
total_ds = total_ds.alives_and_recent_termed
end
if %w(Dcmgr::Models::Image).member?(model_class.to_s)
total_ds = total_ds.or(:is_public=>true)
end
partial_ds = total_ds.dup.order(:id.desc)
partial_ds = partial_ds.limit(limit, start) if limit.is_a?(Integer)
results = partial_ds.all.map {|i|
if %w(Dcmgr::Models::Image).member?(model_class.to_s)
i.to_api_document(@account.canonical_uuid)
else
i.to_api_document
end
}
res = [{
:owner_total => total_ds.count,
:start => start,
:limit => limit,
:results=> results
}]
end
|