diff --git a/README.md b/README.md index 3474e151..656f651f 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/kafka/resource_kafka_user_scram_credential.go b/kafka/resource_kafka_user_scram_credential.go index 1068bda8..1d75519a 100644 --- a/kafka/resource_kafka_user_scram_credential.go +++ b/kafka/resource_kafka_user_scram_credential.go @@ -2,7 +2,9 @@ package kafka import ( "context" + "fmt" "log" + "strings" "github.com/IBM/sarama" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -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, @@ -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)