Class: Orchestrator::Api::SystemsController

Inherits:
Orchestrator::ApiController show all
Defined in:
app/controllers/orchestrator/api/systems_controller.rb

Instance Method Summary collapse

Methods inherited from Base

#options

Instance Method Details

#countObject

return the count of a module type in a system



185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 185

def count
    params.require(:module)
    sys = System.get(id)
    if sys
        mod = params.permit(:module)[:module]
        render json: {count: sys.count(mod)}
    else
        render nothing: true, status: :not_found
    end
end

#createObject



77
78
79
80
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 77

def create
    cs = ControlSystem.new(safe_params)
    save_and_respond cs
end

#destroyObject



82
83
84
85
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 82

def destroy
    @cs.delete # expires the cache in after callback
    render :nothing => true
end

#execObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 113

def exec
    # Run a function in a system module (async request)
    params.require(:module)
    params.require(:method)
    sys = System.get(id)
    if sys
        para = params.permit(:module, :index, :method, {args: []}).tap do |whitelist|
            whitelist[:args] = params[:args]
        end
        index = para[:index]
        mod = sys.get(para[:module].to_sym, index.nil? ? 0 : (index.to_i - 1))
        if mod
            user = current_user
            mod.thread.schedule do
                perform_exec(mod, para, user)
            end
            throw :async
        else
            render nothing: true, status: :not_found
        end
    else
        render nothing: true, status: :not_found
    end
end

#funcsObject

returns a list of functions available to call



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
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 158

def funcs
    params.require(:module)
    sys = System.get(id)
    if sys
        para = params.permit(:module, :index)
        index = para[:index]
        index = index.nil? ? 0 : (index.to_i - 1);

        mod = sys.get(para[:module].to_sym, index)
        if mod
            funcs = mod.instance.public_methods(false)
            priv = []
            funcs.each do |func|
                if ::Orchestrator::Core::PROTECTED[func]
                    priv << func
                end
            end
            render json: (funcs - priv)
        else
            render nothing: true, status: :not_found
        end
    else
        render nothing: true, status: :not_found
    end
end

#indexObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 15

def index
    query = @@elastic.query(params)
    query.sort = [{name: "asc"}]

    # Filter systems via zone_id
    if params.has_key? :zone_id
        zone_id = params.permit(:zone_id)[:zone_id]
        query.filter({
            zones: [zone_id]
        })
    end

    # filter via module_id
    if params.has_key? :module_id
        module_id = params.permit(:module_id)[:module_id]
        query.filter({
            modules: [module_id]
        })
    end

    respond_with @@elastic.search(query)
end

#removeObject

Removes the module from the system and deletes it if not used elsewhere



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 55

def remove
    module_id = params.permit(:module_id)[:module_id]
    mod = ::Orchestrator::Module.find module_id

    if @cs.modules.include? module_id
        remove = true

        @cs.modules.delete(module_id)
        @cs.save!

        ControlSystem.using_module(module_id).each do |cs|
            if cs.id != @cs.id
                remove = false
                break
            end
        end

        mod.delete if remove
    end
    render :nothing => true
end

#showObject



38
39
40
41
42
43
44
45
46
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 38

def show
    if params.has_key? :complete
        respond_with @cs, {
            methods: [:module_data, :zone_data]
        }
    else
        respond_with @cs
    end
end

#startObject

Additional Functions:



92
93
94
95
96
97
98
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 92

def start
    # Start all modules in the system
    @cs.modules.each do |mod_id|
        load_and_start mod_id
    end
    render :nothing => true
end

#stateObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 138

def state
    # Status defined as a system module
    params.require(:module)
    params.require(:lookup)
    sys = System.get(id)
    if sys
        para = params.permit(:module, :index, :lookup)
        index = para[:index]
        mod = sys.get(para[:module].to_sym, index.nil? ? 0 : (index.to_i - 1))
        if mod
            render json: mod.status[para[:lookup].to_sym]
        else
            render nothing: true, status: :not_found
        end
    else
        render nothing: true, status: :not_found
    end
end

#stopObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 100

def stop
    # Stop all modules in the system (shared or not)
    @cs.modules.each do |mod_id|
        mod = control.loaded? mod_id
        if mod
            mod.thread.next_tick do
                mod.stop
            end
        end
    end
    render :nothing => true
end

#typesObject

return the list of a module types in a system



197
198
199
200
201
202
203
204
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 197

def types
    sys = System.get(id)
    if sys
        render json: sys.modules
    else
        render nothing: true, status: :not_found
    end
end

#updateObject



48
49
50
51
52
# File 'app/controllers/orchestrator/api/systems_controller.rb', line 48

def update
    @cs.update(safe_params)
    #save_and_respond(@cs) # save deletes the system cache
    respond_with :api, @cs
end