Skip to content

Commit 5c1a00f

Browse files
authored
logging: Add query related message types (#169)
1 parent 8db10b2 commit 5c1a00f

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

logging_query.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package tfjson
4+
5+
import "encoding/json"
6+
7+
const (
8+
MessageListStart LogMessageType = "list_start"
9+
MessageListResourceFound LogMessageType = "list_resource_found"
10+
MessageListComplete LogMessageType = "list_complete"
11+
)
12+
13+
// ListStartMessage represents "query" result message of type "list_start"
14+
type ListStartMessage struct {
15+
baseLogMessage
16+
ListStart ListStartData `json:"list_start"`
17+
}
18+
19+
type ListStartData struct {
20+
Address string `json:"address"`
21+
ResourceType string `json:"resource_type"`
22+
InputConfig map[string]json.RawMessage `json:"input_config,omitempty"`
23+
}
24+
25+
// ListResourceFoundMessage represents "query" result message of type "list_resource_found"
26+
type ListResourceFoundMessage struct {
27+
baseLogMessage
28+
ListResourceFound ListResourceFoundData `json:"list_resource_found"`
29+
}
30+
31+
type ListResourceFoundData struct {
32+
Address string `json:"address"`
33+
DisplayName string `json:"display_name"`
34+
Identity map[string]json.RawMessage `json:"identity"`
35+
ResourceType string `json:"resource_type"`
36+
ResourceObject map[string]json.RawMessage `json:"resource_object,omitempty"`
37+
Config string `json:"config,omitempty"`
38+
ImportConfig string `json:"import_config,omitempty"`
39+
}
40+
41+
// ListCompleteMessage represents "query" result message of type "list_complete"
42+
type ListCompleteMessage struct {
43+
baseLogMessage
44+
ListComplete ListCompleteData `json:"list_complete"`
45+
}
46+
47+
type ListCompleteData struct {
48+
Address string `json:"address"`
49+
ResourceType string `json:"resource_type"`
50+
Total int `json:"total"`
51+
}

logging_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package tfjson
44

55
import (
6+
"encoding/json"
67
"testing"
78
"time"
89

@@ -97,3 +98,70 @@ func TestLogging_generic(t *testing.T) {
9798
}
9899
}
99100
}
101+
102+
func TestLogging_query(t *testing.T) {
103+
testCases := []struct {
104+
rawMessage string
105+
expectedMessage LogMsg
106+
}{
107+
{
108+
`{"@level":"info","@message":"list.concept_pet.pets: Starting query...","@module":"terraform.ui","@timestamp":"2025-08-28T18:07:11.534006+00:00","list_start":{"address":"list.concept_pet.pets","resource_type":"concept_pet"},"type":"list_start"}`,
109+
ListStartMessage{
110+
baseLogMessage: baseLogMessage{
111+
Lvl: Info,
112+
Msg: "list.concept_pet.pets: Starting query...",
113+
Time: time.Date(2025, 8, 28, 18, 7, 11, 534006000, time.UTC),
114+
},
115+
ListStart: ListStartData{
116+
Address: "list.concept_pet.pets",
117+
ResourceType: "concept_pet",
118+
InputConfig: nil,
119+
},
120+
},
121+
},
122+
{
123+
`{"@level":"info","@message":"list.concept_pet.pets: Result found","@module":"terraform.ui","@timestamp":"2025-08-28T18:07:11.534589+00:00","list_resource_found":{"address":"list.concept_pet.pets","display_name":"This is a easy-antelope","identity":{"id":"easy-antelope","legs":6},"resource_type":"concept_pet"},"type":"list_resource_found"}`,
124+
ListResourceFoundMessage{
125+
baseLogMessage: baseLogMessage{
126+
Lvl: Info,
127+
Msg: "list.concept_pet.pets: Result found",
128+
Time: time.Date(2025, 8, 28, 18, 7, 11, 534589000, time.UTC),
129+
},
130+
ListResourceFound: ListResourceFoundData{
131+
Address: "list.concept_pet.pets",
132+
ResourceType: "concept_pet",
133+
DisplayName: "This is a easy-antelope",
134+
Identity: map[string]json.RawMessage{
135+
"id": json.RawMessage(`"easy-antelope"`),
136+
"legs": json.RawMessage("6"),
137+
},
138+
},
139+
},
140+
},
141+
{
142+
`{"@level":"info","@message":"list.concept_pet.pets: List complete","@module":"terraform.ui","@timestamp":"2025-08-28T18:07:11.534661+00:00","list_complete":{"address":"list.concept_pet.pets","resource_type":"concept_pet","total":5},"type":"list_complete"}`,
143+
ListCompleteMessage{
144+
baseLogMessage: baseLogMessage{
145+
Lvl: Info,
146+
Msg: "list.concept_pet.pets: List complete",
147+
Time: time.Date(2025, 8, 28, 18, 7, 11, 534661000, time.UTC),
148+
},
149+
ListComplete: ListCompleteData{
150+
Address: "list.concept_pet.pets",
151+
ResourceType: "concept_pet",
152+
Total: 5,
153+
},
154+
},
155+
},
156+
}
157+
158+
for _, tc := range testCases {
159+
msg, err := UnmarshalLogMessage([]byte(tc.rawMessage))
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
if diff := cmp.Diff(tc.expectedMessage, msg, cmpOpts); diff != "" {
164+
t.Fatalf("unexpected message: %s", diff)
165+
}
166+
}
167+
}

logging_types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ var allLogMessageTypes = []any{
2121
LogMessage{},
2222
DiagnosticLogMessage{},
2323
UnknownLogMessage{},
24+
25+
// query
26+
ListStartMessage{},
27+
ListResourceFoundMessage{},
28+
ListCompleteMessage{},
2429
}
2530

2631
func unmarshalByType(t LogMessageType, b []byte) (LogMsg, error) {
2732
switch t {
33+
34+
// generic
2835
case MessageTypeVersion:
2936
v := VersionLogMessage{}
3037
return v, json.Unmarshal(b, &v)
@@ -34,6 +41,17 @@ func unmarshalByType(t LogMessageType, b []byte) (LogMsg, error) {
3441
case MessageTypeDiagnostic:
3542
v := DiagnosticLogMessage{}
3643
return v, json.Unmarshal(b, &v)
44+
45+
// query
46+
case MessageListStart:
47+
v := ListStartMessage{}
48+
return v, json.Unmarshal(b, &v)
49+
case MessageListResourceFound:
50+
v := ListResourceFoundMessage{}
51+
return v, json.Unmarshal(b, &v)
52+
case MessageListComplete:
53+
v := ListCompleteMessage{}
54+
return v, json.Unmarshal(b, &v)
3755
}
3856

3957
v := UnknownLogMessage{}

0 commit comments

Comments
 (0)