Implemented identities datasource

This commit is contained in:
Julien Dessaux 2023-07-31 00:31:43 +02:00
parent ec8e96003f
commit 6642a66a1d
Signed by: adyxax
GPG key ID: F92E51B86E07177E
6 changed files with 262 additions and 0 deletions

View file

@ -1,3 +1,8 @@
## 0.0.5 - 2023-07-31
### Features
* Add identities datasource
## 0.0.4 - 2023-06-25
### Notes

View file

@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "eventline_identities Data Source - terraform-provider-eventline"
subcategory: ""
description: |-
Eventline identities data source
---
# eventline_identities (Data Source)
Eventline identities data source
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `project_id` (String) Project id
### Read-Only
- `elements` (Attributes List) Identities list (see [below for nested schema](#nestedatt--elements))
<a id="nestedatt--elements"></a>
### Nested Schema for `elements`
Read-Only:
- `connector` (String) The connector used for the identity.
- `data` (String, Sensitive) The json raw data of the identity.
- `id` (String) The identifier of the identity.
- `name` (String) The name of the identity.
- `status` (String) The status of the identity.
- `type` (String) The type of the identity.

View file

@ -181,6 +181,34 @@ func (c *Client) UpdateProject(project *eventline.Project) error {
return c.SendRequest("PUT", uri, project, nil)
}
func (c *Client) FetchIdentities() (Identities, error) {
var identities Identities
cursor := eventline.Cursor{Size: 20}
for {
var page IdentityPage
uri := NewURL("identities")
uri.RawQuery = cursor.Query().Encode()
err := c.SendRequest("GET", uri, nil, &page)
if err != nil {
return nil, err
}
identities = append(identities, page.Elements...)
if page.Next == nil {
break
}
cursor = *page.Next
}
return identities, nil
}
func (c *Client) ReplayEvent(id string) (*eventline.Event, error) {
var event eventline.Event

73
external/evcli/identities.go vendored Normal file
View file

@ -0,0 +1,73 @@
package evcli
import (
"encoding/json"
"fmt"
"time"
"github.com/exograd/eventline/pkg/eventline"
"github.com/exograd/eventline/pkg/utils"
)
type IdentityPage struct {
Elements Identities `json:"elements"`
Previous *eventline.Cursor `json:"previous,omitempty"`
Next *eventline.Cursor `json:"next,omitempty"`
}
type Identity struct {
Id eventline.Id `json:"id"`
ProjectId *eventline.Id `json:"project_id"`
Name string `json:"name"`
Status eventline.IdentityStatus `json:"status"`
ErrorMessage string `json:"error_message,omitempty"`
CreationTime time.Time `json:"creation_time"`
UpdateTime time.Time `json:"update_time"`
LastUseTime *time.Time `json:"last_use_time,omitempty"`
RefreshTime *time.Time `json:"refresh_time,omitempty"`
Connector string `json:"connector"`
Type string `json:"type"`
Data eventline.IdentityData `json:"-"`
RawData json.RawMessage `json:"data"`
}
type Identities []*Identity
func (i *Identity) SortKey(sort string) (key string) {
switch sort {
case "id":
key = i.Id.String()
case "name":
key = i.Name
default:
utils.Panicf("unknown identity sort %q", sort)
}
return
}
func (pi *Identity) MarshalJSON() ([]byte, error) {
type Identity2 Identity
i := Identity2(*pi)
data, err := json.Marshal(i.Data)
if err != nil {
return nil, fmt.Errorf("cannot encode data: %w", err)
}
i.RawData = data
return json.Marshal(i)
}
func (pi *Identity) UnmarshalJSON(data []byte) error {
type Identity2 Identity
i := Identity2(*pi)
if err := json.Unmarshal(data, &i); err != nil {
return err
}
*pi = Identity(i)
return nil
}

View file

@ -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)...)
}

View file

@ -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,
}