Skip to content

Commit

Permalink
feat: implement dedicated_server_operating_systems data source (#52)
Browse files Browse the repository at this point in the history
* feat: implement dedicated_server_operating_systems data source

---------

Co-authored-by: Imtiaz Ahmed <[email protected]>
  • Loading branch information
imtiazPabel and Imtiaz Ahmed authored Aug 28, 2024
1 parent 020cd06 commit 590475a
Show file tree
Hide file tree
Showing 30 changed files with 562 additions and 40 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* @alrayyes
* @majidkarimizadeh
* @imtiazPabel
* @arunvarghe
33 changes: 33 additions & 0 deletions docs/data-sources/dedicated_server_operating_systems.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_operating_systems Data Source - leaseweb"
subcategory: ""
description: |-
---

# leaseweb_dedicated_server_operating_systems (Data Source)



## Example Usage

```terraform
# List all Dedicated server operating systems
data "leaseweb_dedicated_server_operating_systems" "all" {}
```

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

### Read-Only

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

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

Read-Only:

- `id` (String)
- `name` (String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# List all Dedicated server operating systems
data "leaseweb_dedicated_server_operating_systems" "all" {}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,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-20240826091031-91d30abf3f1f
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240827104230-17ba71259431
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 @@ -84,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-20240823204242-4ba0660f739c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280 // 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
16 changes: 12 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,16 @@ 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-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/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/dedicatedServer v0.0.0-20240827104230-17ba71259431 h1:v89yJ0HM1PxfJjfBPBtVbL8OE+1ZUvaqTCUjsn68120=
github.com/leaseweb/leaseweb-go-sdk/dedicatedServer v0.0.0-20240827104230-17ba71259431/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/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 @@ -269,8 +277,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-20240823204242-4ba0660f739c h1:Kqjm4WpoWvwhMPcrAczoTyMySQmYa9Wy2iL6Con4zn8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280 h1:XQMA2e105XNlEZ8NRF0HqnUOZzP14sUSsgL09kpdNnU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280/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
10 changes: 10 additions & 0 deletions internal/core/domain/dedicated_server/operating_system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dedicated_server

type OperatingSystem struct {
Id string
Name string
}

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

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
got := NewOperatingSystem("id", "name")
want := OperatingSystem{Id: "id", Name: "name"}

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

type OperatingSystems []OperatingSystem
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,12 +9,18 @@ import (

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

// GetAllOperatingSystems retrieves dedicated_server.OperatingSystems from the dedicated_server api.
GetAllOperatingSystems(ctx context.Context) (
domain.OperatingSystems,
*shared.RepositoryError,
)

// GetAllControlPanels retrieves dedicated_server.ControlPanels from the dedicated_server api.
GetAllControlPanels(ctx context.Context) (
domain.ControlPanels,
Expand Down
3 changes: 3 additions & 0 deletions internal/core/ports/dedicated_server_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type DedicatedServerService interface {
// GetAllDedicatedServers gets dedicated_server.DedicatedServers.
GetAllDedicatedServers(ctx context.Context) (domain.DedicatedServers, *errors.ServiceError)

// GetAllOperatingSystems gets dedicated_server.OperatingSystems.
GetAllOperatingSystems(ctx context.Context) (domain.OperatingSystems, *errors.ServiceError)

// GetAllControlPanels gets dedicated_server.ControlPanels.
GetAllControlPanels(ctx context.Context) (domain.ControlPanels, *errors.ServiceError)
}
24 changes: 20 additions & 4 deletions internal/core/services/dedicated_server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type Service struct {
dedicatedServerRepository ports.DedicatedServerRepository
}

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

dedicatedServers, err := srv.dedicatedServerRepository.GetAllDedicatedServers(ctx)
dedicatedServers, err := s.dedicatedServerRepository.GetAllDedicatedServers(ctx)
if err != nil {
return nil, errors.NewFromRepositoryError(
"GetAllDedicatedServers",
Expand All @@ -29,12 +29,28 @@ func (srv Service) GetAllDedicatedServers(ctx context.Context) (
return dedicatedServers, nil
}

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

operatingSystems, err := s.dedicatedServerRepository.GetAllOperatingSystems(ctx)
if err != nil {
return nil, errors.NewFromRepositoryError(
"GetAllOperatingSystems",
*err,
)
}

return operatingSystems, nil
}

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

controlPanels, err := srv.dedicatedServerRepository.GetAllControlPanels(ctx)
controlPanels, err := s.dedicatedServerRepository.GetAllControlPanels(ctx)
if err != nil {
return nil, errors.NewFromRepositoryError(
"GetAllControlPanels",
Expand Down
44 changes: 42 additions & 2 deletions internal/core/services/dedicated_server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ var (

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

getAllDedicatedServerError *sharedRepository.RepositoryError
getAllControlPanelError *sharedRepository.RepositoryError
getAllDedicatedServerError *sharedRepository.RepositoryError
getAllOperatingSystemsError *sharedRepository.RepositoryError
getAllControlPanelError *sharedRepository.RepositoryError
}

func (r *repositorySpy) GetAllDedicatedServers(ctx context.Context) (
Expand All @@ -30,6 +32,13 @@ func (r *repositorySpy) GetAllDedicatedServers(ctx context.Context) (
return r.dedicatedServers, r.getAllDedicatedServerError
}

func (r *repositorySpy) GetAllOperatingSystems(ctx context.Context) (
domain.OperatingSystems,
*sharedRepository.RepositoryError,
) {
return r.operatingSystems, r.getAllOperatingSystemsError
}

func (r *repositorySpy) GetAllControlPanels(ctx context.Context) (
domain.ControlPanels,
*sharedRepository.RepositoryError,
Expand Down Expand Up @@ -104,6 +113,37 @@ func TestService_GetAllDedicatedServers(t *testing.T) {
)
}

func TestService_GetAllOperatingSystems(t *testing.T) {
t.Run(
"service passes back dedicated server from repository",
func(t *testing.T) {

want := domain.OperatingSystems{{Id: "123456"}}

service := New(&repositorySpy{operatingSystems: want})
got, err := service.GetAllOperatingSystems(context.TODO())

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

t.Run(
"error from repository getAllOperatingSystems bubbles up",
func(t *testing.T) {

want := "some error"
generalError := sharedRepository.NewGeneralError("", errors.New(want))

service := New(&repositorySpy{getAllOperatingSystemsError: generalError})
_, err := service.GetAllOperatingSystems(context.TODO())

assert.Error(t, err)
assert.ErrorContains(t, err, want)
},
)
}

func TestService_GetAllControlPanels(t *testing.T) {
t.Run(
"service passes back control panels from repository",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/data_sources/dedicated_server/model"
)

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

Expand Down Expand Up @@ -181,6 +181,22 @@ func adaptPciCard(pciCard domain.PciCard) model.PciCard {
}
}

// AdaptOperatingSystems adapts domain.OperatingSystems to model.OperatingSystems.
func AdaptOperatingSystems(domainOs domain.OperatingSystems) (modelOs model.OperatingSystems) {
for _, os := range domainOs {
modelOs.OperatingSystems = append(modelOs.OperatingSystems, adaptOperatingSystem(os))
}

return
}

func adaptOperatingSystem(domainOs domain.OperatingSystem) model.OperatingSystem {
return model.OperatingSystem{
Id: basetypes.NewStringValue(domainOs.Id),
Name: basetypes.NewStringValue(domainOs.Name),
}
}

// AdaptControlPanels adapts model.ControlPanels to dedicated_server.ControlPanels.
func AdaptControlPanels(domainControlPanels domain.ControlPanels) model.ControlPanels {
var controlPanels model.ControlPanels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,23 @@ func TestAdaptDedicatedServers(t *testing.T) {
assert.Equal(t, "id2", got.DedicatedServers[1].Id.ValueString())
}

func TestAdaptOperatingSystems(t *testing.T) {

got := AdaptOperatingSystems(domain.OperatingSystems{{Id: "id1"}, {Id: "id2"}})

assert.Len(t, got.OperatingSystems, 2)
assert.Equal(t, "id1", got.OperatingSystems[0].Id.ValueString())
assert.Equal(t, "id2", got.OperatingSystems[1].Id.ValueString())
}

func Test_adaptOperatingSystem(t *testing.T) {

got := adaptOperatingSystem(domain.NewOperatingSystem("id", "name"))

assert.Equal(t, "id", got.Id.ValueString())
assert.Equal(t, "name", got.Name.ValueString())
}

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

Expand Down
30 changes: 23 additions & 7 deletions internal/facades/dedicated_server/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/leaseweb/terraform-provider-leaseweb/internal/core/ports"
"github.com/leaseweb/terraform-provider-leaseweb/internal/facades/dedicated_server/data_adapters/to_data_source_model"
"github.com/leaseweb/terraform-provider-leaseweb/internal/facades/shared"
dataSourceModel "github.com/leaseweb/terraform-provider-leaseweb/internal/provider/data_sources/dedicated_server/model"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/data_sources/dedicated_server/model"
)

// DedicatedServerFacade handles all communication between provider & the core.
Expand All @@ -15,11 +15,11 @@ type DedicatedServerFacade struct {
}

// GetAllDedicatedServers retrieves model.DedicatedServers.
func (d DedicatedServerFacade) GetAllDedicatedServers(ctx context.Context) (
*dataSourceModel.DedicatedServers,
func (f DedicatedServerFacade) GetAllDedicatedServers(ctx context.Context) (
*model.DedicatedServers,
*shared.FacadeError,
) {
dedicatedServers, err := d.dedicatedServerService.GetAllDedicatedServers(ctx)
dedicatedServers, err := f.dedicatedServerService.GetAllDedicatedServers(ctx)
if err != nil {
return nil, shared.NewFromServicesError("GetAllDedicatedServers", err)
}
Expand All @@ -29,12 +29,28 @@ func (d DedicatedServerFacade) GetAllDedicatedServers(ctx context.Context) (
return &dataSourceDedicatedServers, nil
}

// GetAllOperatingSystems retrieve model.OperatingSystems.
func (f DedicatedServerFacade) GetAllOperatingSystems(ctx context.Context) (
*model.OperatingSystems,
*shared.FacadeError,
) {
operatingSystems, err := f.dedicatedServerService.GetAllOperatingSystems(ctx)

if err != nil {
return nil, shared.NewFromServicesError("GetAllOperatingSystems", err)
}

dataSourceOperatingSystems := to_data_source_model.AdaptOperatingSystems(operatingSystems)

return &dataSourceOperatingSystems, nil
}

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

0 comments on commit 590475a

Please sign in to comment.