From 6642a66a1df5db5f2f9780905e83ba2c412128f6 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 31 Jul 2023 00:31:43 +0200 Subject: Implemented identities datasource --- internal/provider/identities_data_source.go | 119 ++++++++++++++++++++++++++++ internal/provider/provider.go | 1 + 2 files changed, 120 insertions(+) create mode 100644 internal/provider/identities_data_source.go (limited to 'internal') diff --git a/internal/provider/identities_data_source.go b/internal/provider/identities_data_source.go new file mode 100644 index 0000000..07d8e0e --- /dev/null +++ b/internal/provider/identities_data_source.go @@ -0,0 +1,119 @@ +package provider + +import ( + "context" + "fmt" + + "git.adyxax.org/adyxax/terraform-provider-eventline/external/evcli" + "github.com/exograd/go-daemon/ksuid" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +type IdentitiesDataSource struct { + client *evcli.Client +} + +var _ datasource.DataSource = &IdentitiesDataSource{} // Ensure provider defined types fully satisfy framework interfaces +func NewIdentitiesDataSource() datasource.DataSource { + return &IdentitiesDataSource{} +} + +type IdentitiesDataSourceModel struct { + Elements []IdentityDataSourceModel `tfsdk:"elements"` + ProjectId types.String `tfsdk:"project_id"` +} +type IdentityDataSourceModel struct { + Connector types.String `tfsdk:"connector"` + Id types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + RawData types.String `tfsdk:"data"` + Status types.String `tfsdk:"status"` + Type types.String `tfsdk:"type"` +} + +func (d *IdentitiesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_identities" +} + +func (d *IdentitiesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "elements": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "connector": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The connector used for the identity.", + }, + "data": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The json raw data of the identity.", + Sensitive: true, + }, + "id": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The identifier of the identity.", + }, + "name": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The name of the identity.", + }, + "status": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The status of the identity.", + }, + "type": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The type of the identity.", + }, + }, + }, + MarkdownDescription: "Identities list", + }, + "project_id": schema.StringAttribute{ + MarkdownDescription: "Project id", + Required: true, + }, + }, + MarkdownDescription: "Eventline identities data source", + } +} + +func (d *IdentitiesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + d.client, _ = req.ProviderData.(*evcli.Client) +} + +func (d *IdentitiesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data IdentitiesDataSourceModel + 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 + identities, err := d.client.FetchIdentities() + if err != nil { + resp.Diagnostics.AddError("FetchIdentities", fmt.Sprintf("Unable to fetch identities, got error: %s", err)) + return + } + identityList := make([]IdentityDataSourceModel, len(identities)) + for i, identity := range identities { + identityList[i] = IdentityDataSourceModel{ + Connector: types.StringValue(identity.Connector), + Id: types.StringValue(identity.Id.String()), + Name: types.StringValue(identity.Name), + RawData: types.StringValue(string(identity.RawData)), + Status: types.StringValue(string(identity.Status)), + Type: types.StringValue(identity.Type), + } + } + data.Elements = identityList + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 6f3bb65..8f1808e 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -76,6 +76,7 @@ func (p *Provider) Resources(ctx context.Context) []func() resource.Resource { func (p *Provider) DataSources(ctx context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ + NewIdentitiesDataSource, NewJobsDataSource, NewProjectsDataSource, } -- cgit v1.2.3