Skip to content

Commit

Permalink
[GEN-1540] Add update action mutation (#1607)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
alonkeyval authored Oct 22, 2024
1 parent 62934de commit d456363
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
23 changes: 20 additions & 3 deletions frontend/graph/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 68 additions & 1 deletion frontend/services/actions/addclusterinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions frontend/services/actions/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package services

const (
ActionTypeAddClusterInfo = "AddClusterInfo"
ActionTypeDeleteAttribute = "DeleteAttribute"
// Add more action types as needed
)

0 comments on commit d456363

Please sign in to comment.