Skip to content

Commit

Permalink
feat(alert): implement ImportState function for Alert resource
Browse files Browse the repository at this point in the history
Signed-off-by: Prashant Shahi <[email protected]>
  • Loading branch information
prashant-shahi committed Jul 8, 2024
1 parent dd6b9d6 commit 58e9514
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 41 deletions.
18 changes: 18 additions & 0 deletions examples/resources/existing-alert/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
signoz = {
source = "registry.terraform.io/signoz/signoz"
}
}
}

provider "signoz" {
endpoint = "http://localhost:3301"
# access_token = "ACCESS_TOKEN"
}

resource "signoz_alert" "existing_alert" { }

output "existing_alert" {
value = signoz_alert.existing_alert
}
45 changes: 23 additions & 22 deletions signoz/internal/provider/datasource/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ type alertModel struct {
Version types.String `tfsdk:"version"`
}

// Configure adds the provider configured client to the data source.
func (d *alertDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*client.Client)
if !ok {
addErr(
&resp.Diagnostics,
fmt.Errorf("unexpected data source configure type. Expected *client.Client, got: %T. "+
"Please report this issue to the provider developers", req.ProviderData),
SigNozAlert,
)

return
}

d.client = client
}

// Metadata returns the data source type name.
func (d *alertDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = SigNozAlert
Expand Down Expand Up @@ -185,25 +208,3 @@ func (d *alertDataSource) Read(ctx context.Context, req datasource.ReadRequest,
// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

// Configure adds the provider configured client to the data source.
func (d *alertDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*client.Client)
if !ok {
addErr(
&resp.Diagnostics,
fmt.Errorf("unexpected data source configure type. Expected *client.Client, got: %T. Please report this issue to the provider developers", req.ProviderData),
SigNozAlert,
)

return
}

d.client = client
}
48 changes: 29 additions & 19 deletions signoz/internal/provider/resource/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/SigNoz/terraform-provider-signoz/signoz/internal/model"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
Expand All @@ -22,7 +23,9 @@ import (

// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &alertResource{}
_ resource.Resource = &alertResource{}
_ resource.ResourceWithConfigure = &alertResource{}
_ resource.ResourceWithImportState = &alertResource{}
)

// NewAlertResource is a helper function to simplify the provider implementation.
Expand Down Expand Up @@ -60,6 +63,27 @@ type alertResourceModel struct {
UpdateBy types.String `tfsdk:"update_by"`
}

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

client, ok := req.ProviderData.(*client.Client)
if !ok {
addErr(
&resp.Diagnostics,
fmt.Errorf("unexpected data source configure type. Expected *client.Client, got: %T. "+
"Please report this issue to the provider developers", req.ProviderData),
SigNozAlert,
)

return
}

r.client = client
}

// Metadata returns the resource type name.
func (r *alertResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = SigNozAlert
Expand Down Expand Up @@ -439,22 +463,8 @@ func (r *alertResource) Delete(ctx context.Context, req resource.DeleteRequest,
}
}

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

client, ok := req.ProviderData.(*client.Client)
if !ok {
addErr(
&resp.Diagnostics,
fmt.Errorf("unexpected data source configure type. Expected *client.Client, got: %T. Please report this issue to the provider developers", req.ProviderData),
SigNozAlert,
)

return
}

r.client = client
// ImportState imports Terraform state into the resource.
func (r *alertResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

0 comments on commit 58e9514

Please sign in to comment.