Skip to content

Commit

Permalink
Merge pull request #51 from infra-monkey/fix/datasource
Browse files Browse the repository at this point in the history
fix: datasource must fail during plan is resource is absent
  • Loading branch information
RomanButsiy authored Feb 5, 2025
2 parents a109001 + 95406c1 commit c17282c
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 71 deletions.
10 changes: 2 additions & 8 deletions freeipa/dns_record_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -108,13 +107,8 @@ func (r *dnsRecordDataSource) Read(ctx context.Context, req datasource.ReadReque
res, err := r.client.DnsrecordShow(&reqArgs, &optArgs)
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa dns record %s: %s", data.RecordName.ValueString(), res.String()))
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("DNS record %s not found", data.RecordName.ValueString()))
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa DNS zone: %s", err))
return
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}
if res != nil {
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa dns zone: %s", res.Result.String()))
Expand Down
11 changes: 2 additions & 9 deletions freeipa/dns_zone_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -203,14 +202,8 @@ func (r *dnsZoneDataSource) Read(ctx context.Context, req datasource.ReadRequest
res, err := r.client.DnszoneShow(&ipa.DnszoneShowArgs{}, &optArgs)
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa dns zone %s: %s", data.ZoneName.ValueString(), res.String()))
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("DNS zone %s not found", data.ZoneName.ValueString()))
resp.State.RemoveResource(ctx)
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa DNS zone: %s", err))
return
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}

if res != nil {
Expand Down
9 changes: 2 additions & 7 deletions freeipa/group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -175,12 +174,8 @@ func (r *UserGroupDataSource) Read(ctx context.Context, req datasource.ReadReque
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa group %s", data.Name.ValueString()))
res, err := r.client.GroupShow(&args, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Group %s not found", data.Name.ValueString()))
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("[DEBUG] Group %s not found: %s", data.Name.ValueString(), err))
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa group %v", res))
if res != nil {
Expand Down
10 changes: 2 additions & 8 deletions freeipa/hbac_policy_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -158,13 +157,8 @@ func (r *HbacPolicyDataSource) Read(ctx context.Context, req datasource.ReadRequ

res, err := r.client.HbacruleShow(&args, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa hbac policy: %s", err))
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa hbac policy: %s", err))
return
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}

if res.Result.Description != nil {
Expand Down
10 changes: 2 additions & 8 deletions freeipa/host_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -208,13 +207,8 @@ func (r *HostDataSource) Read(ctx context.Context, req datasource.ReadRequest, r

res, err := r.client.HostShow(&args, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
resp.Diagnostics.AddError("Client Error", "[DEBUG] Host not found")
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa host: %s", err))
return
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}
if res != nil {
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa host %s", res.Result.String()))
Expand Down
9 changes: 2 additions & 7 deletions freeipa/hostgroup_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -164,12 +163,8 @@ func (r *HostGroupDataSource) Read(ctx context.Context, req datasource.ReadReque
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa hostgroup %s", data.Name.ValueString()))
res, err := r.client.HostgroupShow(&args, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Hostgroup %s not found", data.Name.ValueString()))
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("[DEBUG] Hostgroup %s not found: %s", data.Name.ValueString(), err))
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa hostgroup %v", res))
if res != nil {
Expand Down
10 changes: 2 additions & 8 deletions freeipa/sudo_cmdgroup_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -107,13 +106,8 @@ func (r *SudoCmdGroupDataSource) Read(ctx context.Context, req datasource.ReadRe

res, err := r.client.SudocmdgroupShow(&args, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
resp.Diagnostics.AddError("Client Error", "[DEBUG] Sudo command group not found")
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa sudo command group: %s", err))
return
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}
if res != nil {
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] Read freeipa sudo command group %s", res.Result.String()))
Expand Down
10 changes: 2 additions & 8 deletions freeipa/sudo_rule_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package freeipa
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -203,13 +202,8 @@ func (r *SudoRuleDataSource) Read(ctx context.Context, req datasource.ReadReques

res, err := r.client.SudoruleShow(&args, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa sudo rule: %s", err))
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading freeipa sudo rule: %s", err))
return
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}

if res.Result.Description != nil {
Expand Down
10 changes: 2 additions & 8 deletions freeipa/user_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ package freeipa
import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
ipa "github.com/infra-monkey/go-freeipa/freeipa"
)

Expand Down Expand Up @@ -306,12 +304,8 @@ func (r *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r

res, err := r.client.UserShow(&ipa.UserShowArgs{}, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
tflog.Debug(ctx, fmt.Sprintf("[DEBUG] User %s not found", data.UID.ValueString()))
return
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error reading user %s: %s", data.UID.ValueString(), err))
}
resp.Diagnostics.AddError("Client Error", err.Error())
return
}

if res.Result.Cn != nil {
Expand Down

0 comments on commit c17282c

Please sign in to comment.