-
Notifications
You must be signed in to change notification settings - Fork 292
feat(api): add PATCH /sandboxes/{id}/metadata #2464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
levb
wants to merge
2
commits into
main
Choose a base branch
from
lev-update-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,50 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/api/internal/api" | ||
| "github.com/e2b-dev/infra/packages/api/internal/utils" | ||
| "github.com/e2b-dev/infra/packages/auth/pkg/auth" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/ginutils" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/telemetry" | ||
| ) | ||
|
|
||
| func (a *APIStore) PutSandboxesSandboxIDMetadata( | ||
| c *gin.Context, | ||
| sandboxID string, | ||
| ) { | ||
| ctx := c.Request.Context() | ||
|
|
||
| var err error | ||
| sandboxID, err = utils.ShortID(sandboxID) | ||
| if err != nil { | ||
| a.sendAPIStoreError(c, http.StatusBadRequest, "Invalid sandbox ID") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| team := auth.MustGetTeamInfo(c) | ||
|
|
||
| body, err := ginutils.ParseBody[api.PutSandboxesSandboxIDMetadataJSONRequestBody](ctx, c) | ||
| if err != nil { | ||
| a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) | ||
| telemetry.ReportCriticalError(ctx, "error when parsing request", err) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| metadata := map[string]string(body) | ||
|
|
||
| if apiErr := a.orchestrator.UpdateSandboxMetadata(ctx, team.ID, sandboxID, metadata); apiErr != nil { | ||
| telemetry.ReportErrorByCode(ctx, apiErr.Code, "error updating sandbox metadata", apiErr.Err) | ||
| a.sendAPIStoreError(c, apiErr.Code, apiErr.ClientMsg) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| c.Status(http.StatusNoContent) | ||
| } |
This file contains hidden or 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,100 @@ | ||
| package orchestrator | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/google/uuid" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/trace" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/api/internal/api" | ||
| "github.com/e2b-dev/infra/packages/api/internal/sandbox" | ||
| "github.com/e2b-dev/infra/packages/api/internal/utils" | ||
| orchestratorgrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/telemetry" | ||
| ) | ||
|
|
||
| func (o *Orchestrator) UpdateSandboxMetadata( | ||
| ctx context.Context, | ||
| teamID uuid.UUID, | ||
| sandboxID string, | ||
| metadata map[string]string, | ||
| ) *api.APIError { | ||
| updateFunc := func(sbx sandbox.Sandbox) (sandbox.Sandbox, error) { | ||
| if sbx.State != sandbox.StateRunning { | ||
| return sbx, &sandbox.NotRunningError{SandboxID: sandboxID, State: sbx.State} | ||
| } | ||
|
|
||
| sbx.Metadata = metadata | ||
|
|
||
| return sbx, nil | ||
| } | ||
|
|
||
| var sbxNotRunningErr *sandbox.NotRunningError | ||
|
|
||
| sbx, err := o.sandboxStore.Update(ctx, teamID, sandboxID, updateFunc) | ||
| if err != nil { | ||
| switch { | ||
| case errors.As(err, &sbxNotRunningErr): | ||
| return &api.APIError{Code: http.StatusConflict, ClientMsg: utils.SandboxChangingStateMsg(sandboxID, sbxNotRunningErr.State), Err: err} | ||
| case errors.Is(err, sandbox.ErrNotFound): | ||
| return &api.APIError{Code: http.StatusNotFound, ClientMsg: utils.SandboxNotFoundMsg(sandboxID), Err: err} | ||
| default: | ||
| return &api.APIError{Code: http.StatusInternalServerError, ClientMsg: "Error updating sandbox metadata", Err: err} | ||
| } | ||
| } | ||
|
|
||
| return o.updateSandboxMetadataOnNode(ctx, sbx, metadata) | ||
|
Check failure on line 52 in packages/api/internal/orchestrator/update_metadata.go
|
||
| } | ||
|
|
||
| func (o *Orchestrator) updateSandboxMetadataOnNode( | ||
| ctx context.Context, | ||
| sbx sandbox.Sandbox, | ||
| metadata map[string]string, | ||
| ) *api.APIError { | ||
| ctx, span := tracer.Start(ctx, "update-sandbox-metadata-on-node", | ||
| trace.WithAttributes( | ||
| attribute.String("instance.id", sbx.SandboxID), | ||
| ), | ||
| ) | ||
| defer span.End() | ||
|
|
||
| node := o.getOrConnectNode(ctx, sbx.ClusterID, sbx.NodeID) | ||
| if node == nil { | ||
| return &api.APIError{ | ||
| Code: http.StatusInternalServerError, | ||
| ClientMsg: fmt.Sprintf("Node hosting sandbox '%s' not found", sbx.SandboxID), | ||
| Err: fmt.Errorf("node '%s' not found for cluster '%s'", sbx.NodeID, sbx.ClusterID), | ||
| } | ||
| } | ||
|
|
||
| client, ctx := node.GetClient(ctx) | ||
| _, err := client.Sandbox.Update(ctx, &orchestratorgrpc.SandboxUpdateRequest{ | ||
| SandboxId: sbx.SandboxID, | ||
| Metadata: &orchestratorgrpc.SandboxMetadataUpdate{Entries: metadata}, | ||
| }) | ||
| if err != nil { | ||
| grpcErr, ok := status.FromError(err) | ||
| if ok && grpcErr.Code() == codes.NotFound { | ||
| return &api.APIError{Code: http.StatusNotFound, ClientMsg: utils.SandboxNotFoundMsg(sbx.SandboxID), Err: err} | ||
| } | ||
|
|
||
| err = utils.UnwrapGRPCError(err) | ||
| telemetry.ReportCriticalError(ctx, "failed to update sandbox metadata on node", err) | ||
|
|
||
| return &api.APIError{ | ||
| Code: http.StatusInternalServerError, | ||
| ClientMsg: "Error applying metadata to sandbox", | ||
| Err: fmt.Errorf("failed to update sandbox metadata on node: %w", err), | ||
| } | ||
| } | ||
|
|
||
| telemetry.ReportEvent(ctx, "Updated sandbox metadata on node") | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.