Skip to content

Commit

Permalink
fix: remove telementry hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Gamunu Balagalla <gamunu@fastcode.io>
  • Loading branch information
gamunu committed Jul 17, 2024
1 parent e595118 commit 1ec6ee1
Show file tree
Hide file tree
Showing 12 changed files with 9 additions and 299 deletions.
41 changes: 0 additions & 41 deletions .github/workflows/add-copyright-headers.yml

This file was deleted.

59 changes: 0 additions & 59 deletions .github/workflows/build-pr.yml

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:

build:
runs-on: ubuntu-latest
needs: [generate-provider-schemas]
strategy:
matrix:
include:
Expand Down
36 changes: 0 additions & 36 deletions .github/workflows/check-changelog.yml

This file was deleted.

2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chigopher/pathlib v0.19.1 h1:RoLlUJc0CqBGwq239cilyhxPNLXTK+HXoASGyGznx5A=
github.com/chigopher/pathlib v0.19.1/go.mod h1:tzC1dZLW8o33UQpWkNkhvPwL5n4yyFRFm/jL1YGWFvY=
github.com/chigopher/pathlib v1.0.0 h1:SbsCrFX4vDf4M2d8mT/RTzuVlKOjTKoPHK0HidsQFak=
github.com/chigopher/pathlib v1.0.0/go.mod h1:3+YPPV21mU9vyw8Mjp+F33CyCfE6iOzinpiqBcccv7I=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
2 changes: 0 additions & 2 deletions internal/features/modules/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ package hooks
import (
"log"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/hashicorp/terraform-ls/internal/features/modules/state"
"github.com/hashicorp/terraform-ls/internal/registry"
)

type Hooks struct {
ModStore *state.ModuleStore
RegistryClient registry.Client
AlgoliaClient *search.Client
Logger *log.Logger
}
50 changes: 2 additions & 48 deletions internal/features/modules/hooks/module_source_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,16 @@ package hooks

import (
"context"
"strings"

"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/zclconf/go-cty/cty"
"strings"
)

type RegistryModule struct {
FullName string `json:"full-name"`
Description string `json:"description"`
}

const algoliaModuleIndex = "tf-registry:prod:modules"

func (h *Hooks) fetchModulesFromAlgolia(ctx context.Context, term string) ([]RegistryModule, error) {
modules := make([]RegistryModule, 0)

index := h.AlgoliaClient.InitIndex(algoliaModuleIndex)
params := []interface{}{
ctx, // transport.Request will magically extract the context from here
opt.AttributesToRetrieve("full-name", "description"),
opt.HitsPerPage(10),
}

res, err := index.Search(term, params...)
if err != nil {
return modules, err
}

err = res.UnmarshalHits(&modules)
if err != nil {
return modules, err

}

return modules, nil
}

func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]decoder.Candidate, error) {
candidates := make([]decoder.Candidate, 0)
prefix := value.AsString()
Expand All @@ -54,24 +25,7 @@ func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]d
return candidates, nil
}

if h.AlgoliaClient == nil {
return candidates, nil
}

modules, err := h.fetchModulesFromAlgolia(ctx, prefix)
if err != nil {
h.Logger.Printf("Error fetching modules from Algolia: %#v", err)
return candidates, err
}

for _, mod := range modules {
c := decoder.ExpressionCompletionCandidate(decoder.ExpressionCandidate{
Value: cty.StringVal(mod.FullName),
Detail: "registry",
Description: lang.PlainText(mod.Description),
})
candidates = append(candidates, c)
}
//TODO: figure out how we can do module autocompletion

return candidates, nil
}
43 changes: 6 additions & 37 deletions internal/features/modules/hooks/module_source_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package hooks
import (
"context"
"crypto/tls"
"fmt"
"io"
"log"
"net"
Expand Down Expand Up @@ -91,28 +90,9 @@ func TestHooks_RegistryModuleSources(t *testing.T) {
t.Fatal(err)
}

searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/1/indexes/tf-registry%3Aprod%3Amodules/query" {
b, _ := io.ReadAll(r.Body)

if strings.Contains(string(b), "query=aws") {
w.Write([]byte(responseAWS))
return
} else if strings.Contains(string(b), "query=err") {
http.Error(w, responseErr, http.StatusForbidden)
return
}

w.Write([]byte(responseEmpty))
return
}
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))

h := &Hooks{
ModStore: store,
AlgoliaClient: searchClient,
Logger: log.New(io.Discard, "", 0),
ModStore: store,
Logger: log.New(io.Discard, "", 0),
}

tests := []struct {
Expand Down Expand Up @@ -186,15 +166,9 @@ func TestHooks_RegistryModuleSourcesCtxCancel(t *testing.T) {
t.Fatal(err)
}

searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))

h := &Hooks{
ModStore: store,
AlgoliaClient: searchClient,
Logger: log.New(io.Discard, "", 0),
ModStore: store,
Logger: log.New(io.Discard, "", 0),
}

_, err = h.RegistryModuleSources(ctx, cty.StringVal("aws"))
Expand All @@ -220,14 +194,9 @@ func TestHooks_RegistryModuleSourcesIgnore(t *testing.T) {
t.Fatal(err)
}

searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))

h := &Hooks{
ModStore: store,
AlgoliaClient: searchClient,
Logger: log.New(io.Discard, "", 0),
ModStore: store,
Logger: log.New(io.Discard, "", 0),
}

tests := []struct {
Expand Down
13 changes: 0 additions & 13 deletions internal/features/modules/modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"io"
"log"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/algolia"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/eventbus"
fdecoder "github.com/hashicorp/terraform-ls/internal/features/modules/decoder"
Expand All @@ -23,7 +21,6 @@ import (
"github.com/hashicorp/terraform-ls/internal/langserver/diagnostics"
"github.com/hashicorp/terraform-ls/internal/registry"
globalState "github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/telemetry"
"github.com/hashicorp/terraform-schema/backend"
tfmod "github.com/hashicorp/terraform-schema/module"
)
Expand Down Expand Up @@ -170,11 +167,6 @@ func (f *ModulesFeature) AppendCompletionHooks(srvCtx context.Context, decoderCo
Logger: f.logger,
}

credentials, ok := algolia.CredentialsFromContext(srvCtx)
if ok {
h.AlgoliaClient = search.NewClient(credentials.AppID, credentials.APIKey)
}

decoderContext.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources
decoderContext.CompletionHooks["CompleteRegistryModuleSources"] = h.RegistryModuleSources
decoderContext.CompletionHooks["CompleteRegistryModuleVersions"] = h.RegistryModuleVersions
Expand Down Expand Up @@ -247,11 +239,6 @@ func (f *ModulesFeature) Telemetry(path string) map[string]interface{} {
if len(mod.Meta.ProviderRequirements) > 0 {
reqs := make(map[string]string, 0)
for pAddr, cons := range mod.Meta.ProviderRequirements {
if telemetry.IsPublicProvider(pAddr) {
reqs[pAddr.String()] = cons.String()
continue
}

// anonymize any unknown providers or the ones not publicly listed
id, err := f.stateStore.ProviderSchemas.GetProviderID(pAddr)
if err != nil {
Expand Down
Loading

0 comments on commit 1ec6ee1

Please sign in to comment.