From df4b85fc9c05da507b74d3910a86026e8a139d2e Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Sat, 11 Oct 2014 15:33:03 +0200 Subject: [PATCH] Add missing support.rb file --- src/sunstone/routes/support.rb | 192 +++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 src/sunstone/routes/support.rb diff --git a/src/sunstone/routes/support.rb b/src/sunstone/routes/support.rb new file mode 100644 index 0000000000..de7659fb20 --- /dev/null +++ b/src/sunstone/routes/support.rb @@ -0,0 +1,192 @@ +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'zendesk_api' + +helpers do + def zendesk_client + client = ZendeskAPI::Client.new do |config| + # Mandatory: + + config.url = "https://opennebula.zendesk.com/api/v2" # e.g. https://mydesk.zendesk.com/api/v2 + + # Basic / Token Authentication + config.username = session["zendesk_email"] + + # Choose one of the following depending on your authentication choice + # config.token = "your zendesk token" + config.password = session["zendesk_password"] + + # OAuth Authentication + # config.access_token = "your OAuth access token" + + # Optional: + + # Retry uses middleware to notify the user + # when hitting the rate limit, sleep automatically, + # then retry the request. + config.retry = true + + # Logger prints to STDERR by default, to e.g. print to stdout: + # require 'logger' + # config.logger = Logger.new(STDOUT) + + # Changes Faraday adapter + # config.adapter = :patron + + # Merged with the default client options hash + # config.client_options = { :ssl => false } + + # When getting the error 'hostname does not match the server certificate' + # use the API at https://yoursubdomain.zendesk.com/api/v2 + end + + if client.current_user.nil? || client.current_user.id.nil? + error 401, "Zendesk account credentials are incorrect" + else + return client + end + end + + def zrequest_to_one(zrequest) + one_zrequest = { + "id" => zrequest.id, + "url" => zrequest.url, + "subject" => zrequest.subject, + "description" => zrequest.description, + "status" => zrequest.status, + "created_at" => zrequest.created_at, + "updated_at" => zrequest.updated_at, + "comments" => [] + } + + zrequest.custom_fields.each { |field| + case field.id + when 391130 + one_zrequest["opennebula_version"] = field.value + when 391197 + one_zrequest["severity"] = field.value + end + } + + if zrequest.comments + comment = zrequest.comments.delete_at(0) + one_zrequest["html_description"] = comment.html_body + + zrequest.comments.each{ |comment| + one_zrequest["comments"] << { + "created_at" => comment.created_at, + "html_body" => comment.html_body, + "author_id" => comment.author_id, + "body" => comment.body + } + } + end + + return one_zrequest + end +end + +get '/support/request' do + zrequests = zendesk_client.requests({:status => "open,pending"}) + + open_requests = 0 + pending_requests = 0 + one_zrequests = { + "REQUEST_POOL" => { + "REQUEST" => [] + } + } + + zrequests.each { |zrequest| + if zrequest.status == "pending" + pending_requests += 1 + elsif zrequest.status == "open" + open_requests +=1 + end + + one_zrequests["REQUEST_POOL"]["REQUEST"] << zrequest_to_one(zrequest) + } + + one_zrequests["open_requests"] = open_requests + one_zrequests["pending_requests"] = pending_requests + + [200, JSON.pretty_generate(one_zrequests)] +end + +get '/support/request/:id' do + zrequest = zendesk_client.requests.find(:id => params[:id]) + # TODO check error + one_zrequest = { + "REQUEST" => zrequest_to_one(zrequest) + } + + [200, JSON.pretty_generate(one_zrequest)] +end + +post '/support/request' do + body_hash = JSON.parse(@request_body) + pp body_hash + STDOUT.flush + zrequest = zendesk_client.requests.create({ + :subject => body_hash['subject'], + :comment => { :value => body_hash['description'] }, + :custom_fields => [ + {:id => 391197, :value => body_hash['severity']}, + {:id => 391130, :value => body_hash['opennebula_version']} + ] + }) + + [201, JSON.pretty_generate(zrequest_to_one(zrequest))] +end + +post '/support/request/:id/action' do + body_hash = JSON.parse(@request_body) + if body_hash["action"]["params"]['comment'] + comment_value = body_hash["action"]["params"]['comment']['value'] + else + logger.error("[OpenNebula Support] Missing comment message") + error = Error.new(e.message) + error 403, error.to_json + end + + zrequest = zendesk_client.requests.find(:id => params[:id]) + # TODO check error + + zrequest.comment = {"value" => body_hash["action"]["params"]['comment']['value']} + zrequest.save! + + one_zrequest = { + "REQUEST" => zrequest_to_one(zrequest) + } + + [201, JSON.pretty_generate(one_zrequest)] +end + + +post '/support/credentials' do + body_hash = JSON.parse(@request_body) + if body_hash["email"].nil? || body_hash["password"].nil? + error 401, "Zendesk credentials not provided" + end + + session["zendesk_email"] = body_hash["email"] + session["zendesk_password"] = body_hash["password"] + + zendesk_client + + [201, ""] +end