-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
ttl.go
119 lines (102 loc) · 3.52 KB
/
ttl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package dynamo
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// UpdateTTL is a request to enable or disable a table's time to live functionality.
// Note that when time to live is enabled, items will typically be deleted within 48 hours
// and items that are expired but not yet deleted will still appear in your database.
// See: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateTimeToLive.html
type UpdateTTL struct {
table Table
attrib string
enabled bool
}
// UpdateTTL begins a new request to enable or disable this table's time to live.
// The name of the attribute to use for expiring items is specified by attribute.
// TTL will be enabled when enabled is true and disabled when it is false.
// The time to live attribute must be stored as Unix time in seconds.
// Items without this attribute won't be deleted.
func (table Table) UpdateTTL(attribute string, enabled bool) *UpdateTTL {
return &UpdateTTL{
table: table,
attrib: attribute,
enabled: enabled,
}
}
// Run executes this request.
func (ttl *UpdateTTL) Run(ctx context.Context) error {
input := ttl.input()
err := ttl.table.db.retry(ctx, func() error {
_, err := ttl.table.db.client.UpdateTimeToLive(ctx, input)
return err
})
return err
}
func (ttl *UpdateTTL) input() *dynamodb.UpdateTimeToLiveInput {
return &dynamodb.UpdateTimeToLiveInput{
TableName: aws.String(ttl.table.Name()),
TimeToLiveSpecification: &types.TimeToLiveSpecification{
Enabled: aws.Bool(ttl.enabled),
AttributeName: aws.String(ttl.attrib),
},
}
}
// DescribeTTL is a request to obtain details about a table's time to live configuration.
type DescribeTTL struct {
table Table
}
// DescribeTTL begins a new request to obtain details about this table's time to live configuration.
func (table Table) DescribeTTL() *DescribeTTL {
return &DescribeTTL{table}
}
// Run executes this request and returns details about time to live, or an error.
func (d *DescribeTTL) Run(ctx context.Context) (TTLDescription, error) {
input := d.input()
var result *dynamodb.DescribeTimeToLiveOutput
err := d.table.db.retry(ctx, func() error {
var err error
result, err = d.table.db.client.DescribeTimeToLive(ctx, input)
return err
})
if err != nil {
return TTLDescription{}, err
}
desc := TTLDescription{
Status: TTLDisabled,
}
if result.TimeToLiveDescription.TimeToLiveStatus != "" {
desc.Status = TTLStatus(result.TimeToLiveDescription.TimeToLiveStatus)
}
if result.TimeToLiveDescription.AttributeName != nil {
desc.Attribute = *result.TimeToLiveDescription.AttributeName
}
return desc, nil
}
func (d *DescribeTTL) input() *dynamodb.DescribeTimeToLiveInput {
return &dynamodb.DescribeTimeToLiveInput{
TableName: aws.String(d.table.Name()),
}
}
// TTLDescription represents time to live configuration details for a table.
type TTLDescription struct {
// Attribute is the name of the time to live attribute for the table. Empty if disabled.
Attribute string
// Status is the table's time to live status.
Status TTLStatus
}
// Enabled returns true if time to live is enabled (and has finished enabling).
func (td TTLDescription) Enabled() bool {
return td.Status == TTLEnabled
}
// TTLStatus represents a table's time to live status.
type TTLStatus string
// Possible time to live statuses.
const (
TTLEnabled TTLStatus = "ENABLED"
TTLEnabling TTLStatus = "ENABLING"
TTLDisabled TTLStatus = "DISABLED"
TTLDisabling TTLStatus = "DISABLING"
)