Skip to content

Commit ec69ace

Browse files
authored
Merge pull request #348 from ns1-terraform/PENG-6244-4
Moving billing usage from resource to data source
2 parents f9b689a + b0d9940 commit ec69ace

File tree

8 files changed

+167
-118
lines changed

8 files changed

+167
-118
lines changed

examples/billing_usage.tf

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
11
# Get query usage data for the given timeframe
2-
resource "ns1_billing_usage" "queries" {
2+
data "ns1_billing_usage" "queries" {
33
metric_type = "queries"
4-
from = 1731605824 # 2024-11-15 00:00:00 UTC
5-
to = 1734197824 # 2024-12-15 00:00:00 UTC
4+
from = 1738368000 # 2025-02-01 00:00:00 UTC
5+
to = 1740787199 # 2025-02-28 23:59:59 UTC
66
}
77

88
# Get account limits data for the given timeframe
9-
resource "ns1_billing_usage" "limits" {
9+
data "ns1_billing_usage" "limits" {
1010
metric_type = "limits"
11-
from = 1731605824 # 2024-11-15 00:00:00 UTC
12-
to = 1734197824 # 2024-12-15 00:00:00 UTC
11+
from = 1738368000 # 2025-02-01 00:00:00 UTC
12+
to = 1740787199 # 2025-02-28 23:59:59 UTC
1313
}
1414

1515
# Get RUM decisions usage data for the given timeframe
16-
resource "ns1_billing_usage" "decisions" {
16+
data "ns1_billing_usage" "decisions" {
1717
metric_type = "decisions"
18-
from = 1731605824 # 2024-11-15 00:00:00 UTC
19-
to = 1734197824 # 2024-12-15 00:00:00 UTC
18+
from = 1738368000 # 2025-02-01 00:00:00 UTC
19+
to = 1740787199 # 2025-02-28 23:59:59 UTC
2020
}
2121

2222
# Get filter chains usage data
23-
resource "ns1_billing_usage" "filter_chains" {
23+
data "ns1_billing_usage" "filter_chains" {
2424
metric_type = "filter-chains"
2525
}
2626

2727
# Get monitoring jobs usage data
28-
resource "ns1_billing_usage" "monitors" {
28+
data "ns1_billing_usage" "monitors" {
2929
metric_type = "monitors"
3030
}
3131

3232
# Get records usage data
33-
resource "ns1_billing_usage" "records" {
33+
data "ns1_billing_usage" "records" {
3434
metric_type = "records"
3535
}
3636

3737
# Example of using the data in other resources
3838
output "total_queries" {
39-
value = ns1_billing_usage.queries.clean_queries
39+
value = data.ns1_billing_usage.queries.clean_queries
4040
}
4141

4242
output "total_ddos_queries" {
43-
value = ns1_billing_usage.queries.ddos_queries
43+
value = data.ns1_billing_usage.queries.ddos_queries
4444
}
4545

4646
output "total_nxd_responses" {
47-
value = ns1_billing_usage.queries.nxd_responses
47+
value = data.ns1_billing_usage.queries.nxd_responses
4848
}
4949

5050
output "queries_limit" {
51-
value = ns1_billing_usage.limits.queries_limit
51+
value = data.ns1_billing_usage.limits.queries_limit
5252
}
5353

5454
output "total_decisions" {
55-
value = ns1_billing_usage.decisions.total_usage
55+
value = data.ns1_billing_usage.decisions.total_usage
5656
}
5757

5858
output "decisions_limit" {
59-
value = ns1_billing_usage.limits.decisions_limit
59+
value = data.ns1_billing_usage.limits.decisions_limit
6060
}
6161

6262
output "total_filter_chains" {
63-
value = ns1_billing_usage.filter_chains.total_usage
63+
value = data.ns1_billing_usage.filter_chains.total_usage
6464
}
6565

6666
output "filter_chains_limit" {
67-
value = ns1_billing_usage.limits.filter_chains_limit
67+
value = data.ns1_billing_usage.limits.filter_chains_limit
6868
}
6969

7070
output "total_monitors" {
71-
value = ns1_billing_usage.monitors.total_usage
71+
value = data.ns1_billing_usage.monitors.total_usage
7272
}
7373

7474
output "monitors_limit" {
75-
value = ns1_billing_usage.limits.monitors_limit
75+
value = data.ns1_billing_usage.limits.monitors_limit
7676
}
7777

7878
output "total_records" {
79-
value = ns1_billing_usage.records.total_usage
79+
value = data.ns1_billing_usage.records.total_usage
8080
}
8181

8282
output "records_limit" {
83-
value = ns1_billing_usage.limits.records_limit
83+
value = data.ns1_billing_usage.limits.records_limit
8484
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/hashicorp/go-retryablehttp v0.7.7
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
99
github.com/stretchr/testify v1.8.1
10-
gopkg.in/ns1/ns1-go.v2 v2.14.0
10+
gopkg.in/ns1/ns1-go.v2 v2.14.1
1111
)
1212

1313
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ gopkg.in/ns1/ns1-go.v2 v2.13.0 h1:I5NNqI9Bi1SGK92TVkOvLTwux5LNrix/99H2datVh48=
255255
gopkg.in/ns1/ns1-go.v2 v2.13.0/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
256256
gopkg.in/ns1/ns1-go.v2 v2.14.0 h1:qLuHV8qp/bv/vClA8P5V0fv0Mo+k8w/WM29jBjRD6oc=
257257
gopkg.in/ns1/ns1-go.v2 v2.14.0/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
258+
gopkg.in/ns1/ns1-go.v2 v2.14.1 h1:wruE2g1uB90kMW+jHW8BtWa1HvNkqDfyf7SacTKWtBY=
259+
gopkg.in/ns1/ns1-go.v2 v2.14.1/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
258260
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
259261
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
260262
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

ns1/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
var (
22-
clientVersion = "2.6.1"
22+
clientVersion = "2.6.2"
2323
providerUserAgent = "tf-ns1" + "/" + clientVersion
2424
defaultRetryMax = 3
2525
)

ns1/examples/billing_usage.tf

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
# Get query usage data for the given timeframe
2-
resource "ns1_billing_usage" "queries" {
2+
data "ns1_billing_usage" "queries" {
33
metric_type = "queries"
4-
from = 1731605824 # 2024-11-15 00:00:00 UTC
5-
to = 1734197824 # 2024-12-15 00:00:00 UTC
4+
from = 1738368000 # 2025-02-01 00:00:00 UTC
5+
to = 1740787199 # 2025-02-28 23:59:59 UTC
66
}
77

88
# Get account limits data for the given timeframe
9-
resource "ns1_billing_usage" "limits" {
9+
data "ns1_billing_usage" "limits" {
1010
metric_type = "limits"
11-
from = 1731605824 # 2024-11-15 00:00:00 UTC
12-
to = 1734197824 # 2024-12-15 00:00:00 UTC
11+
from = 1738368000 # 2025-02-01 00:00:00 UTC
12+
to = 1740787199 # 2025-02-28 23:59:59 UTC
1313
}
1414

1515
# Get RUM decisions usage data for the given timeframe
16-
resource "ns1_billing_usage" "decisions" {
16+
data "ns1_billing_usage" "decisions" {
1717
metric_type = "decisions"
18-
from = 1731605824 # 2024-11-15 00:00:00 UTC
19-
to = 1734197824 # 2024-12-15 00:00:00 UTC
18+
from = 1738368000 # 2025-02-01 00:00:00 UTC
19+
to = 1740787199 # 2025-02-28 23:59:59 UTC
2020
}
2121

2222
# Get filter chains usage data
23-
resource "ns1_billing_usage" "filter_chains" {
23+
data "ns1_billing_usage" "filter_chains" {
2424
metric_type = "filter-chains"
2525
}
2626

2727
# Get monitoring jobs usage data
28-
resource "ns1_billing_usage" "monitors" {
28+
data "ns1_billing_usage" "monitors" {
2929
metric_type = "monitors"
3030
}
3131

3232
# Get records usage data
33-
resource "ns1_billing_usage" "records" {
33+
data "ns1_billing_usage" "records" {
3434
metric_type = "records"
3535
}

ns1/resource_billing_usage.go

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@ package ns1
22

33
import (
44
"fmt"
5+
56
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
67
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
78
)
89

10+
const (
11+
MetricTypeQueries = "queries"
12+
MetricTypeLimits = "limits"
13+
MetricTypeDecisions = "decisions"
14+
MetricTypeRecords = "records"
15+
MetricTypeFilterChains = "filter-chains"
16+
MetricTypeMonitors = "monitors"
17+
)
18+
919
var MetricTypeStringEnum = NewStringEnum([]string{
10-
"queries",
11-
"decisions",
12-
"records",
13-
"filter-chains",
14-
"monitors",
15-
"limits",
20+
MetricTypeQueries,
21+
MetricTypeDecisions,
22+
MetricTypeRecords,
23+
MetricTypeFilterChains,
24+
MetricTypeMonitors,
25+
MetricTypeLimits,
1626
})
1727

28+
var timeFrameRequiredMetricTypes = []string{
29+
MetricTypeQueries,
30+
MetricTypeLimits,
31+
MetricTypeDecisions,
32+
}
33+
1834
func billingUsageResource() *schema.Resource {
1935
return &schema.Resource{
2036
Schema: map[string]*schema.Schema{
@@ -25,11 +41,11 @@ func billingUsageResource() *schema.Resource {
2541
},
2642
"from": {
2743
Type: schema.TypeInt,
28-
Required: true,
44+
Optional: true,
2945
},
3046
"to": {
3147
Type: schema.TypeInt,
32-
Required: true,
48+
Optional: true,
3349
},
3450
// Queries specific fields
3551
"clean_queries": {
@@ -143,27 +159,50 @@ func billingUsageResource() *schema.Resource {
143159
}
144160
}
145161

162+
// Helper function to check if a string is in a slice
163+
func contains(slice []string, item string) bool {
164+
for _, s := range slice {
165+
if s == item {
166+
return true
167+
}
168+
}
169+
return false
170+
}
171+
146172
// BillingUsageRead reads the billing usage data from NS1
147173
func billingUsageRead(d *schema.ResourceData, meta interface{}) error {
148174
client := meta.(*ns1.Client)
149175
metricType := d.Get("metric_type").(string)
150-
from := int32(d.Get("from").(int))
151-
to := int32(d.Get("to").(int))
176+
177+
// Validate that from and to are provided for metric types that require them
178+
if contains(timeFrameRequiredMetricTypes, metricType) {
179+
_, okFrom := d.GetOk("from")
180+
_, okTo := d.GetOk("to")
181+
if !okFrom || !okTo {
182+
return fmt.Errorf("from and to parameters are required for metric_type: %s", metricType)
183+
}
184+
}
152185

153186
var err error
154187

155188
switch metricType {
156-
case "queries":
189+
case MetricTypeQueries:
190+
from := int32(d.Get("from").(int))
191+
to := int32(d.Get("to").(int))
157192
err = readQueriesUsage(d, client, from, to)
158-
case "limits":
193+
case MetricTypeLimits:
194+
from := int32(d.Get("from").(int))
195+
to := int32(d.Get("to").(int))
159196
err = readLimitsUsage(d, client, from, to)
160-
case "decisions":
197+
case MetricTypeDecisions:
198+
from := int32(d.Get("from").(int))
199+
to := int32(d.Get("to").(int))
161200
err = readDecisionsUsage(d, client, from, to)
162-
case "filter-chains":
201+
case MetricTypeFilterChains:
163202
err = readFilterChainsUsage(d, client)
164-
case "monitors":
203+
case MetricTypeMonitors:
165204
err = readMonitorsUsage(d, client)
166-
case "records":
205+
case MetricTypeRecords:
167206
err = readRecordsUsage(d, client)
168207
default:
169208
return fmt.Errorf("unsupported metric type: %s", metricType)
@@ -173,7 +212,15 @@ func billingUsageRead(d *schema.ResourceData, meta interface{}) error {
173212
return err
174213
}
175214

176-
d.SetId(fmt.Sprintf("%s-%d-%d", metricType, from, to))
215+
// Set a unique ID for the data source
216+
if contains(timeFrameRequiredMetricTypes, metricType) {
217+
from := int32(d.Get("from").(int))
218+
to := int32(d.Get("to").(int))
219+
d.SetId(fmt.Sprintf("%s-%d-%d", metricType, from, to))
220+
} else {
221+
d.SetId(metricType)
222+
}
223+
177224
return nil
178225
}
179226

0 commit comments

Comments
 (0)