mno_enterprise-nimbus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +7 -0
  2. data/APACHE-LICENSE-2.0 +201 -0
  3. data/README.md +28 -0
  4. data/Rakefile +27 -0
  5. data/app/assets/config/mno_enterprise_nimbus_manifest.js +1 -0
  6. data/app/assets/stylesheets/mno_enterprise/nimbus/application.css +15 -0
  7. data/app/controllers/mno_enterprise/nimbus/application_controller.rb +7 -0
  8. data/app/controllers/mno_enterprise/nimbus/v4/app_instances_controller.rb +36 -0
  9. data/app/controllers/mno_enterprise/nimbus/v4/apps_controller.rb +22 -0
  10. data/app/controllers/mno_enterprise/nimbus/v4/base_controller.rb +281 -0
  11. data/app/controllers/mno_enterprise/nimbus/v4/events_controller.rb +52 -0
  12. data/app/controllers/mno_enterprise/nimbus/v4/organizations_controller.rb +37 -0
  13. data/app/controllers/mno_enterprise/nimbus/v4/resource_instances_controller.rb +134 -0
  14. data/app/controllers/mno_enterprise/nimbus/v4/services_controller.rb +20 -0
  15. data/app/controllers/mno_enterprise/nimbus/v4/statistics_controller.rb +40 -0
  16. data/app/controllers/mno_enterprise/nimbus/v4/users_controller.rb +83 -0
  17. data/app/helpers/mno_enterprise/nimbus/application_helper.rb +6 -0
  18. data/app/jobs/mno_enterprise/nimbus/application_job.rb +6 -0
  19. data/app/mailers/mno_enterprise/nimbus/application_mailer.rb +8 -0
  20. data/app/models/mno_enterprise/nimbus/application_record.rb +7 -0
  21. data/app/views/layouts/mno_enterprise/nimbus/application.html.erb +14 -0
  22. data/app/views/mno_enterprise/nimbus/v4/services/schema.json +970 -0
  23. data/app/views/mno_enterprise/nimbus/v4/services/schema_dev.json +970 -0
  24. data/app/views/mno_enterprise/nimbus/v4/services/schema_uat.json +970 -0
  25. data/config/routes.rb +17 -0
  26. data/lib/mno_enterprise/nimbus/engine.rb +7 -0
  27. data/lib/mno_enterprise/nimbus/version.rb +5 -0
  28. data/lib/mno_enterprise/nimbus.rb +14 -0
  29. data/lib/tasks/mno_enterprise/nimbus_tasks.rake +4 -0
  30. metadata +184 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c68a95b3a7ede2cd90be31db90ad9682144f33ac
