Skip to content

Commit

Permalink
provider: Simplify Defined.net HTTP API client's data model (#11)
Browse files Browse the repository at this point in the history
Replace response oriented data model with resource oriented data model.
  • Loading branch information
janartodesk authored Oct 22, 2024
1 parent f0a22b4 commit 4c924d3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 99 deletions.
26 changes: 12 additions & 14 deletions internal/definednet/enrollmentcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ import (
"net/http"
)

// EnrollmentCode is a data model for Defined.net host enrollment code.
type EnrollmentCode struct {
Code string `json:"code"`
LifetimeSeconds int `json:"lifetimeSeconds"`
}

// CreateEnrollmentCode creates a Defined.net host enrollment code.
func CreateEnrollmentCode(ctx context.Context, client Client, req CreateEnrollmentCodeRequest) (*CreateEnrollmentCodeResponse, error) {
var resp Response[CreateEnrollmentCodeResponse]
func CreateEnrollmentCode(ctx context.Context, client Client, req CreateEnrollmentCodeRequest) (*EnrollmentCode, error) {
var resp Response[EnrollmentCode]
if err := client.Do(ctx, http.MethodPost, []string{"v1", "hosts", req.ID, "enrollment-code"}, nil, &resp); err != nil {
return nil, err
}

return &resp.Data, nil
}

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

// CreateEnrollmentCodeResponse is a response data model for CreateEnrollmentCode endpoint.
CreateEnrollmentCodeResponse struct {
Code string `json:"code"`
LifetimeSeconds int `json:"lifetimeSeconds"`
}
)
// CreateEnrollmentCodeRequest is a request data model for CreateEnrollmentCode endpoint.
type CreateEnrollmentCodeRequest struct {
ID string
}
134 changes: 49 additions & 85 deletions internal/definednet/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,120 +5,84 @@ import (
"net/http"
)

// 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"`
}

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

return &resp.Data, 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 {
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"`
}
)
// CreateHostRequest is a request data model for CreateHost endpoint.
type 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"`
}

// 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
}
)
// DeleteHostRequest is a request data model for DeleteHost endpoint.
type DeleteHostRequest struct {
ID string
}

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

return &resp.Data, 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 {
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"`
}
)
// GetHostRequest is a request data model for GetHost endpoint.
type GetHostRequest struct {
ID string
}

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

return &resp.Data, 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 {
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"`
}
)
// 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"`
}

0 comments on commit 4c924d3

Please sign in to comment.