Class: Jpi::V1::Impac::KpisController

Inherits:
BaseResourceController
  • Object
show all
Defined in:
app/controllers/mno_enterprise/jpi/v1/impac/kpis_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /jpi/v1/impac/dashboards/:dashboard_id/kpis



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/mno_enterprise/jpi/v1/impac/kpis_controller.rb', line 6

def create
  whitelist = %w(name endpoint source element_watched targets metadata extra_params)
  attrs = (params[:kpi] || {}).select { |k,v| whitelist.include?(k.to_s) } 

  if dashboard
    authorize! :manage_impac, dashboard

    # TODO: Her will perform a request there which could be avoided
    @kpi = dashboard.kpis.create(attrs)
    if @kpi
      render 'show'
    else
      render json: @kpi.errors, status: :bad_request
    end
  else
    render json: { errors: "Dashboard id #{params[:dashboard_id]} doesn't exist" }, status: :not_found
  end
end

#destroyObject

DELETE /jpi/v1/impac/kpis/:id



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/mno_enterprise/jpi/v1/impac/kpis_controller.rb', line 52

def destroy
  # Will call GET kpi route on Maestrano
  @kpi = MnoEnterprise::Impac::Kpi.find(params[:id])
  
  if @kpi
    authorize! :manage_impac, @kpi.dashboard
    if @kpi.destroy
      head status: :ok
    else
      render json: { errors: 'Unable to delete this widget' }, status: :bad_request
    end
  else
    render json: {errors: "Kpi id #{params[:id]} doesn't exist" }, status: :not_found
  end
end

#updateObject

PUT /jpi/v1/impac/kpis/:id



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/mno_enterprise/jpi/v1/impac/kpis_controller.rb', line 26

def update
  whitelist = %w(name element_watched targets extra_params)
  attrs = (params[:kpi] || {}).select { |k,v| whitelist.include?(k.to_s) }.symbolize_keys 
  
  # Find kpi and assign
  # Will call GET kpi route on Maestrano
  @kpi = Impac::Kpi.find(params[:id])
  authorize! :manage_impac, @kpi.dashboard
  if @kpi
    # metadata will be merged instead of replaced
    p = HashWithIndifferentAccess.new(params[:kpi])
    if p[:metadata] && p[:metadata].is_a?(Hash)
      attrs[:metadata] = @kpi.settings.merge(p[:metadata])
    end

    if @kpi.update(attrs)
      render 'show'
    else
      render json: @kpi.errors, status: :bad_request
    end
  else
    render json: { errors: "Kpi id #{params[:id]} doesn't exist" }, status: :not_found
  end
end