Skip to content

Commit

Permalink
Merge pull request #229 from alrayyes/feat/dns-data-source
Browse files Browse the repository at this point in the history
feat: implement dns resource record sets data source
  • Loading branch information
alrayyes authored Dec 24, 2024
2 parents 575dce2 + 9214c36 commit 60f4a71
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PUBLIC_CLOUD_API_SPEC_URL=https://raw.githubusercontent.com/Leaseweb/leaseweb-go-sdk/main/publiccloud/api/openapi.yaml
DEDICATED_SERVER_API_SPEC_URL=https://raw.githubusercontent.com/Leaseweb/leaseweb-go-sdk/main/dedicatedserver/api/openapi.yaml
DNS_API_SPEC_URL=https://raw.githubusercontent.com/Leaseweb/leaseweb-go-sdk/main/dns/api/openapi.yaml
#PUBLIC_CLOUD_API_SPEC_URL=http://host.docker.internal:8081/publicCloud.json
#DEDICATED_SERVER_API_SPEC_URL=http://host.docker.internal:8081/dedicatedServer.json
#DNS_SERVER_API_SPEC_URL=http://host.docker.internal:8081/dns.json
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ services:
- path: .env
extra_hosts:
- "host.docker.internal:host-gateway"
prism_dns:
container_name: prism_dns
build:
dockerfile: docker/prism/Dockerfile
command: >
mock -p 4010 --host 0.0.0.0
${DNS_API_SPEC_URL}
env_file:
- path: .env
extra_hosts:
- "host.docker.internal:host-gateway"
prism-proxy:
build:
dockerfile: docker/caddy/Dockerfile
Expand All @@ -64,6 +75,7 @@ services:
depends_on:
- prism_public_cloud
- prism_dedicated_server
- prism_dns

