Repository: threefoldtech/zos
Pull request #391 (2019-11-21 07:45:40)

provisiond: sort the reservation when the are received from BCDB 

codecov[bot]: # [Codecov](https://codecov.io/gh/threefoldtech/zos/pull/391?src=pr&el=h1) Report
> Merging [#391](https://codecov.io/gh/threefoldtech/zos/pull/391?src=pr&el=desc) into [master](https://codecov.io/gh/threefoldtech/zos/commit/937e4116e56356ddf2786d7e73cd00d66640328b?src=pr&el=desc) will **increase** coverage by `0.2%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/threefoldtech/zos/pull/391/graphs/tree.svg?width=650&token=f7Vmq1OAEm&height=150&src=pr)](https://codecov.io/gh/threefoldtech/zos/pull/391?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master     #391     +/-   ##
=========================================
+ Coverage   40.92%   41.12%   +0.2%     
=========================================
  Files          62       62             
  Lines        3851     3861     +10     
=========================================
+ Hits         1576     1588     +12     
+ Misses       2062     2060      -2     
  Partials      213      213
```


| [Impacted Files](https://codecov.io/gh/threefoldtech/zos/pull/391?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...is/types/provision/tfgrid\_reservation\_network\_1.go](https://codecov.io/gh/threefoldtech/zos/pull/391/diff?src=pr&el=tree#diff-cGtnL2dlZGlzL3R5cGVzL3Byb3Zpc2lvbi90ZmdyaWRfcmVzZXJ2YXRpb25fbmV0d29ya18xLmdv) | `76.31% <100%> (+0.64%)` | :arrow_up: |
| [pkg/gedis/commands\_provision.go](https://codecov.io/gh/threefoldtech/zos/pull/391/diff?src=pr&el=tree#diff-cGtnL2dlZGlzL2NvbW1hbmRzX3Byb3Zpc2lvbi5nbw==) | `46.92% <100%> (+2.18%)` | :arrow_up: |
| [pkg/flist/flist.go](https://codecov.io/gh/threefoldtech/zos/pull/391/diff?src=pr&el=tree#diff-cGtnL2ZsaXN0L2ZsaXN0Lmdv) | `56.54% <0%> (+1.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/threefoldtech/zos/pull/391?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/threefoldtech/zos/pull/391?src=pr&el=footer). Last update [937e411...2bf1eee](https://codecov.io/gh/threefoldtech/zos/pull/391?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/gedis/commands_provision.go b/pkg/gedis/commands_provision.go
index fbfc9203..285ef762 100644
--- a/pkg/gedis/commands_provision.go
+++ b/pkg/gedis/commands_provision.go
@@ -3,6 +3,7 @@ package gedis
 import (
 	"encoding/json"
 	"fmt"
+	"sort"
 	"time"
 
 	"github.com/threefoldtech/zos/pkg/schema"
@@ -13,6 +14,16 @@ import (
 	"github.com/threefoldtech/zos/pkg"
 )
 
+// provisionOrder is used to sort the workload type
+// in the right order for provisiond
+var provisionOrder = map[provision.ReservationType]int{
+	provision.DebugReservation:     0,
+	provision.NetworkReservation:   1,
+	provision.ZDBReservation:       2,
+	provision.VolumeReservation:    3,
+	provision.ContainerReservation: 4,
+}
+
 // Reserve provision.Reserver
 func (g *Gedis) Reserve(r *provision.Reservation, nodeID pkg.Identifier) (string, error) {
 	res := types.TfgridReservation1{
@@ -112,6 +123,12 @@ func (g *Gedis) Poll(nodeID pkg.Identifier, from uint64) ([]*provision.Reservati
 		reservations[i] = r
 	}
 
+	// sorts the primitive in the oder they need to be processed by provisiond
+	// network, zdb, volumes, container
+	sort.Slice(reservations, func(i int, j int) bool {
+		return provisionOrder[reservations[i].Type] < provisionOrder[reservations[j].Type]
+	})
+
 	return reservations, nil
 }
 
diff --git a/pkg/gedis/types/provision/tfgrid_reservation_network_1.go b/pkg/gedis/types/provision/tfgrid_reservation_network_1.go
index 958bb851..7640396b 100644
--- a/pkg/gedis/types/provision/tfgrid_reservation_network_1.go
+++ b/pkg/gedis/types/provision/tfgrid_reservation_network_1.go
@@ -68,9 +68,10 @@ func (r TfgridNetworkNetResource1) ToProvisionType() (pkg.NetResource, error) {
 
 // WireguardPeer1 jsx Schema
 type WireguardPeer1 struct {
-	PublicKey  string   `json:"public_key"`
-	Endpoint   string   `json:"endpoint"`
-	AllowedIPs []string `json:"allowed_iprange"`
+	PublicKey  string         `json:"public_key"`
+	Endpoint   string         `json:"endpoint"`
+	AllowedIPs []string       `json:"allowed_iprange"`
+	IPRange    schema.IPRange `json:"iprange"`
 }
 
 //ToProvisionType converts WireguardPeer1 to pkg.Peer
@@ -79,6 +80,7 @@ func (p WireguardPeer1) ToProvisionType() (pkg.Peer, error) {
 		WGPublicKey: p.PublicKey,
 		Endpoint:    p.Endpoint,
 		AllowedIPs:  make([]types.IPNet, len(p.AllowedIPs)),
+		Subnet:      types.NewIPNetFromSchema(p.IPRange),
 	}
 
 	var err error
diff --git a/pkg/schema/types.go b/pkg/schema/types.go
index 5e9f9cf1..7e4ee264 100644
--- a/pkg/schema/types.go
+++ b/pkg/schema/types.go
@@ -7,6 +7,8 @@ import (
 	"regexp"
 	"strconv"
 	"time"
+
+	"math/big"
 )
 
 var (
@@ -32,6 +34,16 @@ func (n Numeric) Float64() (float64, error) {
 	return strconv.ParseFloat(string(n), 64)
 }
 
+// BigInt returns parsed big.Int value
+func (n Numeric) BigInt() (*big.Int, error) {
+	bi := new(big.Int)
+	_, ok := bi.SetString(string(n), 10)
+	if !ok {
+		return nil, fmt.Errorf("failed to return '%s' as a big Int", n)
+	}
+	return bi, nil
+}
+
 // UnmarshalJSON method
 func (n *Numeric) UnmarshalJSON(in []byte) error {
 	var v interface{}


Test patch:
diff --git a/pkg/gedis/commands_provision_test.go b/pkg/gedis/commands_provision_test.go
index d2517e2f..df206a53 100644
--- a/pkg/gedis/commands_provision_test.go
+++ b/pkg/gedis/commands_provision_test.go
@@ -85,9 +85,9 @@ func TestProvisionPoll(t *testing.T) {
 
 	require.NoError(err)
 	require.Len(reservations, 2)
-	require.Equal(reservations[0].ID, "1-1")
-	require.Equal(reservations[0].Type, provision.VolumeReservation)
-	require.Equal(reservations[1].Type, provision.ZDBReservation)
+	require.Equal(reservations[0].ID, "2-1")
+	require.Equal(reservations[0].Type, provision.ZDBReservation)
+	require.Equal(reservations[1].Type, provision.VolumeReservation)
 	conn.AssertCalled(t, "Close")
 
 	args = Args{
@@ -115,8 +115,8 @@ func TestProvisionPoll(t *testing.T) {
 
 	require.NoError(err)
 	require.Len(reservations, 2)
-	require.Equal(reservations[0].Type, provision.VolumeReservation)
-	require.Equal(reservations[1].Type, provision.ZDBReservation)
+	require.Equal(reservations[0].Type, provision.ZDBReservation)
+	require.Equal(reservations[1].Type, provision.VolumeReservation)
 	conn.AssertCalled(t, "Close")
 
 }
diff --git a/pkg/schema/types_test.go b/pkg/schema/types_test.go
index 7c3dc092..22c7b60d 100644
--- a/pkg/schema/types_test.go
+++ b/pkg/schema/types_test.go
@@ -2,6 +2,7 @@ package schema
 
 import (
 	"encoding/json"
+	"fmt"
 	"net"
 	"testing"
 	"time"
@@ -38,6 +39,23 @@ func TestParseDate(t *testing.T) {
 	}
 }
 
+func TestNumericBigInt(t *testing.T) {
+	const inputValue = "1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757"
+	var n Numeric
+	err := n.UnmarshalJSON([]byte(fmt.Sprintf(`"%s"`, inputValue)))
+	if err != nil {
+		t.Fatal(err)
+	}
+	bi, err := n.BigInt()
+	if err != nil {
+		t.Fatal(err)
+	}
+	output := bi.String()
+	if inputValue != output {
+		t.Fatalf("%s != %s", inputValue, output)
+	}
+}
+
 func TestDateNil(t *testing.T) {
 	var d Date