Created
May 13, 2020 20:20
-
-
Save krogebry/60a889fa631fe84724f65eb609773b4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hssm | |
import ( | |
"fmt" | |
"regexp" | |
"encoding/json" | |
"github.com/aws/aws-sdk-go/aws/awserr" | |
"github.com/aws/aws-sdk-go/service/ssm" | |
"github.com/aws/aws-sdk-go/service/ssm/ssmiface" | |
) | |
func isJSONString(s string) bool { | |
var js string | |
return json.Unmarshal([]byte(s), &js) == nil | |
} | |
func isJSON(s string) bool { | |
var js map[string]interface{} | |
return json.Unmarshal([]byte(s), &js) == nil | |
} | |
// GetSSMParameter gets a parameter from the AWS Simple Systems Manager service. | |
func GetSSMParameter(svc ssmiface.SSMAPI, name string, defaultValue *string, decrypt bool) (*string, error) { | |
regex := "([a-zA-Z0-9\\.\\-_/]*)" | |
r, _ := regexp.Compile(regex) | |
match := r.FindString(name) | |
if match == "" { | |
return nil, fmt.Errorf("There is an invalid character in the name of the parameter: %s. It should match %s", name, regex) | |
} | |
// Create the request to SSM | |
getParameterInput := &ssm.GetParameterInput{ | |
Name: &name, | |
WithDecryption: &decrypt, | |
} | |
// Get the parameter from SSM | |
param, err := svc.GetParameter(getParameterInput) | |
// Cast err to awserr.Error to handle specific error codes. | |
aerr, ok := err.(awserr.Error) | |
if ok && aerr.Code() == ssm.ErrCodeParameterNotFound { | |
// Specific error code handling | |
if defaultValue != nil { | |
return defaultValue, nil | |
} | |
return nil, err | |
} | |
if aerr != nil { | |
return nil, err | |
} | |
payload := param.Parameter.Value | |
if isJSONString(payload) { | |
data := json.Marshall(payload) | |
}else{ | |
data := json.Marshall(fmt.Sprintf("{'data': '%d'}", payload)) | |
} | |
// return param.Parameter.Value, nil | |
return json.Unmarshall(data), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
root@bf278f1c4b92:/go/src/github.com/codacy/helm-ssm# make install
cat: .version: No such file or directory
dep ensure
mkdir -p /go/src/github.com/codacy/helm-ssm/_dist
sed -i 's/version:.*/version: "''"/g' plugin.yaml
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o helm-ssm -ldflags "-X main.version=" ./cmd
github.com/codacy/helm-ssm/internal
internal/ssm.go:55:18: cannot use payload (type *string) as type string in argument to isJSONString
internal/ssm.go:56:13: undefined: json.Marshall
internal/ssm.go:58:13: undefined: json.Marshall
internal/ssm.go:62:10: undefined: json.Unmarshall
internal/ssm.go:62:26: undefined: data
make: *** [Makefile:37: dist] Error 2