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
@@ -0,0 +1,37 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::OrganizationsController < Nimbus::V4::BaseController
3
+
4
+ #===========================================================
5
+ # Private
6
+ #===========================================================
7
+ private
8
+ def resource_klass
9
+ MnoEnterprise::Organization
10
+ end
11
+
12
+ # Retrieve all groups
13
+ def get_resources
14
+ MnoEnterprise::Organization.all
15
+ end
16
+
17
+ # Retrieve a group via id
18
+ def get_resource(uid)
19
+ MnoEnterprise::Organization.find_by({uid: uid})
20
+ end
21
+
22
+ # Define the list of parameters for creating a resource
23
+ def params_create_whitelist
24
+ [:name]
25
+ end
26
+
27
+ def after_create_resource(resource)
28
+ resource.free_trial_end_at = 15.years.from_now
29
+ resource.save
30
+ end
31
+
32
+ # Hash representation of the resource
33
+ def hash_for(resource)
34
+ hash_for_organization resource
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,134 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::ResourceInstancesController < Nimbus::V4::BaseController
3
+
4
+ # POST /api/nimbus/v4/resource_instances
5
+ # Create new resource and return representation
6
+ def create
7
+
8
+ unless attributes = params[:data]
9
+ render_error VALIDATION_ERROR, 'Body does not contain the field: data'
10
+ return false
11
+ end
12
+
13
+ unless attributes['organization']
14
+ render_error VALIDATION_ERROR, 'Data does not contains the fields: organization'
15
+ return false
16
+ end
17
+ users = attributes['users']
18
+ unless users && users.any?
19
+ render_error VALIDATION_ERROR, 'Resource should contain at least one user'
20
+ return false
21
+ end
22
+
23
+ users_validation_errors = []
24
+ users.each do |hash|
25
+ email = hash[:email]
26
+ user = MnoEnterprise::User.find_by(email: email)
27
+ users_validation_errors << "User [#{user.uid}] already exist with email: #{email}" if user
28
+ end
29
+ if users_validation_errors.any?
30
+ render_errors VALIDATION_ERROR, users_validation_errors
31
+ return false
32
+ end
33
+ # Create organization
34
+ organization = provision_organization(attributes['organization'])
35
+ if organization.errors.any?
36
+ render_errors VALIDATION_ERROR, organization.errors
37
+ return false
38
+ end
39
+
40
+ # Create users - handle single object and array
41
+ attributes['users'].each_with_index do |hash, index|
42
+ hash = hash.merge('organization' => organization)
43
+ # Create the first user as a Super Admin
44
+ hash['role'] = 'Super Admin' if index == 0
45
+ provision_user(hash)
46
+ end
47
+
48
+ # Create app_instances - handle single object and array
49
+ [attributes['app_instances']].flatten.compact.each do |hash|
50
+ organization.app_instances.create hash
51
+ end
52
+
53
+ after_create_resource(organization)
54
+
55
+ render_response(organization.reload, 201)
56
+ end
57
+
58
+ private
59
+
60
+ def resource_klass
61
+ Organization
62
+ end
63
+
64
+ # Retrieve all organizations
65
+ def get_resources
66
+ MnoEnterprise::Organization.all
67
+ end
68
+
69
+ # Retrieve an organization via uid
70
+ def get_resource(id)
71
+ MnoEnterprise::Organization.find_by({uid: id})
72
+ end
73
+
74
+ # Creation attributes for users
75
+ def user_params_create_whitelist
76
+ [:name, :surname, :email, :role, :organization]
77
+ end
78
+
79
+ # Creation attributes for organizations
80
+ def organization_params_create_whitelist
81
+ [:name]
82
+ end
83
+
84
+ def provision_organization(attributes)
85
+ attrs = attributes.select { |k, v| organization_params_create_whitelist.include?(k.to_sym) }
86
+
87
+ res = MnoEnterprise::Organization.new(attrs)
88
+ res.free_trial_end_at = 15.years.from_now
89
+
90
+ res.save
91
+ res
92
+ end
93
+
94
+ def provision_user(attributes)
95
+ attrs = attributes.select { |k, v| user_params_create_whitelist.include?(k.to_sym) }
96
+
97
+ res = MnoEnterprise::User.new(attrs.reject { |k, v| [:role, :organization].include?(k.to_sym) })
98
+ res.password = Devise.friendly_token.first(15)
99
+ res.save
100
+
101
+ # Update free trial after creation
102
+ res.free_trial_end_at = 15.years.from_now
103
+ res.save
104
+
105
+ # Add user to organization
106
+ if attrs['organization']
107
+ args = [res, attrs['role']].compact
108
+ attrs['organization'].add_user(*args)
109
+ end
110
+ res
111
+ end
112
+
113
+ def after_create_resource(resource)
114
+ resource.free_trial_end_at = 15.years.from_now
115
+ resource.save
116
+ end
117
+
118
+ # Hash representation of the resource
119
+ def hash_for(resource)
120
+ resp = {id: resource.uid}
121
+ # Add organization
122
+ resp[:organization] = hash_for_organization resource
123
+ # Add app_instances
124
+ resp[:app_instances] = resource.app_instances.map do |r|
125
+ hash_for_app_instance r
126
+ end
127
+ # Add users
128
+ resp[:users] = resource.users.map do |r|
129
+ hash_for_user r
130
+ end
131
+ resp
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,20 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::ServicesController < Nimbus::V4::BaseController
3
+ respond_to :json
4
+
5
+ # GET nimbus/v4/service/schema
6
+ # Return the API
7
+ def schema
8
+ render 'schema_dev', formats: [:json] if Rails.env.test? || Rails.env.development?
9
+ render 'schema_uat', formats: [:json] if Rails.env.uat?
10
+ render 'schema', formats: [:json] if Rails.env.production?
11
+ end
12
+
13
+ # GET nimbus/v4/service/status
14
+ # Return service status
15
+ def status
16
+ @start_time = Time.now
17
+ render json: {data: {available: true, response_time: ((Time.now - @start_time)*1000).round}}
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::StatisticsController < Nimbus::V4::BaseController
3
+
4
+ private
5
+
6
+ def resource_klass
7
+ Object
8
+ end
9
+
10
+ # Retrieve all resources
11
+ def get_resources
12
+ [get_resource(1)]
13
+ end
14
+
15
+ # Retrieve a resource via uid
16
+ def get_resource(id)
17
+ {
18
+ id: 'current-billing',
19
+ title: "Current Billing",
20
+ type: "value",
21
+ value: 299.90
22
+ }
23
+ end
24
+
25
+ # Provision a new resource for this Organization
26
+ def build_resource(attributes)
27
+ false
28
+ end
29
+
30
+ # Terminate
31
+ def destroy_resource(resource)
32
+ return false
33
+ end
34
+
35
+ # Hash representation of the resource
36
+ def hash_for(resource)
37
+ resource
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,83 @@
1
+ module MnoEnterprise
2
+ class Nimbus::V4::UsersController < Nimbus::V4::BaseController
3
+
4
+ # POST /nimbus/v4/organization/:organization_id/users
5
+ # Create new resource and return representation
6
+ # --
7
+ # Override parent
8
+ # Attempt to fetch user via email before creation
9
+ def create
10
+ unless params[:data]
11
+ render_error VALIDATION_ERROR, 'Body does not contain the field: data'
12
+ return false
13
+ end
14
+ attributes = params[:data].select { |k, v| params_create_whitelist.include?(k.to_sym) }
15
+ email = attributes[:email]
16
+ user = MnoEnterprise::User.find_by(email: email)
17
+ if user
18
+ render_error VALIDATION_ERROR, "User [#{user.uid}] already exist with email: #{email}"
19
+ return
20
+ end
21
+
22
+
23
+ @resource ||= build_resource(attributes)
24
+
25
+ # Save resource or set error code and status
26
+ unless @resource.save
27
+ render_error VALIDATION_ERROR, @resource.errors
28
+ return
29
+ end
30
+
31
+ after_create_resource(@resource, attributes)
32
+
33
+ render_response(@resource)
34
+ end
35
+
36
+ private
37
+ def resource_klass
38
+ MnoEnterprise::User
39
+ end
40
+
41
+ # Define which attributes can be edited during creation
42
+ def params_create_whitelist
43
+ [:name, :surname, :email, :role]
44
+ end
45
+
46
+ # Retrieve all user
47
+ def get_resources
48
+ parent.users
49
+ end
50
+
51
+ # Retrieve a user via uid
52
+ def get_resource(uid)
53
+ MnoEnterprise::User.find_by(uid: uid)
54
+ end
55
+
56
+ # Build a resource for creation
57
+ def build_resource(attributes)
58
+ attrs = attributes.select { |k, v| params_create_whitelist.include?(k.to_sym) }
59
+ res = MnoEnterprise::User.new attrs
60
+ res.password = Devise.friendly_token.first(15)
61
+ res
62
+ end
63
+
64
+ # Update a resource
65
+ def update_resource(resource, attributes)
66
+ super(resource, attributes.reject { |k, v| k.to_sym == :role })
67
+ end
68
+
69
+ # Add user to the parent once created
70
+ def after_create_resource(resource, opts = {})
71
+ resource.free_trial_end_at = 15.years.from_now
72
+ # Add user to organization
73
+ parent.add_user resource
74
+ resource.save
75
+ end
76
+
77
+
78
+ # Resource JSON view
79
+ def hash_for(resource)
80
+ hash_for_user resource
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,6 @@
1
+ module MnoEnterprise
2
+ module Nimbus
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module MnoEnterprise
2
+ module Nimbus
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module MnoEnterprise
2
+ module Nimbus
3
+ class ApplicationMailer < ActionMailer::Base
4
+ default from: 'from@example.com'
5
+ layout 'mailer'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module MnoEnterprise
2
+ module Nimbus
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Mno enterprise nimbus</title>
5
+ <%= stylesheet_link_tag "mno_enterprise/nimbus/application", media: "all" %>
6
+ <%= javascript_include_tag "mno_enterprise/nimbus/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>