Skip to content

Commit 9e2f537

Browse files
authored
Add TFC usage detection (hashicorp#1208)
This builds on the existing methods to detect remote backend usage and adds detection of cloud blocks in the terraform block
1 parent 56a6ba7 commit 9e2f537

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ require (
1313
github.com/hashicorp/go-uuid v1.0.3
1414
github.com/hashicorp/go-version v1.6.0
1515
github.com/hashicorp/hc-install v0.5.0
16-
github.com/hashicorp/hcl-lang v0.0.0-20230213134107-bb2205f5b041
16+
github.com/hashicorp/hcl-lang v0.0.0-20230310081458-97626944945e
1717
github.com/hashicorp/hcl/v2 v2.16.2
1818
github.com/hashicorp/terraform-exec v0.18.1
1919
github.com/hashicorp/terraform-json v0.16.0
2020
github.com/hashicorp/terraform-registry-address v0.1.0
21-
github.com/hashicorp/terraform-schema v0.0.0-20230215154105-62b19298872f
21+
github.com/hashicorp/terraform-schema v0.0.0-20230310150226-9019fdc7a59a
2222
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
2323
github.com/mitchellh/cli v1.1.5
2424
github.com/mitchellh/go-homedir v1.1.0

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ github.com/hashicorp/hc-install v0.5.0 h1:D9bl4KayIYKEeJ4vUDe9L5huqxZXczKaykSRcm
209209
github.com/hashicorp/hc-install v0.5.0/go.mod h1:JyzMfbzfSBSjoDCRPna1vi/24BEDxFaCPfdHtM5SCdo=
210210
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
211211
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
212-
github.com/hashicorp/hcl-lang v0.0.0-20230213134107-bb2205f5b041 h1:GAhIS/EmlsuNzRTesYncXPNDjca8i5dJzU50WzkLIEc=
213-
github.com/hashicorp/hcl-lang v0.0.0-20230213134107-bb2205f5b041/go.mod h1:alGNoXhoyCKUDHKM6iK88G5bgPym9agemDf6AEB26j0=
212+
github.com/hashicorp/hcl-lang v0.0.0-20230310081458-97626944945e h1:3VRs8PujogY3D+hKc9ChpKK7rP+y/WTaDAKjORrOxk8=
213+
github.com/hashicorp/hcl-lang v0.0.0-20230310081458-97626944945e/go.mod h1:m+ZB0CmTRHSo14j2INkDA+QZdzGBJRYTrOzf+4Xuj1k=
214214
github.com/hashicorp/hcl/v2 v2.16.2 h1:mpkHZh/Tv+xet3sy3F9Ld4FyI2tUpWe9x3XtPx9f1a0=
215215
github.com/hashicorp/hcl/v2 v2.16.2/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
216216
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
@@ -220,8 +220,8 @@ github.com/hashicorp/terraform-json v0.16.0 h1:UKkeWRWb23do5LNAFlh/K3N0ymn1qTOO8
220220
github.com/hashicorp/terraform-json v0.16.0/go.mod h1:v0Ufk9jJnk6tcIZvScHvetlKfiNTC+WS21mnXIlc0B0=
221221
github.com/hashicorp/terraform-registry-address v0.1.0 h1:W6JkV9wbum+m516rCl5/NjKxCyTVaaUBbzYcMzBDO3U=
222222
github.com/hashicorp/terraform-registry-address v0.1.0/go.mod h1:EnyO2jYO6j29DTHbJcm00E5nQTFeTtyZH3H5ycydQ5A=
223-
github.com/hashicorp/terraform-schema v0.0.0-20230215154105-62b19298872f h1:QpboEFKLwIvvp0D6UWWzAx4/j1nCj1snfTRh3PgRq4A=
224-
github.com/hashicorp/terraform-schema v0.0.0-20230215154105-62b19298872f/go.mod h1:JSXoOylwIc+IbKDP+QLygHz5yBEgK1B/zKTp6YGSSyg=
223+
github.com/hashicorp/terraform-schema v0.0.0-20230310150226-9019fdc7a59a h1:6pAhW/C4H5QEr3mOnikXgmw803+/UB8/z0nyWVTqZGQ=
224+
github.com/hashicorp/terraform-schema v0.0.0-20230310150226-9019fdc7a59a/go.mod h1:tzE93Y7+GD50yiIiVq+nmS+TdGtCRq3XnyLR9UXv5z0=
225225
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
226226
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
227227
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=

internal/langserver/handlers/hooks_module.go

+19
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ func moduleTelemetryData(mod *state.Module, ch state.ModuleChanges, store *state
4444
if len(mod.Meta.CoreRequirements) > 0 {
4545
properties["tfRequirements"] = mod.Meta.CoreRequirements.String()
4646
}
47+
if mod.Meta.Cloud != nil {
48+
properties["cloud"] = true
49+
50+
hostname := mod.Meta.Cloud.Hostname
51+
52+
// https://developer.hashicorp.com/terraform/language/settings/terraform-cloud#usage-example
53+
// Required for Terraform Enterprise;
54+
// Defaults to app.terraform.io for Terraform Cloud
55+
if hostname == "" {
56+
hostname = "app.terraform.io"
57+
}
58+
59+
// anonymize any non-default hostnames
60+
if hostname != "app.terraform.io" {
61+
hostname = "custom-hostname"
62+
}
63+
64+
properties["cloud.hostname"] = hostname
65+
}
4766
if mod.Meta.Backend != nil {
4867
properties["backend"] = mod.Meta.Backend.Type
4968
if data, ok := mod.Meta.Backend.Data.(*backend.Remote); ok {

internal/state/module.go

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/hcl-lang/reference"
1010
"github.com/hashicorp/hcl/v2"
1111
tfaddr "github.com/hashicorp/terraform-registry-address"
12+
"github.com/hashicorp/terraform-schema/backend"
1213
tfmod "github.com/hashicorp/terraform-schema/module"
1314
"github.com/hashicorp/terraform-schema/registry"
1415

@@ -20,6 +21,7 @@ import (
2021
type ModuleMetadata struct {
2122
CoreRequirements version.Constraints
2223
Backend *tfmod.Backend
24+
Cloud *backend.Cloud
2325
ProviderReferences map[tfmod.ProviderRef]tfaddr.Provider
2426
ProviderRequirements tfmod.ProviderRequirements
2527
Variables map[string]tfmod.Variable
@@ -35,6 +37,10 @@ func (mm ModuleMetadata) Copy() ModuleMetadata {
3537
Filenames: mm.Filenames,
3638
}
3739

40+
if mm.Cloud != nil {
41+
newMm.Cloud = mm.Cloud
42+
}
43+
3844
if mm.Backend != nil {
3945
newMm.Backend = &tfmod.Backend{
4046
Type: mm.Backend.Type,
@@ -924,6 +930,7 @@ func (s *ModuleStore) UpdateMetadata(path string, meta *tfmod.Meta, mErr error)
924930
mod := oldMod.Copy()
925931
mod.Meta = ModuleMetadata{
926932
CoreRequirements: meta.CoreRequirements,
933+
Cloud: meta.Cloud,
927934
Backend: meta.Backend,
928935
ProviderReferences: meta.ProviderReferences,
929936
ProviderRequirements: meta.ProviderRequirements,

internal/state/module_changes.go

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type ModuleChanges struct {
3131

3232
CoreRequirements bool
3333
Backend bool
34+
Cloud bool
3435
ProviderRequirements bool
3536
TerraformVersion bool
3637
InstalledProviders bool
@@ -77,6 +78,9 @@ func (s *ModuleStore) queueModuleChange(txn *memdb.Txn, oldMod, newMod *Module)
7778
if len(newMod.Meta.CoreRequirements) > 0 {
7879
cb.Changes.CoreRequirements = true
7980
}
81+
if newMod.Meta.Cloud != nil {
82+
cb.Changes.Cloud = true
83+
}
8084
if newMod.Meta.Backend != nil {
8185
cb.Changes.Backend = true
8286
}
@@ -96,6 +100,9 @@ func (s *ModuleStore) queueModuleChange(txn *memdb.Txn, oldMod, newMod *Module)
96100
if len(oldMod.Meta.CoreRequirements) > 0 {
97101
cb.Changes.CoreRequirements = true
98102
}
103+
if oldMod.Meta.Cloud != nil {
104+
cb.Changes.Cloud = true
105+
}
99106
if oldMod.Meta.Backend != nil {
100107
cb.Changes.Backend = true
101108
}
@@ -116,6 +123,9 @@ func (s *ModuleStore) queueModuleChange(txn *memdb.Txn, oldMod, newMod *Module)
116123
if !oldMod.Meta.Backend.Equals(newMod.Meta.Backend) {
117124
cb.Changes.Backend = true
118125
}
126+
if !oldMod.Meta.Cloud.Equals(newMod.Meta.Cloud) {
127+
cb.Changes.Cloud = true
128+
}
119129
if !oldMod.Meta.ProviderRequirements.Equals(newMod.Meta.ProviderRequirements) {
120130
cb.Changes.ProviderRequirements = true
121131
}

0 commit comments

Comments
 (0)