Skip to content

Commit

Permalink
refactor: embed markdown descriptions for datacenter (#794)
Browse files Browse the repository at this point in the history
This embeds the description from external markdown files.

In addition, the datacenter list markdown description was using the same
as for the datacenter data source, the markdown file now contains the
new description picked from `website/docs/d/datacenters.html.md`
  • Loading branch information
jooola authored Nov 22, 2023
1 parent 9734cce commit 4811531
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
36 changes: 9 additions & 27 deletions internal/datacenter/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datacenter
import (
"context"
"crypto/sha1"
_ "embed"
"fmt"
"sort"
"strconv"
Expand Down Expand Up @@ -145,23 +146,13 @@ func (d *dataSource) Configure(_ context.Context, req datasource.ConfigureReques
}
}

//go:embed data_source.md
var dataSourceMarkdownDescription string

// Schema should return the schema for this data source.
func (d *dataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema.Attributes = getCommonDataSchema()

resp.Schema.MarkdownDescription = `
Provides details about a specific Hetzner Cloud Datacenter.
Use this resource to get detailed information about specific datacenter.
## Example Usage
` + "```" + `hcl
data "hcloud_datacenter" "ds_1" {
name = "fsn1-dc14"
}
data "hcloud_datacenter" "ds_2" {
id = 4
}
` + "```"
resp.Schema.MarkdownDescription = dataSourceMarkdownDescription
}

// ConfigValidators returns a list of ConfigValidators. Each ConfigValidator's Validate method will be called when validating the data source.
Expand Down Expand Up @@ -256,6 +247,9 @@ func (d *dataSourceList) Configure(_ context.Context, req datasource.ConfigureRe
}
}

//go:embed data_source_list.md
var dataSourceListMarkdownDescription string

// Schema should return the schema for this data source.
func (d *dataSourceList) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema.Attributes = map[string]schema.Attribute{
Expand Down Expand Up @@ -285,19 +279,7 @@ func (d *dataSourceList) Schema(_ context.Context, _ datasource.SchemaRequest, r
},
}

resp.Schema.MarkdownDescription = `
Provides details about a specific Hetzner Cloud Datacenter.
Use this resource to get detailed information about specific datacenter.
## Example Usage
` + "```" + `hcl
data "hcloud_datacenter" "ds_1" {
name = "fsn1-dc8"
}
data "hcloud_datacenter" "ds_2" {
id = 4
}
` + "```"
resp.Schema.MarkdownDescription = dataSourceListMarkdownDescription
}

type resourceDataList struct {
Expand Down
15 changes: 15 additions & 0 deletions internal/datacenter/data_source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Provides details about a specific Hetzner Cloud Datacenter.

Use this resource to get detailed information about a specific datacenter.

## Example

```hcl
data "hcloud_datacenter" "datacenter1" {
name = "fsn1-dc14"
}
data "hcloud_datacenter" "datacenter2" {
id = 4
}
```
18 changes: 18 additions & 0 deletions internal/datacenter/data_source_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Provides a list of available Hetzner Cloud Datacenters.

This resource may be useful to create highly available infrastructure, distributed across several datacenters.

## Example

```hcl
data "hcloud_datacenters" "datacenters" {}
resource "hcloud_server" "workers" {
count = 5
name = "node${count.index}"
image = "debian-11"
server_type = "cx31"
datacenter = element(data.hcloud_datacenters.datacenters.datacenters, count.index).name
}
```

0 comments on commit 4811531

Please sign in to comment.