volumes:
golang:
5 changes: 5 additions & 0 deletions docker/caddy/prism/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ route /bareMetals/v2/* {
reverse_proxy prism_dedicated_server:4010
}

route /hosting/v2/* {
uri strip_prefix /hosting/v2
reverse_proxy prism_dns:4010
}

log {
format console
}
11 changes: 11 additions & 0 deletions docker/docker-compose-acc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ services:
- path: .env
extra_hosts:
- "host.docker.internal:host-gateway"
prism_dns:
container_name: prism_dns
build:
dockerfile: prism/Dockerfile
command: >
mock -p 4010 --host 0.0.0.0
${DNS_API_SPEC_URL}
env_file:
- path: .env
extra_hosts:
- "host.docker.internal:host-gateway"
prism-proxy:
build:
dockerfile: caddy/Dockerfile
Expand Down
42 changes: 42 additions & 0 deletions docs/data-sources/dns_resource_record_sets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "leaseweb_dns_resource_record_sets Data Source - leaseweb"
subcategory: ""
description: |-
List resource record sets
---

# leaseweb_dns_resource_record_sets (Data Source)

List resource record sets

## Example Usage

```terraform
# List DNS resource record sets for example.com
data "leaseweb_dns_resource_record_sets" "all" {
domain_name = "example.com"
}
```

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

### Required

- `domain_name` (String) Domain Name

### Read-Only

- `info_message` (String) Optional additional information
- `resource_record_sets` (Attributes List) Array of resource record sets (see [below for nested schema](#nestedatt--resource_record_sets))

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

Read-Only:

- `content` (List of String) Array of resource record set Content entries
- `name` (String) Name of the resource record set
- `ttl` (Number) Time to live of the resource record set
- `type` (String) Type of the resource record set
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# List DNS resource record sets for example.com
data "leaseweb_dns_resource_record_sets" "all" {
domain_name = "example.com"
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ require (
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
golang.org/x/crypto v0.30.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.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-20241216192217-9240e9c98484 // indirect
google.golang.org/grpc v1.69.0 // indirect
google.golang.org/grpc v1.69.2 // indirect
google.golang.org/protobuf v1.36.0 // indirect
gopkg.in/validator.v2 v2.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,17 @@ go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HY
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -222,8 +222,8 @@ google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAs
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 h1:Z7FRVJPSMaHQxD0uXU8WdgFh8PseLM8Q8NzhnpMrBhQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
google.golang.org/grpc v1.69.0 h1:quSiOM1GJPmPH5XtU+BCoVXcDVJJAzNcoyfC2cCjGkI=
google.golang.org/grpc v1.69.0/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/dns/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package dns implements functionality for dns data sources & resources.
package dns
132 changes: 132 additions & 0 deletions internal/provider/dns/resource_record_sets_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package dns

import (
"context"

"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/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/leaseweb/terraform-provider-leaseweb/internal/utils"
)

var (
_ datasource.DataSourceWithConfigure = &resourceRecordSet{}
)

type resourceRecordSetsDataSourceModel struct {
DomainName types.String `tfsdk:"domain_name"`
InfoMessage types.String `tfsdk:"info_message"`
ResourceRecordSets []resourceRecordSetDataSourceModel `tfsdk:"resource_record_sets"`
}

type resourceRecordSetDataSourceModel struct {
Name types.String `tfsdk:"name"`
RecordType types.String `tfsdk:"type"`
Content []string `tfsdk:"content"`
TTL types.Int32 `tfsdk:"ttl"`
}

type resourceRecordSet struct {
utils.DataSourceAPI
}

func (r *resourceRecordSet) Schema(
_ context.Context,
_ datasource.SchemaRequest,
response *datasource.SchemaResponse,
) {
response.Schema = schema.Schema{
Description: "List resource record sets",
Attributes: map[string]schema.Attribute{
"domain_name": schema.StringAttribute{
Required: true,
Description: "Domain Name",
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"info_message": schema.StringAttribute{
Computed: true,
Description: "Optional additional information",
},
"resource_record_sets": schema.ListNestedAttribute{
Description: "Array of resource record sets",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
Description: "Name of the resource record set",
},
"type": schema.StringAttribute{
Computed: true,
Description: "Type of the resource record set",
},
"content": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Array of resource record set Content entries",
},
"ttl": schema.Int32Attribute{
Computed: true,
Description: "Time to live of the resource record set",
},
},
},
},
},
}
}

func (r *resourceRecordSet) Read(
ctx context.Context,
request datasource.ReadRequest,
response *datasource.ReadResponse,
) {
var config resourceRecordSetsDataSourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &config)...)

result, httpResponse, err := r.DNSAPI.GetResourceRecordSetList(
ctx,
config.DomainName.ValueString(),
).Execute()
if err != nil {
utils.SdkError(ctx, &response.Diagnostics, err, httpResponse)
return
}

var resourceRecordSets []resourceRecordSetDataSourceModel
for _, resourceRecordSetDetails := range result.GetResourceRecordSets() {
resourceRecordSets = append(
resourceRecordSets,
resourceRecordSetDataSourceModel{
Name: basetypes.NewStringValue(resourceRecordSetDetails.GetName()),
RecordType: basetypes.NewStringValue(string(resourceRecordSetDetails.GetType())),
Content: resourceRecordSetDetails.GetContent(),
TTL: basetypes.NewInt32Value(int32(resourceRecordSetDetails.GetTtl())),
},
)
}

response.Diagnostics.Append(
response.State.Set(
ctx,
resourceRecordSetsDataSourceModel{
DomainName: config.DomainName,
InfoMessage: basetypes.NewStringValue(result.GetInfoMessage()),
ResourceRecordSets: resourceRecordSets,
},
)...,
)
}

func NewResourceRecordSetsDataSource() datasource.DataSource {
return &resourceRecordSet{
DataSourceAPI: utils.DataSourceAPI{
Name: "dns_resource_record_sets",
},
}
}
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/client"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/dedicatedserver"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/dns"
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/publiccloud"
)

Expand Down Expand Up @@ -165,6 +166,7 @@ func (p *leasewebProvider) DataSources(_ context.Context) []func() datasource.Da
publiccloud.NewLoadBalancerListenersDataSource,
publiccloud.NewTargetGroupsDataSource,
publiccloud.NewISOsDataSource,
dns.NewResourceRecordSetsDataSource,
}
}

Expand Down
65 changes: 65 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3324,3 +3324,68 @@ func TestAccPublicCloudInstanceIsoResource(t *testing.T) {
})
})
}

func TestAccDnsResourceRecordSetsDataSource(t *testing.T) {
t.Run("domain_name is required", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + `
data "leaseweb_dns_resource_record_sets" "test" {
}`,
ExpectError: regexp.MustCompile(
"The argument \"domain_name\" is required, but no definition was found",
),
},
},
})
})

t.Run("reading data succeeds", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: providerConfig + `
data "leaseweb_dns_resource_record_sets" "test" {
domain_name = "example.com"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.leaseweb_dns_resource_record_sets.test",
"resource_record_sets.#",
"13",
),
resource.TestCheckResourceAttr(
"data.leaseweb_dns_resource_record_sets.test",
"resource_record_sets.0.name",
"example.com.",
),
resource.TestCheckResourceAttr(
"data.leaseweb_dns_resource_record_sets.test",
"resource_record_sets.0.type",
"A",
),
resource.TestCheckResourceAttr(
"data.leaseweb_dns_resource_record_sets.test",
"resource_record_sets.0.content.#",
"3",
),
resource.TestCheckResourceAttr(
"data.leaseweb_dns_resource_record_sets.test",
"resource_record_sets.0.content.0",
"85.17.150.50",
),
resource.TestCheckResourceAttr(
"data.leaseweb_dns_resource_record_sets.test",
"resource_record_sets.0.ttl",
"300",
),
),
},
},
})
})
}

0 comments on commit 60f4a71

Please sign in to comment.