Repository: cloudflare/cloudflare-go
Pull request #1503 (2024-02-08 19:17:09)

MT / MWAN: Adds IPsec tunnel healthcheck direction & rate parameters
<!-- 
Provide a general summary of your changes in the title above. You should
remove this overview, any sections and any section descriptions you
don't need below before submitting. There isn't a strict requirement to
use this template if you can structure your description and still cover
these points. 
-->

## Description
This PR adds healthcheck direction and rate parameters to the Magic IPsec Healthcheck type.

<!--
Describe your changes in detail through motivation and context. Why is
this change required? What problem does it solve? If it fixes an open
issue, link to the issue using GitHub's closing issues keywords[1].
-->
## Has your change been tested?
Updated tests & ran a test against an actual environment.

<!--
Explain how the change has been tested and what you ran to confirm your
change affects other parts of the code. Automated tests are generally
expected and changes without tests should explain why they aren't
required.
-->


## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [x] This change is using publicly documented in [cloudflare/api-schemas](https://github.com/cloudflare/api-schemas) 
      and relies on stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/



Patch:
diff --git a/.changelog/1503.txt b/.changelog/1503.txt
new file mode 100644
index 000000000..4798139e0
--- /dev/null
+++ b/.changelog/1503.txt
@@ -0,0 +1,3 @@
+```release-note:enhancement
+magic-transit: Adds IPsec tunnel healthcheck direction & rate parameters
+```
diff --git a/magic_transit_tunnel_healthcheck.go b/magic_transit_tunnel_healthcheck.go
index f79761e19..ced8a40f4 100644
--- a/magic_transit_tunnel_healthcheck.go
+++ b/magic_transit_tunnel_healthcheck.go
@@ -2,7 +2,9 @@ package cloudflare
 
 // MagicTransitTunnelHealthcheck contains information about a tunnel health check.
 type MagicTransitTunnelHealthcheck struct {
-	Enabled bool   `json:"enabled"`
-	Target  string `json:"target,omitempty"`
-	Type    string `json:"type,omitempty"`
+	Enabled   bool   `json:"enabled"`
+	Target    string `json:"target,omitempty"`
+	Type      string `json:"type,omitempty"`
+	Rate      string `json:"rate,omitempty"`
+	Direction string `json:"direction,omitempty"`
 }


Test patch:
diff --git a/magic_transit_ipsec_tunnel_test.go b/magic_transit_ipsec_tunnel_test.go
index ef7a0624a..65f896da3 100644
--- a/magic_transit_ipsec_tunnel_test.go
+++ b/magic_transit_ipsec_tunnel_test.go
@@ -162,6 +162,68 @@ func TestCreateMagicTransitIPsecTunnels(t *testing.T) {
 	}
 }
 
+func TestCreateMagicTransitIPsecTunnelsWithHealthcheck(t *testing.T) {
+	setup()
+	defer teardown()
+
+	handler := func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
+		w.Header().Set("content-type", "application/json")
+		fmt.Fprint(w, `{
+      "success": true,
+      "errors": [],
+      "messages": [],
+      "result": {
+        "ipsec_tunnels": [
+          {
+            "id": "c4a7362d577a6c3019a474fd6f485821",
+            "created_on": "2017-06-14T00:00:00Z",
+            "modified_on": "2017-06-14T05:20:00Z",
+            "name": "IPsec_1",
+            "customer_endpoint": "203.0.113.1",
+            "cloudflare_endpoint": "203.0.113.2",
+            "interface_address": "192.0.2.0/31",
+            "description": "Tunnel for ISP X",
+            "health_check": {
+              "enabled": true,
+              "type": "reply",
+              "rate": "mid",
+              "direction": "bidirectional"
+            }
+          }
+        ]
+      }
+    }`)
+	}
+
+	mux.HandleFunc("/accounts/"+testAccountID+"/magic/ipsec_tunnels", handler)
+
+	createdOn, _ := time.Parse(time.RFC3339, "2017-06-14T00:00:00Z")
+	modifiedOn, _ := time.Parse(time.RFC3339, "2017-06-14T05:20:00Z")
+
+	want := []MagicTransitIPsecTunnel{{
+		ID:                 "c4a7362d577a6c3019a474fd6f485821",
+		CreatedOn:          &createdOn,
+		ModifiedOn:         &modifiedOn,
+		Name:               "IPsec_1",
+		CustomerEndpoint:   "203.0.113.1",
+		CloudflareEndpoint: "203.0.113.2",
+		InterfaceAddress:   "192.0.2.0/31",
+		Description:        "Tunnel for ISP X",
+		HealthCheck: &MagicTransitTunnelHealthcheck{
+			Enabled:   true,
+			Type:      "reply",
+			Rate:      "mid",
+			Direction: "bidirectional",
+		},
+	}}
+
+	actual, err := client.CreateMagicTransitIPsecTunnels(context.Background(), testAccountID, want)
+	if assert.NoError(t, err) {
+		assert.Equal(t, want, actual)
+	}
+}
+
 func TestUpdateMagicTransitIPsecTunnel(t *testing.T) {
 	setup()
 	defer teardown()