Skip to content

Commit

Permalink
[GEN-1546] Add delete attr new api (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
alonkeyval authored Oct 22, 2024
1 parent 9dbdc21 commit f1ac07b
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 90 deletions.
96 changes: 35 additions & 61 deletions frontend/graph/generated.go

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

14 changes: 7 additions & 7 deletions frontend/graph/model/models_gen.go

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

2 changes: 1 addition & 1 deletion frontend/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ type DeleteAttributeAction implements Action {
notes: String
disable: Boolean!
signals: [SignalType!]!
details: [DeleteAttribute!]!
details: [String!]!
}

input ActionInput {
Expand Down
29 changes: 8 additions & 21 deletions frontend/graph/schema.resolvers.go

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

124 changes: 124 additions & 0 deletions frontend/services/actions/deleteattribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package services

import (
"context"
"encoding/json"
"fmt"

"github.com/odigos-io/odigos/api/actions/v1alpha1"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/frontend/graph/model"
"github.com/odigos-io/odigos/frontend/kube"
"github.com/odigos-io/odigos/frontend/services"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type DeleteAttributeDetails struct {
AttributeNamesToDelete []string `json:"attributeNamesToDelete"`
}

// CreateDeleteAttribute creates a new DeleteAttribute action in Kubernetes
func CreateDeleteAttribute(ctx context.Context, action model.ActionInput) (model.Action, error) {
odigosns := consts.DefaultOdigosNamespace

var details DeleteAttributeDetails
err := json.Unmarshal([]byte(action.Details), &details)
if err != nil {
return nil, fmt.Errorf("invalid details for DeleteAttribute: %v", err)
}

signals, err := services.ConvertSignals(action.Signals)
if err != nil {
return nil, fmt.Errorf("failed to convert signals: %v", err)
}

deleteAttributeAction := &v1alpha1.DeleteAttribute{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "da-",
},
Spec: v1alpha1.DeleteAttributeSpec{
ActionName: services.DerefString(action.Name),
Notes: services.DerefString(action.Notes),
Disabled: action.Disable,
Signals: signals,
AttributeNamesToDelete: details.AttributeNamesToDelete,
},
}

generatedAction, err := kube.DefaultClient.ActionsClient.DeleteAttributes(odigosns).Create(ctx, deleteAttributeAction, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("failed to create DeleteAttribute: %v", err)
}

response := &model.DeleteAttributeAction{
ID: generatedAction.Name,
Type: ActionTypeDeleteAttribute,
Name: action.Name,
Notes: action.Notes,
Disable: action.Disable,
Signals: action.Signals,
Details: details.AttributeNamesToDelete,
}

return response, nil
}

// UpdateDeleteAttribute updates an existing DeleteAttribute action in Kubernetes
func UpdateDeleteAttribute(ctx context.Context, id string, action model.ActionInput) (model.Action, error) {
odigosns := consts.DefaultOdigosNamespace

existingAction, err := kube.DefaultClient.ActionsClient.DeleteAttributes(odigosns).Get(ctx, id, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to fetch DeleteAttribute: %v", err)
}

var details DeleteAttributeDetails
err = json.Unmarshal([]byte(action.Details), &details)
if err != nil {
return nil, fmt.Errorf("invalid details for DeleteAttribute: %v", err)
}

signals, err := services.ConvertSignals(action.Signals)
if err != nil {
return nil, fmt.Errorf("failed to convert signals: %v", err)
}

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.AttributeNamesToDelete = details.AttributeNamesToDelete

updatedAction, err := kube.DefaultClient.ActionsClient.DeleteAttributes(odigosns).Update(ctx, existingAction, metav1.UpdateOptions{})
if err != nil {
return nil, fmt.Errorf("failed to update DeleteAttribute: %v", err)
}

response := &model.DeleteAttributeAction{
ID: updatedAction.Name,
Type: ActionTypeDeleteAttribute,
Name: action.Name,
Notes: action.Notes,
Disable: action.Disable,
Signals: action.Signals,
Details: details.AttributeNamesToDelete,
}

return response, nil
}

// DeleteDeleteAttribute deletes an existing DeleteAttribute action from Kubernetes
func DeleteDeleteAttribute(ctx context.Context, id string) error {
odigosns := consts.DefaultOdigosNamespace

err := kube.DefaultClient.ActionsClient.DeleteAttributes(odigosns).Delete(ctx, id, metav1.DeleteOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("DeleteAttribute action with ID %s not found", id)
}
return fmt.Errorf("failed to delete DeleteAttribute action: %v", err)
}

return nil
}

0 comments on commit f1ac07b

Please sign in to comment.