-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update netaddr which made all structs opaque, which is good (#61)
- Loading branch information
Showing
9 changed files
with
179 additions
and
116 deletions.
There are no files selected for viewing
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
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,65 @@ | ||
package ipam | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type prefixJSON struct { | ||
Prefix | ||
AvailableChildPrefixes map[string]bool // available child prefixes of this prefix | ||
// TODO remove this in the next release | ||
ChildPrefixLength int // the length of the child prefixes. Legacy to migrate existing prefixes stored in the db to set the IsParent on reads. | ||
IsParent bool // set to true if there are child prefixes | ||
IPs map[string]bool // The ips contained in this prefix | ||
Version int64 // Version is used for optimistic locking | ||
} | ||
|
||
func (p prefixJSON) toPrefix() Prefix { | ||
// Legacy support only on reading from database, convert to isParent. | ||
// TODO remove this in the next release | ||
if p.ChildPrefixLength > 0 { | ||
p.IsParent = true | ||
} | ||
return Prefix{ | ||
Cidr: p.Cidr, | ||
ParentCidr: p.ParentCidr, | ||
availableChildPrefixes: p.AvailableChildPrefixes, | ||
childPrefixLength: p.ChildPrefixLength, | ||
isParent: p.IsParent, | ||
ips: p.IPs, | ||
version: p.Version, | ||
} | ||
} | ||
|
||
func (p Prefix) toPrefixJSON() prefixJSON { | ||
return prefixJSON{ | ||
Prefix: Prefix{ | ||
Cidr: p.Cidr, | ||
ParentCidr: p.ParentCidr, | ||
}, | ||
AvailableChildPrefixes: p.availableChildPrefixes, | ||
IsParent: p.isParent, | ||
// TODO remove this in the next release | ||
ChildPrefixLength: p.childPrefixLength, | ||
IPs: p.ips, | ||
Version: p.version, | ||
} | ||
} | ||
|
||
func (p Prefix) toJSON() ([]byte, error) { | ||
pj, err := json.Marshal(p.toPrefixJSON()) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to marshal prefix:%w", err) | ||
} | ||
return pj, nil | ||
} | ||
|
||
func fromJSON(js []byte) (Prefix, error) { | ||
var pre prefixJSON | ||
err := json.Unmarshal(js, &pre) | ||
if err != nil { | ||
return Prefix{}, fmt.Errorf("unable to unmarshal prefix:%w", err) | ||
} | ||
return pre.toPrefix(), 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
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
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