Skip to content

Commit

Permalink
Add SCRAM user credentials import (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanchosPancho authored May 24, 2024
1 parent 77f24e8 commit 34b7d8d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ resource "kafka_user_scram_credential" "test" {
}
```
#### Importing Existing SCRAM user credentials
For import, use as a parameter the items separated by `|` character. Quote it to avoid shell expansion.
```sh
# Fields in shell notation are
# ${username}|${scram_mechanism}|${password}
terraform import kafka_user_scram_credential.test 'user1|SCRAM-SHA-256|password'
```
#### Properties
| Property | Description |
Expand Down
22 changes: 22 additions & 0 deletions kafka/resource_kafka_user_scram_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package kafka

import (
"context"
"fmt"
"log"
"strings"

"github.com/IBM/sarama"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -19,6 +21,9 @@ func kafkaUserScramCredentialResource() *schema.Resource {
ReadContext: userScramCredentialRead,
UpdateContext: userScramCredentialUpdate,
DeleteContext: userScramCredentialDelete,
Importer: &schema.ResourceImporter{
StateContext: importSCRAM,
},
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Expand Down Expand Up @@ -53,6 +58,23 @@ func kafkaUserScramCredentialResource() *schema.Resource {
}
}

func importSCRAM(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "|")
if len(parts) == 3 {
errSet := errSetter{d: d}
errSet.Set("username", parts[0])
errSet.Set("scram_mechanism", parts[1])
errSet.Set("password", parts[2])
if errSet.err != nil {
return nil, errSet.err
}
} else {
return nil, fmt.Errorf("Failed importing resource; expected format is username|scram_mechanism|password - got %v segments instead of 3", len(parts))
}

return []*schema.ResourceData{d}, nil
}

func userScramCredentialCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] Creating user scram credential")
c := meta.(*LazyClient)
Expand Down

0 comments on commit 34b7d8d

Please sign in to comment.