Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SCRAM user credentials import #421

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading