Skip to content

Commit

Permalink
Allow for ssm invoke without client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Quackenbush committed Jun 21, 2021
1 parent 7e38458 commit 7975373
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions modules/aws/ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func GetParameterE(t testing.TestingT, awsRegion string, keyName string) (string
return "", err
}

resp, err := ssmClient.GetParameter(&ssm.GetParameterInput{Name: aws.String(keyName), WithDecryption: aws.Bool(true)})
return GetParameterWithClientE(t, ssmClient, keyName)
}
func GetParameterWithClientE(t testing.TestingT, client *ssm.SSM, keyName string) (string, error) {
resp, err := client.GetParameter(&ssm.GetParameterInput{Name: aws.String(keyName), WithDecryption: aws.Bool(true)})
if err != nil {
return "", err
}
Expand All @@ -48,8 +51,10 @@ func PutParameterE(t testing.TestingT, awsRegion string, keyName string, keyDesc
if err != nil {
return 0, err
}

resp, err := ssmClient.PutParameter(&ssm.PutParameterInput{Name: aws.String(keyName), Description: aws.String(keyDescription), Value: aws.String(keyValue), Type: aws.String("SecureString")})
return PutParameterWithClientE(t, ssmClient, keyName, keyDescription, keyValue)
}
func PutParameterWithClientE(t testing.TestingT, client *ssm.SSM, keyName string, keyDescription string, keyValue string) (int64, error) {
resp, err := client.PutParameter(&ssm.PutParameterInput{Name: aws.String(keyName), Description: aws.String(keyDescription), Value: aws.String(keyValue), Type: aws.String("SecureString")})
if err != nil {
return 0, err
}
Expand All @@ -69,8 +74,10 @@ func DeleteParameterE(t testing.TestingT, awsRegion string, keyName string) erro
if err != nil {
return err
}

_, err = ssmClient.DeleteParameter(&ssm.DeleteParameterInput{Name: aws.String(keyName)})
return DeleteParameterWithClientE(t, ssmClient, keyName)
}
func DeleteParameterWithClientE(t testing.TestingT, client *ssm.SSM, keyName string) error {
_, err := client.DeleteParameter(&ssm.DeleteParameterInput{Name: aws.String(keyName)})
if err != nil {
return err
}
Expand All @@ -97,6 +104,14 @@ func NewSsmClientE(t testing.TestingT, region string) (*ssm.SSM, error) {

// WaitForSsmInstanceE waits until the instance get registered to the SSM inventory.
func WaitForSsmInstanceE(t testing.TestingT, awsRegion, instanceID string, timeout time.Duration) error {
client, err := NewSsmClientE(t, awsRegion)
if err != nil {
return err
}
return WaitForSsmInstanceWithClientE(t, client, instanceID, timeout)
}

func WaitForSsmInstanceWithClientE(t testing.TestingT, client *ssm.SSM, instanceID string, timeout time.Duration) error {
timeBetweenRetries := 2 * time.Second
maxRetries := int(timeout.Seconds() / timeBetweenRetries.Seconds())
description := fmt.Sprintf("Waiting for %s to appear in the SSM inventory", instanceID)
Expand All @@ -111,7 +126,6 @@ func WaitForSsmInstanceE(t testing.TestingT, awsRegion, instanceID string, timeo
},
}
_, err := retry.DoWithRetryE(t, description, maxRetries, timeBetweenRetries, func() (string, error) {
client := NewSsmClient(t, awsRegion)
resp, err := client.GetInventory(input)

if err != nil {
Expand Down Expand Up @@ -152,14 +166,19 @@ type CommandOutput struct {
func CheckSsmCommandE(t testing.TestingT, awsRegion, instanceID, command string, timeout time.Duration) (*CommandOutput, error) {
logger.Logf(t, "Running command '%s' on EC2 instance with ID '%s'", command, instanceID)

timeBetweenRetries := 2 * time.Second
maxRetries := int(timeout.Seconds() / timeBetweenRetries.Seconds())

// Now that we know the instance in the SSM inventory, we can send the command
client, err := NewSsmClientE(t, awsRegion)
if err != nil {
return nil, err
}
return CheckSSMCommandWithClientE(t, client, instanceID, command, timeout)
}

func CheckSSMCommandWithClientE(t testing.TestingT, client *ssm.SSM, instanceID, command string, timeout time.Duration) (*CommandOutput, error) {

timeBetweenRetries := 2 * time.Second
maxRetries := int(timeout.Seconds() / timeBetweenRetries.Seconds())

resp, err := client.SendCommand(&ssm.SendCommandInput{
Comment: aws.String("Terratest SSM"),
DocumentName: aws.String("AWS-RunShellScript"),
Expand Down

0 comments on commit 7975373

Please sign in to comment.