-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_page.go
174 lines (165 loc) · 5.38 KB
/
custom_page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package readme
import (
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/liveoaklabs/readme-api-go-client/readme"
)
// customPageDataSourceModel is the data source model used by the
// readme_custom_page and readme_custom_pages data sources.
type customPageDataSourceModel struct {
Algolia types.Object `tfsdk:"algolia"`
Body types.String `tfsdk:"body"`
CreatedAt types.String `tfsdk:"created_at"`
FullScreen types.Bool `tfsdk:"fullscreen"`
HTML types.String `tfsdk:"html"`
HTMLMode types.Bool `tfsdk:"htmlmode"`
Hidden types.Bool `tfsdk:"hidden"`
ID types.String `tfsdk:"id"`
Metadata *docMetadata `tfsdk:"metadata"`
Revision types.Int64 `tfsdk:"revision"`
Slug types.String `tfsdk:"slug"`
Title types.String `tfsdk:"title"`
UpdatedAt types.String `tfsdk:"updated_at"`
}
// customPageDatasourceMapToModel maps a readme.CustomPage to a customPageDataSourceModel
// for use in the readme_custom_page and readme_custom_pages data sources.
func customPageDatasourceMapToModel(page readme.CustomPage) customPageDataSourceModel {
image := []string{}
if page.Metadata.Image != nil {
for _, i := range page.Metadata.Image {
image = append(image, fmt.Sprintf("%v", i))
}
}
model := customPageDataSourceModel{
Title: types.StringValue(page.Title),
Slug: types.StringValue(page.Slug),
Body: types.StringValue(page.Body),
HTML: types.StringValue(page.HTML),
HTMLMode: types.BoolValue(page.HTMLMode),
FullScreen: types.BoolValue(page.Fullscreen),
Hidden: types.BoolValue(page.Hidden),
Revision: types.Int64Value(int64(page.Revision)),
ID: types.StringValue(page.ID),
CreatedAt: types.StringValue(page.CreatedAt),
UpdatedAt: types.StringValue(page.UpdatedAt),
Metadata: &docMetadata{
Image: image,
Title: page.Metadata.Title,
Description: page.Metadata.Description,
},
Algolia: docModelAlgoliaValue(page.Algolia),
}
return model
}
// customPageResourceMapToModel maps a readme.CustomPage to a customPageResourceModel
// for use in the readme_custom_page resource.
func customPageResourceMapToModel(
page readme.CustomPage,
plan customPageResourceModel,
) customPageResourceModel {
if plan.Body.IsUnknown() {
plan.Body = types.StringValue("")
}
if plan.HTML.IsUnknown() {
plan.HTML = types.StringValue("")
}
return customPageResourceModel{
Algolia: docModelAlgoliaValue(page.Algolia),
Body: plan.Body,
BodyClean: types.StringValue(page.Body),
CreatedAt: types.StringValue(page.CreatedAt),
FullScreen: types.BoolValue(page.Fullscreen),
HTML: plan.HTML,
HTMLClean: types.StringValue(page.HTML),
HTMLMode: types.BoolValue(page.HTMLMode),
Hidden: types.BoolValue(page.Hidden),
ID: types.StringValue(page.ID),
Metadata: docModelMetadataValue(page.Metadata),
Revision: types.Int64Value(int64(page.Revision)),
Slug: types.StringValue(page.Slug),
Title: types.StringValue(page.Title),
UpdatedAt: types.StringValue(page.UpdatedAt),
}
}
// customPageDataSourceSchema returns the schema for the
// readme_custom_page and readme_custom_pages data sources.
func customPageDataSourceSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"title": schema.StringAttribute{
Description: "The title of the custom page.",
Computed: true,
},
"slug": schema.StringAttribute{
Description: "The slug of the custom page.",
Computed: true,
},
"body": schema.StringAttribute{
Description: "The body of the custom page.",
Computed: true,
},
"html": schema.StringAttribute{
Description: "The HTML of the custom page.",
Computed: true,
},
"htmlmode": schema.BoolAttribute{
Description: "Whether the custom page is in HTML mode.",
Computed: true,
},
"fullscreen": schema.BoolAttribute{
Description: "Whether the custom page is in fullscreen mode.",
Computed: true,
},
"hidden": schema.BoolAttribute{
Description: "Whether the custom page is hidden.",
Computed: true,
},
"revision": schema.Int64Attribute{
Description: "The revision of the custom page.",
Computed: true,
},
"id": schema.StringAttribute{
Description: "The ID of the custom page.",
Computed: true,
},
"created_at": schema.StringAttribute{
Description: "The date the custom page was created.",
Computed: true,
},
"updated_at": schema.StringAttribute{
Description: "The date the custom page was last updated.",
Computed: true,
},
"algolia": schema.SingleNestedAttribute{
Description: "Metadata about the Algolia search integration. " +
"See <https://docs.readme.com/main/docs/search> for more information.",
Computed: true,
Attributes: map[string]schema.Attribute{
"publish_pending": schema.BoolAttribute{
Computed: true,
},
"record_count": schema.Int64Attribute{
Computed: true,
},
"updated_at": schema.StringAttribute{
Computed: true,
},
},
},
"metadata": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"description": schema.StringAttribute{
Computed: true,
},
"image": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
"title": schema.StringAttribute{
Computed: true,
},
},
},
}
}