From 843f69e5754e1491858c8aac8011fbff620b85ff Mon Sep 17 00:00:00 2001 From: Janar Todesk Date: Tue, 22 Oct 2024 10:54:23 +0300 Subject: [PATCH] provider: Simplify Defined.net HTTP API client's data model Replace response oriented data model with resource oriented data model. --- internal/definednet/enrollmentcode.go | 26 +++-- internal/definednet/host.go | 134 ++++++++++---------------- 2 files changed, 61 insertions(+), 99 deletions(-) diff --git a/internal/definednet/enrollmentcode.go b/internal/definednet/enrollmentcode.go index a94cc0d..1268d25 100644 --- a/internal/definednet/enrollmentcode.go +++ b/internal/definednet/enrollmentcode.go @@ -5,9 +5,15 @@ 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 } @@ -15,15 +21,7 @@ func CreateEnrollmentCode(ctx context.Context, client Client, req CreateEnrollme 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 +} diff --git a/internal/definednet/host.go b/internal/definednet/host.go index 0b72c29..264d239 100644 --- a/internal/definednet/host.go +++ b/internal/definednet/host.go @@ -5,9 +5,23 @@ 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 } @@ -15,50 +29,32 @@ func CreateHost(ctx context.Context, client Client, req CreateHostRequest) (*Cre 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 } @@ -66,30 +62,14 @@ func GetHost(ctx context.Context, client Client, req GetHostRequest) (*GetHostRe 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 } @@ -97,28 +77,12 @@ func UpdateHost(ctx context.Context, client Client, req UpdateHostRequest) (*Upd 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"` +}