Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider: Implement Defined.net hosts HTTP API client #7

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions internal/definednet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/onsi/gomega/gstruct"
"github.com/samber/lo"
"github.com/sendsmaily/terraform-provider-definednet/internal/definednet"
)

Expand Down Expand Up @@ -39,18 +38,6 @@ var _ = Describe("API client's invariants", func() {
})

var _ = Describe("executing API requests", func() {
var (
server *ghttp.Server
client definednet.Client
)

BeforeEach(func() {
server = ghttp.NewServer()
DeferCleanup(server.Close)

client = lo.Must(definednet.NewClient(server.URL(), "supersecret"))
})

Context("request headers", func() {
Specify("all requests are executed with common HTTP headers", func(ctx SpecContext) {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, nil))
Expand Down Expand Up @@ -149,18 +136,6 @@ var _ = Describe("handling HTTP API success responses", func() {
} `json:"nested"`
}

var (
server *ghttp.Server
client definednet.Client
)

BeforeEach(func() {
server = ghttp.NewServer()
DeferCleanup(server.Close)

client = lo.Must(definednet.NewClient(server.URL(), "supersecret"))
})

Specify("API responses are deserialized into passed response payload container", func(ctx SpecContext) {
var container response

Expand Down Expand Up @@ -215,18 +190,6 @@ var _ = Describe("handling HTTP API success responses", func() {
})

var _ = Describe("handling HTTP API error responses", func() {
var (
server *ghttp.Server
client definednet.Client
)

BeforeEach(func() {
server = ghttp.NewServer()
DeferCleanup(server.Close)

client = lo.Must(definednet.NewClient(server.URL(), "supersecret"))
})

Specify("API errors are reported to the user", func(ctx SpecContext) {
server.AppendHandlers(ghttp.RespondWithJSONEncoded(
http.StatusBadRequest,
Expand Down
130 changes: 130 additions & 0 deletions internal/definednet/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package definednet

import (
"context"
"net/http"
)

// CreateHost creates a Defined.net host.
func CreateHost(ctx context.Context, client Client, req CreateHostRequest) (*CreateHostResponse, error) {
var resp CreateHostResponse
if err := client.Do(ctx, http.MethodPost, []string{"v1", "hosts"}, req, &resp); err != nil {
return nil, err
}

return &resp, nil
}

type (
// CreateHostRequest is a request data model for CreateHost endpoint.
CreateHostRequest struct {
NetworkID string `json:"networkID"`
RoleID string `json:"roleID"`
Name string `json:"name"`
IPAddress string `json:"ipAddress"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
IsLighthouse bool `json:"isLighthouse"`
IsRelay bool `json:"isRelay"`
Tags []string `json:"tags"`
}

// CreateHostResponse is a response data model for CreateHost endpoint.
CreateHostResponse struct {
Data struct {
ID string `json:"id"`
NetworkID string `json:"networkID"`
RoleID string `json:"roleID"`
Name string `json:"name"`
IPAddress string `json:"ipAddress"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
IsLighthouse bool `json:"isLighthouse"`
IsRelay bool `json:"isRelay"`
Tags []string `json:"tags"`
} `json:"data"`
}
)

// DeleteHost deletes a Defined.net host.
func DeleteHost(ctx context.Context, client Client, req DeleteHostRequest) error {
return client.Do(ctx, http.MethodDelete, []string{"v1", "hosts", req.ID}, nil, nil)
}

type (
// DeleteHostRequest is a request data model for DeleteHost endpoint.
DeleteHostRequest struct {
ID string
}
)

// GetHost retrieves a Defined.net host.
func GetHost(ctx context.Context, client Client, req GetHostRequest) (*GetHostResponse, error) {
var resp GetHostResponse
if err := client.Do(ctx, http.MethodGet, []string{"v1", "hosts", req.ID}, nil, &resp); err != nil {
return nil, err
}

return &resp, nil
}

type (
// GetHostRequest is a request data model for GetHost endpoint.
GetHostRequest struct {
ID string
}

// GetHostResponse is a response data model for GetHost endpoint.
GetHostResponse struct {
Data struct {
ID string `json:"id"`
NetworkID string `json:"networkID"`
RoleID string `json:"roleID"`
Name string `json:"name"`
IPAddress string `json:"ipAddress"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
IsLighthouse bool `json:"isLighthouse"`
IsRelay bool `json:"isRelay"`
Tags []string `json:"tags"`
} `json:"data"`
}
)

// UpdateHost updates a Defined.net host.
func UpdateHost(ctx context.Context, client Client, req UpdateHostRequest) (*UpdateHostResponse, error) {
var resp UpdateHostResponse
if err := client.Do(ctx, http.MethodPut, []string{"v1", "hosts", req.ID}, req, &resp); err != nil {
return nil, err
}

return &resp, nil
}

type (
// UpdateHostRequest is a request data model for UpdateHost endpoint.
UpdateHostRequest struct {
ID string `json:"-"`
RoleID string `json:"roleID"`
Name string `json:"name"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
Tags []string `json:"tags"`
}

// UpdateHostResponse is a response data model for UpdateHost endpoint.
UpdateHostResponse struct {
Data struct {
ID string `json:"id"`
NetworkID string `json:"networkID"`
RoleID string `json:"roleID"`
Name string `json:"name"`
IPAddress string `json:"ipAddress"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
IsLighthouse bool `json:"isLighthouse"`
IsRelay bool `json:"isRelay"`
Tags []string `json:"tags"`
} `json:"data"`
}
)
178 changes: 178 additions & 0 deletions internal/definednet/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package definednet_test

import (
"net/http"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
. "github.com/onsi/gomega/gstruct"
"github.com/sendsmaily/terraform-provider-definednet/internal/definednet"
)

var _ = Describe("creating hosts", func() {
Specify("hosts are created on Defined.net", func(ctx SpecContext) {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodPost, "/v1/hosts"),
ghttp.VerifyJSONRepresenting(map[string]any{
"networkID": "network-id",
"roleID": "role-id",
"name": "host.smaily.testing",
"ipAddress": "10.0.0.1",
"staticAddresses": []string{"127.0.0.1:8484", "172.16.0.1:8484"},
"listenPort": 8484,
"isLighthouse": true,
"isRelay": true,
"tags": []string{"tag:one", "tag:two"},
}),
ghttp.RespondWithJSONEncoded(http.StatusOK, map[string]any{}),
))

Expect(definednet.CreateHost(ctx, client, definednet.CreateHostRequest{
NetworkID: "network-id",
RoleID: "role-id",
Name: "host.smaily.testing",
IPAddress: "10.0.0.1",
StaticAddresses: []string{"127.0.0.1:8484", "172.16.0.1:8484"},
ListenPort: 8484,
IsLighthouse: true,
IsRelay: true,
Tags: []string{"tag:one", "tag:two"},
})).Error().NotTo(HaveOccurred())
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})

Specify("Defined.net responses are returned", func(ctx SpecContext) {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, hostJSONResponse))
Expect(definednet.CreateHost(ctx, client, definednet.CreateHostRequest{})).
To(PointTo(MatchAllFields(Fields{
"Data": MatchAllFields(Fields{
"ID": Equal("host-id"),
"NetworkID": Equal("network-id"),
"RoleID": Equal("role-id"),
"Name": Equal("host.smaily.testing"),
"IPAddress": Equal("10.0.0.1"),
"StaticAddresses": HaveExactElements("127.0.0.1:8484", "172.16.0.1:8484"),
"ListenPort": Equal(8484),
"IsLighthouse": BeTrue(),
"IsRelay": BeTrue(),
"Tags": HaveExactElements("tag:one", "tag:two"),
}),
})))
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})
})

var _ = Describe("deleting hosts", func() {
Specify("hosts are deleted from Defined.net", func(ctx SpecContext) {
server.AppendHandlers(ghttp.VerifyRequest(http.MethodDelete, "/v1/hosts/host-id"))
Expect(definednet.DeleteHost(ctx, client, definednet.DeleteHostRequest{
ID: "host-id",
})).To(Succeed())
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})
})

var _ = Describe("getting hosts", func() {
Specify("Defined.net responses are returned", func(ctx SpecContext) {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodGet, "/v1/hosts/host-id"),
ghttp.RespondWith(http.StatusOK, hostJSONResponse)),
)

Expect(definednet.GetHost(ctx, client, definednet.GetHostRequest{
ID: "host-id",
})).To(PointTo(MatchAllFields(Fields{
"Data": MatchAllFields(Fields{
"ID": Equal("host-id"),
"NetworkID": Equal("network-id"),
"RoleID": Equal("role-id"),
"Name": Equal("host.smaily.testing"),
"IPAddress": Equal("10.0.0.1"),
"StaticAddresses": HaveExactElements("127.0.0.1:8484", "172.16.0.1:8484"),
"ListenPort": Equal(8484),
"IsLighthouse": BeTrue(),
"IsRelay": BeTrue(),
"Tags": HaveExactElements("tag:one", "tag:two"),
}),
})))
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})
})

var _ = Describe("updating hosts", func() {
Specify("hosts are updated on Defined.net", func(ctx SpecContext) {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodPut, "/v1/hosts/host-id"),
ghttp.VerifyJSONRepresenting(map[string]any{
"roleID": "role-id",
"name": "host.smaily.testing",
"staticAddresses": []string{"127.0.0.1:8484", "172.16.0.1:8484"},
"listenPort": 8484,
"tags": []string{"tag:one", "tag:two"},
}),
ghttp.RespondWithJSONEncoded(http.StatusOK, map[string]any{}),
))

Expect(definednet.UpdateHost(ctx, client, definednet.UpdateHostRequest{
ID: "host-id",
RoleID: "role-id",
Name: "host.smaily.testing",
StaticAddresses: []string{"127.0.0.1:8484", "172.16.0.1:8484"},
ListenPort: 8484,
Tags: []string{"tag:one", "tag:two"},
})).Error().NotTo(HaveOccurred())
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})

Specify("Defined.net responses are returned", func(ctx SpecContext) {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, hostJSONResponse))
Expect(definednet.UpdateHost(ctx, client, definednet.UpdateHostRequest{})).
To(PointTo(MatchAllFields(Fields{
"Data": MatchAllFields(Fields{
"ID": Equal("host-id"),
"NetworkID": Equal("network-id"),
"RoleID": Equal("role-id"),
"Name": Equal("host.smaily.testing"),
"IPAddress": Equal("10.0.0.1"),
"StaticAddresses": HaveExactElements("127.0.0.1:8484", "172.16.0.1:8484"),
"ListenPort": Equal(8484),
"IsLighthouse": BeTrue(),
"IsRelay": BeTrue(),
"Tags": HaveExactElements("tag:one", "tag:two"),
}),
})))
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})
})

var hostJSONResponse = `{
"data": {
"createdAt": "2024-10-18T08:37:30Z",
"id": "host-id",
"ipAddress": "10.0.0.1",
"isBlocked": false,
"isLighthouse": true,
"isRelay": true,
"listenPort": 8484,
"name": "host.smaily.testing",
"networkID": "network-id",
"organizationID": "org-id",
"roleID": "role-id",
"staticAddresses": [
"127.0.0.1:8484",
"172.16.0.1:8484"
],
"tags": [
"tag:one",
"tag:two"
],
"metadata": {
"lastSeenAt": "2023-01-25T18:15:27Z",
"platform": "dnclient",
"updateAvailable": false,
"version": "0.1.9"
}
},
"metadata": {}
}`
Loading