UpdateInstance updates an instance.
(ctx context.Context, req *connect.Request[v1pb.UpdateInstanceRequest])
| 507 | |
| 508 | // UpdateInstance updates an instance. |
| 509 | func (s *InstanceService) UpdateInstance(ctx context.Context, req *connect.Request[v1pb.UpdateInstanceRequest]) (*connect.Response[v1pb.Instance], error) { |
| 510 | if req.Msg.Instance == nil { |
| 511 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("instance must be set")) |
| 512 | } |
| 513 | if req.Msg.UpdateMask == nil { |
| 514 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("update_mask must be set")) |
| 515 | } |
| 516 | |
| 517 | instance, err := getInstanceMessage(ctx, s.store, req.Msg.Instance.Name) |
| 518 | if err != nil { |
| 519 | if strings.Contains(err.Error(), "not found") && req.Msg.AllowMissing { |
| 520 | // When allow_missing is true and instance doesn't exist, create a new one |
| 521 | instanceID, ierr := common.GetInstanceID(req.Msg.Instance.Name) |
| 522 | if ierr != nil { |
| 523 | return nil, connect.NewError(connect.CodeInvalidArgument, ierr) |
| 524 | } |
| 525 | |
| 526 | return s.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{ |
| 527 | InstanceId: instanceID, |
| 528 | Instance: req.Msg.Instance, |
| 529 | })) |
| 530 | } |
| 531 | return nil, connect.NewError(connect.CodeInvalidArgument, err) |
| 532 | } |
| 533 | if instance.Deleted { |
| 534 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("instance %q has been deleted", req.Msg.Instance.Name)) |
| 535 | } |
| 536 | |
| 537 | metadata := proto.CloneOf(instance.Metadata) |
| 538 | patch := &store.UpdateInstanceMessage{ |
| 539 | ResourceID: &instance.ResourceID, |
| 540 | Workspace: instance.Workspace, |
| 541 | Metadata: metadata, |
| 542 | } |
| 543 | updateActivation := false |
| 544 | updateSyncInterval := false |
| 545 | for _, path := range req.Msg.UpdateMask.Paths { |
| 546 | switch path { |
| 547 | case "title": |
| 548 | patch.Metadata.Title = req.Msg.Instance.Title |
| 549 | case "environment": |
| 550 | if req.Msg.Instance.Environment == nil || *req.Msg.Instance.Environment == "" { |
| 551 | // Clear the environment if null or empty string is provided |
| 552 | patch.EnvironmentID = new("") |
| 553 | } else { |
| 554 | envID, err := common.GetEnvironmentID(*req.Msg.Instance.Environment) |
| 555 | if err != nil { |
| 556 | return nil, connect.NewError(connect.CodeInvalidArgument, err) |
| 557 | } |
| 558 | environment, err := s.store.GetEnvironmentByID(ctx, common.GetWorkspaceIDFromContext(ctx), envID) |
| 559 | if err != nil { |
| 560 | return nil, connect.NewError(connect.CodeInternal, err) |
| 561 | } |
| 562 | if environment == nil { |
| 563 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("environment %q not found", envID)) |
| 564 | } |
| 565 | patch.EnvironmentID = &envID |
| 566 | } |
no test coverage detected