Refactor the resource and add api helper functions

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga
2024-12-11 16:16:23 +01:00
parent e5a6f9e965
commit 9322a92ee9
4 changed files with 47 additions and 27 deletions

View File

@@ -1,9 +1,5 @@
package types
import (
"github.com/netbirdio/netbird/management/server/networks/resources/types"
)
// PolicyUpdateOperationType operation type
type PolicyUpdateOperationType int
@@ -46,13 +42,13 @@ type PolicyRule struct {
Destinations []string `gorm:"serializer:json"`
// DestinationResource policy destination resource that the rule is applied to
DestinationResource types.Resource `gorm:"serializer:json"`
DestinationResource Resource `gorm:"serializer:json"`
// Sources policy source groups
Sources []string `gorm:"serializer:json"`
// SourceResource policy source resource that the rule is applied to
SourceResource types.Resource `gorm:"serializer:json"`
SourceResource Resource `gorm:"serializer:json"`
// Bidirectional define if the rule is applicable in both directions, sources, and destinations
Bidirectional bool

View File

@@ -0,0 +1,30 @@
package types
import (
"github.com/netbirdio/netbird/management/server/http/api"
)
type Resource struct {
ID string
Type string
}
func (r *Resource) ToAPIResponse() *api.Resource {
if r.ID == "" && r.Type == "" {
return nil
}
return &api.Resource{
Id: r.ID,
Type: api.ResourceType(r.Type),
}
}
func (r *Resource) FromAPIRequest(req *api.Resource) {
if req == nil {
return
}
r.ID = req.Id
r.Type = string(req.Type)
}