Skip to content

ENG-24388 Add Resource Structured Parameter Value Service #62

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/api/services/v3/cmp/ResourceStructuredParameterValueService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import crud from '../../../crudOperations'

const buildUrl = (resourceStructuredId) =>
`v3/cmp/resourcesStructured/${resourceStructuredId}/parameterValues`

/**
* @typedef {object} ResourceStructuredParameterValue
* @property {string} fieldId An id of a Parameter
* @property {string} value The value for the parameter on this ResourceStructured
*/

export default {
/**
* Retrieve a list of existing Parameter Values on the given Resource Structured
* @param {string} resourceStructuredId id or global_id of the Resource Structured
* @param {object} options anything parsable by URLSearchParams. See useful options here https://docs.cloudbolt.io/articles/#!cloudbolt-latest-docs/api-conventions/a/h2__904191799
* @returns {Promise} resolves with a list of Parameter Values
*/
list: (resourceStructuredId, options) =>
crud.getItems(buildUrl(resourceStructuredId), options),

/**
* Retrieve an existing Parameter Value by id on the given Resource Structured
* @param {string} resourceStructuredId id or global_id of the Resource Structured
* @param {string} id global_id of the Parameter
* @param {object} options anything parsable by URLSearchParams. See useful options here https://docs.cloudbolt.io/articles/#!cloudbolt-latest-docs/api-conventions/a/h2__904191799
* @returns {Promise} resolves with a cloudbolt API Response object of the Parameter Value object
*/
get: (resourceStructuredId, id, options) =>
crud.getItemById(buildUrl(resourceStructuredId), id, options),

/**
* Create a new Parameter Value on the given Resource Structured
* @param {string} resourceStructuredId id or global_id of the Resource Structured
* @param {ResourceStructuredParameterValue} newParameterValue new Parameter Value object definition
* @returns {Promise} resolves with a new Parameter Value object with all server-filled fields
*/
create: (resourceStructuredId, newParameterValue) =>
crud.createNewItem(buildUrl(resourceStructuredId), newParameterValue),

/**
* Update an existing Parameter Value on the given Resource Structured
* @param {string} resourceStructuredId id or global_id of the Resource Structured
* @param {string} id global_id of the Parameter
* @param {ResourceStructuredParameterValue} updatedParameterValue new Parameter Value object definition
* @returns {Promise} resolves with a new Parameter Value object with all server-filled fields
* @returns {Promise} resolves with a cloudbolt API Response of the updated Parameter Value object
*/
update: (resourceStructuredId, id, updatedParameterValue) =>
crud.patchItemById(
buildUrl(resourceStructuredId),
id,
updatedParameterValue
),

/**
* Delete an existing Parameter Value
* @param {string} resourceStructuredId id or global_id of the Resource Structured
* @param {string} id global_id of the Parameter
* @returns {Promise} resolves with a cloudbolt API Success Response
*/
delete: (resourceStructuredId, id) =>
crud.deleteItemById(buildUrl(resourceStructuredId), id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { baseApi } from '../../../baseApi'
import ResourceStructuredParameterValueService from './ResourceStructuredParameterValueService'

const RS_ID = 'RSC-123'
const BASE_URL = `/v3/cmp/resourcesStructured/${RS_ID}/parameterValues/`
const VAL_ID = 'VAL-123'
const VAL_URL = `${BASE_URL}${VAL_ID}/`
const goodPayload = {
fieldId: VAL_ID,
value: 'test value'
}
const mockResponse = { data: { hello: 'world' } }

test('list calls the correct endpoint', async () => {
const mockFn = jest.spyOn(baseApi, 'get').mockResolvedValue(mockResponse)
await ResourceStructuredParameterValueService.list(RS_ID)
expect(mockFn).toHaveBeenCalledWith(BASE_URL)
})

test('get calls the correct endpoint', async () => {
const mockFn = jest.spyOn(baseApi, 'get').mockResolvedValue(mockResponse)
await ResourceStructuredParameterValueService.get(RS_ID, VAL_ID)
expect(mockFn).toHaveBeenCalledWith(VAL_URL)
})

test('create calls the correct endpoint', async () => {
const mockFn = jest.spyOn(baseApi, 'post').mockResolvedValue(mockResponse)
await ResourceStructuredParameterValueService.create(RS_ID, goodPayload)
expect(mockFn).toHaveBeenCalledWith(BASE_URL, goodPayload)
})

test('update calls the correct endpoint', async () => {
const mockFn = jest.spyOn(baseApi, 'patch').mockResolvedValue(mockResponse)
await ResourceStructuredParameterValueService.update(
RS_ID,
VAL_ID,
goodPayload
)
expect(mockFn).toHaveBeenCalledWith(VAL_URL, goodPayload)
})

test('delete calls the correct endpoint', async () => {
const mockFn = jest.spyOn(baseApi, 'delete').mockResolvedValue({})
await ResourceStructuredParameterValueService.delete(RS_ID, VAL_ID)
expect(mockFn).toHaveBeenCalledWith(VAL_URL)
})
5 changes: 4 additions & 1 deletion src/api/services/v3/cmp/ResourcesStructuredService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import crud from '../../../crudOperations'
import parameterValues from './ResourceStructuredParameterValueService'

const URL = 'v3/cmp/resourcesStructured'

Expand All @@ -16,5 +17,7 @@ export default {
* @param {Object} options anything parsable by URLSearchParams. See useful options here https://docs.cloudbolt.io/articles/#!cloudbolt-latest-docs/api-conventions/a/h2__904191799
* @returns {Promise} resolves with a cloudbolt API Response object of the Structured Resource object
*/
get: (id, options) => crud.getItemById(URL, id, options)
get: (id, options) => crud.getItemById(URL, id, options),

parameterValues
}
4 changes: 4 additions & 0 deletions src/api/services/v3/cmp/ResourcesStructuredService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ test('get calls the correct endpoint', async () => {
`${RESOURCES_STRUCTURED_ENDPOINT}resource-id/`
)
})

test('has a parameterValues service', () => {
expect(ResourcesStructuredService.parameterValues).toBeDefined()
})