Skip to content

Commit

Permalink
Merge pull request #78 from joshbeard/modifiers-frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbeard authored Oct 24, 2023
2 parents 7baf2d2 + 305e059 commit dc10fb9
Show file tree
Hide file tree
Showing 14 changed files with 1,041 additions and 265 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ lint: modverify vet gofumpt lines golangci-lint ## Run all linters
## Testing ##
.PHONY: test
test: ## Run unit and race tests with 'go test'
go test -v -count=1 -parallel=4 -coverprofile=coverage.txt -covermode count ./readme/...
go test -race -short ./readme/...
go test -v -count=1 -parallel=4 -coverprofile=coverage.txt -covermode count ./readme/
go test -race -short ./readme/

## Coverage ##
.PHONY: coverage
Expand Down
7 changes: 4 additions & 3 deletions readme/custom_page_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/liveoaklabs/readme-api-go-client/readme"
"github.com/liveoaklabs/terraform-provider-readme/readme/frontmatter"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -83,7 +84,7 @@ func (r customPageResource) ValidateConfig(

if data.Title.IsNull() {
// check front matter for 'title'.
titleMatter, diag := valueFromFrontMatter(ctx, data.Body.ValueString(), "Title")
titleMatter, diag := frontmatter.GetValue(ctx, data.Body.ValueString(), "Title")
if diag != "" {
resp.Diagnostics.AddAttributeError(
path.Root("title"),
Expand Down Expand Up @@ -226,7 +227,7 @@ func (r *customPageResource) Schema(_ context.Context, _ resource.SchemaRequest,
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
fromMatterString("Title"),
frontmatter.GetString("Title"),
},
},
"slug": schema.StringAttribute{
Expand Down Expand Up @@ -270,7 +271,7 @@ func (r *customPageResource) Schema(_ context.Context, _ resource.SchemaRequest,
Optional: true,
Default: booldefault.StaticBool(true),
PlanModifiers: []planmodifier.Bool{
fromMatterBool("Hidden"),
frontmatter.GetBool("Hidden"),
},
},
"revision": schema.Int64Attribute{
Expand Down
65 changes: 55 additions & 10 deletions readme/doc_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/liveoaklabs/readme-api-go-client/readme"
"github.com/liveoaklabs/terraform-provider-readme/readme/frontmatter"
"github.com/liveoaklabs/terraform-provider-readme/readme/otherattributemodifier"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -70,7 +72,7 @@ func (r docResource) ValidateConfig(
// category or category_slug must be set. If the attributes aren't set, check the body front matter.
if data.Category.IsNull() && data.CategorySlug.IsNull() {
// check front matter for 'category'.
categoryMatter, diag := valueFromFrontMatter(ctx, data.Body.ValueString(), "Category")
categoryMatter, diag := frontmatter.GetValue(ctx, data.Body.ValueString(), "Category")
if diag != "" {
resp.Diagnostics.AddAttributeError(
path.Root("category"),
Expand All @@ -82,7 +84,7 @@ func (r docResource) ValidateConfig(
}

// check front matter for 'category_slug'.
categorySlugMatter, diag := valueFromFrontMatter(
categorySlugMatter, diag := frontmatter.GetValue(
ctx,
data.Body.ValueString(),
"CategorySlug",
Expand Down Expand Up @@ -416,6 +418,23 @@ func (r *docResource) Schema(
},
"updated_at": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
otherattributemodifier.StringModifyString(path.Root("body"), "Body", true),
otherattributemodifier.StringModifyString(path.Root("body_html"), "BodyHTML", true),
otherattributemodifier.StringModifyString(path.Root("category"), "Category", true),
otherattributemodifier.StringModifyString(path.Root("category_slug"), "CategorySlug", true),
otherattributemodifier.BoolModifyString(path.Root("hidden"), "Hidden", true),
otherattributemodifier.Int64ModifyString(path.Root("order"), "Order", true),
otherattributemodifier.StringModifyString(path.Root("parent_doc"), "ParentDoc", true),
otherattributemodifier.StringModifyString(
path.Root("parent_doc_slug"),
"ParentDocSlug",
true,
),
otherattributemodifier.StringModifyString(path.Root("slug"), "Slug", true),
otherattributemodifier.StringModifyString(path.Root("title"), "Title", true),
otherattributemodifier.StringModifyString(path.Root("type"), "Type", true),
},
},
},
},
Expand Down Expand Up @@ -538,7 +557,7 @@ func (r *docResource) Schema(
Optional: true,
Validators: []validator.String{},
PlanModifiers: []planmodifier.String{
fromMatterString("Category"),
frontmatter.GetString("Category"),
},
},
"category_slug": schema.StringAttribute{
Expand All @@ -549,7 +568,7 @@ func (r *docResource) Schema(
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
fromMatterString("CategorySlug"),
frontmatter.GetString("CategorySlug"),
},
},
"created_at": schema.StringAttribute{
Expand Down Expand Up @@ -582,7 +601,7 @@ func (r *docResource) Schema(
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.Bool{
fromMatterBool("Hidden"),
frontmatter.GetBool("Hidden"),
},
},
"icon": schema.StringAttribute{
Expand Down Expand Up @@ -664,7 +683,7 @@ func (r *docResource) Schema(
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.Int64{
fromMatterInt64("Order"),
frontmatter.GetInt64("Order"),
},
},
"parent_doc": schema.StringAttribute{
Expand All @@ -676,7 +695,7 @@ func (r *docResource) Schema(
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
fromMatterString("ParentDoc"),
frontmatter.GetString("ParentDoc"),
},
},
"parent_doc_slug": schema.StringAttribute{
Expand All @@ -687,7 +706,7 @@ func (r *docResource) Schema(
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
fromMatterString("ParentDocSlug"),
frontmatter.GetString("ParentDocSlug"),
},
},
"previous_slug": schema.StringAttribute{
Expand All @@ -700,6 +719,19 @@ func (r *docResource) Schema(
"revision": schema.Int64Attribute{
Description: "A number that is incremented upon doc updates.",
Computed: true,
PlanModifiers: []planmodifier.Int64{
otherattributemodifier.StringModifyInt64(path.Root("body"), "Body", true),
otherattributemodifier.StringModifyInt64(path.Root("body_html"), "BodyHTML", true),
otherattributemodifier.StringModifyInt64(path.Root("category"), "Category", true),
otherattributemodifier.StringModifyInt64(path.Root("category_slug"), "CategorySlug", true),
otherattributemodifier.BoolModifyInt64(path.Root("hidden"), "Hidden", true),
otherattributemodifier.Int64ModifyInt64(path.Root("order"), "Order", true),
otherattributemodifier.StringModifyInt64(path.Root("parent_doc"), "ParentDoc", true),
otherattributemodifier.StringModifyInt64(path.Root("parent_doc_slug"), "ParentDocSlug", true),
otherattributemodifier.StringModifyInt64(path.Root("slug"), "Slug", true),
otherattributemodifier.StringModifyInt64(path.Root("title"), "Title", true),
otherattributemodifier.StringModifyInt64(path.Root("type"), "Type", true),
},
},
"slug": schema.StringAttribute{
Description: "The slug of the doc.",
Expand All @@ -718,7 +750,7 @@ func (r *docResource) Schema(
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
fromMatterString("Title"),
frontmatter.GetString("Title"),
},
},
"type": schema.StringAttribute{
Expand All @@ -729,12 +761,25 @@ func (r *docResource) Schema(
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
fromMatterString("Type"),
frontmatter.GetString("Type"),
},
},
"updated_at": schema.StringAttribute{
Description: "The timestamp of when the doc was last updated.",
Computed: true,
PlanModifiers: []planmodifier.String{
otherattributemodifier.StringModifyString(path.Root("body"), "Body", true),
otherattributemodifier.StringModifyString(path.Root("body_html"), "BodyHTML", true),
otherattributemodifier.StringModifyString(path.Root("category"), "Category", true),
otherattributemodifier.StringModifyString(path.Root("category_slug"), "CategorySlug", true),
otherattributemodifier.BoolModifyString(path.Root("hidden"), "Hidden", true),
otherattributemodifier.Int64ModifyString(path.Root("order"), "Order", true),
otherattributemodifier.StringModifyString(path.Root("parent_doc"), "ParentDoc", true),
otherattributemodifier.StringModifyString(path.Root("parent_doc_slug"), "ParentDocSlug", true),
otherattributemodifier.StringModifyString(path.Root("slug"), "Slug", true),
otherattributemodifier.StringModifyString(path.Root("title"), "Title", true),
otherattributemodifier.StringModifyString(path.Root("type"), "Type", true),
},
},
"user": schema.StringAttribute{
Description: "The ID of the author of the doc in the web editor.",
Expand Down
16 changes: 8 additions & 8 deletions readme/doc_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDocResource(t *testing.T) {
category = "%s"
type = "%s"
}`,
mockDoc.Title, mockDocBodyString, mockDoc.Category, mockDoc.Type,
mockDoc.Title, mockDoc.Body, mockDoc.Category, mockDoc.Type,
),
PreConfig: func() {
docCommonGocks()
Expand All @@ -49,7 +49,7 @@ func TestDocResource(t *testing.T) {
category = "%s"
type = "%s"
}`,
mockDoc.Title, mockDocBodyString, mockDoc.Category, mockDoc.Type,
mockDoc.Title, mockDoc.Body, mockDoc.Category, mockDoc.Type,
),
PreConfig: func() {
gock.OffAll()
Expand Down Expand Up @@ -84,13 +84,13 @@ func TestDocResource(t *testing.T) {
category = "%s"
type = "%s"
}`,
mockDocBodyString, mockDoc.Category, mockDoc.Type,
mockDoc.Body, mockDoc.Category, mockDoc.Type,
),
PreConfig: func() {
gock.OffAll()
gock.New(testURL).Get("/docs/" + mockDoc.Slug).Times(1).Reply(400).JSON(mockAPIError)
},
ExpectError: regexp.MustCompile("API responded with a non-OK status: 400"),
ExpectError: regexp.MustCompile("DOC_NOTFOUND"),
},

// Test update results in error when the update action fails.
Expand All @@ -102,7 +102,7 @@ func TestDocResource(t *testing.T) {
category = "%s"
type = "%s"
}`,
mockDocBodyString, mockDoc.Category, mockDoc.Type,
mockDoc.Body, mockDoc.Category, mockDoc.Type,
),
PreConfig: func() {
gock.OffAll()
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestDocResource(t *testing.T) {
category = "%s"
type = "%s"
}`,
mockDocBodyString, mockDoc.Category, mockDoc.Type,
mockDoc.Body, mockDoc.Category, mockDoc.Type,
),
PreConfig: func() {
gock.OffAll()
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestDocResource(t *testing.T) {
category = "%s"
type = "%s"
}`,
mockDoc.Title, mockDocBodyString, mockDoc.Category, mockDoc.Type,
mockDoc.Title, mockDoc.Body, mockDoc.Category, mockDoc.Type,
),
PreConfig: func() {
gock.OffAll()
Expand All @@ -179,7 +179,7 @@ func TestDocResource(t *testing.T) {
category = "%s"
type = "%s"
}`,
mockDocBodyString, mockDoc.Category, mockDoc.Type,
mockDoc.Body, mockDoc.Category, mockDoc.Type,
),
PreConfig: func() {
gock.OffAll()
Expand Down
9 changes: 1 addition & 8 deletions readme/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ var mockDoc readme.Doc = readme.Doc{
Version: mockVersion.ID,
}

// mockDocBodyString represents a "raw" body string provided in the configuration with extraneous whitespace.
var mockDocBodyString string = fmt.Sprintf(` \n%s\n\n `, mockDoc.Body)

// makeMockDocParent returns a doc for use as a 'parent' doc.
func makeMockDocParent() readme.Doc {
parentDoc := mockDoc
Expand Down Expand Up @@ -200,10 +197,6 @@ func docCommonGocks() {

// docResourceCommonChecks returns all attribute checks for the data source and resource.
func docResourceCommonChecks(mock readme.Doc, prefix string) resource.TestCheckFunc {
if prefix == "data." {
mockDocBodyString = mockDoc.Body
}

return resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
prefix+"readme_doc.test",
Expand Down Expand Up @@ -288,7 +281,7 @@ func docResourceCommonChecks(mock readme.Doc, prefix string) resource.TestCheckF
fmt.Sprintf("%v", mock.API.Results.Codes[0].Status),
),
resource.TestCheckResourceAttr(prefix+"readme_doc.test", "api.url", mock.API.URL),
resource.TestCheckResourceAttr(prefix+"readme_doc.test", "body", mockDocBodyString),
resource.TestCheckResourceAttr(prefix+"readme_doc.test", "body", mock.Body),
resource.TestCheckResourceAttr(prefix+"readme_doc.test", "body_clean", mock.Body),
resource.TestCheckResourceAttr(prefix+"readme_doc.test", "body_html", mock.BodyHTML),
resource.TestCheckResourceAttr(prefix+"readme_doc.test", "category", mock.Category),
Expand Down
Loading

0 comments on commit dc10fb9

Please sign in to comment.