Skip to content

Commit d8a532e

Browse files
committed
Adds script and endpoint to register subscriptions via script, fixes #23
Signed-off-by: Daniel Kastl <[email protected]>
1 parent 42354d2 commit d8a532e

File tree

8 files changed

+114
-25
lines changed

8 files changed

+114
-25
lines changed

app/controllers/subscription_templates_controller.rb

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class SubscriptionTemplatesController < ApplicationController
22
layout 'base'
33

4-
before_action :find_project_by_project_id, except: [:index]
4+
before_action :find_project_by_project_id, except: [:index, :set_subscription_id]
55
before_action :get_issue_statuses, only: [:new, :create, :edit, :update]
66
before_action :get_issue_priorities, only: [:new, :create, :edit, :update]
77
before_action :get_issue_categories, only: [:new, :create, :edit, :update]
8-
before_action :authorize
8+
9+
accept_api_auth :set_subscription_id
10+
before_action :authorize, except: [:set_subscription_id]
911

1012
helper_method :index_path
1113

@@ -52,6 +54,26 @@ def update_subscription_id
5254
end
5355
end
5456

57+
def set_subscription_id
58+
# Check if a valid API key is provided
59+
unless User.current.logged?
60+
render json: { error: 'API key is missing or invalid' }, status: :unauthorized
61+
return
62+
end
63+
64+
@subscription_template = SubscriptionTemplate.find(params[:subscription_template_id])
65+
66+
# Check if the user has permissions to manage subscription templates
67+
unless User.current.allowed_to?(:manage_subscription_templates, @subscription_template.project)
68+
render json: { error: 'You do not have permission to manage subscription templates' }, status: :forbidden
69+
return
70+
end
71+
72+
@subscription_template.update(subscription_id: params[:subscription_id])
73+
74+
render json: { message: 'Subscription ID updated successfully' }, status: :ok
75+
end
76+
5577
def destroy
5678
@subscription_template = find_subscription_template
5779
@subscription_template.destroy
@@ -94,7 +116,8 @@ def prepare_payload
94116
url: URI.join(request.base_url, "/fiware/subscription_template/#{@subscription_template.id}/notification").to_s,
95117
headers: {
96118
"Content-Type": "application/json",
97-
"X-Redmine-API-Key": User.find(@member.user_id).api_key
119+
"X-Redmine-API-Key": User.find(@member.user_id).api_key,
120+
"X-Redmine-GTT-Subscription-Template-URL": URI.join(request.base_url, "/fiware/subscription_template/#{@subscription_template.id}/registration/").to_s
98121
},
99122
method: "POST",
100123
json: {
@@ -159,11 +182,6 @@ def index_path
159182
settings_project_path(@project, tab: 'subscription_templates')
160183
end
161184

162-
def subscription_template_params
163-
params[:subscription_template][:alteration_types] ||= []
164-
params.require(:subscription_template).permit(:standard, :broker_url, :fiware_service, :fiware_servicepath, :subscription_id, :name, :expires, :status, :context, :entities_string, :attrs, :expression_query, :expression_georel, :expression_geometry, :expression_coords, :notify_on_metadata_change, :subject, :description, :attachments_string, :is_private, :project_id, :tracker_id, :version_id, :issue_status_id, :issue_category_id, :issue_priority_id, :member_id, :comment, alteration_types: [])
165-
end
166-
167185
def find_subscription_template
168186
subscription_template_scope.find params[:id]
169187
end
@@ -173,7 +191,7 @@ def find_project_by_project_id
173191
end
174192

175193
def subscription_template_scope
176-
SubscriptionTemplate.order(name: :asc).where(project_id: @project.id)
194+
SubscriptionTemplate.where(project_id: @project.id).order(name: :asc)
177195
end
178196

179197
def get_issue_statuses
@@ -188,4 +206,9 @@ def get_issue_priorities
188206
@issue_priorities = IssuePriority.all.sorted
189207
end
190208

209+
def subscription_template_params
210+
params[:subscription_template][:alteration_types] ||= []
211+
params.require(:subscription_template).permit(:standard, :broker_url, :fiware_service, :fiware_servicepath, :subscription_id, :name, :expires, :status, :context, :entities_string, :attrs, :expression_query, :expression_georel, :expression_geometry, :expression_coords, :notify_on_metadata_change, :subject, :description, :attachments_string, :is_private, :project_id, :tracker_id, :version_id, :issue_status_id, :issue_category_id, :issue_priority_id, :member_id, :comment, alteration_types: [])
212+
end
213+
191214
end

app/views/subscription_templates/unpublish.js.erb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,24 @@ var deleteRequestOptions = {
1111
// Send the DELETE request to the Orion broker
1212
fetch('<%= @broker_url %>', deleteRequestOptions)
1313
.then(response => {
14+
// Get the notification box
15+
var notification = document.getElementById('temporaryNotification');
16+
1417
if (!response.ok) {
15-
throw new Error('Network response was not ok');
18+
console.warn('Warning: Network response was not ok');
19+
notification.textContent = "<%= l(:subscription_unpublished_warning) %>";
20+
} else {
21+
notification.textContent = "<%= l(:subscription_unpublished) %>";
1622
}
1723

24+
// Show the notification box
25+
notification.classList.add('visible');
26+
27+
// Hide the notification box after 3 seconds
28+
setTimeout(function() {
29+
notification.classList.remove('visible');
30+
}, 3000);
31+
1832
// Send the PATCH request to update the subscription_id
1933
return fetch('<%= update_subscription_id_project_subscription_template_path(@project, @subscription_template) %>', {
2034
method: 'PATCH',
@@ -30,19 +44,5 @@ fetch('<%= @broker_url %>', deleteRequestOptions)
3044
// Update the subscription template list
3145
var subscriptionTemplateList = document.getElementById('subscriptionTemplateList');
3246
subscriptionTemplateList.innerHTML = result;
33-
34-
// Get the notification box
35-
var notification = document.getElementById('temporaryNotification');
36-
37-
// Change the text of the notification box
38-
notification.textContent = "<%= l(:subscription_unpublished) %>";
39-
40-
// Show the notification box
41-
notification.classList.add('visible');
42-
43-
// Hide the notification box after 3 seconds
44-
setTimeout(function() {
45-
notification.classList.remove('visible');
46-
}, 3000);
4747
})
4848
.catch(error => console.log('error', error));

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ en:
7373
link_to_unpublish: "Unpublish"
7474
link_to_unpublish_hint: "Unpublish subscription to broker"
7575
subscription_unpublished: "Subscription unpublished from broker"
76+
subscription_unpublished_warning: "There was a problem unpublishing the subscription from the broker"
7677

7778
gtt_fiware:
7879
attachement_not_found: "Attachment not found"

config/locales/ja.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ en:
7373
link_to_unpublish: "Unpublish"
7474
link_to_unpublish_hint: "Unpublish subscription to broker"
7575
subscription_unpublished: "Subscription unpublished from broker"
76+
subscription_unpublished_warning: "There was a problem unpublishing the subscription from the broker"
7677

7778
gtt_fiware:
7879
attachement_not_found: "Attachment not found"

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
# Define route for creating issues with a notification template
3434
scope 'fiware/subscription_template/:subscription_template_id' do
3535
post 'notification', to: 'subscription_issues#create'
36+
get 'registration/:subscription_id', to: 'subscription_templates#set_subscription_id'
3637
end
3738

3839
# Define a route for FIWARE broker subscription templates
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# The base URL for your Orion Context Broker
4+
ORION_URL=${ORION_URL:-"http://app.local:1026"}
5+
6+
# Fetch all subscription IDs using a GET request
7+
# Include additional headers as needed, e.g., for multi-tenant setups or authentication
8+
subscription_ids=$(curl -s -X GET "${ORION_URL}/v2/subscriptions" \
9+
-H "Accept: application/json" | jq -r '.[].id')
10+
11+
# Check if there are any subscriptions to delete
12+
if [ -z "$subscription_ids" ]; then
13+
echo "No subscriptions to delete."
14+
exit 0
15+
fi
16+
17+
# Loop through the subscription IDs and delete each one
18+
for id in $subscription_ids; do
19+
curl -X DELETE "${ORION_URL}/v2/subscriptions/${id}" \
20+
-H "Accept: application/json"
21+
echo "Deleted subscription $id"
22+
done
23+
24+
echo "All subscriptions have been deleted."
25+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# The API key for Redmine
4+
REDMINE_API_KEY=${REDMINE_API_KEY:-$1}
5+
6+
# Check if API key is provided
7+
if [ -z "$REDMINE_API_KEY" ]
8+
then
9+
echo "No API key provided. Usage: ./register_all_subscriptions.sh <api_key> or set the REDMINE_API_KEY environment variable."
10+
exit 1
11+
fi
12+
13+
# The base URL for your Orion Context Broker
14+
ORION_URL=${ORION_URL:-"http://app.local:1026"}
15+
16+
# Fetch all subscriptions using a GET request
17+
subscriptions=$(curl -s -X GET "${ORION_URL}/v2/subscriptions" -H "Accept: application/json")
18+
19+
# Use jq to parse the JSON response and filter subscriptions with the "X-Redmine-GTT-Subscription-Template-URL" header
20+
filtered_subscriptions=$(echo "$subscriptions" | jq -c '.[] | select(.notification.httpCustom.headers."X-Redmine-GTT-Subscription-Template-URL" != null)')
21+
22+
# Check if there are any filtered subscriptions
23+
if [ -z "$filtered_subscriptions" ]; then
24+
echo "No subscriptions with the 'X-Redmine-GTT-Subscription-Template-URL' header found."
25+
exit 0
26+
fi
27+
28+
# Loop through the filtered subscriptions
29+
echo "$filtered_subscriptions" | while read -r subscription; do
30+
# Extract the URL and append the subscription id
31+
url=$(echo "$subscription" | jq -r '(.notification.httpCustom.headers."X-Redmine-GTT-Subscription-Template-URL" + .id)')
32+
33+
# Execute a GET request to the URL
34+
response=$(curl -s -X GET "$url" -H "X-Redmine-API-Key: $REDMINE_API_KEY")
35+
36+
# Print a log message
37+
echo "GET request to $url completed with response: $response"
38+
done

init.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
context: %i( index )
3333
}, read: true
3434
permission :manage_subscription_templates, {
35-
subscription_templates: %i( new edit update create destroy copy publish unpublish update_subscription_id),
35+
subscription_templates: %i( new edit update create destroy copy publish unpublish update_subscription_id set_subscription_id),
3636
projects: %i( manage_subscription_templates )
3737
}, require: :member
3838
end

0 commit comments

Comments
 (0)