summaryrefslogtreecommitdiff
path: root/internal/provider/jobs_data_source.go
blob: 4cea2f8b569410a5980e2cd6aed9ce74fb79f23e (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package provider

import (
	"context"
	"fmt"

	"git.adyxax.org/adyxax/terraform-provider-eventline/external/evcli"
	"github.com/exograd/eventline/pkg/ksuid"
	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

type JobsDataSource struct {
	client *evcli.Client
}

var _ datasource.DataSource = &JobsDataSource{} // Ensure provider defined types fully satisfy framework interfaces
func NewJobsDataSource() datasource.DataSource {
	return &JobsDataSource{}
}

type JobsDataSourceModel struct {
	Elements  []JobDataSourceModel `tfsdk:"elements"`
	ProjectId types.String         `tfsdk:"project_id"`
}
type JobDataSourceModel struct {
	Disabled types.Bool             `tfsdk:"disabled"`
	Id       types.String           `tfsdk:"id"`
	Spec     JobSpecDataSourceModel `tfsdk:"spec"`
}
type JobSpecDataSourceModel struct {
	Concurrent  types.Bool                 `tfsdk:"concurrent"`
	Description types.String               `tfsdk:"description"`
	Environment types.Map                  `tfsdk:"environment"`
	Identities  types.Set                  `tfsdk:"identities"`
	Name        types.String               `tfsdk:"name"`
	Parameters  []ParameterDataSourceModel `tfsdk:"parameters"`
	Retention   types.Int64                `tfsdk:"retention"`
	Runner      *RunnerDataSourceModel     `tfsdk:"runner"`
	Trigger     *TriggerDataSourceModel    `tfsdk:"trigger"`
	Steps       []StepDataSourceModel      `tfsdk:"steps"`
}
type ParameterDataSourceModel struct {
	Description types.String `tfsdk:"description"`
	Environment types.String `tfsdk:"environment"`
	Name        types.String `tfsdk:"name"`
	Type        types.String `tfsdk:"type"`
	Values      types.List   `tfsdk:"values"`
}
type RunnerDataSourceModel struct {
	Name     types.String `tfsdk:"name"`
	Identity types.String `tfsdk:"identity"`
}
type StepDataSourceModel struct {
	Code    types.String                `tfsdk:"code"`
	Command *StepCommandDataSourceModel `tfsdk:"command"`
	Label   types.String                `tfsdk:"label"`
	Script  *StepScriptDataSourceModel  `tfsdk:"script"`
}
type StepCommandDataSourceModel struct {
	Arguments types.List   `tfsdk:"arguments"`
	Name      types.String `tfsdk:"name"`
}
type StepScriptDataSourceModel struct {
	Arguments types.List   `tfsdk:"arguments"`
	Content   types.String `tfsdk:"content"`
	Path      types.String `tfsdk:"path"`
}
type TriggerDataSourceModel struct {
	Event    types.String `tfsdk:"event"`
	Identity types.String `tfsdk:"identity"`
}

func (d *JobsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
	resp.TypeName = req.ProviderTypeName + "_jobs"
}

func (d *JobsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"elements": schema.ListNestedAttribute{
				Computed:            true,
				MarkdownDescription: "The list of jobs.",
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"disabled": schema.BoolAttribute{
							Computed:            true,
							MarkdownDescription: "Whether the job is disabled or not.",
						},
						"id": schema.StringAttribute{
							Computed:            true,
							MarkdownDescription: "The identifier of the job.",
						},
						"spec": schema.SingleNestedAttribute{
							Attributes: map[string]schema.Attribute{
								"concurrent": schema.BoolAttribute{
									Computed:            true,
									MarkdownDescription: "Whether to allow concurrent executions for this job or not.",
								},
								"description": schema.StringAttribute{
									Computed:            true,
									MarkdownDescription: "A textual description of the job.",
								},
								"environment": schema.MapAttribute{
									ElementType:         types.StringType,
									Computed:            true,
									MarkdownDescription: "A set of environment variables mapping names to values to be defined during job execution.",
								},
								"identities": schema.SetAttribute{
									Computed:            true,
									ElementType:         types.StringType,
									MarkdownDescription: "Set of eventline identities names to inject during job execution.",
								},
								"name": schema.StringAttribute{
									Computed:            true,
									MarkdownDescription: "The name of the job.",
								},
								"parameters": schema.ListNestedAttribute{
									Computed: true,
									NestedObject: schema.NestedAttributeObject{
										Attributes: map[string]schema.Attribute{
											"description": schema.StringAttribute{
												Computed:            true,
												MarkdownDescription: "A textual description of the parameter.",
											},
											"environment": schema.StringAttribute{
												Computed:            true,
												MarkdownDescription: "The name of an environment variable to be used to inject the value of this parameter during execution.",
											},
											"name": schema.StringAttribute{
												Computed:            true,
												MarkdownDescription: "The name of the parameter.",
											},
											"type": schema.StringAttribute{
												Computed:            true,
												MarkdownDescription: "The type of the parameter. The following types are supported:\n  - number: Either an integer or an IEEE 754 double precision floating point value.\n  - integer: An integer.\n  - string: A character string.\n  - boolean: A boolean.",
											},
											"values": schema.ListAttribute{
												Computed:            true,
												ElementType:         types.StringType,
												MarkdownDescription: "For parameters of type string, the list of valid values.",
											},
										},
									},
								},
								"retention": schema.Int64Attribute{
									Computed:            true,
									MarkdownDescription: "The number of days after which past executions of this job will be deleted. This value override the global job_retention setting.",
								},
								"runner": schema.SingleNestedAttribute{
									Attributes: map[string]schema.Attribute{
										"name": schema.StringAttribute{
											Computed:            true,
											MarkdownDescription: "The name of the runner.",
										},
										"identity": schema.StringAttribute{
											Computed:            true,
											MarkdownDescription: "The name of an identity to use for runners which require authentication. For example the ssh runner needs an identity to initiate an ssh connection.",
										},
									},
									Computed:            true,
									MarkdownDescription: "The specification of the runner used to execute the job.",
								},
								"trigger": schema.SingleNestedAttribute{
									Attributes: map[string]schema.Attribute{
										"event": schema.StringAttribute{
											Computed:            true,
											MarkdownDescription: "The event to react to formatted as <connector>/<event>.",
										},
										"identity": schema.StringAttribute{
											Computed:            true,
											MarkdownDescription: "The name of an identity to use for events which require authentication. For example the github/push event needs an identity to create the GitHub hook used to listen to push events.",
										},
									},
									Computed:            true,
									MarkdownDescription: "The specification of a trigger indicating when to execute the job.",
								},
								"steps": schema.ListNestedAttribute{
									Computed:            true,
									MarkdownDescription: "A list of steps which will be executed sequentially.",
									NestedObject: schema.NestedAttributeObject{
										Attributes: map[string]schema.Attribute{
											"code": schema.StringAttribute{
												Computed:            true,
												MarkdownDescription: "The fragment of code to execute for this step.",
											},
											"command": schema.SingleNestedAttribute{
												Attributes: map[string]schema.Attribute{
													"arguments": schema.ListAttribute{
														Computed:            true,
														ElementType:         types.StringType,
														MarkdownDescription: "The list of arguments to pass to the command.",
													},
													"name": schema.StringAttribute{
														Computed:            true,
														MarkdownDescription: "The name of the command.",
													},
												},
												Computed:            true,
												MarkdownDescription: "The command to execute for this step.",
											},
											"label": schema.StringAttribute{
												Computed:            true,
												MarkdownDescription: "A short description of the step which will be displayed on the web interface.",
											},
											"script": schema.SingleNestedAttribute{
												Attributes: map[string]schema.Attribute{
													"arguments": schema.ListAttribute{
														Computed:            true,
														ElementType:         types.StringType,
														MarkdownDescription: "The list of arguments to pass to the command.",
													},
													"content": schema.StringAttribute{
														Computed:            true,
														MarkdownDescription: "The script file contents.",
													},
													"path": schema.StringAttribute{
														Computed:            true,
														MarkdownDescription: "The path of the script file relative to the job file.",
													},
												},
												Computed:            true,
												MarkdownDescription: "The command to execute for this step.",
											},
										},
									},
								},
							},
							Computed:            true,
							MarkdownDescription: "The specification of the job.",
						},
					},
				},
			},
			"project_id": schema.StringAttribute{
				MarkdownDescription: "The identifier of the project the jobs are part of.",
				Required:            true,
			},
		},
		MarkdownDescription: "Use this data source to retrieve information about existing eventline jobs.",
	}
}

