github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/aws/dynamodb.go (about)

     1  package aws
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/service/dynamodb"
     6  	"github.com/gruntwork-io/terratest/modules/testing"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  // GetDynamoDbTableTags fetches resource tags of a specified dynamoDB table. This will fail the test if there are any errors
    11  func GetDynamoDbTableTags(t testing.TestingT, region string, tableName string) []*dynamodb.Tag {
    12  	tags, err := GetDynamoDbTableTagsE(t, region, tableName)
    13  	require.NoError(t, err)
    14  	return tags
    15  }
    16  
    17  // GetDynamoDbTableTagsE fetches resource tags of a specified dynamoDB table.
    18  func GetDynamoDbTableTagsE(t testing.TestingT, region string, tableName string) ([]*dynamodb.Tag, error) {
    19  	table := GetDynamoDBTable(t, region, tableName)
    20  	out, err := NewDynamoDBClient(t, region).ListTagsOfResource(&dynamodb.ListTagsOfResourceInput{
    21  		ResourceArn: table.TableArn,
    22  	})
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	return out.Tags, err
    27  }
    28  
    29  // GetDynamoDBTableTimeToLive fetches information about the TTL configuration of a specified dynamoDB table. This will fail the test if there are any errors.
    30  func GetDynamoDBTableTimeToLive(t testing.TestingT, region string, tableName string) *dynamodb.TimeToLiveDescription {
    31  	ttl, err := GetDynamoDBTableTimeToLiveE(t, region, tableName)
    32  	require.NoError(t, err)
    33  	return ttl
    34  }
    35  
    36  // GetDynamoDBTableTimeToLiveE fetches information about the TTL configuration of a specified dynamoDB table.
    37  func GetDynamoDBTableTimeToLiveE(t testing.TestingT, region string, tableName string) (*dynamodb.TimeToLiveDescription, error) {
    38  	out, err := NewDynamoDBClient(t, region).DescribeTimeToLive(&dynamodb.DescribeTimeToLiveInput{
    39  		TableName: aws.String(tableName),
    40  	})
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return out.TimeToLiveDescription, err
    45  }
    46  
    47  // GetDynamoDBTable fetches information about the specified dynamoDB table. This will fail the test if there are any errors.
    48  func GetDynamoDBTable(t testing.TestingT, region string, tableName string) *dynamodb.TableDescription {
    49  	table, err := GetDynamoDBTableE(t, region, tableName)
    50  	require.NoError(t, err)
    51  	return table
    52  }
    53  
    54  // GetDynamoDBTableE fetches information about the specified dynamoDB table.
    55  func GetDynamoDBTableE(t testing.TestingT, region string, tableName string) (*dynamodb.TableDescription, error) {
    56  	out, err := NewDynamoDBClient(t, region).DescribeTable(&dynamodb.DescribeTableInput{
    57  		TableName: aws.String(tableName),
    58  	})
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return out.Table, err
    63  }
    64  
    65  // NewDynamoDBClient creates a DynamoDB client.
    66  func NewDynamoDBClient(t testing.TestingT, region string) *dynamodb.DynamoDB {
    67  	client, err := NewDynamoDBClientE(t, region)
    68  	require.NoError(t, err)
    69  	return client
    70  }
    71  
    72  // NewDynamoDBClientE creates a DynamoDB client.
    73  func NewDynamoDBClientE(t testing.TestingT, region string) (*dynamodb.DynamoDB, error) {
    74  	sess, err := NewAuthenticatedSession(region)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	return dynamodb.New(sess), nil
    79  }