Skip to content

Commit

Permalink
Add the ability to forem_article data source to query an article by p…
Browse files Browse the repository at this point in the history
…ath (#6)

* fix: fix description

* add data source forem_article functionality by username and slug
  • Loading branch information
karvounis authored Apr 9, 2022
1 parent 4d359d4 commit 8d90e41
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 32 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ testacc:
.PHONY: docs
docs:
tfplugindocs generate

.PHONY: tffmt
tffmt:
terraform fmt -recursive .
21 changes: 12 additions & 9 deletions docs/data-sources/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
page_title: "forem_article Data Source - terraform-provider-forem"
subcategory: ""
description: |-
forem_article data source fetches information about a particular article.
forem_article data source fetches information about a particular article. Please, specify either the id of the article or the username and slug combination.
API Docs
https://developers.forem.com/api#operation/getArticleById
---

# forem_article (Data Source)

`forem_article` data source fetches information about a particular article.
`forem_article` data source fetches information about a particular article. Please, specify either the `id` of the article or the `username` and `slug` combination.

## API Docs

Expand All @@ -19,21 +19,24 @@ https://developers.forem.com/api#operation/getArticleById
## Example Usage

```terraform
data "forem_article" "example" {
data "forem_article" "example_id" {
id = "979788"
}
data "forem_article" "example_username_slug" {
username = "admin_mcadmin"
slug = "basic-traefik-configuration-tutorial-593m"
}
```

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

### Required

- `id` (String) ID of the article.

### Optional

- `tags` (List of String) List of tags related to the article.
- `id` (String) ID of the article.
- `slug` (String) Slug of the article.
- `username` (String) User or organization username.

### Read-Only

Expand All @@ -54,8 +57,8 @@ data "forem_article" "example" {
- `published_at` (String) When the article was published.
- `published_timestamp` (String) When the article was published.
- `reading_time_minutes` (Number) Article reading time in minutes.
- `slug` (String) Slug of the article.
- `social_image` (String) Social image of the article.
- `tags` (List of String) List of tags related to the article.
- `title` (String) Title of the article.
- `url` (String) Full article URL.
- `user` (Map of String) User object of the article.
Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
page_title: "forem_user Data Source - terraform-provider-forem"
subcategory: ""
description: |-
`forem_user fetches information about a particular user. You can either use the user's ID or the user's username.
forem_user fetches information about a particular user. You can either use the user's ID or the user's username.
API Docs
https://developers.forem.com/api#operation/getUser
---

# forem_user (Data Source)

``forem_user` fetches information about a particular user. You can either use the user's ID or the user's username.
`forem_user` fetches information about a particular user. You can either use the user's ID or the user's username.

## API Docs

Expand Down
7 changes: 6 additions & 1 deletion examples/data-sources/forem_article/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
data "forem_article" "example" {
data "forem_article" "example_id" {
id = "979788"
}

data "forem_article" "example_username_slug" {
username = "admin_mcadmin"
slug = "basic-traefik-configuration-tutorial-593m"
}
55 changes: 39 additions & 16 deletions forem/data_source_article.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package forem
import (
"context"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -12,15 +13,23 @@ import (

func dataSourceArticle() *schema.Resource {
return &schema.Resource{
Description: "`forem_article` data source fetches information about a particular article." +
Description: "`forem_article` data source fetches information about a particular article. Please, specify either the `id` of the article or the `username` and `slug` combination." +
"\n\n## API Docs\n\n" +
"https://developers.forem.com/api#operation/getArticleById",
ReadContext: dataSourceArticleRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "ID of the article.",
Type: schema.TypeString,
Required: true,
Description: "ID of the article.",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"id", "username", "slug"},
},
"username": {
Description: "User or organization username.",
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"slug"},
AtLeastOneOf: []string{"id", "username", "slug"},
},
"title": {
Description: "Title of the article.",
Expand All @@ -45,13 +54,15 @@ func dataSourceArticle() *schema.Resource {
"tags": {
Description: "List of tags related to the article.",
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"slug": {
Description: "Slug of the article.",
Type: schema.TypeString,
Computed: true,
Description: "Slug of the article.",
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"username"},
AtLeastOneOf: []string{"id", "username", "slug"},
},
"path": {
Description: "Path of the article URL.",
Expand Down Expand Up @@ -154,16 +165,28 @@ func dataSourceArticle() *schema.Resource {
func dataSourceArticleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*dev.Client)

id := d.Get("id").(string)

tflog.Debug(ctx, fmt.Sprintf("Getting article: %s", id))
articlesResp, err := client.GetPublishedArticleByID(id)
if err != nil {
return diag.FromErr(err)
var articlesResp *dev.ArticleVariant
var err error
if v, ok := d.GetOk("id"); ok {
id := v.(string)
tflog.Debug(ctx, fmt.Sprintf("Getting article with ID: %s", id))
articlesResp, err = client.GetPublishedArticleByID(id)
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, fmt.Sprintf("Found article with ID: %s", id))
} else {
username := d.Get("username").(string)
slug := d.Get("slug").(string)
tflog.Debug(ctx, fmt.Sprintf("Getting article with username: %s and slug: %s", username, slug))
articlesResp, err = client.GetPublishedArticleByPath(username, slug)
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, fmt.Sprintf("Found article with username: %s and slug: %s", username, slug))
}
tflog.Debug(ctx, fmt.Sprintf("Found article: %s", id))

d.SetId(id)
d.SetId(strconv.Itoa(int(articlesResp.ID)))
d.Set("title", articlesResp.Article.Title)
d.Set("description", articlesResp.Article.Description)
d.Set("body_markdown", articlesResp.Article.BodyMarkdown)
Expand Down
38 changes: 35 additions & 3 deletions forem/data_source_article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccArticleDataSource(t *testing.T) {
func TestAccArticleDataSource_queryByID(t *testing.T) {
articleID := os.Getenv("TEST_DATA_FOREM_ARTICLE_ID")
resourceName := "data.forem_article.test"

Expand All @@ -17,7 +17,7 @@ func TestAccArticleDataSource(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccArticleDataSourceConfig_id(articleID),
Config: testAccArticleDataSourceConfig_queryByID(articleID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", articleID),
resource.TestCheckResourceAttrSet(resourceName, "title"),
Expand All @@ -30,10 +30,42 @@ func TestAccArticleDataSource(t *testing.T) {
})
}

func testAccArticleDataSourceConfig_id(id string) string {
func TestAccArticleDataSource_queryByUsernameAndSlug(t *testing.T) {
articleUsername := os.Getenv("TEST_DATA_FOREM_ARTICLE_USERNAME")
articleSlug := os.Getenv("TEST_DATA_FOREM_ARTICLE_SLUG")
resourceName := "data.forem_article.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccArticleDataSourceConfig_queryByUsernameAndSlug(articleUsername, articleSlug),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "title"),
resource.TestCheckResourceAttrSet(resourceName, "body_markdown"),
resource.TestCheckResourceAttr(resourceName, "slug", articleSlug),
resource.TestCheckResourceAttrSet(resourceName, "path"),
),
},
},
})
}

func testAccArticleDataSourceConfig_queryByID(id string) string {
return fmt.Sprintf(`
data "forem_article" "test" {
id = "%s"
}
`, id)
}

func testAccArticleDataSourceConfig_queryByUsernameAndSlug(username, slug string) string {
return fmt.Sprintf(`
data "forem_article" "test" {
username = "%s"
slug = "%s"
}
`, username, slug)
}
2 changes: 1 addition & 1 deletion forem/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func dataSourceUser() *schema.Resource {
return &schema.Resource{
Description: "``forem_user` fetches information about a particular user. You can either use the user's ID or the user's username." +
Description: "`forem_user` fetches information about a particular user. You can either use the user's ID or the user's username." +
"\n\n## API Docs\n\n" +
"https://developers.forem.com/api#operation/getUser",
ReadContext: dataSourceUserRead,
Expand Down

0 comments on commit 8d90e41

Please sign in to comment.