Skip to content

Commit 85115e3

Browse files
committed
fix empty response from get requests
1 parent dd3e301 commit 85115e3

File tree

2 files changed

+147
-15
lines changed

2 files changed

+147
-15
lines changed

internal/resource/resource_plugin.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type plusAPIErr struct {
4444
Href string `json:"href"`
4545
}
4646

47+
const emptyResponse = "{}"
48+
4749
var _ bus.Plugin = (*Resource)(nil)
4850

4951
func NewResource(agentConfig *config.Config) *Resource {
@@ -238,6 +240,7 @@ func (r *Resource) handleUpdateStreamServers(ctx context.Context, action *mpi.NG
238240
func (r *Resource) handleGetStreamUpstreams(ctx context.Context, instance *mpi.Instance) {
239241
correlationID := logger.GetCorrelationID(ctx)
240242
instanceID := instance.GetInstanceMeta().GetInstanceId()
243+
streamUpstreamsResponse := emptyResponse
241244

242245
streamUpstreams, err := r.resourceService.GetStreamUpstreams(ctx, instance)
243246
if err != nil {
@@ -249,13 +252,16 @@ func (r *Resource) handleGetStreamUpstreams(ctx context.Context, instance *mpi.I
249252
return
250253
}
251254

252-
streamUpstreamsJSON, err := json.Marshal(streamUpstreams)
253-
if err != nil {
254-
slog.ErrorContext(ctx, "Unable to marshal stream upstreams", "err", err)
255+
if streamUpstreams != nil {
256+
streamUpstreamsJSON, jsonErr := json.Marshal(streamUpstreams)
257+
if jsonErr != nil {
258+
slog.ErrorContext(ctx, "Unable to marshal stream upstreams", "err", err)
259+
}
260+
streamUpstreamsResponse = string(streamUpstreamsJSON)
255261
}
256262

257263
resp := r.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK,
258-
string(streamUpstreamsJSON), instanceID, "")
264+
streamUpstreamsResponse, instanceID, "")
259265

260266
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: resp})
261267
}
@@ -264,6 +270,7 @@ func (r *Resource) handleGetStreamUpstreams(ctx context.Context, instance *mpi.I
264270
func (r *Resource) handleGetUpstreams(ctx context.Context, instance *mpi.Instance) {
265271
correlationID := logger.GetCorrelationID(ctx)
266272
instanceID := instance.GetInstanceMeta().GetInstanceId()
273+
upstreamsResponse := emptyResponse
267274

268275
upstreams, err := r.resourceService.GetUpstreams(ctx, instance)
269276
if err != nil {
@@ -275,13 +282,16 @@ func (r *Resource) handleGetUpstreams(ctx context.Context, instance *mpi.Instanc
275282
return
276283
}
277284

278-
upstreamsJSON, err := json.Marshal(upstreams)
279-
if err != nil {
280-
slog.ErrorContext(ctx, "Unable to marshal upstreams", "err", err)
285+
if upstreams != nil {
286+
upstreamsJSON, jsonErr := json.Marshal(upstreams)
287+
if jsonErr != nil {
288+
slog.ErrorContext(ctx, "Unable to marshal upstreams", "err", err)
289+
}
290+
upstreamsResponse = string(upstreamsJSON)
281291
}
282292

283293
resp := r.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK,
284-
string(upstreamsJSON), instanceID, "")
294+
upstreamsResponse, instanceID, "")
285295

286296
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: resp})
287297
}
@@ -321,6 +331,7 @@ func (r *Resource) handleGetHTTPUpstreamServers(ctx context.Context, action *mpi
321331
) {
322332
correlationID := logger.GetCorrelationID(ctx)
323333
instanceID := instance.GetInstanceMeta().GetInstanceId()
334+
upstreamsResponse := emptyResponse
324335

325336
upstreams, err := r.resourceService.GetHTTPUpstreamServers(ctx, instance,
326337
action.GetGetHttpUpstreamServers().GetHttpUpstreamName())
@@ -333,13 +344,15 @@ func (r *Resource) handleGetHTTPUpstreamServers(ctx context.Context, action *mpi
333344
return
334345
}
335346

336-
upstreamsJSON, err := json.Marshal(upstreams)
337-
if err != nil {
338-
slog.ErrorContext(ctx, "Unable to marshal http upstreams", "err", err)
347+
if upstreams != nil {
348+
upstreamsJSON, jsonErr := json.Marshal(upstreams)
349+
if jsonErr != nil {
350+
slog.ErrorContext(ctx, "Unable to marshal http upstreams", "err", err)
351+
}
352+
upstreamsResponse = string(upstreamsJSON)
339353
}
340-
341354
resp := r.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK,
342-
string(upstreamsJSON), instanceID, "")
355+
upstreamsResponse, instanceID, "")
343356

