Skip to content

Commit

Permalink
Adds extra api calls. there are still several api calls to add, so I …
Browse files Browse the repository at this point in the history
…am adding only the ones required for RBAC testing for now.
  • Loading branch information
scardena committed Mar 12, 2021
1 parent 63a3620 commit 3123873
Showing 1 changed file with 157 additions and 1 deletion.
158 changes: 157 additions & 1 deletion stardog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ def new_user(self, username, password, superuser=False):

self.client.post('/admin/users', json=meta)
return self.user(username)
return User(username, self.client)

def role(self, name):
"""Retrieves an object representing a role.
Expand Down Expand Up @@ -358,6 +357,59 @@ def new_virtual_graph(self, name, mappings, options):
self.client.post('/admin/virtual_graphs', json=meta)
return VirtualGraph(name, self.client)

def datasource(self, name):
"""Retrieves an object representing a DataSource.
Args:
name (str): The name of the data source
Returns:
DataSource: The DataSource object
"""
return DataSource(name, self.client)

def datasources(self):
"""Retrieves all data sources.
Returns:
list[DataSources]: A list of DataSources
"""
r = self.client.get('/admin/data_sources')
data_sources = r.json()['data_sources']
return list(map(lambda name: DataSource(name, self.client), data_sources))

def datasources_info(self):
"""List data sources info
Returns:
list: A list of data sources info
"""

r = self.client.get('/admin/data_sources/list')
return r.json()['data_sources']

def new_datasource(self, name, options):
"""Creates a new DataSource.
Args:
name (str): The name of the data source
options (dict): Data source options
Returns:
User: The new DataSource object
"""

if options is None:
options = {}

meta = {
"name": name,
"options": options
}

self.client.post('/admin/data_sources', json=meta)
return self.datasource(name)

def validate(self):
"""Validates an admin connection.
Expand Down Expand Up @@ -956,3 +1008,107 @@ def available(self):

def __repr__(self):
return self.name


class DataSource(object):
"""Initializes a DataSource
See Also:
https://docs.stardog.com/virtual-graphs/virtual-sources
"""

def __init__(self, name, client):
"""Initializes a DataSource.
Use :meth:`stardog.admin.Admin.data_source`,
:meth:`stardog.admin.Admin.data_sources`, or
:meth:`stardog.admin.Admin.new_data_source` instead of
constructing manually.
"""

self.data_source_name = name
self.path = '/admin/data_sources/{}'.format(name)
self.client = client

@property
def name(self):
"""The name of the data source.
"""
return self.data_source_name

def available(self):
"""Checks if the data source is available.
Returns:
bool: Availability state
"""
r = self.client.get(self.path + '/available')
return bool(r.json())

def refresh_count(self, meta=None):
"""Refresh table row-count estimates
"""

if meta is None:
meta = {}

self.client.post(self.path + '/refresh_counts', json=meta)

def update(self, options=None):
"""Update data source
"""
if options is None:
options = {}

meta = {
"options": options
}

self.client.put(self.path, json=meta)

def delete(self):
"""Deletes a data source
"""

self.client.delete(self.path)

def online(self):
"""Online a data source
"""

self.client.post(self.path + '/online')

def info(self):
"""Get data source info
Returns:
dict: Info
"""

r = self.client.get(self.path + '/info')
return r.json()['info']

def refresh_metadata(self, meta=None):
"""Refresh metadata
"""

if meta is None:
meta = {}

self.client.post(self.path + '/refresh_metadata', json=meta)

def share(self):
"""Share data source
"""

self.client.post(self.path + '/share')

def get_options(self):
"""Get data source options
"""

r = self.client.get(self.path + '/options')
return r.json()['options']

def __repr__(self):
return self.name

0 comments on commit 3123873

Please sign in to comment.