-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue_templates_data_source.go
189 lines (161 loc) · 5.37 KB
/
issue_templates_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
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
hundApiV1 "github.com/hundio/terraform-provider-hund/internal/hund_api_v1"
"github.com/hundio/terraform-provider-hund/internal/models"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ datasource.DataSource = &IssueTemplatesDataSource{}
_ datasource.DataSourceWithConfigure = &IssueTemplatesDataSource{}
)
func NewIssueTemplatesDataSource() datasource.DataSource {
return &IssueTemplatesDataSource{}
}
// IssueTemplatesDataSource defines the data source implementation.
type IssueTemplatesDataSource struct {
client *hundApiV1.Client
}
// IssueTemplatesDataSourceModel describes the data source data model.
type IssueTemplatesDataSourceModel struct {
Kind types.String `tfsdk:"kind"`
IssueTemplates []models.IssueTemplateModel `tfsdk:"issue_templates"`
}
func (d *IssueTemplatesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_issue_templates"
}
func (d *IssueTemplatesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "IssueTemplates data source",
Attributes: map[string]schema.Attribute{
"kind": schema.StringAttribute{
MarkdownDescription: "Return only IssueTemplates for the given kind. Either `issue` or `update`.",
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("issue", "update"),
},
},
"issue_templates": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"kind": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
},
"created_at": schema.StringAttribute{
Computed: true,
},
"updated_at": schema.StringAttribute{
Computed: true,
},
"title": schema.StringAttribute{
Computed: true,
},
"body": schema.StringAttribute{
Computed: true,
},
"title_translations": schema.MapAttribute{
Computed: true,
ElementType: types.StringType,
},
"body_translations": schema.MapAttribute{
Computed: true,
ElementType: types.StringType,
},
"label": schema.StringAttribute{
Computed: true,
},
"variables": schema.MapNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Computed: true,
},
"required": schema.BoolAttribute{
Computed: true,
},
},
},
},
},
},
},
},
}
}
func (d *IssueTemplatesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*hundApiV1.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *hundApiV1.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *IssueTemplatesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data IssueTemplatesDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
limit := 100
rsp, err := d.client.GetAllIssueTemplates(ctx, &hundApiV1.GetAllIssueTemplatesParams{
Kind: (*hundApiV1.GetAllIssueTemplatesParamsKind)(data.Kind.ValueStringPointer()),
Limit: &limit,
})
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Hund Issue Templates",
err.Error(),
)
return
}
templates, err := hundApiV1.ParseGetAllIssueTemplatesResponse(rsp)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Parse Hund Issue Templates",
err.Error(),
)
return
}
if templates.StatusCode() != 200 {
resp.Diagnostics.AddError(
"Failed response code from Hund API",
"Received a non-200 status code: "+fmt.Sprint(templates.StatusCode()),
)
return
}
// data.IssueTemplates = []models.IssueTemplateModel{}
for _, template := range templates.HALJSON200.Data {
templateState, diag := models.ToIssueTemplateModel(template)
resp.Diagnostics.Append(diag...)
data.IssueTemplates = append(data.IssueTemplates, templateState)
}
// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "read a data source")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}