This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent deleted devices from being recreated
This addresses issue: #3 In that it prevents a device to ever get re-created, as this is not allowed by OTA Connect. This seems like a bug in OTA Connect, but was easiest to fix here. Additionally, we think this addresses a bug we had in production: advancedtelematic/ota-device-registry#89 Signed-off-by: Andy Doan <andy@foundries.io>
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (C) 2019 Foundries.io | ||
# Author: Andy Doan <andy@foundries.io> | ||
import contextlib | ||
import os | ||
|
||
from pymysql import Connect | ||
|
||
DB = os.environ.get('DEVICE_REGISTRY_DB', 'device_registry') | ||
DBHOST = os.environ.get('DEVICE_REGISTRY_DBHOST', 'mysql') | ||
DBUSER = os.environ.get('DEVICE_REGISTRY_DBUSER', 'device_registry') | ||
DBPASS = os.environ.get('DEVICE_REGISTRY_DBPASS', 'device_registry') | ||
|
||
|
||
@contextlib.contextmanager | ||
def db_cursor(commit=False): | ||
con = None | ||
try: | ||
con = Connect(host=DBHOST, user=DBUSER, password=DBPASS, db=DB) | ||
with con.cursor() as cur: | ||
yield cur | ||
if commit: | ||
con.commit() | ||
finally: | ||
if con: | ||
con.close() | ||
|
||
|
||
def migrate(): | ||
stmt = ''' | ||
CREATE TABLE IF NOT EXISTS OtaApiDeleted ( | ||
uuid CHAR(36) NOT NULL, | ||
PRIMARY KEY (uuid) | ||
); | ||
''' | ||
with db_cursor() as c: | ||
c.execute(stmt) | ||
|
||
|
||
def device_mark_deleted(device_uuid): | ||
'''Mark a device as deleted so that it won't ever be allowed to be added | ||
again.''' | ||
stmt = 'INSERT INTO OtaApiDeleted (uuid) VALUES (%s)' | ||
with db_cursor(commit=True) as c: | ||
c.execute(stmt, device_uuid) | ||
|
||
|
||
def device_is_deleted(device_uuid): | ||
'''Check to see if this device was deleted. We can't allow it to be | ||
re-added and OTA connect will. Letting a device through will cause | ||
something like: | ||
https://github.com/advancedtelematic/ota-device-registry/issues/89 | ||
''' | ||
stmt = '''SELECT uuid | ||
FROM OtaApiDeleted | ||
WHERE uuid = %s | ||
''' | ||
with db_cursor() as c: | ||
c.execute(stmt, device_uuid) | ||
for r in c: | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters