/
categories_data_source.go
145 lines (126 loc) · 4.12 KB
/
categories_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
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 = &categoriesDataSource{}
_ datasource.DataSourceWithConfigure = &categoriesDataSource{}
)
// categoriesDataSource is the data source implementation.
type categoriesDataSource struct {
client *readme.Client
}
type categoriesModel struct {
ID types.String `tfsdk:"id"`
Categories []categoryListModel `tfsdk:"categories"`
}
type categoryListModel struct {
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"`
VersionID types.String `tfsdk:"version_id"`
}
// NewCategoriesDataSource is a helper function to simplify the provider implementation.
func NewCategoriesDataSource() datasource.DataSource {
return &categoriesDataSource{}
}
// Metadata returns the data source type name.
func (d *categoriesDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_categories"
}
// Schema defines the schema for the data source.
func (d *categoriesDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: "Retrieve all categories for a project on ReadMe.com\n\n" +
"See <https://docs.readme.com/main/reference/getcategories> for more information about this API endpoint.\n\n",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The internal Terraform ID of the categories data source.",
Computed: true,
},
"categories": schema.ListNestedAttribute{
Description: "List of category summaries.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: categoryCommonSchema,
},
},
},
}
}
// Read refreshes the Terraform state with the latest data.
func (d *categoriesDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var state categoriesModel
// Get config.
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get categories metadata from ReadMe API.
categories, apiResponse, err := d.client.Category.GetAll()
if err != nil {
resp.Diagnostics.AddError(
"Unable to retrieve categories metadata.",
clientError(err, apiResponse),
)
return
}
for _, category := range categories {
// Map response body to model.
cat := categoryListModel{
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),
VersionID: types.StringValue(category.Version),
}
state.Categories = append(state.Categories, cat)
}
// The ID attribute is only used by Terraform and the provider internally.
// It doesn't exist in the API for the list of categoriess but is required to be set.
state.ID = types.StringValue("readme_categories")
// 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 *categoriesDataSource) Configure(
ctx context.Context,
req datasource.ConfigureRequest,
_ *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*readme.Client)
}