Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
feat: Update data sources and resources to use GetConn helper to lazi…
Browse files Browse the repository at this point in the history
…ly establish an LDAP connection during Create/Read/Update/Destroy operations
  • Loading branch information
zanecodes committed Dec 7, 2023
1 parent 33056a1 commit 547696a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 32 deletions.
16 changes: 11 additions & 5 deletions internal/provider/ldap_object_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewLDAPObjectDataSource() datasource.DataSource {
}

type LDAPObjectDataSource struct {
conn *ldap.Conn
data LDAPProviderModel
}

type LDAPObjectDatasourceModel struct {
Expand Down Expand Up @@ -69,15 +69,15 @@ func (L *LDAPObjectDataSource) Configure(_ context.Context, request datasource.C
return
}

if conn, ok := request.ProviderData.(*ldap.Conn); !ok {
if data, ok := request.ProviderData.(LDAPProviderModel); !ok {
response.Diagnostics.AddError(
"Unexpected Datasource Configure Type",
fmt.Sprintf("Expected *ldap.Conn, got: %T. Please report this issue to the provider developers.", request.ProviderData),
fmt.Sprintf("Expected LDAPProviderModel, got: %T. Please report this issue to the provider developers.", request.ProviderData),
)

return
} else {
L.conn = conn
L.data = data
}
}

Expand All @@ -87,6 +87,12 @@ func (L *LDAPObjectDataSource) Read(ctx context.Context, request datasource.Read
var attributes map[string][]string
response.Diagnostics.Append(data.Attributes.ElementsAs(ctx, &attributes, false)...)
attributes = make(map[string][]string)
var conn *ldap.Conn

Check failure on line 90 in internal/provider/ldap_object_datasource.go

View workflow job for this annotation

GitHub Actions / Build

S1021: should merge variable declaration with assignment on next line (gosimple)
conn = GetConn(L.data, response.Diagnostics)

if conn == nil {
return
}

var objectClasses []string
response.Diagnostics.Append(data.ObjectClasses.ElementsAs(ctx, &objectClasses, false)...)
Expand All @@ -100,7 +106,7 @@ func (L *LDAPObjectDataSource) Read(ctx context.Context, request datasource.Read
var additionalAttributes []string
response.Diagnostics.Append(data.AdditionalAttributes.ElementsAs(ctx, &additionalAttributes, false)...)

if entry, err := GetEntry(L.conn, data.DN.ValueString(), append(additionalAttributes, "*")...); err != nil {
if entry, err := GetEntry(conn, data.DN.ValueString(), append(additionalAttributes, "*")...); err != nil {
response.Diagnostics.AddError(
"Can not read entry",
err.Error(),
Expand Down
61 changes: 48 additions & 13 deletions internal/provider/ldap_object_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewLDAPObjectResource() resource.Resource {
}

type LDAPObjectResource struct {
conn *ldap.Conn
data LDAPProviderModel
}

type LDAPObjectResourceModel struct {
Expand Down Expand Up @@ -79,15 +79,15 @@ func (L *LDAPObjectResource) Configure(_ context.Context, request resource.Confi
return
}

if conn, ok := request.ProviderData.(*ldap.Conn); !ok {
if data, ok := request.ProviderData.(LDAPProviderModel); !ok {
response.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *ldap.Conn, got: %T. Please report this issue to the provider developers.", request.ProviderData),
fmt.Sprintf("Expected LDAPProviderModel, got: %T. Please report this issue to the provider developers.", request.ProviderData),
)

return
} else {
L.conn = conn
L.data = data
}
}

Expand All @@ -98,7 +98,14 @@ func (L *LDAPObjectResource) Create(ctx context.Context, request resource.Create
return
}

if err := L.addLdapEntry(ctx, data, &response.Diagnostics); err != nil {
var conn *ldap.Conn

Check failure on line 101 in internal/provider/ldap_object_resource.go

View workflow job for this annotation

GitHub Actions / Build

S1021: should merge variable declaration with assignment on next line (gosimple)
conn = GetConn(L.data, response.Diagnostics)

if conn == nil {
return
}

if err := L.addLdapEntry(ctx, data, &response.Diagnostics, conn); err != nil {
response.Diagnostics.AddError(
"Can not add resource",
fmt.Sprintf("LDAP server reported: %s", err),
Expand All @@ -116,7 +123,14 @@ func (L *LDAPObjectResource) Read(ctx context.Context, request resource.ReadRequ
return
}

if entry, err := GetEntry(L.conn, data.DN.ValueString()); err != nil {
var conn *ldap.Conn

Check failure on line 126 in internal/provider/ldap_object_resource.go

View workflow job for this annotation

GitHub Actions / Build

S1021: should merge variable declaration with assignment on next line (gosimple)
conn = GetConn(L.data, response.Diagnostics)

if conn == nil {
return
}

if entry, err := GetEntry(conn, data.DN.ValueString()); err != nil {
response.Diagnostics.AddError(
"Can not read entry",
err.Error(),
Expand All @@ -143,16 +157,23 @@ func (L *LDAPObjectResource) Update(ctx context.Context, request resource.Update
return
}

var conn *ldap.Conn

Check failure on line 160 in internal/provider/ldap_object_resource.go

View workflow job for this annotation

GitHub Actions / Build

S1021: should merge variable declaration with assignment on next line (gosimple)
conn = GetConn(L.data, response.Diagnostics)

if conn == nil {
return
}

// Recreate object if DN changed
if stateData.DN.ValueString() != planData.DN.ValueString() {
if err := L.conn.Del(ldap.NewDelRequest(stateData.DN.ValueString(), []ldap.Control{})); err != nil {
if err := conn.Del(ldap.NewDelRequest(stateData.DN.ValueString(), []ldap.Control{})); err != nil {
response.Diagnostics.AddError(
"Can not delete old DN entry",
fmt.Sprintf("Trying to delete entry of old DN returned: %s", err),
)
return
}
if err := L.addLdapEntry(ctx, planData, &response.Diagnostics); err != nil {
if err := L.addLdapEntry(ctx, planData, &response.Diagnostics, conn); err != nil {
response.Diagnostics.AddError(
"Can not add resource",
fmt.Sprintf("LDAP server reported: %s", err),
Expand Down Expand Up @@ -196,7 +217,7 @@ func (L *LDAPObjectResource) Update(ctx context.Context, request resource.Update
r.Add(attributeType, values)
}
}
if err := L.conn.Modify(r); err != nil {
if err := conn.Modify(r); err != nil {
response.Diagnostics.AddError(
"Can not modify entry",
fmt.Sprintf("LDAP server reported: %s", err),
Expand All @@ -215,7 +236,14 @@ func (L *LDAPObjectResource) Delete(ctx context.Context, request resource.Delete
return
}

if err := L.conn.Del(ldap.NewDelRequest(stateData.DN.ValueString(), []ldap.Control{})); err != nil {
var conn *ldap.Conn

Check failure on line 239 in internal/provider/ldap_object_resource.go

View workflow job for this annotation

GitHub Actions / Build

S1021: should merge variable declaration with assignment on next line (gosimple)
conn = GetConn(L.data, response.Diagnostics)

if conn == nil {
return
}

if err := conn.Del(ldap.NewDelRequest(stateData.DN.ValueString(), []ldap.Control{})); err != nil {
response.Diagnostics.AddError(
"Can not delete entry",
fmt.Sprintf("Trying to delete entry returned: %s", err),
Expand All @@ -225,7 +253,14 @@ func (L *LDAPObjectResource) Delete(ctx context.Context, request resource.Delete
}

func (L *LDAPObjectResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
if entry, err := GetEntry(L.conn, request.ID); err != nil {
var conn *ldap.Conn

Check failure on line 256 in internal/provider/ldap_object_resource.go

View workflow job for this annotation

GitHub Actions / Build

S1021: should merge variable declaration with assignment on next line (gosimple)
conn = GetConn(L.data, response.Diagnostics)

if conn == nil {
return
}

if entry, err := GetEntry(conn, request.ID); err != nil {
response.Diagnostics.AddError(
"Can not read entry",
err.Error(),
Expand Down Expand Up @@ -276,7 +311,7 @@ func (L *LDAPObjectResource) ModifyPlan(ctx context.Context, request resource.Mo
}
}

func (L *LDAPObjectResource) addLdapEntry(ctx context.Context, data *LDAPObjectResourceModel, diagnostics *diag.Diagnostics) error {
func (L *LDAPObjectResource) addLdapEntry(ctx context.Context, data *LDAPObjectResourceModel, diagnostics *diag.Diagnostics, conn *ldap.Conn) error {
var objectClasses []string
diagnostics.Append(data.ObjectClasses.ElementsAs(ctx, &objectClasses, false)...)
if diagnostics.HasError() {
Expand All @@ -296,7 +331,7 @@ func (L *LDAPObjectResource) addLdapEntry(ctx context.Context, data *LDAPObjectR
a.Attribute(attributeType, values)
}

return L.conn.Add(a)
return conn.Add(a)
}

func (L *LDAPObjectResource) isIgnored(ctx context.Context, attributeType string, data *LDAPObjectResourceModel, diagnostics diag.Diagnostics) bool {
Expand Down
16 changes: 11 additions & 5 deletions internal/provider/ldap_search_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewLDAPSearchDataSource() datasource.DataSource {
}

type LDAPSearchDataSource struct {
conn *ldap.Conn
data LDAPProviderModel
}

type LDAPSearchDatasourceModel struct {
Expand Down Expand Up @@ -80,15 +80,15 @@ func (L *LDAPSearchDataSource) Configure(_ context.Context, request datasource.C
return
}

if conn, ok := request.ProviderData.(*ldap.Conn); !ok {
if data, ok := request.ProviderData.(LDAPProviderModel); !ok {
response.Diagnostics.AddError(
"Unexpected Datasource Configure Type",
fmt.Sprintf("Expected *ldap.Conn, got: %T. Please report this issue to the provider developers.", request.ProviderData),
fmt.Sprintf("Expected LDAPProviderModel, got: %T. Please report this issue to the provider developers.", request.ProviderData),
)

return
} else {
L.conn = conn
L.data = data
}
}

Expand All @@ -98,6 +98,12 @@ func (L *LDAPSearchDataSource) Read(ctx context.Context, request datasource.Read

var additionalAttributes []string
response.Diagnostics.Append(data.AdditionalAttributes.ElementsAs(ctx, &additionalAttributes, false)...)
var conn *ldap.Conn

Check failure on line 101 in internal/provider/ldap_search_datasource.go

View workflow job for this annotation

GitHub Actions / Build

S1021: should merge variable declaration with assignment on next line (gosimple)
conn = GetConn(L.data, response.Diagnostics)

if conn == nil {
return
}

var scope int

Expand Down Expand Up @@ -126,7 +132,7 @@ func (L *LDAPSearchDataSource) Read(ctx context.Context, request datasource.Read

s := ldap.NewSearchRequest(data.BaseDN.ValueString(), scope, 0, 0, 0, false, filter, append(additionalAttributes, "*"), []ldap.Control{})

if result, err := L.conn.Search(s); err != nil {
if result, err := conn.Search(s); err != nil {
response.Diagnostics.AddError(
"Can not read entry",
err.Error(),
Expand Down
11 changes: 2 additions & 9 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,8 @@ func (p *LDAPProvider) Configure(ctx context.Context, req provider.ConfigureRequ
return
}

var conn *ldap.Conn
conn = GetConn(data, resp.Diagnostics)

if conn == null {
return
}

resp.DataSourceData = conn
resp.ResourceData = conn
resp.DataSourceData = data
resp.ResourceData = data
}

func (p *LDAPProvider) Resources(_ context.Context) []func() resource.Resource {
Expand Down

0 comments on commit 547696a

Please sign in to comment.