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

@@ -14,7 +14,6 @@ import (
"github.com/netbirdio/netbird/management/server/http/configs"
"github.com/netbirdio/netbird/management/server/http/util"
"github.com/netbirdio/netbird/management/server/jwtclaims"
networkTypes "github.com/netbirdio/netbird/management/server/networks/resources/types"
"github.com/netbirdio/netbird/management/server/status"
"github.com/netbirdio/netbird/management/server/types"
)
@@ -182,10 +181,9 @@ func (h *handler) savePolicy(w http.ResponseWriter, r *http.Request, accountID s
if hasSourceResource {
// TODO: validate the resource id and type
pr.SourceResource = networkTypes.Resource{
ID: rule.SourceResource.Id,
Type: string(rule.SourceResource.Type),
}
sourceResource := &types.Resource{}
sourceResource.FromAPIRequest(rule.SourceResource)
pr.SourceResource = *sourceResource
}
if hasDestinations {
@@ -194,10 +192,9 @@ func (h *handler) savePolicy(w http.ResponseWriter, r *http.Request, accountID s
if hasDestinationResource {
// TODO: validate the resource id and type
pr.DestinationResource = networkTypes.Resource{
ID: rule.DestinationResource.Id,
Type: string(rule.DestinationResource.Type),
}
destinationResource := &types.Resource{}
destinationResource.FromAPIRequest(rule.DestinationResource)
pr.DestinationResource = *destinationResource
}
pr.Enabled = rule.Enabled
@@ -382,13 +379,15 @@ func toPolicyResponse(groups []*nbgroup.Group, policy *types.Policy) *api.Policy
rID := r.ID
rDescription := r.Description
rule := api.PolicyRule{
Id: &rID,
Name: r.Name,
Enabled: r.Enabled,
Description: &rDescription,
Bidirectional: r.Bidirectional,
Protocol: api.PolicyRuleProtocol(r.Protocol),
Action: api.PolicyRuleAction(r.Action),
Id: &rID,
Name: r.Name,
Enabled: r.Enabled,
Description: &rDescription,
Bidirectional: r.Bidirectional,
Protocol: api.PolicyRuleProtocol(r.Protocol),
Action: api.PolicyRuleAction(r.Action),
SourceResource: r.SourceResource.ToAPIResponse(),
DestinationResource: r.DestinationResource.ToAPIResponse(),
}
if len(r.Ports) != 0 {

View File

@@ -12,11 +12,6 @@ import (
"github.com/netbirdio/netbird/management/server/http/api"
)
type Resource struct {
ID string
Type string
}
type NetworkResourceType string
const (

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)
}