2017-04-28 04:06:18 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2017 - present Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-01-20 06:55:44 +08:00
|
|
|
module Services
|
|
|
|
class LiveEventsSubscriptionService
|
|
|
|
class << self
|
|
|
|
def available?
|
|
|
|
settings.present?
|
|
|
|
end
|
|
|
|
|
2017-02-09 03:04:05 +08:00
|
|
|
def tool_proxy_subscription(tool_proxy, subscription_id)
|
|
|
|
options = { headers: headers(tool_proxy_jwt_body(tool_proxy)) }
|
|
|
|
request(:get, "/api/subscriptions/#{subscription_id}", options)
|
|
|
|
end
|
|
|
|
|
2017-06-24 03:04:43 +08:00
|
|
|
def tool_proxy_subscriptions(tool_proxy, optional_headers = {})
|
2017-06-15 03:51:41 +08:00
|
|
|
options = { headers: headers(tool_proxy_jwt_body(tool_proxy), optional_headers) }
|
2017-01-20 06:55:44 +08:00
|
|
|
request(:get, '/api/subscriptions', options)
|
|
|
|
end
|
|
|
|
|
Add create to live event subscription service
fixes PLAT-2184
Test plan:
* With the subscription service and canvas configured to use the same
encryption and signing secret
* Start up the subscription service and a rails console
* In the console run the following commands
ToolProxy = Struct.new("ToolProxy", :guid, :product_family)
Family = Struct.new("Family", :developer_key)
f = Family.new(10000000000003)
tp = ToolProxy.new('hahahah', f)
subscription = {
"RootAccountId" => "1",
"EventTypes" => ["submission_created"],
"ContextType" => "quiz",
"ContextId" => "5001",
"Format" => "live-event",
"TransportType" => "sqs",
"TransportMetadata" => {
"Url" => "http://sqs.docker"
},
"UserId" => "3000",
}
res = Services::LiveEventsSubscriptionService.create_tool_proxy_subscription(tp, subscription)
* Ensure that you get a 200 response back with the newly created
subscription in it
* Ensure that dynamo has the new record
Change-Id: Ifa7f68983efbcb79058854fbd3ba802505b8d4b2
Reviewed-on: https://gerrit.instructure.com/101069
Tested-by: Jenkins
Reviewed-by: Matthew Wheeler <mwheeler@instructure.com>
Reviewed-by: Nathan Mills <nathanm@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Andrew Butterfield <abutterfield@instructure.com>
2017-02-04 06:57:17 +08:00
|
|
|
def create_tool_proxy_subscription(tool_proxy, subscription)
|
|
|
|
options = {
|
2017-02-09 03:04:05 +08:00
|
|
|
headers: headers(tool_proxy_jwt_body(tool_proxy), { 'Content-Type' => 'application/json' }),
|
Add create to live event subscription service
fixes PLAT-2184
Test plan:
* With the subscription service and canvas configured to use the same
encryption and signing secret
* Start up the subscription service and a rails console
* In the console run the following commands
ToolProxy = Struct.new("ToolProxy", :guid, :product_family)
Family = Struct.new("Family", :developer_key)
f = Family.new(10000000000003)
tp = ToolProxy.new('hahahah', f)
subscription = {
"RootAccountId" => "1",
"EventTypes" => ["submission_created"],
"ContextType" => "quiz",
"ContextId" => "5001",
"Format" => "live-event",
"TransportType" => "sqs",
"TransportMetadata" => {
"Url" => "http://sqs.docker"
},
"UserId" => "3000",
}
res = Services::LiveEventsSubscriptionService.create_tool_proxy_subscription(tp, subscription)
* Ensure that you get a 200 response back with the newly created
subscription in it
* Ensure that dynamo has the new record
Change-Id: Ifa7f68983efbcb79058854fbd3ba802505b8d4b2
Reviewed-on: https://gerrit.instructure.com/101069
Tested-by: Jenkins
Reviewed-by: Matthew Wheeler <mwheeler@instructure.com>
Reviewed-by: Nathan Mills <nathanm@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Andrew Butterfield <abutterfield@instructure.com>
2017-02-04 06:57:17 +08:00
|
|
|
body: subscription.to_json
|
|
|
|
}
|
|
|
|
request(:post, '/api/subscriptions', options)
|
|
|
|
end
|
|
|
|
|
Add ability to update a subscription
fixes PLAT-2239
Test plan:
* With the subscription service and canvas configured to use the same
encryption key and signing secret
* Start up the subscription service and a rails console
* In the console run the following commands being sure to use your
developer key and the right subscription Id
ToolProxy = Struct.new("ToolProxy", :guid, :product_family)
Family = Struct.new("Family", :developer_key)
f = Family.new(10000000000003)
tp = ToolProxy.new('hahahah', f)
subscription = {
"Id" => "some uuid",
"RootAccountId" => "1",
"EventTypes" => ["submission_created"],
"ContextType" => "course",
"ContextId" => "5001",
"Format" => "live-event",
"TransportType" => "sqs",
"TransportMetadata" => {
"Url" => "http://sqs.docker"
},
}
res = Services::LiveEventsSubscriptionService.update_tool_proxy_subscription(tp, "the subscription id", subscription)
* Ensure that you get a 200 back with a copy of the updated subscription
* Ensure that dynamo has the full subscription persisted with the
changes you made
Change-Id: Ia2fd506f45f414e5a1940a9eef978d1b4c566397
Reviewed-on: https://gerrit.instructure.com/102341
Tested-by: Jenkins
Reviewed-by: Nathan Mills <nathanm@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Andrew Butterfield <abutterfield@instructure.com>
2017-02-16 06:34:31 +08:00
|
|
|
def update_tool_proxy_subscription(tool_proxy, subscription_id, subscription)
|
|
|
|
options = {
|
|
|
|
headers: headers(tool_proxy_jwt_body(tool_proxy), { 'Content-Type' => 'application/json' }),
|
|
|
|
body: subscription.to_json
|
|
|
|
}
|
|
|
|
request(:put, "/api/subscriptions/#{subscription_id}", options)
|
|
|
|
end
|
|
|
|
|
2017-02-09 03:12:11 +08:00
|
|
|
def destroy_tool_proxy_subscription(tool_proxy, subscription_id)
|
|
|
|
options = { headers: headers(tool_proxy_jwt_body(tool_proxy)) }
|
|
|
|
request(:delete, "/api/subscriptions/#{subscription_id}", options)
|
|
|
|
end
|
|
|
|
|
2017-04-18 03:51:48 +08:00
|
|
|
def destroy_all_tool_proxy_subscriptions(tool_proxy)
|
|
|
|
options = { headers: headers(tool_proxy_jwt_body(tool_proxy)) }
|
|
|
|
request(:delete, "/api/subscriptions", options)
|
|
|
|
end
|
|
|
|
|
2017-01-20 06:55:44 +08:00
|
|
|
private
|
|
|
|
def request(method, endpoint, options = {})
|
2017-01-27 07:29:30 +08:00
|
|
|
Canvas.timeout_protection("live-events-subscription-service-session", raise_on_timeout: true) do
|
|
|
|
HTTParty.send(method, "#{settings['app-host']}#{endpoint}", options.merge(timeout: 10))
|
|
|
|
end
|
2017-01-20 06:55:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def headers(jwt_body, headers = {})
|
|
|
|
token = Canvas::Security::ServicesJwt.generate(jwt_body)
|
|
|
|
headers['Authorization'] = "Bearer #{token}"
|
|
|
|
headers
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings
|
2017-09-08 05:11:43 +08:00
|
|
|
Canvas::DynamicSettings.find("live-events-subscription-service", default_ttl: 5.minutes)
|
2017-01-12 00:13:28 +08:00
|
|
|
rescue Imperium::TimeoutError => e
|
2017-01-20 06:55:44 +08:00
|
|
|
Canvas::Errors.capture_exception(:live_events_subscription, e)
|
|
|
|
nil
|
|
|
|
end
|
2017-02-09 03:04:05 +08:00
|
|
|
|
|
|
|
def tool_proxy_jwt_body(tool_proxy, options = {})
|
|
|
|
options.merge({
|
|
|
|
sub: "ltiToolProxy:#{tool_proxy.guid}",
|
2017-02-24 04:40:44 +08:00
|
|
|
DeveloperKey: tool_proxy.product_family.developer_key.global_id.to_s,
|
2017-04-08 01:05:31 +08:00
|
|
|
RootAccountId: (tool_proxy.context.global_root_account_id || tool_proxy.context.global_id).to_s,
|
|
|
|
RootAccountUUID: tool_proxy.context.root_account.uuid
|
2017-02-09 03:04:05 +08:00
|
|
|
})
|
|
|
|
end
|
2017-01-20 06:55:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|