4
+ data.tar.gz: 9a214548748d0d83bd22c069ce68d05fdb8f0283
5
+ SHA512:
6
+ metadata.gz: 1e8ad341ab14d1aeafaa56663b8939203760db157b3f3d7389eb9f7b60ade60bcfc0af949c7745f475360ccbec925b5917acb64cf83fd323bc219ad67693f114
7
+ data.tar.gz: c60fbdc317ab556a8ff152b97027833dd2c4856abc6d71f9b9357d88af7cc03bcb3cd67a0a6d94d7af58d475b1455dee0e0751b6bee7d3b65d605f37a93fc46a
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # MnoEnterprise::Nimbus
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'mno_enterprise-nimbus'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install mno_enterprise-nimbus
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [Apache 2.0 License](https://opensource.org/licenses/Apache-2.0).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'MnoEnterprise::Nimbus'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+ require 'bundler/gem_tasks'
22
+ require 'rspec/core/rake_task'
23
+
24
+ desc "Run all specs in spec directory (excluding plugin specs)"
25
+ RSpec::Core::RakeTask.new(:spec)
26
+
27
+ task default: :spec
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/mno_enterprise/nimbus .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,7 @@
1
+ module MnoEnterprise
2
+ module Nimbus
3
+ class ApplicationController < ActionController::Base
4
+ protect_from_forgery with: :exception
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::AppInstancesController < Nimbus::V4::BaseController
3
+
4
+ private
5
+
6
+ def resource_klass
7
+ MnoEnterprise::AppInstance
8
+ end
9
+
10
+ def get_resources
11
+ parent.app_instances
12
+ end
13
+
14
+ def get_resource(uid)
15
+ MnoEnterprise::AppInstance.find_by(uid: uid)
16
+ end
17
+
18
+ def params_create_whitelist
19
+ [:product]
20
+ end
21
+
22
+ def build_resource(attributes)
23
+ attrs = attributes.dup
24
+ parent.app_instances.create attrs
25
+ end
26
+
27
+ def destroy_resource(resource)
28
+ return false unless resource
29
+ resource.terminate
30
+ end
31
+
32
+ def hash_for(resource)
33
+ hash_for_app_instance resource
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::AppsController < Nimbus::V4::BaseController
3
+
4
+ private
5
+ ATTRIBUTES = [:nid, :name, :description, :created_at, :updated_at, :logo, :version, :website, :slug, :categories,
6
+ :key_benefits, :key_features, :testimonials, :tiny_description]
7
+
8
+ def resource_klass
9
+ MnoEnterprise::App
10
+ end
11
+
12
+ # Retrieve all user
13
+ def get_resources
14
+ MnoEnterprise::App.all
15
+ end
16
+
17
+ # Hash representation of the resource
18
+ def hash_for(resource)
19
+ resource.attributes.slice(*ATTRIBUTES)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,281 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::BaseController < ApplicationController
3
+ before_filter :authenticate_remote_client!
4
+ before_filter :check_parent_resource
5
+ around_filter :prepare_and_handle_error
6
+
7
+ class ResponseCode
8
+ attr_accessor :error_code, :http_code
9
+
10
+ def initialize(error_code, http_code)
11
+ @error_code = error_code
12
+ @http_code = http_code
13
+ end
14
+ end
15
+
16
+ INTERNAL_ERROR = ResponseCode.new('unknownError', 500)
17
+ RESOURCE_NOT_FOUND = ResponseCode.new('notFound', 404)
18
+ PARENT_RESOURCE_NOT_FOUND = ResponseCode.new('parentNotFound', 404)
19
+ AUTHENTICATION_ERROR = ResponseCode.new('notAuthenticated', 401)
20
+ VALIDATION_ERROR = ResponseCode.new('validationError', 400)
21
+ NO_ERROR = ResponseCode.new('noError', 200)
22
+
23
+ respond_to :json
24
+
25
+ # GET /api/nimbus/v4/<resources_name>
26
+ # Get all resources
27
+ def index
28
+ render_response(get_resources)
29
+ end
30
+
31
+ # GET /api/nimbus/v4/:id
32
+ # Show specific resource
33
+ def show
34
+ @resource = get_resource(params[:id])
35
+
36
+ if @resource
37
+ render_response(@resource)
38
+ return
39
+ else
40
+ render_error RESOURCE_NOT_FOUND, 'Resource not found: ' + params[:id]
41
+ return
42
+ end
43
+ end
44
+
45
+ # POST /api/nimbus/v4/<resources_name>
46
+ # Create new resource and return representation
47
+ def create
48
+ unless params[:data]
49
+ render_error VALIDATION_ERROR, 'Body does not contain the field: data'
50
+ return false
51
+ end
52
+
53
+ attributes = params[:data].select { |k, v| params_create_whitelist.include?(k.to_sym) }
54
+
55
+ @resource = build_resource(attributes)
56
+
57
+ # Save resource or set error code and status
58
+ unless @resource.save
59
+ render_errors VALIDATION_ERROR, @resource.errors
60
+ return
61
+ end
62
+
63
+ after_create_resource(@resource)
64
+
65
+ render_response(@resource, 201)
66
+ end
67
+
68
+ # PUT /api/nimbus/v4/<resources_name>/:id
69
+ # Change, suspend or reactivate a resource
70
+ # Operation passed in body: {"operation":"suspend"}
71
+ def update
72
+ attributes = (params[:data] || {}).select { |k, v| params_update_whitelist.include?(k.to_sym) }
73
+
74
+ @resource = get_resource(params[:id])
75
+
76
+ # Check resource exists
77
+ unless @resource
78
+ render_error RESOURCE_NOT_FOUND, 'Resource not found: ' + params[:id]
79
+ return false
80
+ end
81
+
82
+ if attributes.any?
83
+ update_resource(@resource, attributes)
84
+ unless @resource.save
85
+ render_errors VALIDATION_ERROR, @resource.errors
86
+ return false
87
+ end
88
+ end
89
+
90
+ after_update_resource(@resource, attributes)
91
+
92
+ render_response(@resource)
93
+ end
94
+
95
+
96
+ # DELETE /api/nimbus/v4/<resources_name>/:id
97
+ # Delete a resource
98
+ def destroy
99
+ @resource = get_resource(params[:id])
100
+ # Check resource exists
101
+ unless @resource
102
+ render_error RESOURCE_NOT_FOUND, 'Resource not found: ' + params[:id]
103
+ return
104
+ end
105
+
106
+ destroy_resource(@resource)
107
+ render_response(@resource)
108
+ end
109
+
110
+ #=======================================
111
+ # Private
112
+ #=======================================
113
+ private
114
+
115
+ # Retrieve the parent resource in a before filter
116
+ def parent
117
+ @parent ||= MnoEnterprise::Organization.find_by({uid: params[:organization_id]})
118
+ end
119
+
120
+ def check_parent_resource
121
+ if params[:organization_id] && !parent
122
+ render_error PARENT_RESOURCE_NOT_FOUND, 'Parent resource not found: ' + params[:organization_id]
123
+ return false
124
+ end
125
+ true
126
+ end
127
+
128
+ # Around filter
129
+ # Return properly formatted json message on error
130
+ def prepare_and_handle_error
131
+ begin
132
+ yield
133
+ rescue Exception => e
134
+ logger.error e
135
+ render_error INTERNAL_ERROR, 'Internal error'
136
+ end
137
+ end
138
+
139
+ # Read the token from the http header and
140
+ # check that it matches the Westcon auth settings
141
+ def authenticate_remote_client!
142
+ valid = false
143
+
144
+ # Check authentication
145
+ authenticate_with_http_basic do |login, password|
146
+ valid = Settings.westcon.api_login == login && Settings.westcon.api_pwd == password
147
+ end
148
+
149
+ # Block access in production (for the moment)
150
+ if (valid || Rails.env.development?)
151
+ return true
152
+ else
153
+ render_error AUTHENTICATION_ERROR, 'Authentication error'
154
+ return true
155
+ end
156
+ end
157
+
158
+ # Define the list of parameters for creating a resource
159
+ def params_create_whitelist
160
+ []
161
+ end
162
+
163
+ # Define the list of parameters for updating a resource
164
+ # Returns params_create_whitelist by default
165
+ def params_update_whitelist
166
+ params_create_whitelist
167
+ end
168
+
169
+
170
+ # Render an error message with the provided
171
+ # code and header
172
+ def render_error(response_code, message)
173
+ errors = [{
174
+ id: UUID.new.generate,
175
+ code: response_code.error_code.underscore.dasherize,
176
+ title: message,
177
+ detail: message
178
+ }]
179
+ render json: {error: response_code.error_code, result: {errors: errors}}, status: response_code.http_code
180
+ end
181
+
182
+
183
+ def render_errors(response_code, errors)
184
+ render({json: {error: response_code.error_code, result: {errors: process_errors(errors)}}, status: response_code.http_code})
185
+ end
186
+
187
+
188
+ def process_errors(errors)
189
+ raise 'call process_error for a single error' if errors.kind_of?(String)
190
+ # error may be either an array of string, either an iterable of error and error_val
191
+ # In the first case, error_val will be nil
192
+ errors.map do |error, error_val|
193
+ field = error_val ? error : nil
194
+ label = [error, error_val].compact.join(' ')
195
+ code = label.parameterize.gsub(/\-\d+/, '')
196
+ {
197
+ id: UUID.new.generate,
198
+ code: code,
199
+ title: label,
200
+ detail: label,
201
+ attribute: field,
202
+ value: error_val
203
+ }
204
+ end
205
+ end
206
+
207
+ def resource_klass
208
+ raise NotImplemented
209
+ end
210
+
211
+ def hash_for(resource)
212
+ raise NotImplemented
213
+ end
214
+
215
+ # Build a resource for creation
216
+ # Give unlimited free trial
217
+ def build_resource(attributes)
218
+ attrs = attributes.dup
219
+ res = resource_klass.new
220
+ res.assign_attributes(attrs)
221
+ res
222
+ end
223
+
224
+ # Update a resource
225
+ def update_resource(resource, attributes)
226
+ resource.assign_attributes(attributes.select { |k, v| params_update_whitelist.include?(k.to_sym) })
227
+ end
228
+
229
+ # TODO: Call some sort of "terminate" method on Organization
230
+ # model which also terminates all the related app_instances
231
+ # Destroy a resource
232
+ def destroy_resource(resource)
233
+ resource.destroy
234
+ end
235
+
236
+ # Hook called after the resource has been
237
+ # successfully created (optional)
238
+ def after_create_resource(resource, opts = {})
239
+ end
240
+
241
+ # Hook called after the resource has been
242
+ # successfully updated (optional)
243
+ def after_update_resource(resource, opts = {})
244
+ end
245
+
246
+ # Render a resource or resources view
247
+ def render_response(resource_or_list, http_code = 200)
248
+ resp = resource_or_list.kind_of?(Array) ? resource_or_list.map { |e| hash_for(e) } : hash_for(resource_or_list)
249
+ render json: {result: resp, error: NO_ERROR.error_code}, status: http_code
250
+ end
251
+
252
+ def hash_for_organization(resource)
253
+ {
254
+ id: resource.uid,
255
+ name: resource.name,
256
+ status: resource.suspended? ? 'suspended' : 'active',
257
+ }
258
+ end
259
+
260
+ def hash_for_user(resource)
261
+ {
262
+ id: resource.uid,
263
+ name: resource.name,
264
+ surname: resource.surname,
265
+ email: resource.email,
266
+ role: resource.role
267
+ }
268
+ end
269
+
270
+ def hash_for_app_instance(resource)
271
+ {
272
+ id: resource.uid,
273
+ product: resource.app.nid,
274
+ status: resource.status,
275
+ stack: resource.stack,
276
+ created_at: resource.created_at
277
+ }
278
+ end
279
+
280
+ end
281
+ end
@@ -0,0 +1,52 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::EventsController < Nimbus::V4::BaseController
3
+
4
+ private
5
+ def hash_for(resource)
6
+ resource
7
+ end
8
+
9
+ def resource_klass
10
+ MnoEnterprise::Invoice
11
+ end
12
+
13
+ # Retrieve all resources
14
+ def get_resources
15
+ app_instances_by_uid = MnoEnterprise::AppInstance.all.index_by(&:uid)
16
+ invoices_request = MnoEnterprise::Invoice.all
17
+ invoices_request.params[:currency] = Settings.westcon.api.events.currency
18
+ invoices_request.to_a.select { |invoice| invoice[:updated_at] != nil }.map { |invoice|
19
+ invoice.billing_summary.each_with_index.map { |bill, i|
20
+ # price_tag is deprecated
21
+ bill['lines'].each_with_index.map { |line, j|
22
+ price = line[:converted_price] || line[:price] || Monetize.parse(line[:price_tag])
23
+ app_instance = app_instances_by_uid[bill[:app_uid]]
24
+ if app_instance
25
+ product = app_instance.app.nid
26
+ end
27
+ {
28
+ id: "evt-#{invoice.id}b#{i}l#{j}",
29
+ event_type: 'financial',
30
+ amount_cents: price.cents,
31
+ amount_currency: price.currency.iso_code,
32
+ label: bill[:label] +' - ' + line[:label],
33
+ tx_type: 'charge',
34
+ organization_id: invoice.organization.uid,
35
+ type: bill[:app_uid] ? 'app' : 'other',
36
+ app_instance_id: bill[:app_uid],
37
+ app_instance_product: product,
38
+ started_at: invoice.started_at,
39
+ ended_at: invoice.ended_at,
40
+ created_at: invoice.created_at
41
+
42
+ }
43
+ }.flatten
44
+ }
45
+ }.flatten
46
+ end
47
+
48
+ def get_resource(id)
49
+ get_resources.find { |e| e[:id] == id }
50
+ end
51
+ end
52
+ end