Skip to content

Commit

Permalink
Merge branch 'main' into pass-ephemeral-variables-to-terraform-apply
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt authored Nov 7, 2024
2 parents eccf39a + eeacced commit 9097369
Show file tree
Hide file tree
Showing 29 changed files with 338 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ jobs:
- name: "Code consistency checks"
run: |
make fmtcheck importscheck copyright generate staticcheck exhaustive protobuf
make fmtcheck importscheck vetcheck copyright generate staticcheck exhaustive protobuf
if [[ -n "$(git status --porcelain)" ]]; then
echo >&2 "ERROR: Generated files are inconsistent. Run 'make generate' and 'make protobuf' locally and then commit the updated files."
git >&2 status --porcelain
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ fmtcheck:
importscheck:
"$(CURDIR)/scripts/goimportscheck.sh"

vetcheck:
@echo "==> Checking that the code complies with go vet requirements"
@go vet ./...

staticcheck:
"$(CURDIR)/scripts/staticcheck.sh"

Expand Down Expand Up @@ -52,4 +56,4 @@ website/build-local:
# under parallel conditions.
.NOTPARALLEL:

.PHONY: fmtcheck importscheck generate protobuf staticcheck syncdeps website website/local website/build-local
.PHONY: fmtcheck importscheck vetcheck generate protobuf staticcheck syncdeps website website/local website/build-local
4 changes: 2 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func initCommands(
View: views.NewView(streams).SetRunningInAutomation(inAutomation),

Color: true,
GlobalPluginDirs: globalPluginDirs(),
GlobalPluginDirs: cliconfig.GlobalPluginDirs(),
Ui: Ui,

Services: services,
Expand Down Expand Up @@ -475,6 +475,6 @@ func makeShutdownCh() <-chan struct{} {
}

func credentialsSource(config *cliconfig.Config) (auth.CredentialsSource, error) {
helperPlugins := pluginDiscovery.FindPlugins("credentials", globalPluginDirs())
helperPlugins := pluginDiscovery.FindPlugins("credentials", cliconfig.GlobalPluginDirs())
return config.CredentialsSource(helperPlugins)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ require (
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,9 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo=
github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
Expand Down
4 changes: 2 additions & 2 deletions internal/addrs/module_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ func TestModuleInstanceEqual_false(t *testing.T) {
func BenchmarkStringShort(b *testing.B) {
addr, _ := ParseModuleInstanceStr(`module.foo`)
for n := 0; n < b.N; n++ {
addr.String()
_ = addr.String()
}
}

func BenchmarkStringLong(b *testing.B) {
addr, _ := ParseModuleInstanceStr(`module.southamerica-brazil-region.module.user-regional-desktops.module.user-name`)
for n := 0; n < b.N; n++ {
addr.String()
_ = addr.String()
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/addrs/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ func TestModuleString(t *testing.T) {
func BenchmarkModuleStringShort(b *testing.B) {
module := Module{"a", "b"}
for n := 0; n < b.N; n++ {
module.String()
_ = module.String()
}
}

func BenchmarkModuleStringLong(b *testing.B) {
module := Module{"southamerica-brazil-region", "user-regional-desktop", "user-name"}
for n := 0; n < b.N; n++ {
module.String()
_ = module.String()
}
}
2 changes: 1 addition & 1 deletion internal/backend/remote-state/azure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions internal/backend/remote-state/azure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/remote-state/kubernetes/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions internal/backend/remote-state/kubernetes/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
4 changes: 4 additions & 0 deletions internal/command/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ func TestApply_parallelism(t *testing.T) {
// to proceed in unison.
beginCtx, begin := context.WithCancel(context.Background())

// This just makes go vet happy, in reality the function will never exit if
// begin() isn't called inside ApplyResourceChangeFn.
defer begin()

// Since our mock provider has its own mutex preventing concurrent calls
// to ApplyResourceChange, we need to use a number of separate providers
// here. They will all have the same mock implementation function assigned
Expand Down
10 changes: 4 additions & 6 deletions plugins.go → internal/command/cliconfig/plugins.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package main
package cliconfig

import (
"fmt"
"log"
"path/filepath"
"runtime"

"github.com/hashicorp/terraform/internal/command/cliconfig"
)

// globalPluginDirs returns directories that should be searched for
// GlobalPluginDirs returns directories that should be searched for
// globally-installed plugins (not specific to the current configuration).
//
// Earlier entries in this slice get priority over later when multiple copies
// of the same plugin version are found, but newer versions always override
// older versions where both satisfy the provider version constraints.
func globalPluginDirs() []string {
func GlobalPluginDirs() []string {
var ret []string
// Look in ~/.terraform.d/plugins/ , or its equivalent on non-UNIX
dir, err := cliconfig.ConfigDir()
dir, err := ConfigDir()
if err != nil {
log.Printf("[ERROR] Error finding global config directory: %s", err)
} else {
Expand Down
4 changes: 4 additions & 0 deletions internal/command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,10 @@ func TestPlan_parallelism(t *testing.T) {
// to proceed in unison.
beginCtx, begin := context.WithCancel(context.Background())

// This just makes go vet happy, in reality the function will never exit if
// begin() isn't called inside ApplyResourceChangeFn.
defer begin()

// Since our mock provider has its own mutex preventing concurrent calls
// to ApplyResourceChange, we need to use a number of separate providers
// here. They will all have the same mock implementation function assigned
Expand Down
7 changes: 7 additions & 0 deletions internal/command/views/hook_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ func (h *jsonHook) PostRefresh(id terraform.HookResourceIdentity, dk addrs.Depos
}

func (h *jsonHook) PreEphemeralOp(id terraform.HookResourceIdentity, action plans.Action) (terraform.HookAction, error) {
// this uses the same plans.Read action as a data source to indicate that
// the ephemeral resource can't be processed until apply, so there is no
// progress hook
if action == plans.Read {
return terraform.HookActionContinue, nil
}

h.view.Hook(json.NewEphemeralOpStart(id.Addr, action))
progress := resourceProgress{
addr: id.Addr,
Expand Down
25 changes: 3 additions & 22 deletions internal/command/views/hook_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func TestJSONHook_EphemeralOp_progress(t *testing.T) {
action, err := hook.PreEphemeralOp(testJSONHookResourceID(addr), plans.Open)
testHookReturnValues(t, action, err)

time.Sleep(3100 * time.Millisecond)
time.Sleep(2005 * time.Millisecond)

action, err = hook.PostEphemeralOp(testJSONHookResourceID(addr), plans.Open, nil)
testHookReturnValues(t, action, err)
Expand Down Expand Up @@ -478,31 +478,12 @@ func TestJSONHook_EphemeralOp_progress(t *testing.T) {
},
{
"@level": "info",
"@message": "test_instance.boop: Still opening... [3s elapsed]",
"@module": "terraform.ui",
"type": "ephemeral_op_progress",
"hook": map[string]interface{}{
"action": string("open"),
"elapsed_seconds": float64(3),
"resource": map[string]interface{}{
"addr": string("test_instance.boop"),
"implied_provider": string("test"),
"module": string(""),
"resource": string("test_instance.boop"),
"resource_key": nil,
"resource_name": string("boop"),
"resource_type": string("test_instance"),
},
},
},
{
"@level": "info",
"@message": "test_instance.boop: Opening complete after 3s",
"@message": "test_instance.boop: Opening complete after 2s",
"@module": "terraform.ui",
"type": "ephemeral_op_complete",
"hook": map[string]interface{}{
"action": string("open"),
"elapsed_seconds": float64(3),
"elapsed_seconds": float64(2),
"resource": map[string]interface{}{
"addr": string("test_instance.boop"),
"implied_provider": string("test"),
Expand Down
20 changes: 15 additions & 5 deletions internal/command/views/hook_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ func (h *UiHook) PreEphemeralOp(rId terraform.HookResourceIdentity, action plans
var operation string
var op uiResourceOp
switch action {
case plans.Read:
// FIXME: this uses the same semantics as data sources, where "read"
// means deferred until apply, but because data sources don't implement
// hooks, and the meaning of Read is overloaded, we can't rely on any
// existing hooks
operation = "Configuration unknown, deferring..."
case plans.Open:
operation = "Opening..."
op = uiResourceOpen
Expand All @@ -367,6 +373,15 @@ func (h *UiHook) PreEphemeralOp(rId terraform.HookResourceIdentity, action plans
return terraform.HookActionContinue, nil
}

h.println(fmt.Sprintf(
h.view.colorize.Color("[reset][bold]%s: %s"),
rId.Addr, operation,
))

if action == plans.Read {
return terraform.HookActionContinue, nil
}

uiState := uiResourceState{
Address: key,
Op: op,
Expand All @@ -379,11 +394,6 @@ func (h *UiHook) PreEphemeralOp(rId terraform.HookResourceIdentity, action plans
h.resources[key] = uiState
h.resourcesLock.Unlock()

h.println(fmt.Sprintf(
h.view.colorize.Color("[reset][bold]%s: %s"),
rId.Addr, operation,
))

go h.stillRunning(uiState)

return terraform.HookActionContinue, nil
Expand Down
1 change: 0 additions & 1 deletion internal/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func (c *Config) TargetExists(target addrs.Targetable) bool {
default:
panic(fmt.Errorf("unrecognized targetable type: %d", target.AddrType()))
}
return true
}

// EntersNewPackage returns true if this call is to an external module, either
Expand Down
1 change: 0 additions & 1 deletion internal/grpcwrap/provider6.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ func (p *provider6) CloseEphemeralResource(_ context.Context, req *tfplugin6.Clo

func (p *provider6) GetFunctions(context.Context, *tfplugin6.GetFunctions_Request) (*tfplugin6.GetFunctions_Response, error) {
panic("unimplemented")
return nil, nil
}

func (p *provider6) CallFunction(_ context.Context, req *tfplugin6.CallFunction_Request) (*tfplugin6.CallFunction_Response, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/moduletest/hcl/variable_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestFileVariables(t *testing.T) {
FileVariables: func() map[string]hcl.Expression {
vars := make(map[string]hcl.Expression)
for name, value := range tc.Values {
expr, diags := hclsyntax.ParseExpression([]byte(value), "test.tf", hcl.Pos{0, 0, 0})
expr, diags := hclsyntax.ParseExpression([]byte(value), "test.tf", hcl.Pos{Line: 0, Column: 0, Byte: 0})
if len(diags) > 0 {
t.Fatalf("unexpected errors: %v", diags)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/resources/ephemeral/ephemeral_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ type resourceInstanceInternal struct {
func (r *resourceInstanceInternal) close(ctx context.Context) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics

// if the resource could not be opened, there will not be anything to close either
if r.impl == nil {
return diags
}

// Stop renewing, if indeed we are. If we previously saw any errors during
// renewing then they finally get returned here, to be reported along with
// any errors during close.
Expand Down
40 changes: 0 additions & 40 deletions internal/rpcapi/credentials_source.go

This file was deleted.

Loading

0 comments on commit 9097369

Please sign in to comment.