Skip to content

Commit dd1de28

Browse files
committed
Added test, improved documentation.
1 parent 05ee0dc commit dd1de28

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

docs/resources/counter.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
page_title: "persistent_counter Resource - terraform-provider-persistent"
44
subcategory: ""
55
description: |-
6-
Persistent counter
6+
Persistent counter. Generates sequentially increasing number counters for the strings specified
7+
in the `keys` variable. As long as a specified key exist, it will always receive the same counter
8+
value. No counter value is re-used even if a new key is added and counter values will only ever
9+
increase.
710
---
811

912
# persistent_counter (Resource)
1013

11-
Persistent counter
14+
Persistent counter. Generates sequentially increasing number counters for the strings specified
15+
in the `keys` variable. As long as a specified key exist, it will always receive the same counter
16+
value. No counter value is re-used even if a new key is added and counter values will only ever
17+
increase.
1218

1319

1420

@@ -17,16 +23,16 @@ Persistent counter
1723

1824
### Required
1925

20-
- `keys` (List of String)
26+
- `keys` (List of String) List of keys to generate counters for.
2127

2228
### Optional
2329

2430
- `initial_value` (Number) The initial value to use for the counter.
25-
- `values` (Map of Number) A map of strings that will cause a change to the counter when any of the values change.
31+
- `values` (Map of Number) A map of keys to counter values.
2632

2733
### Read-Only
2834

29-
- `id` (String) Identifier
35+
- `id` (String) Identifier (always fixed)
3036
- `last_value` (Number) The last value that was used for the counter.
3137

3238

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "persistent_counter" "example" {
2+
initial_value = 5
3+
keys = ["a", "b", "d"]
4+
}
5+
# persistent_counter.example.values = { a = 5, b = 6, c = 7 }

internal/provider/counter_resource.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,23 @@ func (r *PersistentCounterResource) Metadata(ctx context.Context, req resource.M
3939

4040
func (r *PersistentCounterResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
4141
resp.Schema = schema.Schema{
42-
// This description is used by the documentation generator and the language server.
43-
MarkdownDescription: "Persistent counter",
42+
MarkdownDescription: `
43+
Persistent counter. Generates sequentially increasing number counters for the strings specified
44+
in the ` + "`keys`" + ` variable. As long as a specified key exist, it will always receive the same counter
45+
value. No counter value is re-used even if a new key is added and counter values will only ever
46+
increase.
47+
`,
4448

4549
Attributes: map[string]schema.Attribute{
4650
"id": schema.StringAttribute{
47-
Computed: true,
48-
MarkdownDescription: "Identifier",
51+
Computed: true,
52+
Description: "Identifier (always fixed)",
4953
PlanModifiers: []planmodifier.String{
5054
stringplanmodifier.UseStateForUnknown(),
5155
},
5256
},
5357
"keys": schema.ListAttribute{
58+
Description: "List of keys to generate counters for.",
5459
ElementType: types.StringType,
5560
Required: true,
5661
},
@@ -71,7 +76,7 @@ func (r *PersistentCounterResource) Schema(ctx context.Context, req resource.Sch
7176
ElementType: types.Int64Type,
7277
Optional: true,
7378
Computed: true,
74-
Description: "A map of strings that will cause a change to the counter when any of the values change.",
79+
Description: "A map of keys to counter values.",
7580
},
7681
},
7782
}

internal/provider/counter_resource_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ func TestAccPersistentCounterResource(t *testing.T) {
4444
resource.TestCheckResourceAttr("persistent_counter.test", "values.e", "4"),
4545
),
4646
},
47+
{
48+
Config: testAccCounterInitialResourceConfig(),
49+
Check: resource.ComposeAggregateTestCheckFunc(
50+
resource.TestCheckResourceAttr("persistent_counter.initial-value", "initial_value", "5"),
51+
resource.TestCheckResourceAttr("persistent_counter.initial-value", "last_value", "8"),
52+
resource.TestCheckResourceAttr("persistent_counter.initial-value", "values.a", "5"),
53+
resource.TestCheckResourceAttr("persistent_counter.initial-value", "values.b", "6"),
54+
resource.TestCheckResourceAttr("persistent_counter.initial-value", "values.c", "7"),
55+
resource.TestCheckResourceAttr("persistent_counter.initial-value", "values.d", "8"),
56+
),
57+
},
4758
},
4859
})
4960
}
@@ -74,3 +85,12 @@ resource "persistent_counter" "test" {
7485
}
7586
`
7687
}
88+
89+
func testAccCounterInitialResourceConfig() string {
90+
return `
91+
resource "persistent_counter" "initial-value" {
92+
initial_value = 5
93+
keys = ["a", "b", "c", "d"]
94+
}
95+
`
96+
}

0 commit comments

Comments
 (0)