forked from rucio/rucio
/
account.py
265 lines (197 loc) · 9.07 KB
/
account.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# 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.
from typing import TYPE_CHECKING
import rucio.api.permission
import rucio.common.exception
import rucio.core.identity
from rucio.common.schema import validate_schema
from rucio.common.types import InternalAccount
from rucio.common.utils import api_update_return_dict
from rucio.core import account as account_core
from rucio.core.rse import get_rse_id
from rucio.db.sqla.constants import AccountType
from rucio.db.sqla.session import read_session, stream_session, transactional_session
if TYPE_CHECKING:
from sqlalchemy.orm import Session
@transactional_session
def add_account(account, type_, email, issuer, vo='def', *, session: "Session"):
"""
Creates an account with the provided account name, contact information, etc.
:param account: The account name.
:param type_: The account type
:param email: The Email address associated with the account.
:param issuer: The issuer account_core.
:param vo: The VO to act on.
:param session: The database session in use.
"""
validate_schema(name='account', obj=account, vo=vo)
kwargs = {'account': account, 'type': type_}
if not rucio.api.permission.has_permission(issuer=issuer, vo=vo, action='add_account', kwargs=kwargs, session=session):
raise rucio.common.exception.AccessDenied('Account %s can not add account' % (issuer))
account = InternalAccount(account, vo=vo)
account_core.add_account(account, AccountType[type_.upper()], email, session=session)
@transactional_session
def del_account(account, issuer, vo='def', *, session: "Session"):
"""
Disables an account with the provided account name.
:param account: The account name.
:param issuer: The issuer account.
:param vo: The VO to act on.
:param session: The database session in use.
"""
kwargs = {'account': account}
if not rucio.api.permission.has_permission(issuer=issuer, vo=vo, action='del_account', kwargs=kwargs, session=session):
raise rucio.common.exception.AccessDenied('Account %s can not delete account' % (issuer))
account = InternalAccount(account, vo=vo)
account_core.del_account(account, session=session)
@read_session
def get_account_info(account, vo='def', *, session: "Session"):
"""
Returns the info like the statistics information associated to an account_core.
:param account: The account name.
:returns: A list with all account information.
:param vo: The VO to act on.
:param session: The database session in use.
"""
account = InternalAccount(account, vo=vo)
acc = account_core.get_account(account, session=session)
acc.account = acc.account.external
return acc
@transactional_session
def update_account(account, key, value, issuer='root', vo='def', *, session: "Session"):
""" Update a property of an account_core.
:param account: Name of the account_core.
:param key: Account property like status.
:param value: Property value.
:param issuer: The issuer account
:param vo: The VO to act on.
:param session: The database session in use.
"""
validate_schema(name='account', obj=account, vo=vo)
kwargs = {}
if not rucio.api.permission.has_permission(issuer=issuer, vo=vo, action='update_account', kwargs=kwargs, session=session):
raise rucio.common.exception.AccessDenied('Account %s can not change %s of the account' % (issuer, key))
account = InternalAccount(account, vo=vo)
return account_core.update_account(account, key, value, session=session)
@stream_session
def list_accounts(filter_={}, vo='def', *, session: "Session"):
"""
Lists all the Rucio account names.
REST API: http://<host>:<port>/rucio/accounts
:param filter_: Dictionary of attributes by which the input data should be filtered
:param vo: The VO to act on.
:param session: The database session in use.
:returns: List of all accounts.
"""
# If filter is empty, create a new dict to avoid overwriting the function's default
if not filter_:
filter_ = {}
if 'account' in filter_:
filter_['account'] = InternalAccount(filter_['account'], vo=vo)
else:
filter_['account'] = InternalAccount(account='*', vo=vo)
for result in account_core.list_accounts(filter_=filter_, session=session):
yield api_update_return_dict(result, session=session)
@read_session
def account_exists(account, vo='def', *, session: "Session"):
"""
Checks to see if account exists. This procedure does not check it's status.
:param account: Name of the account.
:param vo: The VO to act on.
:param session: The database session in use.
:returns: True if found, otherwise false.
"""
account = InternalAccount(account, vo=vo)
return account_core.account_exists(account, session=session)
@read_session
def list_identities(account, vo='def', *, session: "Session"):
"""
List all identities on an account_core.
:param account: The account name.
:param vo: The VO to act on.
:param session: The database session in use.
"""
account = InternalAccount(account, vo=vo)
return account_core.list_identities(account, session=session)
@read_session
def list_account_attributes(account, vo='def', *, session: "Session"):
"""
Returns all the attributes for the given account.
:param account: The account name.
:param vo: The VO to act on
:param session: The database session in use.
"""
account = InternalAccount(account, vo=vo)
return account_core.list_account_attributes(account, session=session)
@transactional_session
def add_account_attribute(key, value, account, issuer, vo='def', *, session: "Session"):
"""
Add an attribute to an account.
:param key: attribute key.
:param value: attribute value.
:param account: The account name.
:param issuer: The issuer account.
:param vo: The VO to act on.
:param session: The database session in use.
"""
validate_schema(name='account_attribute', obj=key, vo=vo)
validate_schema(name='account_attribute', obj=value, vo=vo)
kwargs = {'account': account, 'key': key, 'value': value}
if not rucio.api.permission.has_permission(issuer=issuer, vo=vo, action='add_attribute', kwargs=kwargs, session=session):
raise rucio.common.exception.AccessDenied('Account %s can not add attributes' % (issuer))
account = InternalAccount(account, vo=vo)
account_core.add_account_attribute(account, key, value, session=session)
@transactional_session
def del_account_attribute(key, account, issuer, vo='def', *, session: "Session"):
"""
Delete an attribute to an account.
:param key: attribute key.
:param account: The account name.
:param issuer: The issuer account.
:param vo: The VO to act on.
:param session: The database session in use.
"""
kwargs = {'account': account, 'key': key}
if not rucio.api.permission.has_permission(issuer=issuer, vo=vo, action='del_attribute', kwargs=kwargs, session=session):
raise rucio.common.exception.AccessDenied('Account %s can not delete attribute' % (issuer))
account = InternalAccount(account, vo=vo)
account_core.del_account_attribute(account, key, session=session)
@read_session
def get_usage(rse, account, issuer, vo='def', *, session: "Session"):
"""
Returns current values of the specified counter, or raises CounterNotFound if the counter does not exist.
:param rse: The RSE.
:param account: The account name.
:param issuer: The issuer account.
:param vo: The VO to act on.
:param session: The database session in use.
:returns: A dictionary with total and bytes.
"""
rse_id = get_rse_id(rse=rse, vo=vo, session=session)
account = InternalAccount(account, vo=vo)
return account_core.get_usage(rse_id, account, session=session)
@read_session
def get_usage_history(rse, account, issuer, vo='def', *, session: "Session"):
"""
Returns historical values of the specified counter, or raises CounterNotFound if the counter does not exist.
:param rse: The RSE.
:param account: The account name.
:param issuer: The issuer account.
:param vo: The VO to act on.
:param session: The database session in use.
:returns: A dictionary with total and bytes.
"""
rse_id = get_rse_id(rse=rse, vo=vo, session=session)
account = InternalAccount(account, vo=vo)
return account_core.get_usage_history(rse_id, account, session=session)