-
Notifications
You must be signed in to change notification settings - Fork 1
/
category_data_source.go
191 lines (171 loc) · 5.7 KB
/
category_data_source.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package readme
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/liveoaklabs/readme-api-go-client/readme"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &categoryDataSource{}
_ datasource.DataSourceWithConfigure = &categoryDataSource{}
)
// categoryDataSource is the data source implementation.
type categoryDataSource struct {
client *readme.Client
}
// categoryModel maps an API specification to the apiSpecification schema data.
type categoryModel struct {
CategoryType types.String `tfsdk:"category_type"`
CreatedAt types.String `tfsdk:"created_at"`
ID types.String `tfsdk:"id"`
Order types.Int64 `tfsdk:"order"`
Project types.String `tfsdk:"project"`
Reference types.Bool `tfsdk:"reference"`
Slug types.String `tfsdk:"slug"`
Title types.String `tfsdk:"title"`
Type types.String `tfsdk:"type"`
Version types.String `tfsdk:"version"`
VersionID types.String `tfsdk:"version_id"`
}
// NewCategoryDataSource is a helper function to simplify the provider implementation.
func NewCategoryDataSource() datasource.DataSource {
return &categoryDataSource{}
}
// Metadata returns the data source type name.
func (d *categoryDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_category"
}
// categoryCommonSchema returns the category schema that is common for the single category schema used in this data
// source and the list of categories used by the `readme_categories` data source.
var categoryCommonSchema = map[string]schema.Attribute{
"created_at": schema.StringAttribute{
Description: "Timestamp of when the version was created.",
Computed: true,
},
"id": schema.StringAttribute{
Description: "The ID of the category.",
Computed: true,
},
"order": schema.Int64Attribute{
Description: "The order of the category.",
Computed: true,
},
"project": schema.StringAttribute{
Description: "The ID of the project the category is in.",
Computed: true,
},
"reference": schema.BoolAttribute{
Description: "Indicates whether the category is a reference or not.",
Computed: true,
},
"slug": schema.StringAttribute{
Description: "The slug of the category.",
Required: true,
},
"title": schema.StringAttribute{
Description: "The title of the category.",
Computed: true,
},
"type": schema.StringAttribute{
Description: "The category type.",
Computed: true,
},
"version_id": schema.StringAttribute{
Description: "The version the category is associated with.",
Computed: true,
},
}
// categorySingleSchema returns the Terraform data source schema for a single category, which adds a 'category_type' attribute
// to the common schema.
//
// This returns the common category schema with the `category_type` attribute, which is not used in the
// `readme_categories` data source.
func categorySingleSchema() map[string]schema.Attribute {
mergedSchema := map[string]schema.Attribute{
"category_type": schema.StringAttribute{
Description: "The category type (different than 'type').",
Computed: true,
},
"version": schema.StringAttribute{
Description: "The 'semver-ish' value of the version the category is under.",
Computed: true,
},
}
// Merge the detail schema with the common schema.
for k, v := range categoryCommonSchema {
mergedSchema[k] = v
}
return mergedSchema
}
// Schema defines the schema for the data source.
func (d *categoryDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: "Retrieve metadata about a category on ReadMe.com\n\n" +
"See <https://docs.readme.com/main/reference/getcategory> for more information about this API endpoint.\n\n",
Attributes: categorySingleSchema(),
}
}
// Read refreshes the Terraform state with the latest data.
func (d *categoryDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var state categoryModel
// Get config
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get category metadata from ReadMe API
category, apiResponse, err := d.client.Category.Get(state.Slug.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Unable to retrieve category metadata.",
clientError(err, apiResponse),
)
return
}
// Map response body to model
state = categoryModel{
CategoryType: types.StringValue(category.CategoryType),
CreatedAt: types.StringValue(category.CreatedAt),
ID: types.StringValue(category.ID),
Order: types.Int64Value(int64(category.Order)),
Project: types.StringValue(category.Project),
Reference: types.BoolValue(category.Reference),
Slug: types.StringValue(category.Slug),
Title: types.StringValue(category.Title),
Type: types.StringValue(category.Type),
Version: types.StringValue(versionClean(ctx, d.client, category.Version)),
VersionID: types.StringValue(category.Version),
}
// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the data source.
func (d *categoryDataSource) Configure(
ctx context.Context,
req datasource.ConfigureRequest,
_ *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*readme.Client)
}