1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Manuel Imperiale aa014c2191
NOISSUE - Add view and list serials endpoints in certs service (#1483)
* NOISSUE - Add view and list serials endpoints in certs service

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix vault-unseal.sh script

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename Cert field days_valid into hours_valid

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix provision service

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use ownerID, rename daysValid -> hoursValid

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add key_type to api

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix tabulation

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add expiration date in view response

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename HoursValid -> Expiration and remove unecessary expiration convertion

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add ListSerials tests and fix mocks

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix RetrieveByThing count

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add ViewCert tests

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add missing error check

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Simplify API

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Revert Makefile

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* NOISSUE - Add view and list serials endpoints in certs service

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix vault-unseal.sh script

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename Cert field days_valid into hours_valid

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix provision service

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use ownerID, rename daysValid -> hoursValid

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add key_type to api

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix tabulation

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add expiration date in view response

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename HoursValid -> Expiration and remove unecessary expiration convertion

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add ListSerials tests and fix mocks

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix RetrieveByThing count

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add ViewCert tests

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add missing error check

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Simplify API

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Revert Makefile

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rm if else

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename HoursValid -> TTL

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* revert typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* revert typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename hoursValid -> ttl

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2022-01-04 19:42:13 +01:00

137 lines
2.8 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mocks
import (
"context"
"sync"
"github.com/mainflux/mainflux/certs"
)
var _ certs.Repository = (*certsRepoMock)(nil)
type certsRepoMock struct {
mu sync.Mutex
counter uint64
certsBySerial map[string]certs.Cert
certsByThingID map[string]map[string][]certs.Cert
}
// NewCertsRepository creates in-memory certs repository.
func NewCertsRepository() certs.Repository {
return &certsRepoMock{
certsBySerial: make(map[string]certs.Cert),
certsByThingID: make(map[string]map[string][]certs.Cert),
}
}
func (c *certsRepoMock) Save(ctx context.Context, cert certs.Cert) (string, error) {
c.mu.Lock()
defer c.mu.Unlock()
crt := certs.Cert{
OwnerID: cert.OwnerID,
ThingID: cert.ThingID,
Serial: cert.Serial,
Expire: cert.Expire,
}
_, ok := c.certsByThingID[cert.OwnerID][cert.ThingID]
switch ok {
case false:
c.certsByThingID[cert.OwnerID] = map[string][]certs.Cert{
cert.ThingID: []certs.Cert{crt},
}
default:
c.certsByThingID[cert.OwnerID][cert.ThingID] = append(c.certsByThingID[cert.OwnerID][cert.ThingID], crt)
}
c.certsBySerial[cert.Serial] = crt
c.counter++
return cert.Serial, nil
}
func (c *certsRepoMock) RetrieveAll(ctx context.Context, ownerID string, offset, limit uint64) (certs.Page, error) {
c.mu.Lock()
defer c.mu.Unlock()
if limit <= 0 {
return certs.Page{}, nil
}
oc, ok := c.certsByThingID[ownerID]
if !ok {
return certs.Page{}, certs.ErrNotFound
}
var crts []certs.Cert
for _, tc := range oc {
for i, v := range tc {
if uint64(i) >= offset && uint64(i) < offset+limit {
crts = append(crts, v)
}
}
}
page := certs.Page{
Certs: crts,
Total: c.counter,
Offset: offset,
Limit: limit,
}
return page, nil
}
func (c *certsRepoMock) Remove(ctx context.Context, ownerID, serial string) error {
c.mu.Lock()
defer c.mu.Unlock()
crt, ok := c.certsBySerial[serial]
if !ok {
return certs.ErrNotFound
}
delete(c.certsBySerial, crt.Serial)
delete(c.certsByThingID, crt.ThingID)
return nil
}
func (c *certsRepoMock) RetrieveByThing(ctx context.Context, ownerID, thingID string, offset, limit uint64) (certs.Page, error) {
c.mu.Lock()
defer c.mu.Unlock()
if limit <= 0 {
return certs.Page{}, nil
}
cs, ok := c.certsByThingID[ownerID][thingID]
if !ok {
return certs.Page{}, certs.ErrNotFound
}
var crts []certs.Cert
for i, v := range cs {
if uint64(i) >= offset && uint64(i) < offset+limit {
crts = append(crts, v)
}
}
page := certs.Page{
Certs: crts,
Total: c.counter,
Offset: offset,
Limit: limit,
}
return page, nil
}
func (c *certsRepoMock) RetrieveBySerial(ctx context.Context, ownerID, serialID string) (certs.Cert, error) {
c.mu.Lock()
defer c.mu.Unlock()
crt, ok := c.certsBySerial[serialID]
if !ok {
return certs.Cert{}, certs.ErrNotFound
}
return crt, nil
}