Repository: hetznercloud/hcloud-go
Pull request #142 (2020-07-17 06:21:22)

LB Traffic fields
Add `IncludedTraffic`, `OutgoingTraffic` and `IngoingTraffic` fields to `LoadBalancer`


Patch:
diff --git a/CHANGES.md b/CHANGES.md
index 7529ce7..dd1ccc4 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -4,6 +4,7 @@
 
 * Fix nil pointer dereference when creating a Load Balancer with HTTP(S)
   service and not providing HTTP-specific options
+* Add `IncludedTraffic`, `OutgoingTraffic` and `IngoingTraffic` fields to `LoadBalancer`
 
 ## v1.18.2
 
diff --git a/hcloud/load_balancer.go b/hcloud/load_balancer.go
index 254a011..a929509 100644
--- a/hcloud/load_balancer.go
+++ b/hcloud/load_balancer.go
@@ -27,6 +27,9 @@ type LoadBalancer struct {
 	Protection       LoadBalancerProtection
 	Labels           map[string]string
 	Created          time.Time
+	IncludedTraffic  uint64
+	OutgoingTraffic  uint64
+	IngoingTraffic   uint64
 }
 
 // LoadBalancerPublicNet represents a Load Balancer's public network.
diff --git a/hcloud/schema.go b/hcloud/schema.go
index 53047c4..e497af6 100644
--- a/hcloud/schema.go
+++ b/hcloud/schema.go
@@ -431,8 +431,9 @@ func LoadBalancerFromSchema(s schema.LoadBalancer) *LoadBalancer {
 		Protection: LoadBalancerProtection{
 			Delete: s.Protection.Delete,
 		},
-		Labels:  map[string]string{},
-		Created: s.Created,
+		Labels:          map[string]string{},
+		Created:         s.Created,
+		IncludedTraffic: s.IncludedTraffic,
 	}
 	for _, privateNet := range s.PrivateNet {
 		l.PrivateNet = append(l.PrivateNet, LoadBalancerPrivateNet{
@@ -440,6 +441,12 @@ func LoadBalancerFromSchema(s schema.LoadBalancer) *LoadBalancer {
 			IP:      net.ParseIP(privateNet.IP),
 		})
 	}
+	if s.OutgoingTraffic != nil {
+		l.OutgoingTraffic = *s.OutgoingTraffic
+	}
+	if s.IngoingTraffic != nil {
+		l.IngoingTraffic = *s.IngoingTraffic
+	}
 	for _, service := range s.Services {
 		l.Services = append(l.Services, LoadBalancerServiceFromSchema(service))
 	}
diff --git a/hcloud/schema/load_balancer.go b/hcloud/schema/load_balancer.go
index a2f1039..fe5b6b9 100644
--- a/hcloud/schema/load_balancer.go
+++ b/hcloud/schema/load_balancer.go
@@ -15,6 +15,9 @@ type LoadBalancer struct {
 	Services         []LoadBalancerService    `json:"services"`
 	Targets          []LoadBalancerTarget     `json:"targets"`
 	Algorithm        LoadBalancerAlgorithm    `json:"algorithm"`
+	IncludedTraffic  uint64                   `json:"included_traffic"`
+	OutgoingTraffic  *uint64                  `json:"outgoing_traffic"`
+	IngoingTraffic   *uint64                  `json:"ingoing_traffic"`
 }
 
 type LoadBalancerPublicNet struct {


Test patch:
diff --git a/hcloud/schema_test.go b/hcloud/schema_test.go
index e627234..6be7fde 100644
--- a/hcloud/schema_test.go
+++ b/hcloud/schema_test.go
@@ -1268,6 +1268,9 @@ func TestLoadBalancerFromSchema(t *testing.T) {
 				}
 			]
 		},
+		"outgoing_traffic": 123456,
+		"ingoing_traffic": 7891011,
+		"included_traffic": 654321,
 		"protection": {
 			"delete": false
 		},
@@ -1368,6 +1371,15 @@ func TestLoadBalancerFromSchema(t *testing.T) {
 	if loadBalancer.Algorithm.Type != "round_robin" {
 		t.Errorf("unexpected Algorithm.Type: %v", loadBalancer.Algorithm.Type)
 	}
+	if loadBalancer.IncludedTraffic != 654321 {
+		t.Errorf("unexpected included traffic: %v", loadBalancer.IncludedTraffic)
+	}
+	if loadBalancer.OutgoingTraffic != 123456 {
+		t.Errorf("unexpected outgoing traffic: %v", loadBalancer.OutgoingTraffic)
+	}
+	if loadBalancer.IngoingTraffic != 7891011 {
+		t.Errorf("unexpected ingoing traffic: %v", loadBalancer.IngoingTraffic)
+	}
 }
 
 func TestLoadBalancerServiceFromSchema(t *testing.T) {