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,
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
126
127
128
129
130
131
132
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 126
def create_volume_from_snapshot(account_id, snapshot_id)
vs = find_by_uuid(:VolumeSnapshot, snapshot_id)
raise UnknownVolumeSnapshot if vs.nil?
raise InvalidVolumeState unless vs.state.to_s == 'available'
vs.create_volume(account_id)
end
|
#examine_owner(account_resource) ⇒ Object
134
135
136
137
138
139
140
141
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 134
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_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!().
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 115
def handle_exception!(boom)
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
|
#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_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
|
# 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 98
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
end
end
|
#select_index(model_class, data) ⇒ Object
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
|
# File 'lib/dcmgr/endpoints/core_api.rb', line 143
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
|