diff options
author | Julien Dessaux | 2023-07-31 00:31:43 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-31 00:31:43 +0200 |
commit | 6642a66a1df5db5f2f9780905e83ba2c412128f6 (patch) | |
tree | 35d04ade200db806152bc80cd6cf33ed6bf23052 | |
parent | Upgrade notes (diff) | |
download | terraform-provider-eventline-6642a66a1df5db5f2f9780905e83ba2c412128f6.tar.gz terraform-provider-eventline-6642a66a1df5db5f2f9780905e83ba2c412128f6.tar.bz2 terraform-provider-eventline-6642a66a1df5db5f2f9780905e83ba2c412128f6.zip |
Implemented identities datasourcev0.0.5
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | docs/data-sources/identities.md | 36 | ||||
-rw-r--r-- | external/evcli/client.go | 28 | ||||
-rw-r--r-- | external/evcli/identities.go | 73 | ||||
-rw-r--r-- | internal/provider/identities_data_source.go | 119 | ||||
-rw-r--r-- | internal/provider/provider.go | 1 |
6 files changed, 262 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index c5a4d5f..a518c28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.0.5 - 2023-07-31 + +### Features +* Add identities datasource + ## 0.0.4 - 2023-06-25 ### Notes diff --git a/docs/data-sources/identities.md b/docs/data-sources/identities.md new file mode 100644 index 0000000..57416ff --- /dev/null +++ b/docs/data-sources/identities.md @@ -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. diff --git a/external/evcli/client.go b/external/evcli/client.go index b9d5032..ce3d4a0 100644 --- a/external/evcli/client.go +++ b/external/evcli/client.go @@ -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 diff --git a/external/evcli/identities.go b/external/evcli/identities.go new file mode 100644 index 0000000..f89ae1b --- /dev/null +++ b/external/evcli/identities.go @@ -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 +} 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, } |