-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[management] Add structs for new networks concept (#3006)
- Loading branch information
1 parent
97bb74f
commit 65ec185
Showing
8 changed files
with
358 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,19 @@ | ||
package networks | ||
|
||
import "github.com/rs/xid" | ||
|
||
type Network struct { | ||
ID string `gorm:"index"` | ||
AccountID string `gorm:"index"` | ||
Name string | ||
Description string | ||
} | ||
|
||
func NewNetwork(accountId, name, description string) *Network { | ||
return &Network{ | ||
ID: xid.New().String(), | ||
AccountID: accountId, | ||
Name: name, | ||
Description: description, | ||
} | ||
} |
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,68 @@ | ||
package networks | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/rs/xid" | ||
) | ||
|
||
type NetworkResourceType string | ||
|
||
const ( | ||
host NetworkResourceType = "Host" | ||
subnet NetworkResourceType = "Subnet" | ||
domain NetworkResourceType = "Domain" | ||
) | ||
|
||
func (p NetworkResourceType) String() string { | ||
return string(p) | ||
} | ||
|
||
type NetworkResource struct { | ||
ID string `gorm:"index"` | ||
NetworkID string `gorm:"index"` | ||
AccountID string `gorm:"index"` | ||
Type NetworkResourceType | ||
Address string | ||
} | ||
|
||
func NewNetworkResource(accountID, networkID, address string) (*NetworkResource, error) { | ||
resourceType, err := getResourceType(address) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid address: %w", err) | ||
} | ||
|
||
return &NetworkResource{ | ||
ID: xid.New().String(), | ||
AccountID: accountID, | ||
NetworkID: networkID, | ||
Type: resourceType, | ||
Address: address, | ||
}, nil | ||
} | ||
|
||
// getResourceType returns the type of the resource based on the address | ||
func getResourceType(address string) (NetworkResourceType, error) { | ||
if ip, cidr, err := net.ParseCIDR(address); err == nil { | ||
ones, _ := cidr.Mask.Size() | ||
if strings.HasSuffix(address, "/32") || (ip != nil && ones == 32) { | ||
return host, nil | ||
} | ||
return subnet, nil | ||
} | ||
|
||
if net.ParseIP(address) != nil { | ||
return host, nil | ||
} | ||
|
||
domainRegex := regexp.MustCompile(`^(\*\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$`) | ||
if domainRegex.MatchString(address) { | ||
return domain, nil | ||
} | ||
|
||
return "", errors.New("not a host, subnet, or domain") | ||
} |
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,41 @@ | ||
package networks | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetResourceType(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expectedType NetworkResourceType | ||
expectedErr bool | ||
}{ | ||
// Valid host IPs | ||
{"1.1.1.1", host, false}, | ||
{"1.1.1.1/32", host, false}, | ||
// Valid subnets | ||
{"192.168.1.0/24", subnet, false}, | ||
{"10.0.0.0/16", subnet, false}, | ||
// Valid domains | ||
{"example.com", domain, false}, | ||
{"*.example.com", domain, false}, | ||
{"sub.example.com", domain, false}, | ||
// Invalid inputs | ||
{"invalid", "", true}, | ||
{"1.1.1.1/abc", "", true}, | ||
{"1234", "", true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.input, func(t *testing.T) { | ||
result, err := getResourceType(tt.input) | ||
if result != tt.expectedType { | ||
t.Errorf("Expected type %v, got %v", tt.expectedType, result) | ||
} | ||
|
||
if tt.expectedErr && err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
}) | ||
} | ||
} |
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,33 @@ | ||
package networks | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/rs/xid" | ||
) | ||
|
||
type NetworkRouter struct { | ||
ID string `gorm:"index"` | ||
NetworkID string `gorm:"index"` | ||
AccountID string `gorm:"index"` | ||
Peer string | ||
PeerGroups []string `gorm:"serializer:json"` | ||
Masquerade bool | ||
Metric int | ||
} | ||
|
||
func NewNetworkRouter(accountID string, networkID string, peer string, peerGroups []string, masquerade bool, metric int) (*NetworkRouter, error) { | ||
if peer != "" && len(peerGroups) > 0 { | ||
return nil, errors.New("peer and peerGroups cannot be set at the same time") | ||
} | ||
|
||
return &NetworkRouter{ | ||
ID: xid.New().String(), | ||
AccountID: accountID, | ||
NetworkID: networkID, | ||
Peer: peer, | ||
PeerGroups: peerGroups, | ||
Masquerade: masquerade, | ||
Metric: metric, | ||
}, nil | ||
} |
Oops, something went wrong.