Skip to content

Commit

Permalink
Delete device references
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Jan 14, 2024
1 parent df6d477 commit 7b4a632
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
24 changes: 14 additions & 10 deletions internal/data/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ func GetDevice(username, id string) (device Device, err error) {

err = json.Unmarshal(response.Kvs[0].Value, &device)

// TODO write custom marhaller
// if endpoint.Valid {
// device.Endpoint = stringToUDPaddr(endpoint.String)
// }

return
}

Expand Down Expand Up @@ -206,13 +201,24 @@ func DeleteDevice(username, id string) error {
}

func DeleteDevices(username string) error {

// TODO delete references
_, err := etcd.Delete(context.Background(), fmt.Sprintf("device-%s-", username), clientv3.WithPrefix())
deleted, err := etcd.Delete(context.Background(), fmt.Sprintf("device-%s-", username), clientv3.WithPrefix())
if err != nil {
return err
}

ops := []clientv3.Op{}
for _, reference := range deleted.PrevKvs {

var d Device
err := json.Unmarshal(reference.Value, &d)
if err != nil {
return err
}

ops = append(ops, clientv3.OpDelete("devicesref-"+d.Publickey), clientv3.OpDelete("deviceref-"+d.Address))
}

_, err = etcd.Txn(context.Background()).Then(ops...).Commit()
return err
}

Expand All @@ -237,8 +243,6 @@ func UpdateDevicePublicKey(username, address string, publicKey wgtypes.Key) erro
})
}

//CREATE TABLE Devices(address string primary key, username string not null, publickey string not null unique, endpoint string, attempts integer DEFAULT 0 not null);

func GetDeviceByAddress(address string) (device Device, err error) {

realKey, err := etcd.Get(context.Background(), "deviceref-"+address)
Expand Down
1 change: 1 addition & 0 deletions internal/data/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func Load(path string) error {
cfg := embed.NewConfig()
cfg.Name = config.Values().Clustering.Name
cfg.InitialClusterToken = "wag-test"
cfg.LogLevel = "error"
cfg.ListenPeerUrls = parseUrls(config.Values().Clustering.ListenAddresses...)
cfg.ListenClientUrls = parseUrls(fmt.Sprintf("http://127.0.0.1:%d", 2480))
cfg.AdvertisePeerUrls = cfg.ListenPeerUrls
Expand Down

0 comments on commit 7b4a632

Please sign in to comment.