func (d *JobsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
	d.client, _ = req.ProviderData.(*evcli.Client)
}

func (d *JobsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var data JobsDataSourceModel
	resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
	if resp.Diagnostics.HasError() {
		return
	}
	var id ksuid.KSUID
	if err := id.Parse(data.ProjectId.ValueString()); err != nil {
		resp.Diagnostics.AddError("KsuidParse", fmt.Sprintf("Unable to parse project id, got error: %s", err))
		return
	}
	d.client.ProjectId = &id
	jobs, err := d.client.FetchJobs()
	if err != nil {
		resp.Diagnostics.AddError("FetchJobs", fmt.Sprintf("Unable to fetch jobs, got error: %s", err))
		return
	}
	jobList := make([]JobDataSourceModel, len(jobs))
	for i, job := range jobs {
		environment, _ := types.MapValueFrom(ctx, types.StringType, job.Spec.Environment)
		identities, _ := types.SetValueFrom(ctx, types.StringType, job.Spec.Identities)
		jobList[i] = JobDataSourceModel{
			Disabled: types.BoolValue(job.Disabled),
			Id:       types.StringValue(job.Id.String()),
			Spec: JobSpecDataSourceModel{
				Concurrent:  types.BoolValue(job.Spec.Concurrent),
				Description: types.StringValue(job.Spec.Description),
				Environment: environment,
				Identities:  identities,
				Name:        types.StringValue(job.Spec.Name),
				Retention:   types.Int64Value(int64(job.Spec.Retention)),
			},
		}
		jobParameters := make([]ParameterDataSourceModel, len(job.Spec.Parameters))
		for j, parameter := range job.Spec.Parameters {
			values, _ := types.ListValueFrom(ctx, types.StringType, parameter.Values)
			jobParameters[j] = ParameterDataSourceModel{
				Description: types.StringValue(parameter.Description),
				Environment: types.StringValue(parameter.Environment),
				Name:        types.StringValue(parameter.Name),
				Type:        types.StringValue(string(parameter.Type)),
				Values:      values,
			}
		}
		jobList[i].Spec.Parameters = jobParameters
		if job.Spec.Runner != nil {
			jobList[i].Spec.Runner = &RunnerDataSourceModel{
				Name:     types.StringValue(job.Spec.Runner.Name),
				Identity: types.StringValue(job.Spec.Runner.Identity),
			}
		}
		jobSteps := make([]StepDataSourceModel, len(job.Spec.Steps))
		for j, step := range job.Spec.Steps {
			jobSteps[j] = StepDataSourceModel{
				Code:  types.StringValue(step.Code),
				Label: types.StringValue(step.Label),
			}
			if step.Command != nil {
				arguments, _ := types.ListValueFrom(ctx, types.StringType, step.Command.Arguments)
				jobSteps[j].Command = &StepCommandDataSourceModel{
					Arguments: arguments,
					Name:      types.StringValue(step.Command.Name),
				}
			}
			if step.Script != nil {
				arguments, _ := types.ListValueFrom(ctx, types.StringType, step.Script.Arguments)
				jobSteps[j].Script = &StepScriptDataSourceModel{
					Arguments: arguments,
					Content:   types.StringValue(step.Script.Content),
					Path:      types.StringValue(step.Script.Path),
				}
			}
		}
		jobList[i].Spec.Steps = jobSteps
		if job.Spec.Trigger != nil {
			jobList[i].Spec.Trigger = &TriggerDataSourceModel{
				Event:    types.StringValue(job.Spec.Trigger.Event.String()),
				Identity: types.StringValue(job.Spec.Trigger.Identity),
			}
		}
	}
	data.Elements = jobList
	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}