Skip to content

implement resources API part 2#17749

Open
aalves08 wants to merge 1 commit into
rancher:masterfrom
aalves08:resources-api-part-2.1
Open

implement resources API part 2#17749
aalves08 wants to merge 1 commit into
rancher:masterfrom
aalves08:resources-api-part-2.1

Conversation

@aalves08
Copy link
Copy Markdown
Member

Summary

Fixes #16898

Implements Resources API Part 2: create, update, patch, and delete operations at both the ResourcesApi level (resources.cluster.* / resources.mgmt.*) and the ResourceInstanceApi level (resourceInstance.*).

NOTE: demo interaction with persistentvolume list view to test (to be deleted in the end)

Occurred changes and/or fixed issues

New write operations added to the Resources API:

Method Level RBAC HTTP Cache
resources.cluster.create(data) ResourcesApi canCreate POST Updates store
resources.cluster.patch(type, id, data) ResourcesApi None PATCH (merge-patch) No cache update
resources.cluster.update(type, id, data) ResourcesApi None PUT (cleanForSave) No cache update
resources.cluster.delete(type, id) ResourcesApi None DELETE No cache update
resourceInstance.patch(data) ResourceInstanceApi canEdit PATCH (merge-patch) Merges response into store
resourceInstance.update() ResourceInstanceApi canEdit PUT (via model.save) Updates store
resourceInstance.delete() ResourceInstanceApi canDelete DELETE (via model.remove) Updates store

All methods are available on both cluster and mgmt scopes (ClusterApi and MgmtApi are type aliases to ResourcesApi).

Existing read operations (find, findAll, findFiltered) now wrap results in ResourceInstanceImpl, so every resource returned by the API has patch(), update(), and delete() available out of the box.

Technical notes summary

  • ResourceInstanceImpl wrapping: All resources returned from the API (find, findAll, findFiltered, create) are now wrapped in ResourceInstanceImpl. This provides the instance-level methods (patch, update, delete) on every returned resource.
  • _model is non-enumerable: The underlying store model reference is stored via Object.defineProperty with enumerable: false. This prevents structuredClone and JSON.stringify from attempting to serialize store internals (functions, dispatch refs, circular references), which is critical for Vue 3 reactive compatibility.
  • declare keyword for _model: Uses declare _model: any instead of a class field initializer to avoid TypeScript emitting a runtime assignment that would override the non-enumerable Object.defineProperty set in the constructor.
  • invalidatePageCache: false: The resourceInstance.patch() method explicitly passes invalidatePageCache: false when dispatching load after a successful patch. Without this, the store mutation defaults invalidatePageCache to true, which clears paginated list data and causes resources to disappear from list views.
  • toJSON fallback: ResourceInstanceImpl.toJSON() defensively checks for _model.toJSON before calling it, falling back to a shallow spread. This prevents errors when the model lacks a toJSON method (e.g. in test mocks).
  • resourceUrl helper: A new private method on ResourcesApiClassImpl builds resource URLs from schema.linkFor('collection') + '/' + resourceId. It reuses the existing isNamespaced() validation to enforce namespace/name format for namespaced resources.
  • CreateResourceData type: New type exported from the barrel — { type: ResourceType } & Record<string, any> — enforces the mandatory type property on create data.
  • delete() added alongside remove(): remove() is kept for backward compatibility. delete() is the new public method that adds RBAC checking before delegating to model.remove().

Areas or cases that should be tested

  • Create: Verify resources.cluster.create() with valid data, verify permission denial when canCreate is false
  • Patch (ResourcesApi): Verify resources.cluster.patch() sends only partial data with application/merge-patch+json content type, verify namespace validation
  • Patch (ResourceInstanceApi): Verify resourceInstance.patch() checks canEdit, sends partial data, merges response back into the instance, and does not invalidate paginated list views
  • Update (ResourcesApi): Verify resources.cluster.update() runs cleanForSave and sends full data with PUT
  • Update (ResourceInstanceApi): Verify resourceInstance.update() checks canEdit and delegates to model.save()
  • Delete (ResourcesApi): Verify resources.cluster.delete() sends DELETE request with correct URL
  • Delete (ResourceInstanceApi): Verify resourceInstance.delete() checks canDelete and delegates to model.remove()
  • All methods on mgmt scope: Verify the same operations work on resources.mgmt.*
  • Namespaced vs cluster-scoped resources: Verify correct URL construction and namespace validation for both types
  • Tested locally with Chrome

Areas which could experience regressions

  • List views using pagination (findFiltered / findPage): The ResourceInstanceImpl wrapping in findFiltered now maps over both array results and transient .data arrays. Verify paginated lists still render correctly.
  • find / findAll consumers: Any code that uses toStrictEqual or instanceof checks against raw store models will see ResourceInstanceImpl instances instead. Property access is unchanged since getters proxy through to the model.
  • Extensions using ResourceInstanceApi: The interface now exposes patch(), update(), and delete() — no breaking changes, but extensions get new methods in IntelliSense.
  • Vue reactivity with resource instances stored in component data: The _model non-enumerable change should be transparent, but verify that resource data remains reactive when stored in Vue component state.

Screenshot/Video

N/A — API-only changes with no UI modifications.

Checklist

  • The PR is linked to an issue and the linked issue has a Milestone, or no issue is needed
  • The PR has a Milestone
  • The PR template has been filled out
  • The PR has been self reviewed
  • The PR has a reviewer assigned
  • The PR has automated tests or clear instructions for manual tests and the linked issue has appropriate QA labels, or tests are not needed
  • The PR has reviewed with UX and tested in light and dark mode, or there are no UX changes
  • The PR has been reviewed in terms of Accessibility
  • The PR has considered, and if applicable tested with, the three Global Roles Admin, Standard User and User Base

@aalves08 aalves08 added this to the v2.15.0 milestone May 20, 2026
@aalves08 aalves08 requested review from nwmac and richard-cox May 20, 2026 13:59
@aalves08 aalves08 self-assigned this May 20, 2026
@aalves08 aalves08 removed the request for review from nwmac May 20, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Rancher API for K8s resources - part 2

1 participant