Skip to content

Commit

Permalink
Merge pull request #189 from alrayyes/refactor/implement-global-metad…
Browse files Browse the repository at this point in the history
…ata-and-configure

refactor: implement helper to abstract Configure & Metadata provider …
  • Loading branch information
alrayyes authored Dec 19, 2024
2 parents 266862c + 3477368 commit ace2f1b
Show file tree
Hide file tree
Showing 30 changed files with 470 additions and 906 deletions.
10 changes: 10 additions & 0 deletions internal/provider/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client

import (
"github.com/leaseweb/leaseweb-go-sdk/v3/dedicatedserver"
"github.com/leaseweb/leaseweb-go-sdk/v3/dns"
"github.com/leaseweb/leaseweb-go-sdk/v3/publiccloud"
)

Expand All @@ -12,6 +13,7 @@ const userAgentBase = "leaseweb-terraform"
type Client struct {
PubliccloudAPI publiccloud.PubliccloudAPI
DedicatedserverAPI dedicatedserver.DedicatedserverAPI
DNSAPI dns.DnsAPI
}

type Optional struct {
Expand All @@ -22,14 +24,17 @@ type Optional struct {
func NewClient(token string, optional Optional, version string) Client {
publiccloudCFG := publiccloud.NewConfiguration()
dedicatedserverCFG := dedicatedserver.NewConfiguration()
dnsCFG := dns.NewConfiguration()

if optional.Host != nil {
publiccloudCFG.Host = *optional.Host
dedicatedserverCFG.Host = *optional.Host
dnsCFG.Host = *optional.Host
}
if optional.Scheme != nil {
publiccloudCFG.Scheme = *optional.Scheme
dedicatedserverCFG.Scheme = *optional.Scheme
dnsCFG.Scheme = *optional.Scheme
}

userAgent := userAgentBase + "-" + version
Expand All @@ -40,11 +45,16 @@ func NewClient(token string, optional Optional, version string) Client {
dedicatedserverCFG.AddDefaultHeader("X-LSW-Auth", token)
dedicatedserverCFG.UserAgent = userAgent

dnsCFG.AddDefaultHeader("X-LSW-Auth", token)
dnsCFG.UserAgent = userAgent

publiccloudAPI := publiccloud.NewAPIClient(publiccloudCFG)
dedicatedserverAPI := dedicatedserver.NewAPIClient(dedicatedserverCFG)
dnsAPI := dns.NewAPIClient(dnsCFG)

return Client{
PubliccloudAPI: publiccloudAPI.PubliccloudAPI,
DedicatedserverAPI: dedicatedserverAPI.DedicatedserverAPI,
DNSAPI: dnsAPI.DnsAPI,
}
}
40 changes: 6 additions & 34 deletions internal/provider/dedicatedserver/contract_panels_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package dedicatedserver

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/leaseweb/leaseweb-go-sdk/v3/dedicatedserver"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/client"
"github.com/leaseweb/terraform-provider-leaseweb/internal/utils"
)

Expand All @@ -20,8 +18,7 @@ var (
)

type controlPanelsDataSource struct {
name string
client dedicatedserver.DedicatedserverAPI
utils.DataSourceAPI
}

type controlPanelDataSourceModel struct {
Expand All @@ -34,33 +31,6 @@ type controlPanelsDataSourceModel struct {
OperatingSystemId types.String `tfsdk:"operating_system_id"`
}

func (c *controlPanelsDataSource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}

coreClient, ok := req.ProviderData.(client.Client)

if !ok {
utils.ConfigError(&resp.Diagnostics, req.ProviderData)
return
}

c.client = coreClient.DedicatedserverAPI
}

func (c *controlPanelsDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, c.name)
}

func (c *controlPanelsDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
Expand All @@ -76,13 +46,13 @@ func (c *controlPanelsDataSource) Read(

// NOTE: we show only the latest 50 items.
if !config.OperatingSystemId.IsNull() && !config.OperatingSystemId.IsUnknown() {
request := c.client.GetControlPanelListByOperatingSystemId(
request := c.DedicatedserverAPI.GetControlPanelListByOperatingSystemId(
ctx,
config.OperatingSystemId.ValueString(),
).Limit(50)
result, response, err = request.Execute()
} else {
request := c.client.GetControlPanelList(ctx).Limit(50)
request := c.DedicatedserverAPI.GetControlPanelList(ctx).Limit(50)
result, response, err = request.Execute()
}

Expand Down Expand Up @@ -141,6 +111,8 @@ func (c *controlPanelsDataSource) Schema(

func NewControlPanelsDataSource() datasource.DataSource {
return &controlPanelsDataSource{
name: "dedicated_server_control_panels",
DataSourceAPI: utils.DataSourceAPI{
Name: "dedicated_server_control_panels",
},
}
}
38 changes: 5 additions & 33 deletions internal/provider/dedicatedserver/credential_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package dedicatedserver

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/leaseweb/leaseweb-go-sdk/v3/dedicatedserver"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/client"
"github.com/leaseweb/terraform-provider-leaseweb/internal/utils"
)

Expand All @@ -20,8 +18,7 @@ var (
)

type credentialDataSource struct {
name string
client dedicatedserver.DedicatedserverAPI
utils.DataSourceAPI
}

type credentialDataSourceModel struct {
Expand All @@ -31,33 +28,6 @@ type credentialDataSourceModel struct {
Type types.String `tfsdk:"type"`
}

func (c *credentialDataSource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}

coreClient, ok := req.ProviderData.(client.Client)

if !ok {
utils.ConfigError(&resp.Diagnostics, req.ProviderData)
return
}

c.client = coreClient.DedicatedserverAPI
}

func (c *credentialDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, c.name)
}

func (c *credentialDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
Expand Down Expand Up @@ -105,7 +75,7 @@ func (c *credentialDataSource) Read(
credType := dedicatedserver.CredentialType(config.Type.ValueString())
username := config.Username.ValueString()

credential, response, err := c.client.GetServerCredential(
credential, response, err := c.DedicatedserverAPI.GetServerCredential(
ctx,
serverID,
credType,
Expand All @@ -123,6 +93,8 @@ func (c *credentialDataSource) Read(

func NewCredentialDataSource() datasource.DataSource {
return &credentialDataSource{
name: "dedicated_server_credential",
DataSourceAPI: utils.DataSourceAPI{
Name: "dedicated_server_credential",
},
}
}
44 changes: 8 additions & 36 deletions internal/provider/dedicatedserver/credential_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dedicatedserver

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/leaseweb/leaseweb-go-sdk/v3/dedicatedserver"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/client"
"github.com/leaseweb/terraform-provider-leaseweb/internal/utils"
)

Expand All @@ -22,8 +20,7 @@ var (
)

type credentialResource struct {
name string
client dedicatedserver.DedicatedserverAPI
utils.ResourceAPI
}

type credentialResourceModel struct {
Expand All @@ -35,35 +32,10 @@ type credentialResourceModel struct {

func NewCredentialResource() resource.Resource {
return &credentialResource{
name: "dedicated_server_credential",
}
}

func (c *credentialResource) Metadata(
_ context.Context,
req resource.MetadataRequest,
resp *resource.MetadataResponse,
) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, c.name)
}

func (c *credentialResource) Configure(
_ context.Context,
req resource.ConfigureRequest,
resp *resource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}

coreClient, ok := req.ProviderData.(client.Client)

if !ok {
utils.ConfigError(&resp.Diagnostics, req.ProviderData)
return
ResourceAPI: utils.ResourceAPI{
Name: "dedicated_server_credential",
},
}

c.client = coreClient.DedicatedserverAPI
}

func (c *credentialResource) Schema(
Expand Down Expand Up @@ -121,7 +93,7 @@ func (c *credentialResource) Create(
dedicatedserver.CredentialType(plan.Type.ValueString()),
plan.Username.ValueString(),
)
request := c.client.CreateServerCredential(
request := c.DedicatedserverAPI.CreateServerCredential(
ctx,
plan.DedicatedServerId.ValueString(),
).CreateServerCredentialOpts(*opts)
Expand Down Expand Up @@ -155,7 +127,7 @@ func (c *credentialResource) Read(
return
}

request := c.client.GetServerCredential(
request := c.DedicatedserverAPI.GetServerCredential(
ctx,
state.DedicatedServerId.ValueString(),
dedicatedserver.CredentialType(state.Type.ValueString()),
Expand Down Expand Up @@ -194,7 +166,7 @@ func (c *credentialResource) Update(
opts := dedicatedserver.NewUpdateServerCredentialOpts(
plan.Password.ValueString(),
)
request := c.client.UpdateServerCredential(
request := c.DedicatedserverAPI.UpdateServerCredential(
ctx,
plan.DedicatedServerId.ValueString(),
dedicatedserver.CredentialType(plan.Type.ValueString()),
Expand Down Expand Up @@ -230,7 +202,7 @@ func (c *credentialResource) Delete(
return
}

request := c.client.DeleteServerCredential(
request := c.DedicatedserverAPI.DeleteServerCredential(
ctx,
state.DedicatedServerId.ValueString(),
dedicatedserver.CredentialType(state.Type.ValueString()),
Expand Down
Loading

0 comments on commit ace2f1b

Please sign in to comment.