diff --git a/management/server/http/handlers/policies/policies_handler.go b/management/server/http/handlers/policies/policies_handler.go index efa1142b4..0255c773b 100644 --- a/management/server/http/handlers/policies/policies_handler.go +++ b/management/server/http/handlers/policies/policies_handler.go @@ -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 { diff --git a/management/server/networks/resources/types/resource.go b/management/server/networks/resources/types/resource.go index 117561bb2..dd2bdd6b7 100644 --- a/management/server/networks/resources/types/resource.go +++ b/management/server/networks/resources/types/resource.go @@ -12,11 +12,6 @@ import ( "github.com/netbirdio/netbird/management/server/http/api" ) -type Resource struct { - ID string - Type string -} - type NetworkResourceType string const ( diff --git a/management/server/types/policyrule.go b/management/server/types/policyrule.go index 250d7011c..bd9a99292 100644 --- a/management/server/types/policyrule.go +++ b/management/server/types/policyrule.go @@ -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 diff --git a/management/server/types/resource.go b/management/server/types/resource.go new file mode 100644 index 000000000..820872f20 --- /dev/null +++ b/management/server/types/resource.go @@ -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) +}