-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
2,293 additions
and
406 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (c) F5, Inc. | ||
// | ||
// This source code is licensed under the Apache License, Version 2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
package resource | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log/slog" | ||
|
||
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" | ||
"github.com/nginx/agent/v3/internal/datasource/proto" | ||
"github.com/nginx/agent/v3/internal/logger" | ||
) | ||
|
||
const emptyResponse = "{}" | ||
|
||
type APIAction struct { | ||
ResourceService resourceServiceInterface | ||
} | ||
|
||
func (a *APIAction) HandleUpdateStreamServersRequest(ctx context.Context, action *mpi.NGINXPlusAction, | ||
instance *mpi.Instance, | ||
) *mpi.DataPlaneResponse { | ||
correlationID := logger.GetCorrelationID(ctx) | ||
instanceID := instance.GetInstanceMeta().GetInstanceId() | ||
|
||
add, update, del, err := a.ResourceService.UpdateStreamServers(ctx, instance, | ||
action.GetUpdateStreamServers().GetUpstreamStreamName(), action.GetUpdateStreamServers().GetServers()) | ||
if err != nil { | ||
slog.ErrorContext(ctx, "Unable to update stream servers of upstream", "request", | ||
action.GetUpdateHttpUpstreamServers(), "error", err) | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, | ||
"", instanceID, err.Error()) | ||
} | ||
|
||
slog.DebugContext(ctx, "Successfully updated stream upstream servers", "http_upstream_name", | ||
action.GetUpdateHttpUpstreamServers().GetHttpUpstreamName(), "add", len(add), "update", len(update), | ||
"delete", len(del)) | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, | ||
"Successfully updated stream upstream servers", instanceID, "") | ||
} | ||
|
||
func (a *APIAction) HandleGetStreamUpstreamsRequest(ctx context.Context, | ||
instance *mpi.Instance, | ||
) *mpi.DataPlaneResponse { | ||
correlationID := logger.GetCorrelationID(ctx) | ||
instanceID := instance.GetInstanceMeta().GetInstanceId() | ||
streamUpstreamsResponse := emptyResponse | ||
|
||
streamUpstreams, err := a.ResourceService.GetStreamUpstreams(ctx, instance) | ||
if err != nil { | ||
slog.ErrorContext(ctx, "Unable to get stream upstreams", "error", err) | ||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, | ||
"", instanceID, err.Error()) | ||
} | ||
|
||
if streamUpstreams != nil { | ||
streamUpstreamsJSON, jsonErr := json.Marshal(streamUpstreams) | ||
if jsonErr != nil { | ||
slog.ErrorContext(ctx, "Unable to marshal stream upstreams", "err", err) | ||
} | ||
streamUpstreamsResponse = string(streamUpstreamsJSON) | ||
} | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, | ||
streamUpstreamsResponse, instanceID, "") | ||
} | ||
|
||
func (a *APIAction) HandleGetUpstreamsRequest(ctx context.Context, instance *mpi.Instance) *mpi.DataPlaneResponse { | ||
correlationID := logger.GetCorrelationID(ctx) | ||
instanceID := instance.GetInstanceMeta().GetInstanceId() | ||
upstreamsResponse := emptyResponse | ||
|
||
upstreams, err := a.ResourceService.GetUpstreams(ctx, instance) | ||
if err != nil { | ||
slog.InfoContext(ctx, "Unable to get upstreams", "error", err) | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, | ||
"", instanceID, err.Error()) | ||
} | ||
|
||
if upstreams != nil { | ||
upstreamsJSON, jsonErr := json.Marshal(upstreams) | ||
if jsonErr != nil { | ||
slog.ErrorContext(ctx, "Unable to marshal upstreams", "err", err) | ||
} | ||
upstreamsResponse = string(upstreamsJSON) | ||
} | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, | ||
upstreamsResponse, instanceID, "") | ||
} | ||
|
||
func (a *APIAction) HandleUpdateHTTPUpstreamsRequest(ctx context.Context, action *mpi.NGINXPlusAction, | ||
instance *mpi.Instance, | ||
) *mpi.DataPlaneResponse { | ||
correlationID := logger.GetCorrelationID(ctx) | ||
instanceID := instance.GetInstanceMeta().GetInstanceId() | ||
|
||
add, update, del, err := a.ResourceService.UpdateHTTPUpstreamServers(ctx, instance, | ||
action.GetUpdateHttpUpstreamServers().GetHttpUpstreamName(), | ||
action.GetUpdateHttpUpstreamServers().GetServers()) | ||
if err != nil { | ||
slog.ErrorContext(ctx, "Unable to update HTTP servers of upstream", "request", | ||
action.GetUpdateHttpUpstreamServers(), "error", err) | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, | ||
"", instanceID, err.Error()) | ||
} | ||
|
||
slog.DebugContext(ctx, "Successfully updated http upstream servers", "http_upstream_name", | ||
action.GetUpdateHttpUpstreamServers().GetHttpUpstreamName(), "add", len(add), "update", len(update), | ||
"delete", len(del)) | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, | ||
"Successfully updated HTTP Upstreams", instanceID, "") | ||
} | ||
|
||
func (a *APIAction) HandleGetHTTPUpstreamsServersRequest(ctx context.Context, action *mpi.NGINXPlusAction, | ||
instance *mpi.Instance, | ||
) *mpi.DataPlaneResponse { | ||
correlationID := logger.GetCorrelationID(ctx) | ||
instanceID := instance.GetInstanceMeta().GetInstanceId() | ||
upstreamsResponse := emptyResponse | ||
|
||
upstreams, err := a.ResourceService.GetHTTPUpstreamServers(ctx, instance, | ||
action.GetGetHttpUpstreamServers().GetHttpUpstreamName()) | ||
if err != nil { | ||
slog.ErrorContext(ctx, "Unable to get HTTP servers of upstream", "error", err) | ||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, | ||
"", instanceID, err.Error()) | ||
} | ||
|
||
if upstreams != nil { | ||
upstreamsJSON, jsonErr := json.Marshal(upstreams) | ||
if jsonErr != nil { | ||
slog.ErrorContext(ctx, "Unable to marshal http upstreams", "err", err) | ||
} | ||
upstreamsResponse = string(upstreamsJSON) | ||
} | ||
|
||
return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, | ||
upstreamsResponse, instanceID, "") | ||
} |
Oops, something went wrong.