Skip to content

Commit

Permalink
Merge pull request #12 from Ouest-France/SDKContextMigrate
Browse files Browse the repository at this point in the history
Upgrade to SDK new context aware functions
  • Loading branch information
pablo-ruth authored Mar 3, 2021
2 parents d98a675 + 33d2132 commit 33b6f01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
13 changes: 13 additions & 0 deletions ldap/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ldap

// CallerTypeKeyType represents an CallerType context key type
type CallerTypeKeyType string

// DatasourceCallerValueType represents an DatasourceCaller context key type
type DatasourceCallerValueType bool

// CallerTypeKey represents an CallerTypeKey context key
const CallerTypeKey CallerTypeKeyType = "callerType"

// DatasourceCaller represents an DatasourceCallerKeyValue context value
const DatasourceCaller DatasourceCallerValueType = true
9 changes: 6 additions & 3 deletions ldap/data_source_resource_ldap_group.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package ldap

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceLDAPGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceLDAPGroupRead,
ReadContext: dataSourceLDAPGroupRead,

Schema: map[string]*schema.Schema{
"ou": &schema.Schema{
Expand All @@ -34,10 +36,11 @@ func dataSourceLDAPGroup() *schema.Resource {
}
}

func dataSourceLDAPGroupRead(d *schema.ResourceData, m interface{}) error {
func dataSourceLDAPGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))

d.SetId(dn)

return resourceLDAPGroupRead(d, m)
// Add context key to signal the Read is called from a datasource
return resourceLDAPGroupRead(context.WithValue(ctx, CallerTypeKey, DatasourceCaller), d, m)
}
46 changes: 28 additions & 18 deletions ldap/resource_ldap_group.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package ldap

import (
"context"
"fmt"
"strings"

"github.com/Ouest-France/goldap"
"github.com/go-ldap/ldap/v3"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceLDAPGroup() *schema.Resource {
return &schema.Resource{
Create: resourceLDAPGroupCreate,
Read: resourceLDAPGroupRead,
Update: resourceLDAPGroupUpdate,
Delete: resourceLDAPGroupDelete,
CreateContext: resourceLDAPGroupCreate,
ReadContext: resourceLDAPGroupRead,
UpdateContext: resourceLDAPGroupUpdate,
DeleteContext: resourceLDAPGroupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -46,7 +48,7 @@ func resourceLDAPGroup() *schema.Resource {
}
}

func resourceLDAPGroupCreate(d *schema.ResourceData, m interface{}) error {
func resourceLDAPGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*goldap.Client)

dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))
Expand All @@ -59,15 +61,15 @@ func resourceLDAPGroupCreate(d *schema.ResourceData, m interface{}) error {

err := client.CreateGroup(dn, d.Get("name").(string), d.Get("description").(string), members)
if err != nil {
return err
return diag.FromErr(err)
}

d.SetId(dn)

return resourceLDAPGroupRead(d, m)
return resourceLDAPGroupRead(ctx, d, m)
}

func resourceLDAPGroupRead(d *schema.ResourceData, m interface{}) error {
func resourceLDAPGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*goldap.Client)

dn := d.Id()
Expand All @@ -76,28 +78,36 @@ func resourceLDAPGroupRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
if err.(*ldap.Error).ResultCode == 32 {
// Object doesn't exist

// If Read is called from a datasource, return an error
if ctx.Value(CallerTypeKey) == DatasourceCaller {
return diag.Errorf("LDAP group not found: %s", dn)
}

// If not a call from datasource, remove the resource from the state
// and cleanly return
d.SetId("")
return nil
}
return err
return diag.FromErr(err)
}

if err := d.Set("name", attributes["name"][0]); err != nil {
return err
return diag.FromErr(err)
}

// Remove the `CN=<group-name>,` from the DN to get the OU
ou := strings.ReplaceAll(dn, fmt.Sprintf("CN=%s,", attributes["name"][0]), "")
if err := d.Set("ou", ou); err != nil {
return err
return diag.FromErr(err)
}

desc := ""
if val, ok := attributes["description"]; ok {
desc = val[0]
}
if err := d.Set("description", desc); err != nil {
return err
return diag.FromErr(err)
}

members := []string{}
Expand All @@ -108,26 +118,26 @@ func resourceLDAPGroupRead(d *schema.ResourceData, m interface{}) error {
}
err = d.Set("members", members)

return err
return diag.FromErr(err)
}

func resourceLDAPGroupUpdate(d *schema.ResourceData, m interface{}) error {
func resourceLDAPGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*goldap.Client)
dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))

if err := client.UpdateGroup(dn, d.Get("name").(string), d.Get("description").(string)); err != nil {
return err
return diag.FromErr(err)
}

return resourceLDAPGroupRead(d, m)
return resourceLDAPGroupRead(ctx, d, m)
}

func resourceLDAPGroupDelete(d *schema.ResourceData, m interface{}) error {
func resourceLDAPGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*goldap.Client)

dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))

err := client.DeleteGroup(dn)

return err
return diag.FromErr(err)
}

0 comments on commit 33b6f01

Please sign in to comment.