aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2023-08-01 00:32:23 +0200
committerJulien Dessaux2023-08-01 00:32:23 +0200
commit269963bc8f527d4760b48b44b296e874855c2a1c (patch)
treef4fa8c6eab195d4f43e4f6eaf3434ce781c40072
parentImplemented identities datasource (diff)
downloadterraform-provider-eventline-269963bc8f527d4760b48b44b296e874855c2a1c.tar.gz
terraform-provider-eventline-269963bc8f527d4760b48b44b296e874855c2a1c.tar.bz2
terraform-provider-eventline-269963bc8f527d4760b48b44b296e874855c2a1c.zip
Implemented project data source
-rw-r--r--docs/data-sources/project.md30
-rw-r--r--examples/data-sources/eventline_project/data-source.tf3
-rw-r--r--internal/provider/project_data_source.go59
-rw-r--r--internal/provider/provider.go1
4 files changed, 93 insertions, 0 deletions
diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md
new file mode 100644
index 0000000..bf0f077
--- /dev/null
+++ b/docs/data-sources/project.md
@@ -0,0 +1,30 @@
+---
+# generated by https://github.com/hashicorp/terraform-plugin-docs
+page_title: "eventline_project Data Source - terraform-provider-eventline"
+subcategory: ""
+description: |-
+ Use this data source to retrieve information about an existing eventline project from its name.
+---
+
+# eventline_project (Data Source)
+
+Use this data source to retrieve information about an existing eventline project from its name.
+
+## Example Usage
+
+```terraform
+data "project" "main" {
+ name = "main"
+}
+```
+
+<!-- schema generated by tfplugindocs -->
+## Schema
+
+### Required
+
+- `name` (String) The name of the project.
+
+### Read-Only
+
+- `id` (String) The identifier of the project.
diff --git a/examples/data-sources/eventline_project/data-source.tf b/examples/data-sources/eventline_project/data-source.tf
new file mode 100644
index 0000000..c0714e1
--- /dev/null
+++ b/examples/data-sources/eventline_project/data-source.tf
@@ -0,0 +1,3 @@
+data "project" "main" {
+ name = "main"
+}
diff --git a/internal/provider/project_data_source.go b/internal/provider/project_data_source.go
new file mode 100644
index 0000000..444eb51
--- /dev/null
+++ b/internal/provider/project_data_source.go
@@ -0,0 +1,59 @@
+package provider
+
+import (
+ "context"
+ "fmt"
+
+ "git.adyxax.org/adyxax/terraform-provider-eventline/external/evcli"
+ "github.com/hashicorp/terraform-plugin-framework/datasource"
+ "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
+ "github.com/hashicorp/terraform-plugin-framework/types"
+)
+
+type ProjectDataSource struct {
+ client *evcli.Client
+}
+
+var _ datasource.DataSource = &ProjectDataSource{} // Ensure provider defined types fully satisfy framework interfaces
+func NewProjectDataSource() datasource.DataSource {
+ return &ProjectDataSource{}
+}
+
+func (d *ProjectDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
+ resp.TypeName = req.ProviderTypeName + "_project"
+}
+
+func (d *ProjectDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
+ resp.Schema = schema.Schema{
+ Attributes: map[string]schema.Attribute{
+ "id": schema.StringAttribute{
+ Computed: true,
+ MarkdownDescription: "The identifier of the project.",
+ },
+ "name": schema.StringAttribute{
+ MarkdownDescription: "The name of the project.",
+ Required: true,
+ },
+ },
+ MarkdownDescription: "Use this data source to retrieve information about an existing eventline project from its name.",
+ }
+}
+
+func (d *ProjectDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
+ d.client, _ = req.ProviderData.(*evcli.Client)
+}
+
+func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
+ var data ProjectDataSourceModel
+ resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
+ if resp.Diagnostics.HasError() {
+ return
+ }
+ project, err := d.client.FetchProjectByName(data.Name.ValueString())
+ if err != nil {
+ resp.Diagnostics.AddError("FetchProjectByName", fmt.Sprintf("Unable to fetch project, got error: %s", err))
+ return
+ }
+ data = ProjectDataSourceModel{Id: types.StringValue(project.Id.String()), Name: types.StringValue(project.Name)}
+ resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
+}
diff --git a/internal/provider/provider.go b/internal/provider/provider.go
index 8f1808e..5bc5d73 100644
--- a/internal/provider/provider.go
+++ b/internal/provider/provider.go
@@ -78,6 +78,7 @@ func (p *Provider) DataSources(ctx context.Context) []func() datasource.DataSour
return []func() datasource.DataSource{
NewIdentitiesDataSource,
NewJobsDataSource,
+ NewProjectDataSource,
NewProjectsDataSource,
}
}