11

Get the output as

 {
   ARN: "arn:aws:secretsmanager:us-east-2:xxxx:secret:team_dev-Xhzkt6",
   CreatedDate: 2018-07-05 06:50:07 +0000 UTC,
   Name: "team_dev",
   SecretString: "{\"password\":\"test\"}",
   VersionId: "6b65bfe4-7908-474b-9ae6-xxxx",
   VersionStages: ["AWSCURRENT"]
 }

Try to get the value as map with the key SecretString

d.Set("secret_string", output.SecretString)

How can I get it as Schema TypeMap?

With jq, I can easily get it, but not sure how to do this in golang.

Updates

Thanks, @mkopriva

in the aws sdk (github.com/aws/aws-sdk-go/service/secretsmanager/api.go), output (GetSecretValueOutput) is defined as type:"structure"

type GetSecretValueOutput struct {
        _ struct{} `type:"structure"`

And thanks again to provide a test code, I will try it.

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    secretString := "{\"password\":\"test\"}"

    sec := map[string]interface{}{}
    if err := json.Unmarshal([]byte(secretString), &sec); err != nil {
        panic(err)
    }
    fmt.Println(sec)
}

Golang Playground Try Here

9
  • If output is map[string]interface{}, then output["SecretString"]. The returned value's type will be interface{} so you need to type assert it to a string. Commented Jul 5, 2018 at 11:24
  • Thanks, let me try first Commented Jul 5, 2018 at 11:27
  • get this error: invalid operation: output["SecretString"] (type *secretsmanager.GetSecretValueOutput does not support indexing) Commented Jul 5, 2018 at 11:29
  • 1
    Then that means that output is not of Schema TypeMap. Commented Jul 5, 2018 at 11:30
  • 1
    Looking at the source (github.com/aws/aws-sdk-go/blob/master/service/secretsmanager/…) it seems you can do what you were doing in the first place but you need to dereference the field since it is a pointer. Try d.Set("secret_string", *output.SecretString) but first make sure it's not nil. Commented Jul 5, 2018 at 11:33

1 Answer 1

10

@mkopriva

Thanks, please answer this question, so I can vote and accept it.

With your sample code, I did fix the issue.

-       d.Set("secret_string", output.SecretString)
+       var sec map[string]interface{}
+       if err = json.Unmarshal([]byte(*output.SecretString), &sec); err != nil {
+               return err
+       }
+       d.Set("secret_string", sec)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.