Repository navigation
Expand file tree
/
Copy pathscriptorderusage.go
More file actions
55 lines (50 loc) · 1.5 KB
/
Copy pathscriptorderusage.go
File metadata and controls
55 lines (50 loc) · 1.5 KB
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
package provisionerdserver
import (
"encoding/json"
tfjson "github.com/hashicorp/terraform-json"
"golang.org/x/xerrors"
)
type scriptOrderUsage struct {
dataSourceCount int32
ruleCount int32
}
// countScriptOrderUsage counts declarations in the supplied plan configuration,
// so count, for_each, and module instance counts do not inflate usage.
func countScriptOrderUsage(planJSON []byte) (scriptOrderUsage, error) {
var usage scriptOrderUsage
if len(planJSON) == 0 {
return usage, nil
}
var plan struct {
Config *tfjson.Config `json:"configuration"`
}
if err := json.Unmarshal(planJSON, &plan); err != nil {
return usage, xerrors.Errorf("decode plan configuration: %w", err)
}
if plan.Config != nil {
countScriptOrderUsageInModule(plan.Config.RootModule, &usage)
}
return usage, nil
}
func countScriptOrderUsageInModule(module *tfjson.ConfigModule, usage *scriptOrderUsage) {
if module == nil {
return
}
for _, r := range module.Resources {
if r == nil || r.Mode != tfjson.DataResourceMode || r.Type != "coder_script_order" {
continue
}
usage.dataSourceCount++
// Terraform's configuration JSON omits dynamic blocks, so only literal
// rule blocks contribute to this count.
if rules := r.Expressions["rule"]; rules != nil {
// #nosec G115 - Terraform rule counts are expected to fit in int32.
usage.ruleCount += int32(len(rules.NestedBlocks))
}
}
for _, call := range module.ModuleCalls {
if call != nil {
countScriptOrderUsageInModule(call.Module, usage)
}
}
}