Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add data sources #88

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions internal/provider/connection/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package connection

import (
"context"
"fmt"
schemaHelpers "terraform-provider-hookdeck/internal/schemahelpers"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
hookdeckClient "github.com/hookdeck/hookdeck-go-sdk/client"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &connectionDataSource{}
_ datasource.DataSourceWithConfigure = &connectionDataSource{}
)

// NewConnectionDataSource is a helper function to simplify the provider implementation.
func NewConnectionDataSource() datasource.DataSource {
return &connectionDataSource{}
}

// connectionDataSource is the datasource implementation.
type connectionDataSource struct {
client hookdeckClient.Client
}

// Metadata returns the datasource type name.
func (r *connectionDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_connection"
}

// Schema returns the data source schema.
func (r *connectionDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Connection Data Source",
Attributes: schemaHelpers.DataSourceSchemaFromResourceSchema(schemaAttributes(), "id"),
}
}

// Configure adds the provider configured client to the datasource.
func (r *connectionDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*hookdeckClient.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *hookdeckClient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.client = *client
}

// Read refreshes the Terraform state with the latest data.
func (r *connectionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Get data from Terraform state
var data *connectionResourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

// Get refreshed datasource value
connection, err := r.client.Connection.Retrieve(context.Background(), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error reading connection", err.Error())
return
}

// Save refreshed data into Terraform state
data.Refresh(connection)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
9 changes: 9 additions & 0 deletions internal/provider/connection/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
hookdeckClient "github.com/hookdeck/hookdeck-go-sdk/client"
)

Expand All @@ -31,6 +32,14 @@ func (r *connectionResource) Metadata(_ context.Context, req resource.MetadataRe
resp.TypeName = req.ProviderTypeName + "_connection"
}

// Schema returns the resource schema.
func (r *connectionResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Connection Resource",
Attributes: schemaAttributes(),
}
}

// Configure adds the provider configured client to the resource.
func (r *connectionResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
Expand Down
241 changes: 117 additions & 124 deletions internal/provider/connection/schema.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package connection

import (
"context"

"terraform-provider-hookdeck/internal/validators"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func (r *connectionResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
func schemaAttributes() map[string]schema.Attribute {
filterRulePropertySchema := schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
Expand All @@ -36,148 +33,144 @@ func (r *connectionResource) Schema(_ context.Context, _ resource.SchemaRequest,
},
}

resp.Schema = schema.Schema{
MarkdownDescription: "Connection Resource",

Attributes: map[string]schema.Attribute{
"created_at": schema.StringAttribute{
Computed: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: "Date the connection was created",
return map[string]schema.Attribute{
"created_at": schema.StringAttribute{
Computed: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
"description": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: "Description for the connection",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
"destination_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Description: "ID of a destination to bind to the connection",
Description: "Date the connection was created",
},
"description": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
"disabled_at": schema.StringAttribute{
Computed: true,
Optional: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
Description: "Date the connection was disabled",
Description: "Description for the connection",
},
"destination_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
"id": schema.StringAttribute{
Computed: true,
Description: `ID of the connection`,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: "ID of a destination to bind to the connection",
},
"disabled_at": schema.StringAttribute{
Computed: true,
Optional: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
"name": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: `A unique, human-friendly name for the connection`,
Description: "Date the connection was disabled",
},
"id": schema.StringAttribute{
Computed: true,
Description: `ID of the connection`,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
"paused_at": schema.StringAttribute{
Computed: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
Description: "Date the connection was paused",
},
"name": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
"rules": schema.SetNestedAttribute{
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"delay_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"delay": schema.Int64Attribute{
Required: true,
Description: `Delay to introduce in MS`,
},
Description: `A unique, human-friendly name for the connection`,
},
"paused_at": schema.StringAttribute{
Computed: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
Description: "Date the connection was paused",
},
"rules": schema.SetNestedAttribute{
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"delay_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"delay": schema.Int64Attribute{
Required: true,
Description: `Delay to introduce in MS`,
},
},
"filter_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"body": filterRulePropertySchema,
"headers": filterRulePropertySchema,
"path": filterRulePropertySchema,
"query": filterRulePropertySchema,
},
"filter_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"body": filterRulePropertySchema,
"headers": filterRulePropertySchema,
"path": filterRulePropertySchema,
"query": filterRulePropertySchema,
},
Validators: []validator.Object{
validators.AtLeastOneChild(),
},
},
"retry_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"count": schema.Int64Attribute{
Optional: true,
Description: `Maximum number of retries to attempt`,
},
Validators: []validator.Object{
validators.AtLeastOneChild(),
"interval": schema.Int64Attribute{
Optional: true,
Description: `Time in MS between each retry`,
},
},
"retry_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"count": schema.Int64Attribute{
Optional: true,
Description: `Maximum number of retries to attempt`,
},
"interval": schema.Int64Attribute{
Optional: true,
Description: `Time in MS between each retry`,
},
"strategy": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(
"linear",
"exponential",
),
},
MarkdownDescription: `must be one of ["linear", "exponential"]` + "\n" +
`Algorithm to use when calculating delay between retries`,
"strategy": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(
"linear",
"exponential",
),
},
MarkdownDescription: `must be one of ["linear", "exponential"]` + "\n" +
`Algorithm to use when calculating delay between retries`,
},
},
"transform_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"transformation_id": schema.StringAttribute{
Required: true,
Description: `ID of the attached transformation object.`,
},
},
"transform_rule": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"transformation_id": schema.StringAttribute{
Required: true,
Description: `ID of the attached transformation object.`,
},
},
},
Validators: []validator.Object{
validators.ExactlyOneChild(),
},
},
},
"source_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
Validators: []validator.Object{
validators.ExactlyOneChild(),
},
Description: `ID of a source to bind to the connection`,
},
"team_id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: `ID of the workspace`,
},
"source_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
"updated_at": schema.StringAttribute{
Computed: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
Description: `Date the connection was last updated`,
Description: `ID of a source to bind to the connection`,
},
"team_id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: `ID of the workspace`,
},
"updated_at": schema.StringAttribute{
Computed: true,
Validators: []validator.String{
validators.IsRFC3339(),
},
Description: `Date the connection was last updated`,
},
}
}
Loading
Loading