Skip to content

Commit

Permalink
Merge pull request #45 from Leaseweb/datasource-control-panels
Browse files Browse the repository at this point in the history
feat: implement dedicated_server control panels datasource
  • Loading branch information
arunvarghe authored Aug 28, 2024
2 parents 9ccee8e + 4780d0b commit 020cd06
Show file tree
Hide file tree
Showing 32 changed files with 592 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* @alrayyes
* @majidkarimizadeh
* @arunvarghe
2 changes: 1 addition & 1 deletion Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ route /publicCloud/v1/* {
route /bareMetals/v2/* {
uri strip_prefix /bareMetals/v2
reverse_proxy prism_dedicatedServer:4010
}
}
33 changes: 33 additions & 0 deletions docs/data-sources/dedicated_server_control_panels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "leaseweb_dedicated_server_control_panels Data Source - leaseweb"
subcategory: ""
description: |-
---

# leaseweb_dedicated_server_control_panels (Data Source)



## Example Usage

```terraform
# List all Control panels
data "leaseweb_dedicated_server_control_panels" "all" {}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `control_panels` (Attributes List) (see [below for nested schema](#nestedatt--control_panels))

<a id="nestedatt--control_panels"></a>
### Nested Schema for `control_panels`

Read-Only:

- `id` (String) The control panel unique identifier
- `name` (String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# List all Control panels
data "leaseweb_dedicated_server_control_panels" "all" {}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/leaseweb/terraform-provider-leaseweb
go 1.22.3

require (
github.com/google/uuid v1.6.0
github.com/hashicorp/terraform-plugin-docs v0.19.4
github.com/hashicorp/terraform-plugin-framework v1.11.0
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
Expand All @@ -14,6 +13,8 @@ require (
github.com/stretchr/testify v1.9.0
)

require github.com/google/uuid v1.6.0 // indirect

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/Kunde21/markdownfmt/v3 v3.1.0 // indirect
Expand Down Expand Up @@ -53,7 +54,7 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240819091634-010d471a4889
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240826091031-91d30abf3f1f
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
Expand Down Expand Up @@ -83,7 +84,7 @@ require (
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240808171019-573a1156607a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
14 changes: 4 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240809092542-30d3ea354379 h1:b0JiE/yeggAc1PzdV76RMPxC2jhmFpaUVCZy7vONa/8=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240809092542-30d3ea354379/go.mod h1:MSrebSkGmRy0OzTJd+lDXsILYms/13QO7GfNNK/5Mds=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240813104438-58bc818bfeb5 h1:hqcgNwcbAN1fxauIVgLQxVCkCQT6d6LjwCDMcFHN0Z4=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240813104438-58bc818bfeb5/go.mod h1:MSrebSkGmRy0OzTJd+lDXsILYms/13QO7GfNNK/5Mds=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240819091634-010d471a4889 h1:DYHZlxgvoupZNH07z6gGYjDke/hz2q5D2AQ3CEFyE9Y=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240819091634-010d471a4889/go.mod h1:MSrebSkGmRy0OzTJd+lDXsILYms/13QO7GfNNK/5Mds=
github.com/leaseweb/leaseweb-go-sdk/publicCloud v0.0.0-20240809071344-9c6e5ec7d2db h1:eYzMYi6x0gAzxHhDTb0GPEJlXOn3GSy39vUYg9pVtKo=
github.com/leaseweb/leaseweb-go-sdk/publicCloud v0.0.0-20240809071344-9c6e5ec7d2db/go.mod h1:nh4tvDxdD+xNL9qjFqtL1ZL49w+HJq5kMeTKkPs2XHI=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240826091031-91d30abf3f1f h1:2ru2hpXfqQQO3076j8LaaZi4wlPatNxlZfDKgMshCx8=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240826091031-91d30abf3f1f/go.mod h1:MSrebSkGmRy0OzTJd+lDXsILYms/13QO7GfNNK/5Mds=
github.com/leaseweb/leaseweb-go-sdk/publicCloud v0.0.0-20240809092542-30d3ea354379 h1:27h6xQ647JgakAvg+OKYxKS/Ha5FDDWMIT8tyxaKzaU=
github.com/leaseweb/leaseweb-go-sdk/publicCloud v0.0.0-20240809092542-30d3ea354379/go.mod h1:nh4tvDxdD+xNL9qjFqtL1ZL49w+HJq5kMeTKkPs2XHI=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down Expand Up @@ -275,8 +269,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240808171019-573a1156607a h1:EKiZZXueP9/T68B8Nl0GAx9cjbQnCId0yP3qPMgaaHs=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240808171019-573a1156607a/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c h1:Kqjm4WpoWvwhMPcrAczoTyMySQmYa9Wy2iL6Con4zn8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down
17 changes: 17 additions & 0 deletions internal/core/domain/dedicated_server/control_panel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dedicated_server

type ControlPanel struct {
Id string
Name string
}

// NewControlPanel creates ControlPanel.
func NewControlPanel(
id string,
name string,
) ControlPanel {
return ControlPanel{
Id: id,
Name: name,
}
}
13 changes: 13 additions & 0 deletions internal/core/domain/dedicated_server/control_panel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dedicated_server

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewControlPanel(t *testing.T) {
got := NewControlPanel("id", "name")
want := ControlPanel{Id: "id", Name: "name"}

assert.Equal(t, want, got)
}
3 changes: 3 additions & 0 deletions internal/core/domain/dedicated_server/control_panels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dedicated_server

type ControlPanels []ControlPanel
8 changes: 7 additions & 1 deletion internal/core/ports/dedicated_server_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import (

// DedicatedServerRepository is used to connect to dedicated_server api.
type DedicatedServerRepository interface {
// GetAllDedicatedServers Retrieve all dedicated_servers from the dedicated server api.
// GetAllDedicatedServers retrieves dedicated_server.DedicatedServers from the dedicated server api.
GetAllDedicatedServers(ctx context.Context) (
domain.DedicatedServers,
*shared.RepositoryError,
)

// GetAllControlPanels retrieves dedicated_server.ControlPanels from the dedicated_server api.
GetAllControlPanels(ctx context.Context) (
domain.ControlPanels,
*shared.RepositoryError,
)
}
7 changes: 5 additions & 2 deletions internal/core/ports/dedicated_server_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

// DedicatedServerService gets data associated with dedicated_server.
type DedicatedServerService interface {
// GetAllDedicatedServers gets all dedicated servers.
GetAllDedicatedServers(ctx context.Context) (*domain.DedicatedServers, *errors.ServiceError)
// GetAllDedicatedServers gets dedicated_server.DedicatedServers.
GetAllDedicatedServers(ctx context.Context) (domain.DedicatedServers, *errors.ServiceError)

// GetAllControlPanels gets dedicated_server.ControlPanels.
GetAllControlPanels(ctx context.Context) (domain.ControlPanels, *errors.ServiceError)
}
20 changes: 18 additions & 2 deletions internal/core/services/dedicated_server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Service struct {
}

func (srv Service) GetAllDedicatedServers(ctx context.Context) (
*domain.DedicatedServers,
domain.DedicatedServers,
*errors.ServiceError,
) {

Expand All @@ -26,7 +26,23 @@ func (srv Service) GetAllDedicatedServers(ctx context.Context) (
)
}

return &dedicatedServers, nil
return dedicatedServers, nil
}

func (srv Service) GetAllControlPanels(ctx context.Context) (
domain.ControlPanels,
*errors.ServiceError,
) {

controlPanels, err := srv.dedicatedServerRepository.GetAllControlPanels(ctx)
if err != nil {
return nil, errors.NewFromRepositoryError(
"GetAllControlPanels",
*err,
)
}

return controlPanels, nil
}

func New(dedicatedServerRepository ports.DedicatedServerRepository) Service {
Expand Down
54 changes: 49 additions & 5 deletions internal/core/services/dedicated_server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ var (

type repositorySpy struct {
dedicatedServers domain.DedicatedServers
controlPanels domain.ControlPanels

getAllDedicatedServerError *sharedRepository.RepositoryError
getAllControlPanelError *sharedRepository.RepositoryError
}

func (r *repositorySpy) GetAllDedicatedServers(ctx context.Context) (
Expand All @@ -28,8 +30,11 @@ func (r *repositorySpy) GetAllDedicatedServers(ctx context.Context) (
return r.dedicatedServers, r.getAllDedicatedServerError
}

func newRepositorySpy() repositorySpy {
return repositorySpy{}
func (r *repositorySpy) GetAllControlPanels(ctx context.Context) (
domain.ControlPanels,
*sharedRepository.RepositoryError,
) {
return r.controlPanels, r.getAllControlPanelError
}

func TestService_GetAllDedicatedServers(t *testing.T) {
Expand All @@ -45,15 +50,14 @@ func TestService_GetAllDedicatedServers(t *testing.T) {
},
}

spy := newRepositorySpy()
spy.dedicatedServers = want
spy := repositorySpy{dedicatedServers: want}

service := New(&spy)

got, err := service.GetAllDedicatedServers(context.TODO())

assert.Nil(t, err)
assert.Equal(t, want, *got)
assert.Equal(t, want, got)
},
)

Expand Down Expand Up @@ -99,3 +103,43 @@ func TestService_GetAllDedicatedServers(t *testing.T) {
},
)
}

func TestService_GetAllControlPanels(t *testing.T) {
t.Run(
"service passes back control panels from repository",
func(t *testing.T) {

want := domain.ControlPanels{
domain.ControlPanel{
Id: "id",
Name: "name",
},
}
spy := repositorySpy{controlPanels: want}
service := New(&spy)
got, err := service.GetAllControlPanels(context.TODO())

assert.Equal(t, want, got)
assert.Nil(t, err)
},
)

t.Run(
"error from repository getAllControlPanels bubbles up",
func(t *testing.T) {
service := New(
&repositorySpy{
getAllControlPanelError: sharedRepository.NewGeneralError(
"",
errors.New("some error"),
),
},
)

_, err := service.GetAllControlPanels(context.TODO())

assert.Error(t, err)
assert.ErrorContains(t, err, "some error")
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/data_sources/dedicated_server/model"
)

// AdaptDedicatedServers adapts model.DedicatedServers to dedicated_server.DedicatedServers.
func AdaptDedicatedServers(domainDedicatedServers domain.DedicatedServers) model.DedicatedServers {
var dedicatedServers model.DedicatedServers

Expand Down Expand Up @@ -179,3 +180,21 @@ func adaptPciCard(pciCard domain.PciCard) model.PciCard {
Description: basetypes.NewStringValue(pciCard.String()),
}
}

// AdaptControlPanels adapts model.ControlPanels to dedicated_server.ControlPanels.
func AdaptControlPanels(domainControlPanels domain.ControlPanels) model.ControlPanels {
var controlPanels model.ControlPanels

for _, domainControlPanel := range domainControlPanels {
controlPanels.ControlPanels = append(controlPanels.ControlPanels, adaptControlPanel(domainControlPanel))
}

return controlPanels
}

func adaptControlPanel(domainControlPanel domain.ControlPanel) model.ControlPanel {
return model.ControlPanel{
Id: basetypes.NewStringValue(domainControlPanel.Id),
Name: basetypes.NewStringValue(domainControlPanel.Name),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,17 @@ func TestAdaptDedicatedServers(t *testing.T) {
assert.Equal(t, "id1", got.DedicatedServers[0].Id.ValueString())
assert.Equal(t, "id2", got.DedicatedServers[1].Id.ValueString())
}

func TestAdaptControlPanels(t *testing.T) {
got := AdaptControlPanels(domain.ControlPanels{domain.ControlPanel{Id: "id"}})

assert.Len(t, got.ControlPanels, 1)
assert.Equal(t, "id", got.ControlPanels[0].Id.ValueString())
}

func Test_adaptControlPanel(t *testing.T) {
got := adaptControlPanel(domain.NewControlPanel("id", "name"))

assert.Equal(t, "id", got.Id.ValueString())
assert.Equal(t, "name", got.Name.ValueString())
}
25 changes: 20 additions & 5 deletions internal/facades/dedicated_server/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,37 @@ type DedicatedServerFacade struct {
dedicatedServerService ports.DedicatedServerService
}

// GetAllDedicatedServers retrieve all dedicated servers.
func (h DedicatedServerFacade) GetAllDedicatedServers(ctx context.Context) (
// GetAllDedicatedServers retrieves model.DedicatedServers.
func (d DedicatedServerFacade) GetAllDedicatedServers(ctx context.Context) (
*dataSourceModel.DedicatedServers,
*shared.FacadeError,
) {
dedicatedServers, err := h.dedicatedServerService.GetAllDedicatedServers(ctx)
dedicatedServers, err := d.dedicatedServerService.GetAllDedicatedServers(ctx)
if err != nil {
return nil, shared.NewFromServicesError("GetAllDedicatedServers", err)
}

dataSourceDedicatedServers := to_data_source_model.AdaptDedicatedServers(*dedicatedServers)
dataSourceDedicatedServers := to_data_source_model.AdaptDedicatedServers(dedicatedServers)

return &dataSourceDedicatedServers, nil
}

func NewDedicatedServerFacade(dedicatedServerService ports.DedicatedServerService) DedicatedServerFacade {
// GetAllControlPanels retrieves model.ControlPanels.
func (d DedicatedServerFacade) GetAllControlPanels(ctx context.Context) (
*dataSourceModel.ControlPanels,
*shared.FacadeError,
) {
controlPanels, err := d.dedicatedServerService.GetAllControlPanels(ctx)
if err != nil {
return nil, shared.NewFromServicesError("GetAllControlPanels", err)
}

dataSourceControlPanels := to_data_source_model.AdaptControlPanels(controlPanels)

return &dataSourceControlPanels, nil
}

func New(dedicatedServerService ports.DedicatedServerService) DedicatedServerFacade {
return DedicatedServerFacade{
dedicatedServerService: dedicatedServerService,
}
Expand Down
Loading

0 comments on commit 020cd06

Please sign in to comment.