-
Notifications
You must be signed in to change notification settings - Fork 59
/
subscription_mgmt.py
211 lines (186 loc) · 6.88 KB
/
subscription_mgmt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""This is the demo script to show how administrator can manage subscriptions.
This script will not work without replacing parameters with real values.
Its basic goal is to present what can be done with this module and to
ease its usage.
"""
from mstrio.distribution_services import (
CacheType,
CacheUpdateSubscription,
Content,
EmailSubscription,
FileSubscription,
FTPSubscription,
HistoryListSubscription,
list_subscriptions,
Subscription,
SubscriptionManager
)
from mstrio.users_and_groups.user import User
from mstrio.connection import get_connection
# Define a variable which can be later used in a script
PROJECT_NAME = $project_name # Project to connect to
# Create connection based on workstation data
CONN = get_connection(workstationData, project_name=PROJECT_NAME)
# Define variables which can be later used in a script
EMAIL_SUBSCRIPTION_NAME = $email_subscription_name
# see distribution_services/subscription/content.py for available options
FORMAT_TYPE = $format_type
EMAIL_SUBJECT = $email_subject
SCHEDULE_ID = $schedule_id
CONTENT_ID = $content_id
# see distribution_services/subscription/content.py for available options
CONTENT_TYPE = $content_type
RECIPIENT_ID = $recipient_id
RECIPIENT_ID_2 = $recipient_id_2
RECIPIENT_ID_3 = $recipient_id_3
# Create an email subscription
EMAIL_SUB = EmailSubscription.create(
connection=CONN,
name=EMAIL_SUBSCRIPTION_NAME,
project_name=PROJECT_NAME,
contents=Content(
id=CONTENT_ID,
type=CONTENT_TYPE,
personalization=Content.Properties(format_type=FORMAT_TYPE)
),
schedules=[SCHEDULE_ID],
recipients=[RECIPIENT_ID],
email_subject=EMAIL_SUBJECT
)
# Define variables which can be later used in a script
FILE_SUBSCRIPTION_NAME = $file_subscription_name
FILE_NAME = $file_name
ZIP_FILE_NAME = $zip_file_name
ZIP_PASSWORD = $zip_password
# Create a file subscription
FILE_SUB = FileSubscription.create(
connection=CONN,
name=FILE_SUBSCRIPTION_NAME,
project_name=PROJECT_NAME,
contents=Content(
id=CONTENT_ID,
type=CONTENT_TYPE,
personalization=Content.Properties(format_type=FORMAT_TYPE)
),
schedules=[SCHEDULE_ID],
recipients=[RECIPIENT_ID_3],
filename=FILE_NAME,
zip_password_protect=True,
zip_filename=ZIP_FILE_NAME,
zip_password=ZIP_PASSWORD
)
# Define variables which can be later used in a script
FTP_SUBSCRIPTION_NAME = $ftp_subscription_name
FTP_ADDRESS_NAME = $ftp_address_name
PHYSICAL_ADDRESS = $physical_address
DEVICE_ID = $ftp_device_id
# FTP sub needs a user with FTP Device address
# Create FTP address for user
USER = User(connection=CONN, id=RECIPIENT_ID_3)
USER.add_address(
name=FTP_ADDRESS_NAME,
address=PHYSICAL_ADDRESS,
delivery_type='FTP',
device_id=DEVICE_ID,
)
# Create an ftp subscription
FTP_SUB = FTPSubscription.create(
connection=CONN,
name=FTP_SUBSCRIPTION_NAME,
project_name=PROJECT_NAME,
contents=Content(
id=CONTENT_ID,
type=CONTENT_TYPE,
personalization=Content.Properties(format_type=FORMAT_TYPE)
),
schedules=[SCHEDULE_ID],
recipients=[RECIPIENT_ID_3],
filename=FILE_NAME,
zip_password_protect=True,
zip_filename=ZIP_FILE_NAME,
zip_password=ZIP_PASSWORD
)
# Define variables which can be later used in a script
HISTORY_LIST_SUBSCRIPTION_NAME = $history_list_subscription_name
DELIVERY_EXPIRATION_DATE = $delivery_expiration_date
# Create a history list subscription
HL_SUB = HistoryListSubscription.create(
connection=CONN,
name=HISTORY_LIST_SUBSCRIPTION_NAME,
project_name=PROJECT_NAME,
contents=Content(
id=CONTENT_ID,
type=CONTENT_TYPE,
personalization=Content.Properties(format_type=FORMAT_TYPE)
),
schedules=[SCHEDULE_ID],
recipients=[RECIPIENT_ID_3],
do_not_create_update_caches=True,
overwrite_older_version=True,
re_run_hl=True
)
# Define variables which can be later used in a script
CACHE_SUBSCRIPTION_NAME = $cache_subscription_name
DELIVERY_EXPIRATION_TIME_ZONE = $delivery_expiration_time_zone
# Create a cache update subscription
CACHE_UPDATE_SUB = CacheUpdateSubscription.create(
connection=CONN,
project_name=PROJECT_NAME,
name=CACHE_SUBSCRIPTION_NAME,
contents=Content(
id=CONTENT_ID,
type=CONTENT_TYPE,
personalization=Content.Properties(format_type=Content.Properties.FormatType.PDF),
),
schedules=[SCHEDULE_ID],
delivery_expiration_date=DELIVERY_EXPIRATION_DATE,
delivery_expiration_timezone=DELIVERY_EXPIRATION_TIME_ZONE,
send_now=True,
recipients=[RECIPIENT_ID_3],
cache_cache_type=CacheType.RESERVED
) # see distribution_services/subscription/delivery.py for available options
# Define variables which can be later used in a script
CACHE_SUBSCRIPTION_NEW_NAME = $cache_subscription_new_name
OWNER_ID = $owner_id
# Change name and owner of a subscription
CACHE_UPDATE_SUB.alter(name=CACHE_SUBSCRIPTION_NEW_NAME, owner_id=OWNER_ID)
# List a particular subscription type, for example Cache Update
CACHE_UPDATE_SUBS = [
sub for sub in list_subscriptions(CONN, project_name=PROJECT_NAME)
if isinstance(sub, CacheUpdateSubscription)
]
# Initialize manager for subscriptions on a chosen project
sub_mngr = SubscriptionManager(connection=CONN, project_name=PROJECT_NAME)
# Get all subscriptions from the given project (it is possible in two ways)
all_subs = list_subscriptions(connection=CONN, project_name=PROJECT_NAME)
all_subs = sub_mngr.list_subscriptions()
# Execute/delete subscriptions by passing theirs ids or Subscription objects
sub_mngr.execute([EMAIL_SUB.id])
sub_mngr.delete([EMAIL_SUB, FTP_SUB, FILE_SUB, CACHE_UPDATE_SUB], force=True)
# List available recipients of the subscription for the given content (default
# delivery type is an email)
sub_mngr.available_recipients(content_id=CONTENT_ID, content_type=CONTENT_TYPE)
# Get a single subscription
sub = Subscription(connection=CONN, subscription_id=HL_SUB.id, project_name=PROJECT_NAME)
# List all recipients of the given subscription and all available for this
# subscription
sub.recipients
sub.available_recipients()
# Define variables which can be later used in a script
RECIPIENT_ID_4 = $recipient_id_4
# Add/remove recipient(s) with given id(s)
sub.add_recipient(recipients=[RECIPIENT_ID_4])
sub.remove_recipient(recipients=[RECIPIENT_ID_4])
# As an alternate to subscription manager you can execute or delete a subscription
# directly from the subscription itself
sub.execute()
sub.delete(force=True)
# Define variables which can be later used in a script
USER_TO_REMOVE_ID = $user_to_remove_id
ADMIN_USER_ID = $admin_user_id
# Replace a user with an admin in all of its subscriptions (e.g. when user exits
# company)
for s in sub_mngr.list_subscriptions(to_dictionary=False):
if USER_TO_REMOVE_ID in [r['id'] for r in s.recipients]:
s.add_recipient(recipients=ADMIN_USER_ID)
s.remove_recipient(recipients=USER_TO_REMOVE_ID)