Implemented projects datasource
This commit is contained in:
parent
ed756785c6
commit
3d60272592
2 changed files with 155 additions and 0 deletions
75
internal/provider/projects_data_source.go
Normal file
75
internal/provider/projects_data_source.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.adyxax.org/adyxax/terraform-eventline/internal/evcli"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
)
|
||||
|
||||
type projectsDataSource struct {
|
||||
client *evcli.Client
|
||||
}
|
||||
|
||||
var _ datasource.DataSource = &projectsDataSource{} // Ensure provider defined types fully satisfy framework interfaces.
|
||||
func NewProjectsDataSource() datasource.DataSource {
|
||||
return &projectsDataSource{}
|
||||
}
|
||||
|
||||
type ProjectsModel struct {
|
||||
Elements []ProjectModel `tfsdk:"elements"`
|
||||
}
|
||||
type ProjectModel struct {
|
||||
Id types.String `tfsdk:"id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
}
|
||||
|
||||
func (d *projectsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_projects"
|
||||
}
|
||||
|
||||
func (d *projectsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Eventline projects data source",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"elements": schema.ListAttribute{
|
||||
Computed: true,
|
||||
ElementType: types.ObjectType{
|
||||
AttrTypes: map[string]attr.Type{
|
||||
"id": types.StringType,
|
||||
"name": types.StringType,
|
||||
},
|
||||
},
|
||||
MarkdownDescription: "Projects list",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *projectsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
d.client, _ = req.ProviderData.(*evcli.Client)
|
||||
}
|
||||
|
||||
func (d *projectsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data ProjectsModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
projects, err := d.client.FetchProjects()
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("FetchProjects", fmt.Sprintf("Unable to fetch projects, got error: %s", err))
|
||||
return
|
||||
}
|
||||
projectList := make([]ProjectModel, len(projects))
|
||||
for i, project := range projects {
|
||||
projectList[i] = ProjectModel{Id: basetypes.NewStringValue(project.Id.String()), Name: basetypes.NewStringValue(project.Name)}
|
||||
}
|
||||
data.Elements = projectList
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
80
internal/provider/provider.go
Normal file
80
internal/provider/provider.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.adyxax.org/adyxax/terraform-eventline/internal/evcli"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/provider"
|
||||
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
version string
|
||||
}
|
||||
|
||||
var _ provider.Provider = &Provider{} // Ensure provider defined types fully satisfy framework interfaces.
|
||||
func New(version string) func() provider.Provider {
|
||||
return func() provider.Provider {
|
||||
return &Provider{
|
||||
version: version,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ProviderModel struct {
|
||||
ApiKey types.String `tfsdk:"api_key"`
|
||||
Endpoint types.String `tfsdk:"endpoint"`
|
||||
}
|
||||
|
||||
func (p *Provider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
||||
resp.TypeName = "eventline"
|
||||
resp.Version = p.version
|
||||
}
|
||||
|
||||
func (p *Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"api_key": schema.StringAttribute{
|
||||
MarkdownDescription: "Eventline's api key",
|
||||
Required: true,
|
||||
Sensitive: true,
|
||||
},
|
||||
"endpoint": schema.StringAttribute{
|
||||
MarkdownDescription: "Eventline's HTTP endpoint",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
||||
var data ProviderModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
config := evcli.Config{API: evcli.APIConfig{Endpoint: data.Endpoint.ValueString()}}
|
||||
client, err := evcli.NewClient(&config)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("new api client", fmt.Sprintf("Unable to instanciate eventline api client, got error: %s", err))
|
||||
return
|
||||
}
|
||||
client.APIKey = data.ApiKey.ValueString()
|
||||
|
||||
resp.DataSourceData = client
|
||||
resp.ResourceData = client
|
||||
}
|
||||
|
||||
func (p *Provider) Resources(ctx context.Context) []func() resource.Resource {
|
||||
return []func() resource.Resource{}
|
||||
}
|
||||
|
||||
func (p *Provider) DataSources(ctx context.Context) []func() datasource.DataSource {
|
||||
return []func() datasource.DataSource{
|
||||
NewProjectsDataSource,
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue