Skip to content

Commit

Permalink
Add Poll request
Browse files Browse the repository at this point in the history
  • Loading branch information
ngc7293 committed Apr 11, 2024
1 parent b964da2 commit 3f52c5b
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 169 deletions.
24 changes: 17 additions & 7 deletions api/ganymede/v2/device.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ service DeviceService {
rpc ListDevice(ListDeviceRequest) returns (ListDeviceResponse);
rpc DeleteDevice(DeleteDeviceRequest) returns (google.protobuf.Empty);

rpc Poll(PollRequest) returns (PollResponse);

rpc CreateConfig(CreateConfigRequest) returns (Config);
rpc UpdateConfig(UpdateConfigRequest) returns (Config);
rpc GetConfig(GetConfigRequest) returns (Config);
Expand All @@ -29,10 +31,7 @@ message UpdateDeviceRequest {
}

message GetDeviceRequest {
oneof filter {
string device_uid = 1;
string device_mac = 2;
}
string device_uid = 1;
}

message ListDeviceRequest {
Expand All @@ -51,6 +50,20 @@ message DeleteDeviceRequest {
string reason = 2;
}

message PollRequest {
string device_mac = 1;
}

message PollResponse {
string uid = 1;
string display_name = 2;

// Output only. Current device's offset from UTC (including DST if applicable)
int64 timezone_offset_minutes = 10;

Config config = 20;
}

message CreateConfigRequest {
Config config = 1;
}
Expand Down Expand Up @@ -97,9 +110,6 @@ message Device {
// Device timezone in IANA <zone>/<city> format
string timezone = 12;

// Output only. Current device's offset from UTC (including DST if applicable)
int64 timezone_offset_minutes = 13;

string config_uid = 20;
}

Expand Down
50 changes: 31 additions & 19 deletions kubernetes/database.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: psotgres-pvc
name: postgres-pvc
namespace: ganymede
labels:
app.kubernetes.io/name: postgres-pvc
app.kubernetes.io/component: database
spec:
accessModes:
- ReadWriteOnce
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 4Gi

---

apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
namespace: ganymede
labels:
app.kubernetes.io/name: postgres
app.kubernetes.io/component: database
Expand All @@ -35,24 +35,37 @@ spec:
app.kubernetes.io/component: database
spec:
containers:
- name: database
image: postgres:15.3
ports:
- containerPort: 5432
resources:
requests:
cpu: 500m
memory: 256M
limits:
cpu: 2000m
memory: 1G

- name: database
image: postgres:15.3
ports:
- containerPort: 5432
env:
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: postgres
- name: POSTGRES_DB
value: ganymede
resources:
requests:
cpu: 500m
memory: 1G
limits:
cpu: 2000m
memory: 1G
volumeMounts:
- name: postgres-data
mountPath: /var/lib/postgresql/data
volumes:
- name: postgres-data
persistentVolumeClaim:
claimName: postgres-pvc
---

apiVersion: v1
kind: Service
metadata:
name: database
namespace: ganymede
labels:
app.kubernetes.io/name: postgres
app.kubernetes.io/component: database
Expand All @@ -64,5 +77,4 @@ spec:
ports:
- protocol: TCP
port: 5432
targetPort: 5432

targetPort: 5432
72 changes: 35 additions & 37 deletions kubernetes/ganymede.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ apiVersion: v1
kind: ConfigMap
metadata:
name: ganymede-config-cm
namespace: ganymede
labels:
app.kubernetes.io/name: device
app.kubernetes.io/component: app
data:
Ganymede.toml: |
postgres_uri = "postgres://postgres:postgres@localhost:5432/ganymede"
port = 3000
---

apiVersion: apps/v1
kind: Deployment
metadata:
name: ganymede
namespace: ganymede
labels:
app.kubernetes.io/name: ganymede
app.kubernetes.io/component: app
Expand All @@ -32,31 +32,30 @@ spec:
app.kubernetes.io/component: app
spec:
containers:
- name: ganymede
image: ghcr.io/ngc7293/ganymede:latest
ports:
- containerPort: 3000
resources:
requests:
cpu: 500m
memory: 512M
limits:
cpu: 500m
memory: 512M
volumeMounts:
- name: config-volume
mountPath: /app
- name: ganymede
image: ghcr.io/ngc7293/ganymede:latest
ports:
- containerPort: 3000
resources:
requests:
cpu: 500m
memory: 512M
limits:
cpu: 500m
memory: 512M
volumeMounts:
- name: config-volume
mountPath: /app
volumes:
- name: config-volume
configMap:
name: ganymede-config-cm

- name: config-volume
configMap:
name: ganymede-config-cm
---

apiVersion: v1
kind: Service
metadata:
name: ganymede
namespace: ganymede
labels:
app.kubernetes.io/name: ganymede
app.kubernetes.io/component: app
Expand All @@ -69,33 +68,32 @@ spec:
- protocol: TCP
port: 3000
targetPort: 3000

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api
namespace: ganymede
annotations:
nginx.ingress.kubernetes.io/backend-protocol: GRPC
cert-manager.io/issuer: letsencrypt
cert-manager.io/cluster-issuer: letsencrypt
labels:
app.kubernetes.io/name: ganymede
app.kubernetes.io/component: api
spec:
ingressClassName: nginx
tls:
- hosts:
- ganymede.davidbourgault.ca
secretName: ganymede-letsencrypt-cert
- hosts:
- ganymede.davidbourgault.ca
secretName: ganymede-letsencrypt-cert
rules:
- host: ganymede.davidbourgault.ca
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: ganymede
port:
number: 3000
- host: ganymede.davidbourgault.ca
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: ganymede
port:
number: 3000
15 changes: 0 additions & 15 deletions kubernetes/issuer.yaml

This file was deleted.

52 changes: 25 additions & 27 deletions src/device/device/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@ use crate::types::mac::Mac;

use super::{errors::DeviceError, model::DeviceModel};

pub enum UniqueDeviceFilter {
DeviceId(uuid::Uuid),
DeviceMac(Mac),
}

pub enum DeviceFilter {
NameFilter(String),
ConfigId(uuid::Uuid),
None,
}

impl<'a> DomainDatabase<'a> {
pub async fn insert_device(&self, device: DeviceModel) -> Result<DeviceModel, DeviceError> {
if self
.fetch_one_device(UniqueDeviceFilter::DeviceMac(device.mac.clone()))
pub async fn fetch_device_id_for_mac(&self, mac: &Mac) -> Result<Option<uuid::Uuid>, DeviceError> {
let device_id = match sqlx::query_as::<_, (uuid::Uuid,)>("SELECT device_id FROM device WHERE domain_id = $1 AND mac = $2 LIMIT 1")
.bind(self.domain_id())
.bind(mac)
.fetch_one(self.pool())
.await
!= Err(DeviceError::NoSuchDevice)
{
Ok(row) => Some(row.0),
Err(err) => match err {
sqlx::Error::RowNotFound => None,
_ => return Err(DeviceError::DatabaseError(err.to_string())),
}
};

Ok(device_id)
}

pub async fn insert_device(&self, device: DeviceModel) -> Result<DeviceModel, DeviceError> {
if let Some(_) = self.fetch_device_id_for_mac(&device.mac).await? {
return Err(DeviceError::MacConflict);
}

Expand All @@ -42,16 +50,13 @@ impl<'a> DomainDatabase<'a> {
.await
.map_err(|err| DeviceError::DatabaseError(err.to_string()))?;

self.fetch_one_device(UniqueDeviceFilter::DeviceId(device_id))
self.fetch_one_device(device_id)
.await
}

pub async fn update_device(&self, device: DeviceModel) -> Result<DeviceModel, DeviceError> {
if let Ok(existing) = self
.fetch_one_device(UniqueDeviceFilter::DeviceMac(device.mac.clone()))
.await
{
if existing.device_id != device.device_id {
if let Some(existing_id) = self.fetch_device_id_for_mac(&device.mac).await? {
if existing_id != device.device_id {
return Err(DeviceError::MacConflict);
}
}
Expand Down Expand Up @@ -81,24 +86,17 @@ impl<'a> DomainDatabase<'a> {
},
};

self.fetch_one_device(UniqueDeviceFilter::DeviceId(device_id))
self.fetch_one_device(device_id)
.await
}

pub async fn fetch_one_device(
&self,
filter: UniqueDeviceFilter,
device_id: uuid::Uuid,
) -> Result<DeviceModel, DeviceError> {
let mut query = sqlx::QueryBuilder::new("SELECT device_id, domain_id, display_name, mac, config_id, description, timezone FROM device WHERE domain_id = ");
query.push_bind(self.domain_id()).push(" AND ");

match filter {
UniqueDeviceFilter::DeviceId(id) => query.push("device_id = ").push_bind(id),
UniqueDeviceFilter::DeviceMac(mac) => query.push("mac = ").push_bind(mac),
};

let device = match query
.build_query_as::<DeviceModel>()
let device = match sqlx::query_as::<_, DeviceModel>("SELECT device_id, domain_id, display_name, mac, config_id, description, timezone FROM device WHERE domain_id = $1 AND device_id = $2")
.bind(self.domain_id())
.bind(device_id)
.fetch_one(self.pool())
.await
{
Expand Down
Loading

0 comments on commit 3f52c5b

Please sign in to comment.