Skip to content

Commit f6842ce

Browse files
dbanckdsa0x
andauthored
tf query: ready list blocks for beta (#37619) (#37630)
Co-authored-by: Samsondeen <[email protected]>
1 parent 34cbad9 commit f6842ce

31 files changed

+242
-163
lines changed

commands.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ func initCommands(
276276
}, nil
277277
},
278278

279+
"query": func() (cli.Command, error) {
280+
return &command.QueryCommand{
281+
Meta: meta,
282+
}, nil
283+
},
284+
279285
"refresh": func() (cli.Command, error) {
280286
return &command.RefreshCommand{
281287
Meta: meta,
@@ -451,12 +457,6 @@ func initCommands(
451457
}, nil
452458
}
453459

454-
Commands["query"] = func() (cli.Command, error) {
455-
return &command.QueryCommand{
456-
Meta: meta,
457-
}, nil
458-
}
459-
460460
Commands["test cleanup"] = func() (cli.Command, error) {
461461
return &command.TestCleanupCommand{
462462
Meta: meta,

internal/cloud/backend_query.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hashicorp/terraform/internal/genconfig"
2121
"github.com/hashicorp/terraform/internal/terraform"
2222
"github.com/hashicorp/terraform/internal/tfdiags"
23+
"github.com/zclconf/go-cty/cty"
2324
ctyjson "github.com/zclconf/go-cty/cty/json"
2425
)
2526

@@ -276,7 +277,7 @@ func (b *Cloud) cancelQueryRun(cancelCtx context.Context, op *backendrun.Operati
276277
// formatIdentity formats the identity map into a string representation.
277278
// It flattens the map into a string of key=value pairs, separated by commas.
278279
func formatIdentity(identity map[string]json.RawMessage) string {
279-
parts := make([]string, 0, len(identity))
280+
ctyObj := make(map[string]cty.Value, len(identity))
280281
for key, value := range identity {
281282
ty, err := ctyjson.ImpliedType(value)
282283
if err != nil {
@@ -286,9 +287,9 @@ func formatIdentity(identity map[string]json.RawMessage) string {
286287
if err != nil {
287288
continue
288289
}
289-
parts = append(parts, fmt.Sprintf("%s=%s", key, tfdiags.ValueToString(v)))
290+
ctyObj[key] = v
290291
}
291-
return strings.Join(parts, ",")
292+
return tfdiags.ObjectToString(cty.ObjectVal(ctyObj))
292293
}
293294

294295
const queryDefaultHeader = `

internal/cloud/backend_query_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/google/go-cmp/cmp"
1213
"github.com/hashicorp/cli"
1314
"github.com/hashicorp/terraform/internal/addrs"
1415
"github.com/hashicorp/terraform/internal/backend/backendrun"
@@ -121,8 +122,15 @@ func TestCloud_queryJSONBasic(t *testing.T) {
121122
outp := close(t)
122123
gotOut := outp.Stdout()
123124

124-
if !strings.Contains(gotOut, "list.concept_pet.pets id=complete-gannet,legs=6 This is a complete-gannet") {
125-
t.Fatalf("expected query results in output: %s", gotOut)
125+
expectedOut := `list.concept_pet.pets id=large-roughy,legs=2 This is a large-roughy
126+
list.concept_pet.pets id=able-werewolf,legs=5 This is a able-werewolf
127+
list.concept_pet.pets id=complete-gannet,legs=6 This is a complete-gannet
128+
list.concept_pet.pets id=charming-beagle,legs=3 This is a charming-beagle
129+
list.concept_pet.pets id=legal-lamprey,legs=2 This is a legal-lamprey
130+
131+
`
132+
if diff := cmp.Diff(expectedOut, gotOut); diff != "" {
133+
t.Fatalf("expected query results output to be %s, got %s: diff: %s", expectedOut, gotOut, diff)
126134
}
127135

128136
stateMgr, _ := b.StateMgr(testBackendSingleWorkspaceName)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
list "concept_pet" "pets" {}
1+
list "concept_pet" "pets" {
2+
provider = concept
3+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
list "null_resource" "foo" {}
1+
list "null_resource" "foo" {
2+
provider = null
3+
}

internal/command/e2etest/providers_schema_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ func TestProvidersSchema(t *testing.T) {
134134
}
135135
}
136136
},
137+
"list_resource_schemas": {
138+
"simple_resource": {
139+
"version": 0,
140+
"block": {
141+
"attributes": {
142+
"value": {
143+
"type": "string",
144+
"description_kind": "plain",
145+
"optional": true
146+
}
147+
},
148+
"description_kind": "plain"
149+
}
150+
}
151+
},
137152
"resource_identity_schemas": {
138153
"simple_resource": {
139154
"version": 0,
@@ -213,6 +228,21 @@ func TestProvidersSchema(t *testing.T) {
213228
}
214229
}
215230
},
231+
"list_resource_schemas": {
232+
"simple_resource": {
233+
"version": 0,
234+
"block": {
235+
"attributes": {
236+
"value": {
237+
"type": "string",
238+
"description_kind": "plain",
239+
"optional": true
240+
}
241+
},
242+
"description_kind": "plain"
243+
}
244+
}
245+
},
216246
"functions": {
217247
"noop": {
218248
"description": "noop takes any single argument and returns the same value",

internal/command/jsonformat/plan_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8071,7 +8071,7 @@ func runTestCases(t *testing.T, testCases map[string]testCase) {
80718071
return
80728072
}
80738073

8074-
jsonschemas := jsonprovider.MarshalForRenderer(tfschemas, false)
8074+
jsonschemas := jsonprovider.MarshalForRenderer(tfschemas)
80758075
change := structured.FromJsonChange(jsonchanges[0].Change, attribute_path.AlwaysMatcher())
80768076
renderer := Renderer{Colorize: color}
80778077
diff := diff{
@@ -8421,7 +8421,7 @@ func TestResourceChange_deferredActions(t *testing.T) {
84218421
}
84228422

84238423
renderer := Renderer{Colorize: color}
8424-
jsonschemas := jsonprovider.MarshalForRenderer(fullSchema, false)
8424+
jsonschemas := jsonprovider.MarshalForRenderer(fullSchema)
84258425
diffs := precomputeDiffs(Plan{
84268426
DeferredChanges: deferredChanges,
84278427
ProviderSchemas: jsonschemas,
@@ -8714,7 +8714,7 @@ func TestResourceChange_actions(t *testing.T) {
87148714
},
87158715
},
87168716
}
8717-
jsonschemas := jsonprovider.MarshalForRenderer(fullSchema, false)
8717+
jsonschemas := jsonprovider.MarshalForRenderer(fullSchema)
87188718
diffs := precomputeDiffs(Plan{
87198719
ResourceChanges: []jsonplan.ResourceChange{defaultResourceChange},
87208720
ActionInvocations: tc.actionInvocations,

internal/command/jsonformat/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestState(t *testing.T) {
8686
RootModule: root,
8787
RootModuleOutputs: outputs,
8888
ProviderFormatVersion: jsonprovider.FormatVersion,
89-
ProviderSchemas: jsonprovider.MarshalForRenderer(tt.Schemas, false),
89+
ProviderSchemas: jsonprovider.MarshalForRenderer(tt.Schemas),
9090
})
9191

9292
result := done(t).All()

internal/command/jsonprovider/provider.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ func newProviders() *Providers {
4545
// schema into the public structured JSON versions.
4646
//
4747
// This is a format that can be read by the structured plan renderer.
48-
func MarshalForRenderer(s *terraform.Schemas, includeExperimentalSchemas bool) map[string]*Provider {
48+
func MarshalForRenderer(s *terraform.Schemas) map[string]*Provider {
4949
schemas := make(map[string]*Provider, len(s.Providers))
5050
for k, v := range s.Providers {
51-
schemas[k.String()] = marshalProvider(v, includeExperimentalSchemas)
51+
schemas[k.String()] = marshalProvider(v)
5252
}
5353
return schemas
5454
}
5555

56-
func Marshal(s *terraform.Schemas, includeExperimentalSchemas bool) ([]byte, error) {
56+
func Marshal(s *terraform.Schemas) ([]byte, error) {
5757
providers := newProviders()
58-
providers.Schemas = MarshalForRenderer(s, includeExperimentalSchemas)
58+
providers.Schemas = MarshalForRenderer(s)
5959
ret, err := json.Marshal(providers)
6060
return ret, err
6161
}
6262

63-
func marshalProvider(tps providers.ProviderSchema, includeExperimentalSchemas bool) *Provider {
63+
func marshalProvider(tps providers.ProviderSchema) *Provider {
6464
p := &Provider{
6565
Provider: marshalSchema(tps.Provider),
6666
ResourceSchemas: marshalSchemas(tps.ResourceTypes),
@@ -71,20 +71,18 @@ func marshalProvider(tps providers.ProviderSchema, includeExperimentalSchemas bo
7171
ActionSchemas: marshalActionSchemas(tps.Actions),
7272
}
7373

74-
if includeExperimentalSchemas {
75-
// List resource schemas are nested under a "config" block, so we need to
76-
// extract that block to get the actual provider schema for the list resource.
77-
// When getting the provider schemas, Terraform adds this extra level to
78-
// better match the actual configuration structure.
79-
listSchemas := make(map[string]providers.Schema, len(tps.ListResourceTypes))
80-
for k, v := range tps.ListResourceTypes {
81-
listSchemas[k] = providers.Schema{
82-
Body: &v.Body.BlockTypes["config"].Block,
83-
Version: v.Version,
84-
}
74+
// List resource schemas are nested under a "config" block, so we need to
75+
// extract that block to get the actual provider schema for the list resource.
76+
// When getting the provider schemas, Terraform adds this extra level to
77+
// better match the actual configuration structure.
78+
listSchemas := make(map[string]providers.Schema, len(tps.ListResourceTypes))
79+
for k, v := range tps.ListResourceTypes {
80+
listSchemas[k] = providers.Schema{
81+
Body: &v.Body.BlockTypes["config"].Block,
82+
Version: v.Version,
8583
}
86-
p.ListResourceSchemas = marshalSchemas(listSchemas)
8784
}
85+
p.ListResourceSchemas = marshalSchemas(listSchemas)
8886

8987
return p
9088
}

internal/command/jsonprovider/provider_test.go

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,23 @@ var cmpOpts = cmpopts.IgnoreUnexported(Provider{})
2020

2121
func TestMarshalProvider(t *testing.T) {
2222
tests := []struct {
23-
Input providers.ProviderSchema
24-
IncludeExperimental bool
25-
Want *Provider
23+
Input providers.ProviderSchema
24+
Want *Provider
2625
}{
2726
{
2827
providers.ProviderSchema{},
29-
false,
3028
&Provider{
3129
Provider: &Schema{},
3230
ResourceSchemas: map[string]*Schema{},
3331
DataSourceSchemas: map[string]*Schema{},
3432
EphemeralResourceSchemas: map[string]*Schema{},
3533
ResourceIdentitySchemas: map[string]*IdentitySchema{},
34+
ListResourceSchemas: map[string]*Schema{},
3635
ActionSchemas: map[string]*ActionSchema{},
3736
},
3837
},
3938
{
4039
testProvider(),
41-
false,
4240
&Provider{
4341
Provider: &Schema{
4442
Block: &Block{
@@ -212,53 +210,6 @@ func TestMarshalProvider(t *testing.T) {
212210
},
213211
},
214212
},
215-
ResourceIdentitySchemas: map[string]*IdentitySchema{},
216-
ActionSchemas: map[string]*ActionSchema{},
217-
},
218-
},
219-
{
220-
providers.ProviderSchema{
221-
ListResourceTypes: map[string]providers.Schema{
222-
"test_list_resource": {
223-
Version: 1,
224-
Body: &configschema.Block{
225-
Attributes: map[string]*configschema.Attribute{
226-
"data": {
227-
Type: cty.DynamicPseudoType,
228-
Computed: true,
229-
},
230-
},
231-
BlockTypes: map[string]*configschema.NestedBlock{
232-
"config": {
233-
Block: configschema.Block{
234-
Attributes: map[string]*configschema.Attribute{
235-
"filter": {Type: cty.String, Optional: true},
236-
"items": {Type: cty.List(cty.String), Required: true},
237-
},
238-
},
239-
Nesting: configschema.NestingSingle,
240-
},
241-
},
242-
},
243-
},
244-
},
245-
Actions: map[string]providers.ActionSchema{
246-
"test_action": {
247-
ConfigSchema: &configschema.Block{
248-
Attributes: map[string]*configschema.Attribute{
249-
"opt_attr": {Type: cty.String, Optional: true},
250-
"req_attr": {Type: cty.List(cty.String), Required: true},
251-
},
252-
},
253-
},
254-
},
255-
},
256-
true,
257-
&Provider{
258-
Provider: &Schema{},
259-
ResourceSchemas: map[string]*Schema{},
260-
DataSourceSchemas: map[string]*Schema{},
261-
EphemeralResourceSchemas: map[string]*Schema{},
262213
ListResourceSchemas: map[string]*Schema{
263214
"test_list_resource": {
264215
Version: 1,
@@ -305,7 +256,7 @@ func TestMarshalProvider(t *testing.T) {
305256

306257
for i, test := range tests {
307258
t.Run(fmt.Sprint(i), func(t *testing.T) {
308-
got := marshalProvider(test.Input, test.IncludeExperimental)
259+
got := marshalProvider(test.Input)
309260
if diff := cmp.Diff(test.Want, got, cmpOpts); diff != "" {
310261
t.Fatalf("wrong result:\n %s\n", diff)
311262
}
@@ -431,5 +382,15 @@ func testProvider() providers.ProviderSchema {
431382
},
432383
},
433384
},
385+
Actions: map[string]providers.ActionSchema{
386+
"test_action": {
387+
ConfigSchema: &configschema.Block{
388+
Attributes: map[string]*configschema.Attribute{
389+
"opt_attr": {Type: cty.String, Optional: true},
390+
"req_attr": {Type: cty.List(cty.String), Required: true},
391+
},
392+
},
393+
},
394+
},
434395
}
435396
}

0 commit comments

Comments
 (0)