344357
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: resp})
345358
}

test/mock/grpc/README.md

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,11 @@ Example response:
279279
}
280280
]
281281
```
282+
282283
## POST /api/v1/requests
283-
Used to send management plane requests over the Subscribe rpc stream to the NGINX Agent. \
284-
Example request body:
284+
Used to send management plane requests over the Subscribe rpc stream to the NGINX Agent.
285+
286+
Example request body to health request:
285287
```
286288
{
287289
"message_meta": {
@@ -292,6 +294,123 @@ Example request body:
292294
"health_request": {}
293295
}
294296
```
297+
298+
Example request body to get HTTP upstream servers example:
299+
```
300+
{
301+
"message_meta": {
302+
"message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184",
303+
"correlation_id": "79794c1c-8e91-47c1-a92c-b9a0c3f1a263",
304+
"timestamp": "2023-01-15T01:30:15.01Z"
305+
},
306+
"action_request": {
307+
"instance_id": "e8d1bda6-397e-3b98-a179-e500ff99fbc7",
308+
"nginx_plus_action": {
309+
"get_http_upstream_servers": {
310+
"http_upstream_name": "nginx1"
311+
}
312+
}
313+
}
314+
}
315+
```
316+
317+
Example request body to update HTTP upstream servers example:
318+
```
319+
{
320+
"message_meta": {
321+
"message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184",
322+
"correlation_id": "79794c1c-8e91-47c1-a92c-b9a0c3f1a263",
323+
"timestamp": "2023-01-15T01:30:15.01Z"
324+
},
325+
"action_request": {
326+
"instance_id": "e8d1bda6-397e-3b98-a179-e500ff99fbc7",
327+
"nginx_plus_action": {
328+
"update_http_upstream_servers": {
329+
"http_upstream_name": "nginx1",
330+
"servers": [
331+
{
332+
"max_conns": 0,
333+
"max_fails": 3,
334+
"backup": false,
335+
"down": false,
336+
"weight": 1,
337+
"server": "127.0.0.1:9081",
338+
"fail_timeout": "10s",
339+
"slow_start": "0s"
340+
}
341+
]
342+
}
343+
}
344+
}
345+
}
346+
```
347+
348+
Example request body to update upstream stream servers example:
349+
```
350+
{
351+
"message_meta": {
352+
"message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184",
353+
"correlation_id": "79794c1c-8e91-47c1-a92c-b9a0c3f1a263",
354+
"timestamp": "2023-01-15T01:30:15.01Z"
355+
},
356+
"action_request": {
357+
"instance_id": "e8d1bda6-397e-3b98-a179-e500ff99fbc7",
358+
"nginx_plus_action": {
359+
"update_stream_servers": {
360+
"upstream_stream_name": "stream_backend",
361+
"servers": [
362+
{
363+
"id" : 0,
364+
"server" : "10.0.0.1:12348",
365+
"weight" : 1,
366+
"max_conns" : 0,
367+
"max_fails" : 1,
368+
"fail_timeout" : "10s",
369+
"slow_start" : 0,
370+
"backup" : false,
371+
"down" : false
372+
}
373+
]
374+
}
375+
}
376+
}
377+
}
378+
```
379+
380+
Example request body to get upstreams example:
381+
```
382+
{
383+
"message_meta": {
384+
"message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184",
385+
"correlation_id": "79794c1c-8e91-47c1-a92c-b9a0c3f1a263",
386+
"timestamp": "2023-01-15T01:30:15.01Z"
387+
},
388+
"action_request": {
389+
"instance_id": "e8d1bda6-397e-3b98-a179-e500ff99fbc7",
390+
"nginx_plus_action": {
391+
"get_upstreams": {}
392+
}
393+
}
394+
}
395+
```
396+
397+
Example request body to get stream upstreams example:
398+
```
399+
{
400+
"message_meta": {
401+
"message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184",
402+
"correlation_id": "79794c1c-8e91-47c1-a92c-b9a0c3f1a263",
403+
"timestamp": "2023-01-15T01:30:15.01Z"
404+
},
405+
"action_request": {
406+
"instance_id": "e8d1bda6-397e-3b98-a179-e500ff99fbc7",
407+
"nginx_plus_action": {
408+
"get_stream_upstreams": {}
409+
}
410+
}
411+
}
412+
```
413+
295414
## POST /api/v1/instance/\<instance id\>/config/apply
296415
Used to send management plane config apply request over the Subscribe rpc stream to the NGINX Agent for a particular data plane instance.
297416

0 commit comments

Comments
 (0)