Class: Nimbus::V4::ResourceInstancesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/mno_enterprise/nimbus/v4/resource_instances_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /api/nimbus/v4/resource_instances Create new resource and return representation



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/mno_enterprise/nimbus/v4/resource_instances_controller.rb', line 6

def create

  unless attributes = params[:data]
    render_error VALIDATION_ERROR, 'Body does not contain the field: data'
    return false
  end

  unless attributes['organization']
    render_error VALIDATION_ERROR, 'Data does not contains the fields: organization'
    return false
  end
  users = attributes['users']
  unless users && users.any?
    render_error VALIDATION_ERROR, 'Resource should contain at least one user'
    return false
  end

  users_validation_errors = []
  users.each do |hash|
    email = hash[:email]
    user = MnoEnterprise::User.find_by(email: email)
    users_validation_errors << "User [#{user.uid}] already exist with email: #{email}" if user
  end
  if users_validation_errors.any?
    render_errors VALIDATION_ERROR, users_validation_errors
    return false
  end
  # Create organization
  organization = provision_organization(attributes['organization'])
  if organization.errors.any?
    render_errors VALIDATION_ERROR, organization.errors
    return false
  end

  # Create users - handle single object and array
  attributes['users'].each_with_index do |hash, index|
    hash = hash.merge('organization' => organization)
    # Create the first user as a Super Admin
    hash['role'] = 'Super Admin' if index == 0
    provision_user(hash)
  end

  # Create app_instances - handle single object and array
  [attributes['app_instances']].flatten.compact.each do |hash|
    organization.app_instances.create hash
  end

  after_create_resource(organization)

  render_response(organization.reload, 201)
end