From d456363fa5052ee640e6ab4e0c9ddf5185b84d4b Mon Sep 17 00:00:00 2001 From: Alon Braymok <138359965+alonkeyval@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:51:13 +0300 Subject: [PATCH] [GEN-1540] Add update action mutation (#1607) This pull request includes several changes to improve the handling of action types in the `CreateAction` and `UpdateAction` resolvers, as well as the addition of a new update function for `AddClusterInfo` actions. The most important changes involve refactoring action type strings into constants and implementing the `UpdateAddClusterInfo` function. ### Refactoring action types: * [`frontend/graph/schema.resolvers.go`](diffhunk://#diff-8e6e95029056db2c0301fc338e0ca5a04356ce5d45ee9514bbd167f2d85bae70L560-R560): Replaced hardcoded action type strings with constants from `actionservices` for better maintainability. [[1]](diffhunk://#diff-8e6e95029056db2c0301fc338e0ca5a04356ce5d45ee9514bbd167f2d85bae70L560-R560) [[2]](diffhunk://#diff-8e6e95029056db2c0301fc338e0ca5a04356ce5d45ee9514bbd167f2d85bae70L569-R569) ### New update functionality: * [`frontend/graph/schema.resolvers.go`](diffhunk://#diff-8e6e95029056db2c0301fc338e0ca5a04356ce5d45ee9514bbd167f2d85bae70L582-R599): Implemented the `UpdateAction` resolver to handle updating `AddClusterInfo` actions. * [`frontend/services/actions/addclusterinfo.go`](diffhunk://#diff-c2da2861415e61f772e3b44d22810aa34b4eba6070f744709c4b1b6b919a3f09L70-R137): Added the `UpdateAddClusterInfo` function to update existing `AddClusterInfo` actions in Kubernetes. ### Constants definition: * [`frontend/services/actions/constants.go`](diffhunk://#diff-377602090c56364ea3a4d6cdadda1ad9b1c6ba9e122f882e5a1f640b1e62acf8R1-R7): Added constants for action types `ActionTypeAddClusterInfo` and `ActionTypeDeleteAttribute`. --- frontend/graph/schema.resolvers.go | 23 ++++++- frontend/services/actions/addclusterinfo.go | 69 ++++++++++++++++++++- frontend/services/actions/constants.go | 7 +++ 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 frontend/services/actions/constants.go diff --git a/frontend/graph/schema.resolvers.go b/frontend/graph/schema.resolvers.go index 39c6b542c..553cbbf33 100644 --- a/frontend/graph/schema.resolvers.go +++ b/frontend/graph/schema.resolvers.go @@ -557,7 +557,7 @@ func (r *mutationResolver) UpdateDestination(ctx context.Context, id string, des // CreateAction is the resolver for the createAction field. func (r *mutationResolver) CreateAction(ctx context.Context, action model.ActionInput) (model.Action, error) { switch action.Type { - case "AddClusterInfo": + case actionservices.ActionTypeAddClusterInfo: res, err := actionservices.CreateAddClusterInfo(ctx, action) if err != nil { @@ -566,7 +566,7 @@ func (r *mutationResolver) CreateAction(ctx context.Context, action model.Action return res, nil - case "DeleteAttribute": + case actionservices.ActionTypeDeleteAttribute: // Handle other action types... @@ -579,7 +579,24 @@ func (r *mutationResolver) CreateAction(ctx context.Context, action model.Action // UpdateAction is the resolver for the updateAction field. func (r *mutationResolver) UpdateAction(ctx context.Context, id string, action model.ActionInput) (model.Action, error) { - panic(fmt.Errorf("not implemented: UpdateAction - updateAction")) + switch action.Type { + case actionservices.ActionTypeAddClusterInfo: + res, err := actionservices.UpdateAddClusterInfo(ctx, id, action) + + if err != nil { + return nil, fmt.Errorf("failed to update AddClusterInfo: %v", err) + } + + return res, nil + + case actionservices.ActionTypeDeleteAttribute: + // Handle other action types... + + default: + return nil, fmt.Errorf("unsupported action type: %s", action.Type) + } + + return nil, nil } // DeleteAction is the resolver for the deleteAction field. diff --git a/frontend/services/actions/addclusterinfo.go b/frontend/services/actions/addclusterinfo.go index 74f2570e2..aefc359ef 100644 --- a/frontend/services/actions/addclusterinfo.go +++ b/frontend/services/actions/addclusterinfo.go @@ -67,7 +67,74 @@ func CreateAddClusterInfo(ctx context.Context, action model.ActionInput) (model. response := &model.AddClusterInfoAction{ ID: generatedAction.Name, - Type: "AddClusterInfo", + Type: ActionTypeAddClusterInfo, + Name: action.Name, + Notes: action.Notes, + Disable: action.Disable, + Signals: action.Signals, + Details: resDetails, + } + + return response, nil +} + +func UpdateAddClusterInfo(ctx context.Context, id string, action model.ActionInput) (model.Action, error) { + odigosns := consts.DefaultOdigosNamespace + + // Fetch the existing action + existingAction, err := kube.DefaultClient.ActionsClient.AddClusterInfos(odigosns).Get(ctx, id, metav1.GetOptions{}) + if err != nil { + return nil, fmt.Errorf("failed to fetch AddClusterInfo: %v", err) + } + + // Parse the details from action.Details + var details AddClusterInfoDetails + err = json.Unmarshal([]byte(action.Details), &details) + if err != nil { + return nil, fmt.Errorf("invalid details for AddClusterInfo: %v", err) + } + + // Convert signals from action input + signals, err := services.ConvertSignals(action.Signals) + if err != nil { + return nil, fmt.Errorf("failed to convert signals: %v", err) + } + + // Convert ClusterAttributes from model to v1alpha1 + clusterAttributes := make([]v1alpha1.OtelAttributeWithValue, len(details.ClusterAttributes)) + for i, attr := range details.ClusterAttributes { + clusterAttributes[i] = v1alpha1.OtelAttributeWithValue{ + AttributeName: attr.AttributeName, + AttributeStringValue: attr.AttributeStringValue, + } + } + + // Update the existing action with new values + existingAction.Spec.ActionName = services.DerefString(action.Name) + existingAction.Spec.Notes = services.DerefString(action.Notes) + existingAction.Spec.Disabled = action.Disable + existingAction.Spec.Signals = signals + existingAction.Spec.ClusterAttributes = clusterAttributes + + // Update the action in Kubernetes + updatedAction, err := kube.DefaultClient.ActionsClient.AddClusterInfos(odigosns).Update(ctx, existingAction, metav1.UpdateOptions{}) + if err != nil { + return nil, fmt.Errorf("failed to update AddClusterInfo: %v", err) + } + + // Prepare the response model + resDetails := make([]*model.ClusterInfo, len(details.ClusterAttributes)) + for i, attr := range details.ClusterAttributes { + resDetails[i] = &model.ClusterInfo{ + AttributeName: attr.AttributeName, + AttributeStringValue: attr.AttributeStringValue, + } + } + + // Return the updated model as the response + response := &model.AddClusterInfoAction{ + ID: updatedAction.Name, + Type: ActionTypeAddClusterInfo, Name: action.Name, Notes: action.Notes, Disable: action.Disable, diff --git a/frontend/services/actions/constants.go b/frontend/services/actions/constants.go new file mode 100644 index 000000000..fa77420e2 --- /dev/null +++ b/frontend/services/actions/constants.go @@ -0,0 +1,7 @@ +package services + +const ( + ActionTypeAddClusterInfo = "AddClusterInfo" + ActionTypeDeleteAttribute = "DeleteAttribute" + // Add more action types as needed +)