-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NOISSUE - Certs service refactor (#1369)
* remove owner id Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add certs mock Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove not wanted changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * addint tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * addint tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add certs test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add certs test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cert test, remove default implementation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix default value for vault host Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cert test, remove default implementation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * linter cleaning Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments, and logging Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use mocks from other services Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename struct and url path params Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve minor comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * align url params naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix typo Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove struct revoke Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor certRes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
- Loading branch information
Showing
20 changed files
with
750 additions
and
270 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
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
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
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
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,87 @@ | ||
// 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 | ||
certs map[string]certs.Cert | ||
certsByThingID map[string]certs.Cert | ||
} | ||
|
||
// NewCertsRepository creates in-memory certs repository. | ||
func NewCertsRepository() certs.Repository { | ||
return &certsRepoMock{ | ||
certs: make(map[string]certs.Cert), | ||
certsByThingID: make(map[string]certs.Cert), | ||
} | ||
} | ||
|
||
func (c *certsRepoMock) Save(ctx context.Context, cert certs.Cert) (string, error) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
c.certs[cert.Serial] = cert | ||
c.certsByThingID[cert.ThingID] = cert | ||
c.counter++ | ||
return cert.Serial, nil | ||
} | ||
|
||
func (c *certsRepoMock) RetrieveAll(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 | ||
} | ||
|
||
first := offset + 1 | ||
last := first + limit | ||
|
||
var crts []certs.Cert | ||
i := uint64(1) | ||
for _, v := range c.certs { | ||
if i >= first && i < last { | ||
crts = append(crts, v) | ||
} | ||
i++ | ||
} | ||
|
||
page := certs.Page{ | ||
Certs: crts, | ||
Total: c.counter, | ||
Offset: offset, | ||
Limit: limit, | ||
} | ||
return page, nil | ||
} | ||
|
||
func (c *certsRepoMock) Remove(ctx context.Context, serial string) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
crt, ok := c.certs[serial] | ||
if !ok { | ||
return certs.ErrNotFound | ||
} | ||
delete(c.certs, crt.Serial) | ||
delete(c.certsByThingID, crt.ThingID) | ||
return nil | ||
} | ||
|
||
func (c *certsRepoMock) RetrieveByThing(ctx context.Context, thingID string) (certs.Cert, error) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
crt, ok := c.certsByThingID[thingID] | ||
if !ok { | ||
return certs.Cert{}, certs.ErrNotFound | ||
} | ||
return crt, nil | ||
} |
Oops, something went wrong.