Skip to content

Commit

Permalink
Add configuration override capability to the Defined.net HTTP API client
Browse files Browse the repository at this point in the history
  • Loading branch information
janartodesk committed Nov 28, 2024
1 parent 4bafd60 commit e47eb5e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
17 changes: 9 additions & 8 deletions internal/definednet/enrollment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ func CreateEnrollment(ctx context.Context, client Client, req CreateEnrollmentRe

// CreateEnrollmentRequest is a request data model for CreateEnrollment endpoint.
type CreateEnrollmentRequest struct {
NetworkID string `json:"networkID"`
RoleID string `json:"roleID"`
Name string `json:"name"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
IsLighthouse bool `json:"isLighthouse"`
IsRelay bool `json:"isRelay"`
Tags []string `json:"tags"`
NetworkID string `json:"networkID"`
RoleID string `json:"roleID"`
Name string `json:"name"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
IsLighthouse bool `json:"isLighthouse"`
IsRelay bool `json:"isRelay"`
Tags []string `json:"tags"`
ConfigOverrides []ConfigOverride `json:"configOverrides"`
}
17 changes: 16 additions & 1 deletion internal/definednet/enrollment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var _ = Describe("creating host enrollments", func() {
"isLighthouse": true,
"isRelay": true,
"tags": []string{"tag:one", "tag:two"},
"configOverrides": []map[string]string{
{"key": "config.override", "value": "value"},
},
}),
ghttp.RespondWithJSONEncoded(http.StatusOK, map[string]any{}),
))
Expand All @@ -36,6 +39,9 @@ var _ = Describe("creating host enrollments", func() {
IsLighthouse: true,
IsRelay: true,
Tags: []string{"tag:one", "tag:two"},
ConfigOverrides: []definednet.ConfigOverride{
{Key: "config.override", Value: "value"},
},
})).Error().NotTo(HaveOccurred())

Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
Expand All @@ -57,6 +63,12 @@ var _ = Describe("creating host enrollments", func() {
"IsLighthouse": BeTrue(),
"IsRelay": BeTrue(),
"Tags": HaveExactElements("tag:one", "tag:two"),
"ConfigOverrides": HaveExactElements(
MatchAllFields(Fields{
"Key": Equal("config.override"),
"Value": Equal("value"),
}),
),
}),
"EnrollmentCode": MatchAllFields(Fields{
"Code": Equal("supersecret"),
Expand Down Expand Up @@ -95,7 +107,10 @@ var enrollmentJSONResponse = `{
"platform": "dnclient",
"updateAvailable": false,
"version": "0.1.9"
}
},
"configOverrides": [
{"key": "config.override", "value": "value"}
]
},
"enrollmentCode": {
"code": "supersecret",
Expand Down
34 changes: 18 additions & 16 deletions internal/definednet/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (

// Host is a data model for Defined.net host.
type Host 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"`
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"`
ConfigOverrides []ConfigOverride `json:"configOverrides"`
}

// DeleteHost deletes a Defined.net host.
Expand Down Expand Up @@ -56,10 +57,11 @@ func UpdateHost(ctx context.Context, client Client, req UpdateHostRequest) (*Hos

// UpdateHostRequest is a request data model for UpdateHost endpoint.
type 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"`
ID string `json:"-"`
RoleID string `json:"roleID"`
Name string `json:"name"`
StaticAddresses []string `json:"staticAddresses"`
ListenPort int `json:"listenPort"`
Tags []string `json:"tags"`
ConfigOverrides []ConfigOverride `json:"configOverrides"`
}
26 changes: 25 additions & 1 deletion internal/definednet/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ var _ = Describe("getting hosts", func() {
"IsLighthouse": BeTrue(),
"IsRelay": BeTrue(),
"Tags": HaveExactElements("tag:one", "tag:two"),
"ConfigOverrides": HaveExactElements(
MatchAllFields(Fields{
"Key": Equal("config.override"),
"Value": Equal("value"),
}),
),
})))
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})
Expand All @@ -55,6 +61,12 @@ var _ = Describe("updating hosts", func() {
"staticAddresses": []string{"127.0.0.1:8484", "172.16.0.1:8484"},
"listenPort": 8484,
"tags": []string{"tag:one", "tag:two"},
"configOverrides": []map[string]string{
{
"key": "config.override",
"value": "value",
},
},
}),
ghttp.RespondWithJSONEncoded(http.StatusOK, map[string]any{}),
))
Expand All @@ -66,6 +78,9 @@ var _ = Describe("updating hosts", func() {
StaticAddresses: []string{"127.0.0.1:8484", "172.16.0.1:8484"},
ListenPort: 8484,
Tags: []string{"tag:one", "tag:two"},
ConfigOverrides: []definednet.ConfigOverride{
{Key: "config.override", Value: "value"},
},
})).Error().NotTo(HaveOccurred())
Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
})
Expand All @@ -85,6 +100,12 @@ var _ = Describe("updating hosts", func() {
"IsLighthouse": BeTrue(),
"IsRelay": BeTrue(),
"Tags": HaveExactElements("tag:one", "tag:two"),
"ConfigOverrides": HaveExactElements(
MatchAllFields(Fields{
"Key": Equal("config.override"),
"Value": Equal("value"),
}),
),
})))

Expect(server.ReceivedRequests()).NotTo(BeEmpty(), "assert sanity")
Expand Down Expand Up @@ -117,7 +138,10 @@ var hostJSONResponse = `{
"platform": "dnclient",
"updateAvailable": false,
"version": "0.1.9"
}
},
"configOverrides": [
{"key": "config.override", "value": "value"}
]
},
"metadata": {}
}`

0 comments on commit e47eb5e

Please sign in to comment.