Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
brian-dashboard-manager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
brian
brian-dashboard-manager
Commits
e60429c5
Commit
e60429c5
authored
9 months ago
by
Bjarke Madsen
Browse files
Options
Downloads
Patches
Plain Diff
support both grafana 8.2.5 and >11.0
parent
a387c1f8
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
brian_dashboard_manager/grafana/organization.py
+73
-0
73 additions, 0 deletions
brian_dashboard_manager/grafana/organization.py
brian_dashboard_manager/grafana/provision.py
+22
-7
22 additions, 7 deletions
brian_dashboard_manager/grafana/provision.py
with
95 additions
and
7 deletions
brian_dashboard_manager/grafana/organization.py
+
73
−
0
View file @
e60429c5
...
...
@@ -6,6 +6,7 @@ Grafana Organization management helpers.
import
logging
import
random
import
string
from
requests.exceptions
import
HTTPError
from
datetime
import
datetime
from
typing
import
Dict
,
List
,
Union
...
...
@@ -136,6 +137,78 @@ def delete_expired_api_tokens(request: AdminRequest) -> bool:
return
True
def
get_or_create_service_account
(
request
:
AdminRequest
,
org_id
):
"""
Gets a service account for the given organization, or creates one if it does not exist.
:param request: AdminRequest object
:param org_id: organization ID
:param name: service account name
:return: service account definition
"""
switch_active_organization
(
request
,
org_id
)
# get provision service account, if it exists
try
:
service_accounts
=
request
.
get
(
'
api/serviceaccounts?perpage=10&page=1&query=provision
'
).
json
()
if
service_accounts
and
service_accounts
.
get
(
'
totalCount
'
)
>
0
:
service_account
=
service_accounts
.
get
(
'
serviceAccounts
'
)[
0
]
return
service_account
except
HTTPError
as
e
:
if
e
.
response
.
status_code
!=
404
:
raise
e
# create a service account for provisioning
try
:
result
=
request
.
post
(
'
api/serviceaccounts
'
,
json
=
{
'
name
'
:
'
provision
'
,
'
role
'
:
'
Admin
'
,
'
isDisabled
'
:
False
,
}).
json
()
except
HTTPError
as
e
:
print
(
e
)
logger
.
info
(
f
'
Created provision service account for organization #
{
org_id
}
'
)
return
result
def
create_service_account_token
(
request
:
AdminRequest
,
service_account_id
:
int
):
"""
Creates a new API token for the given service account.
:param request: AdminRequest object
:param service_account_id: service account ID
:return: Token definition
"""
data
=
{
'
name
'
:
f
'
provision-token-
{
datetime
.
now
().
isoformat
()
}
'
,
}
result
=
request
.
post
(
f
'
api/serviceaccounts/
{
service_account_id
}
/tokens
'
,
json
=
data
).
json
()
token_id
=
result
.
get
(
'
id
'
)
logger
.
debug
(
f
'
Created API token #
{
token_id
}
for service account #
{
service_account_id
}
'
)
return
result
def
delete_service_account
(
request
:
AdminRequest
,
service_account_id
:
int
):
"""
Deletes a service account with the given ID.
:param request: AdminRequest object
:param service_account_id: service account ID
:return: delete response
"""
assert
service_account_id
is
not
None
result
=
request
.
delete
(
f
'
api/serviceaccounts/
{
service_account_id
}
'
)
logger
.
debug
(
f
'
Deleted service account #
{
service_account_id
}
'
)
return
result
def
set_home_dashboard
(
request
:
TokenRequest
,
is_staff
):
"""
Sets the home dashboard for the organization
...
...
This diff is collapsed.
Click to expand it.
brian_dashboard_manager/grafana/provision.py
+
22
−
7
View file @
e60429c5
...
...
@@ -18,7 +18,8 @@ from brian_dashboard_manager.services.api import fetch_services
from
brian_dashboard_manager.grafana.organization
import
\
get_organizations
,
create_organization
,
create_api_token
,
\
delete_api_token
,
delete_expired_api_tokens
,
set_home_dashboard
delete_api_token
,
delete_expired_api_tokens
,
set_home_dashboard
,
\
get_or_create_service_account
,
delete_service_account
,
create_service_account_token
from
brian_dashboard_manager.grafana.dashboard
import
list_dashboards
,
\
get_dashboard_definitions
,
create_dashboard
,
delete_dashboard
from
brian_dashboard_manager.grafana.datasource
import
\
...
...
@@ -814,10 +815,13 @@ def provision(config, raise_exceptions=False):
"""
start
=
time
.
time
()
token
s
=
[]
account
s
=
[]
all_orgs
=
_provision_orgs
(
config
)
request
=
AdminRequest
(
**
config
)
delete_expired_api_tokens
(
request
)
try
:
delete_expired_api_tokens
(
request
)
except
Exception
:
pass
# needed for older versions of grafana
def
_find_org_config
(
org
):
orgs_to_provision
=
config
.
get
(
'
organizations
'
,
DEFAULT_ORGANIZATIONS
)
...
...
@@ -840,10 +844,17 @@ def provision(config, raise_exceptions=False):
# message logged from _find_org_config
continue
token
=
create_api_token
(
request
,
org_id
)
try
:
token
=
create_api_token
(
request
,
org_id
)
accounts
.
append
((
org_id
,
token
))
except
Exception
:
# create a service account for provisioning (>grafana 11.0)
account
=
get_or_create_service_account
(
request
,
org_id
)
token
=
create_service_account_token
(
request
,
account
[
'
id
'
])
accounts
.
append
((
org_id
,
account
))
token_request
=
TokenRequest
(
token
=
token
[
'
key
'
],
**
config
)
tokens
.
append
((
org_id
,
token
[
'
id
'
]))
logger
.
debug
(
tokens
)
logger
.
debug
(
accounts
)
all_original_dashboards
=
list_dashboards
(
token_request
)
all_original_dashboard_uids
=
{
...
...
@@ -902,7 +913,11 @@ def provision(config, raise_exceptions=False):
folders_to_keep
.
update
(
ignored_folders
)
delete_unknown_folders
(
token_request
,
folders_to_keep
)
delete_api_token
(
request
,
token
[
'
id
'
],
org_id
=
org_id
)
try
:
delete_api_token
(
request
,
token
[
'
id
'
],
org_id
=
org_id
)
except
Exception
:
# we're on a newer version of grafana
delete_service_account
(
request
,
account
[
'
id
'
])
except
Exception
:
logger
.
exception
(
f
'
Error when provisioning org
{
org
[
"
name
"
]
}
'
)
if
raise_exceptions
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment