Skip to content

Commit

Permalink
feat(workspaces): add update Workspace method
Browse files Browse the repository at this point in the history
  • Loading branch information
mijailr committed Oct 3, 2020
1 parent eb39692 commit 6777ae0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/api/endpoints/Workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { AxiosInstance } from 'axios'
import Request from './Request'
import { Workspace, WorkspaceCreate } from '../../types'
import { Workspace, WorkspaceRequest } from '../../types'

export default class Workspaces extends Request {
constructor(client: AxiosInstance) {
super(client)
}

create(organizationName: string, request: WorkspaceCreate): Promise<Workspace> {
const path = `/organizations/${organizationName}/worspaces`
return this.post<Workspace, WorkspaceCreate>(path, request)
create(organizationName: string, request: WorkspaceRequest): Promise<Workspace> {
const path = `/organizations/${organizationName}/workspaces`
return this.post<Workspace, WorkspaceRequest>(path, request)
}

update(organizationName: string, workspaceId: string, request: WorkspaceRequest): Promise<Workspace> {
const path = `/organizations/${organizationName}/workspaces/${workspaceId}`
return this.patch<Workspace, WorkspaceRequest>(path, request)
}
}
4 changes: 2 additions & 2 deletions src/types/Workspace.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Params, TerraformCloudData } from './TerraformCloudData'

export type WorkspaceCreate = Params<'workspaces', WorkspaceCreateAttributes>
export type WorkspaceRequest = Params<'workspaces', WorkspaceRequestAttributes>

export interface WorkspaceCreateAttributes {
export interface WorkspaceRequestAttributes {
name: string
agentPoolId?: string
allowDestroyPlan?: boolean
Expand Down
19 changes: 16 additions & 3 deletions test/api/Workspaces.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import nock from 'nock'
import TerraformCloud from '../../src'
import { WorkspaceRequest } from '../../src/types'
import { WorkspaceMock, WorkspaceNoVcsRequest } from '../mocks'
import { WorkspaceCreate } from '../../src/types'

const { Workspaces } = new TerraformCloud('api-key')
const organizationName = 'organization-name'
const workspaceId = 'workspace-id'

describe('Workspaces endpoints', () => {
it('create a workspace without VCS', async done => {
const scope = nock('https://app.terraform.io/api/v2')
.post(`/organizations/${organizationName}/worspaces`, WorkspaceNoVcsRequest)
.post(`/organizations/${organizationName}/workspaces`, WorkspaceNoVcsRequest)
.reply(201, WorkspaceMock)
const workspace = await Workspaces.create(organizationName, WorkspaceNoVcsRequest as WorkspaceCreate)
const workspace = await Workspaces.create(organizationName, WorkspaceNoVcsRequest as WorkspaceRequest)

expect(workspace.type).toBe('workspaces')
scope.done()
done()
})

it('should update a workspace', async done => {
const scope = nock('https://app.terraform.io/api/v2')
.patch(`/organizations/${organizationName}/workspaces/${workspaceId}`, WorkspaceNoVcsRequest)
.reply(200, WorkspaceMock)

const workspace = await Workspaces.update(organizationName, workspaceId, WorkspaceNoVcsRequest as WorkspaceRequest)

expect(workspace.type).toBe('workspaces')
scope.done()
Expand Down

0 comments on commit 6777ae0

Please sign in to comment.