Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 506d1ed

Browse files
authored
Merge pull request ClearBlade#6 from ClearBlade/collection-management
add collection managment, and first pass at permissions
2 parents 2ab8ced + 5a8fce2 commit 506d1ed

File tree

5 files changed

+167
-3
lines changed

5 files changed

+167
-3
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,71 @@ from clearblade.ClearBladeCore import Developer
579579
# Log in as Big Boi
580580
bigboi = Developer("[email protected]", "th3w@yY0uM0v3")
581581
```
582+
---
583+
### Collections
584+
First you are able to get a list of all current collections within a system.
585+
586+
> Definition: `Developer.getAllCollections(system)`
587+
> Returns: List of collections. Each collection is a dictionary containing the collection name and collectionID.
588+
589+
As a developer, you get full management access to any collection within a system. To create a cloud collection, you need to specify the system it's going to live in, and the name of the new collection you are creating.
590+
591+
> Definition: `Developer.newCollection(system, name)`
592+
> Returns: A Collection object of the newly created Collection
593+
594+
You can also add columns to any Collection. Note: the Collection object you supply should be initialized with a `collectionID` (rather than `collectionName`) in order to add columns. The Collection object returned from `Developer.newCollection` is initialized this way for you for ease of use.
595+
596+
> Definition: `Developer.addColumnToCollection(system, collectionObject, columnName, columnType)`
597+
> Returns: Nothing
598+
599+
Finally, you are able to set the CRUD permissions for a collection via a specific role name. Note: like `addColumnToCollection` you will need to use a Collection object initialized with a `collectionID`
600+
601+
> Definition: `Developer.setPermissionsForCollection(system, collectionObject, Permissions.READ + Permissions.UPDATE, roleName)`
602+
> Returns: Nothing
603+
604+
#### Examples
605+
Creating a new Collection and adding a custom column.
606+
607+
```python
608+
from clearblade.ClearBladeCore import System, Developer
609+
610+
# System credentials
611+
SystemKey = "9abbd2970baabf8aa6d2a9abcc47"
612+
SystemSecret = "9ABBD2970BA6AABFE6E8AEB8B14F"
613+
614+
mySystem = System(SystemKey, SystemSecret)
615+
616+
# Log in as Steve
617+
steve = Developer("[email protected]", "r0s@_p@rks")
618+
619+
# Create new Collection named Tools
620+
toolsCollection = steve.newCollection(mySystem, "Tools")
621+
622+
# Add a column to the collection called last_location with a type of string
623+
steve.addColumnToCollection(mySystem, toolsCollection, "last_location", "string")
624+
```
625+
626+
Updating CRUD permissions for a Collection on a specific role.
627+
628+
```python
629+
from clearblade.ClearBladeCore import System, Developer, Collections, Permissions
630+
631+
# System credentials
632+
SystemKey = "9abbd2970baabf8aa6d2a9abcc47"
633+
SystemSecret = "9ABBD2970BA6AABFE6E8AEB8B14F"
634+
635+
mySystem = System(SystemKey, SystemSecret)
636+
637+
# Log in as Steve
638+
steve = Developer("[email protected]", "r0s@_p@rks")
639+
640+
# Create a Collection object from an existing collection with an id of 8a94dda70bb4c2c59b8298d686f401
641+
collectionObj = mySystem.Collection(steve, collectionID="8a94dda70bb4c2c59b8298d686f401")
642+
643+
# Give the Authenticated role Read and Delete permissions to this collection
644+
michael.setPermissionsForCollection(mySystem, collectionObj, Permissions.READ + Permissions.DELETE, "Authenticated")
645+
```
646+
582647
---
583648
### Devices
584649
As a developer, you get full CRUD access to the device table.

clearblade/Collections.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ class Collection():
88
def __init__(self, system, authenticatedUser, collectionID="", collectionName=""):
99
if collectionID:
1010
self.url = system.url + '/api/v/1/data/' + collectionID
11+
self.collectionID = collectionID
12+
self.collectionName = None
1113
elif collectionName:
1214
self.url = system.url + '/api/v/1/collection/' + system.systemKey + "/" + collectionName
15+
self.collectionName = collectionName
16+
self.collectionID = None
1317
else:
1418
cbLogs.error("You must supply either a collection name or id.") # beep
1519
exit(-1)
@@ -69,3 +73,44 @@ def updateItems(self, query, data):
6973

7074
def deleteItems(self, query):
7175
return restcall.delete(self.url, headers=self.headers, params={"query": json.dumps(query.filters)}, sslVerify=self.sslVerify)
76+
77+
78+
###########################
79+
# DEVELOPER ENDPOINTS #
80+
###########################
81+
82+
def DEVgetAllCollections(developer, system):
83+
url = system.url + "/admin/allcollections"
84+
params = {
85+
"appid": system.systemKey
86+
}
87+
resp = restcall.get(url, headers=developer.headers, params=params, sslVerify=system.sslVerify)
88+
return resp
89+
90+
def DEVnewCollection(developer, system, name):
91+
url = system.url + "/admin/collectionmanagement"
92+
data = {
93+
"appID": system.systemKey,
94+
"name": name
95+
}
96+
resp = restcall.post(url, headers=developer.headers, data=data, sslVerify=system.sslVerify)
97+
cbLogs.info("Successfully created collection: " + name)
98+
return Collection(system, developer, collectionID=resp["collectionID"])
99+
100+
def DEVaddColumnToCollection(developer, system, collection, columnName, columnType):
101+
if not collection.collectionID:
102+
cbLogs.error("You must supply the collection id when adding a column to a collection.")
103+
exit(-1)
104+
url = system.url + "/admin/collectionmanagement"
105+
data = {
106+
"id": collection.collectionID,
107+
"addColumn": {
108+
"id": collection.collectionID,
109+
"name": columnName,
110+
"type": columnType
111+
}
112+
}
113+
resp = restcall.put(url, headers=developer.headers, data=data, sslVerify=system.sslVerify)
114+
cbLogs.info("Successfully added column: " + columnName)
115+
return resp
116+

clearblade/Developers.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import absolute_import
22
from . import restcall
33
from . import cbLogs
4+
from . import Collections
45
from . import Devices
5-
6+
from . import Permissions
67

78
def registerDev(fname, lname, org, email, password, url="https://platform.clearblade.com"):
89
newDevCredentials = {
@@ -65,6 +66,19 @@ def logout(self):
6566
# ~~~~~~~~~~~~~~~~~~~~~~ #
6667
##########################
6768

69+
#################
70+
# Collections #
71+
#################
72+
73+
def getAllCollections(self, system):
74+
return Collections.DEVgetAllCollections(self, system)
75+
76+
def newCollection(self, system, name):
77+
return Collections.DEVnewCollection(self, system, name)
78+
79+
def addColumnToCollection(self, system, collection, columnName, columnType):
80+
return Collections.DEVaddColumnToCollection(self, system, collection, columnName, columnType)
81+
6882
###############
6983
# Devices #
7084
###############
@@ -83,3 +97,11 @@ def updateDevice(self, system, name, updates):
8397

8498
def deleteDevice(self, system, name):
8599
return Devices.DEVdeleteDevice(self, system, name)
100+
101+
#################
102+
# Permissions #
103+
#################
104+
105+
def setPermissionsForCollection(self, system, collection, permissionsLevel, roleName):
106+
return Permissions.DEVsetPermissionsForCollection(self, system, collection, permissionsLevel, roleName)
107+

clearblade/Permissions.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import absolute_import
2+
from . import cbLogs
3+
from . import restcall
4+
5+
6+
READ = 1
7+
CREATE = 2
8+
UPDATE = 4
9+
DELETE = 8
10+
11+
###########################
12+
# DEVELOPER ENDPOINTS #
13+
###########################
14+
15+
def DEVsetPermissionsForCollection(developer, system, collection, permissionsLevel, roleName):
16+
url = system.url + "/admin/user/" + system.systemKey + "/roles"
17+
data = {
18+
"id": roleName,
19+
"changes": {
20+
"collections": [{
21+
"itemInfo": {
22+
"name": collection.collectionName,
23+
"id": collection.collectionID
24+
},
25+
"permissions": permissionsLevel
26+
}]
27+
}
28+
}
29+
resp = restcall.put(url, headers=developer.headers, data=data, sslVerify=system.sslVerify)
30+
cbLogs.info("Successfully updated permissions for role: " + roleName)
31+
return resp
32+

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
name='clearblade',
55
packages=['clearblade'],
66
install_requires=['requests', 'paho-mqtt>=1.3.0'],
7-
version='2.1',
7+
version='2.2',
88
description='A Python SDK for interacting with the ClearBlade Platform.',
99
url='https://github.com/ClearBlade/ClearBlade-Python-SDK',
10-
download_url='https://github.com/ClearBlade/ClearBlade-Python-SDK/archive/v2.0.tar.gz',
10+
download_url='https://github.com/ClearBlade/ClearBlade-Python-SDK/archive/v2.2.tar.gz',
1111
keywords=['clearblade', 'iot', 'sdk'],
1212
maintainer='Aaron Allsbrook',
1313
maintainer_email='[email protected]'

0 commit comments

Comments
 (0)