Repository: hetznercloud/hcloud-go
Pull request #552 (2024-11-11 10:22:36)

fix: do not send primary IPs ID opts to the API
The ID is not part of the request body docs, but was still send. This removes the ID from the request body.

jooola: Should be backported.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/552?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 67.95%. Comparing base [(`b42f2d3`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/b42f2d3ac892a45bdfdf96d7fc7eb39593d4cef4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) to head [(`24be366`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/24be366d4ce2cb98a584da074f6120dc0a75e8b9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #552   +/-   ##
=======================================
  Coverage   67.95%   67.95%           
=======================================
  Files          47       47           
  Lines        4902     4902           
=======================================
  Hits         3331     3331           
  Misses       1163     1163           
  Partials      408      408           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/552?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).


Patch:
diff --git a/hcloud/primary_ip.go b/hcloud/primary_ip.go
index 5d241e6..05c412e 100644
--- a/hcloud/primary_ip.go
+++ b/hcloud/primary_ip.go
@@ -119,7 +119,7 @@ type PrimaryIPUpdateOpts struct {
 // PrimaryIPAssignOpts defines the request to
 // assign a Primary IP to an assignee (usually a server).
 type PrimaryIPAssignOpts struct {
-	ID           int64
+	ID           int64  `json:"-"`
 	AssigneeID   int64  `json:"assignee_id"`
 	AssigneeType string `json:"assignee_type"`
 }
@@ -133,7 +133,7 @@ type PrimaryIPAssignResult struct {
 // PrimaryIPChangeDNSPtrOpts defines the request to
 // change a DNS PTR entry from a Primary IP.
 type PrimaryIPChangeDNSPtrOpts struct {
-	ID     int64
+	ID     int64  `json:"-"`
 	DNSPtr string `json:"dns_ptr"`
 	IP     string `json:"ip"`
 }
@@ -147,8 +147,8 @@ type PrimaryIPChangeDNSPtrResult struct {
 // PrimaryIPChangeProtectionOpts defines the request to
 // change protection configuration of a Primary IP.
 type PrimaryIPChangeProtectionOpts struct {
-	ID     int64
-	Delete bool `json:"delete"`
+	ID     int64 `json:"-"`
+	Delete bool  `json:"delete"`
 }
 
 // PrimaryIPChangeProtectionResult defines the response


Test patch:
diff --git a/hcloud/primary_ip_test.go b/hcloud/primary_ip_test.go
index e53c936..a61c845 100644
--- a/hcloud/primary_ip_test.go
+++ b/hcloud/primary_ip_test.go
@@ -368,18 +368,15 @@ func TestPrimaryIPClient(t *testing.T) {
 				t.Error("expected POST")
 			}
 			w.Header().Set("Content-Type", "application/json")
-			expectedReqBody := PrimaryIPAssignOpts{
-				AssigneeType: "server",
-				AssigneeID:   1,
-				ID:           1,
-			}
+
 			if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
 				t.Fatal(err)
 			}
-			if !cmp.Equal(expectedReqBody, reqBody) {
-				t.Log(cmp.Diff(expectedReqBody, reqBody))
-				t.Error("unexpected request body")
-			}
+
+			assert.Equal(t, int64(1), reqBody.AssigneeID)
+			assert.Equal(t, "server", reqBody.AssigneeType)
+			assert.Equal(t, int64(0), reqBody.ID)
+
 			json.NewEncoder(w).Encode(PrimaryIPAssignResult{
 				Action: schema.Action{ID: 1},
 			})
@@ -428,16 +425,15 @@ func TestPrimaryIPClient(t *testing.T) {
 				t.Error("expected POST")
 			}
 			w.Header().Set("Content-Type", "application/json")
-			expectedReqBody := PrimaryIPChangeDNSPtrOpts{
-				ID: 1,
-			}
+
 			if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
 				t.Fatal(err)
 			}
-			if !cmp.Equal(expectedReqBody, reqBody) {
-				t.Log(cmp.Diff(expectedReqBody, reqBody))
-				t.Error("unexpected request body")
-			}
+
+			assert.Equal(t, "value", reqBody.DNSPtr)
+			assert.Equal(t, "127.0.0.1", reqBody.IP)
+			assert.Equal(t, int64(0), reqBody.ID)
+
 			json.NewEncoder(w).Encode(PrimaryIPChangeDNSPtrResult{
 				Action: schema.Action{ID: 1},
 			})
@@ -445,7 +441,9 @@ func TestPrimaryIPClient(t *testing.T) {
 
 		ctx := context.Background()
 		opts := PrimaryIPChangeDNSPtrOpts{
-			ID: 1,
+			ID:     1,
+			DNSPtr: "value",
+			IP:     "127.0.0.1",
 		}
 
 		action, resp, err := env.Client.PrimaryIP.ChangeDNSPtr(ctx, opts)
@@ -463,17 +461,13 @@ func TestPrimaryIPClient(t *testing.T) {
 				t.Error("expected POST")
 			}
 			w.Header().Set("Content-Type", "application/json")
-			expectedReqBody := PrimaryIPChangeProtectionOpts{
-				ID:     1,
-				Delete: true,
-			}
 			if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
 				t.Fatal(err)
 			}
-			if !cmp.Equal(expectedReqBody, reqBody) {
-				t.Log(cmp.Diff(expectedReqBody, reqBody))
-				t.Error("unexpected request body")
-			}
+
+			assert.True(t, reqBody.Delete)
+			assert.Equal(t, int64(0), reqBody.ID)
+
 			json.NewEncoder(w).Encode(PrimaryIPChangeProtectionResult{
 				Action: schema.Action{ID: 1},
 			})