Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
policy
Manage
Activity
Members
Labels
Plan
Issues
4
Issue boards
Milestones
Wiki
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gaia-X
Trust Services API
policy
Commits
5ace99a8
Commit
5ace99a8
authored
3 years ago
by
Lyuben Penkovski
Browse files
Options
Downloads
Patches
Plain Diff
Refactor locking function to reduce code duplication
parent
469fed2d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!3
HTTP endpoints for policy lock and unlock
Pipeline
#49716
passed with stage
in 33 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
internal/service/policy/service.go
+3
-4
3 additions, 4 deletions
internal/service/policy/service.go
internal/storage/policies_tmp_store.go
+1
-1
1 addition, 1 deletion
internal/storage/policies_tmp_store.go
internal/storage/storage.go
+14
-34
14 additions, 34 deletions
internal/storage/storage.go
with
18 additions
and
39 deletions
internal/service/policy/service.go
+
3
−
4
View file @
5ace99a8
...
...
@@ -13,8 +13,7 @@ import (
type
Storage
interface
{
Policy
(
ctx
context
.
Context
,
name
,
group
,
version
string
)
(
*
storage
.
Policy
,
error
)
LockPolicy
(
ctx
context
.
Context
,
name
,
group
,
version
string
)
error
UnlockPolicy
(
ctx
context
.
Context
,
name
,
group
,
version
string
)
error
SetPolicyLock
(
ctx
context
.
Context
,
name
,
group
,
version
string
,
lock
bool
)
error
}
type
Service
struct
{
...
...
@@ -100,7 +99,7 @@ func (s *Service) Lock(ctx context.Context, req *policy.LockRequest) error {
return
errors
.
New
(
errors
.
Forbidden
,
"policy is already locked"
)
}
if
err
:=
s
.
storage
.
Lock
Policy
(
ctx
,
req
.
PolicyName
,
req
.
Group
,
req
.
Version
);
err
!=
nil
{
if
err
:=
s
.
storage
.
Set
Policy
Lock
(
ctx
,
req
.
PolicyName
,
req
.
Group
,
req
.
Version
,
true
);
err
!=
nil
{
logger
.
Error
(
"error locking policy"
,
zap
.
Error
(
err
))
return
errors
.
New
(
"error locking policy"
,
err
)
}
...
...
@@ -131,7 +130,7 @@ func (s *Service) Unlock(ctx context.Context, req *policy.UnlockRequest) error {
return
errors
.
New
(
errors
.
Forbidden
,
"policy is unlocked"
)
}
if
err
:=
s
.
storage
.
Unlock
Policy
(
ctx
,
req
.
PolicyName
,
req
.
Group
,
req
.
Version
);
err
!=
nil
{
if
err
:=
s
.
storage
.
Set
Policy
Lock
(
ctx
,
req
.
PolicyName
,
req
.
Group
,
req
.
Version
,
false
);
err
!=
nil
{
logger
.
Error
(
"error unlocking policy"
,
zap
.
Error
(
err
))
return
errors
.
New
(
"error unlocking policy"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
internal/storage/policies_tmp_store.go
+
1
−
1
View file @
5ace99a8
...
...
@@ -11,7 +11,7 @@ package storage
// Group: "example",
// Version: "1.0",
// Locked: false,
// LastUpdate
d
: time.Now(),
// LastUpdate: time.Now(),
// Rego: `
// package gaiax
//
...
...
This diff is collapsed.
Click to expand it.
internal/storage/storage.go
+
14
−
34
View file @
5ace99a8
...
...
@@ -12,31 +12,27 @@ import (
)
type
Policy
struct
{
Filename
string
Name
string
Group
string
Version
string
Rego
string
Locked
bool
LastUpdate
d
time
.
Time
Filename
string
Name
string
Group
string
Version
string
Rego
string
Locked
bool
LastUpdate
time
.
Time
}
type
Storage
struct
{
db
*
mongo
.
Client
dbname
string
collection
string
policy
*
mongo
.
Collection
}
func
New
(
db
*
mongo
.
Client
,
dbname
,
collection
string
)
*
Storage
{
return
&
Storage
{
db
:
db
,
dbname
:
dbname
,
collection
:
collection
,
policy
:
db
.
Database
(
dbname
)
.
Collection
(
collection
),
}
}
func
(
s
*
Storage
)
Policy
(
ctx
context
.
Context
,
name
,
group
,
version
string
)
(
*
Policy
,
error
)
{
result
:=
s
.
db
.
Database
(
s
.
dbname
)
.
Collection
(
s
.
collection
)
.
FindOne
(
ctx
,
bson
.
M
{
result
:=
s
.
policy
.
FindOne
(
ctx
,
bson
.
M
{
"name"
:
name
,
"group"
:
group
,
"version"
:
version
,
...
...
@@ -57,8 +53,8 @@ func (s *Storage) Policy(ctx context.Context, name, group, version string) (*Pol
return
&
policy
,
nil
}
func
(
s
*
Storage
)
Lock
Policy
(
ctx
context
.
Context
,
name
,
group
,
version
string
)
error
{
_
,
err
:=
s
.
db
.
Database
(
s
.
dbname
)
.
Collection
(
s
.
collection
)
.
UpdateOne
(
func
(
s
*
Storage
)
Set
Policy
Lock
(
ctx
context
.
Context
,
name
,
group
,
version
string
,
lock
bool
)
error
{
_
,
err
:=
s
.
policy
.
UpdateOne
(
ctx
,
bson
.
M
{
"name"
:
name
,
...
...
@@ -67,24 +63,8 @@ func (s *Storage) LockPolicy(ctx context.Context, name, group, version string) e
},
bson
.
M
{
"$set"
:
bson
.
M
{
"locked"
:
true
,
},
},
)
return
err
}
func
(
s
*
Storage
)
UnlockPolicy
(
ctx
context
.
Context
,
name
,
group
,
version
string
)
error
{
_
,
err
:=
s
.
db
.
Database
(
s
.
dbname
)
.
Collection
(
s
.
collection
)
.
UpdateOne
(
ctx
,
bson
.
M
{
"name"
:
name
,
"group"
:
group
,
"version"
:
version
,
},
bson
.
M
{
"$set"
:
bson
.
M
{
"locked"
:
false
,
"locked"
:
lock
,
"lastUpdate"
:
time
.
Now
(),
},
},
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment