From dba4ec3e3a04e6d805499c767a42cc58e643cd0a Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 13 Sep 2015 14:30:45 -0700 Subject: [PATCH] nomad: adding copy helpers --- nomad/structs/structs.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 910520df2..ef1f6c04b 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -539,6 +539,18 @@ type Resources struct { Networks []*NetworkResource } +// Copy returns a deep copy of the resources +func (r *Resources) Copy() *Resources { + newR := new(Resources) + *newR = *r + n := len(r.Networks) + newR.Networks = make([]*NetworkResource, n) + for i := 0; i < n; i++ { + newR.Networks[i] = r.Networks[i].Copy() + } + return newR +} + // NetIndex finds the matching net index either using IP or // CIDR block lookup func (r *Resources) NetIndex(n *NetworkResource) int { @@ -664,6 +676,15 @@ type NetworkResource struct { DynamicPorts int // Dynamically assigned ports } +// Copy returns a deep copy of the network resource +func (n *NetworkResource) Copy() *NetworkResource { + newR := new(NetworkResource) + *newR = *n + newR.ReservedPorts = make([]int, len(n.ReservedPorts)) + copy(newR.ReservedPorts, n.ReservedPorts) + return newR +} + // Add adds the resources of the delta to this, potentially // returning an error if not possible. func (n *NetworkResource) Add(delta *NetworkResource) {