Skip to content

Commit

Permalink
Merge pull request #87 from pliancy/enhancement/BILL-14-get-egnyte-pr…
Browse files Browse the repository at this point in the history
…otect-usage

feat(protect-plan): add methods to get protect plan(s)
  • Loading branch information
santese authored Aug 31, 2021
2 parents 2caed2d + 6b1dcc9 commit 1c5ebf7
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 7 deletions.
57 changes: 57 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,61 @@ describe('Egnyte', () => {
})
})
})

describe('public methods', () => {
it('returns null given customer does not have egnyte protect', async () => {
jest.spyOn(egnyte, '_authenticate' as never).mockResolvedValue({
authCookie: 'authCookie',
csrfToken: 'csrfToken',
} as never)
jest.spyOn(egnyte, '_egnyteRequest' as never).mockResolvedValue({
data: [
{
protectawesomecustomer: {
storage_stats: {},
},
},
],
} as never)
await expect(
egnyte.getCustomerProtectPlanUsage('SOMEOTHERCUSTOMER'),
).resolves.toBeNull()
})

it('gets protect plan usage', async () => {
const storage_stats = { Used: 100, Unused: 200, Available: 100 }
jest.spyOn(egnyte, '_authenticate' as never).mockResolvedValue({
authCookie: 'authCookie',
csrfToken: 'csrfToken',
} as never)
jest.spyOn(egnyte, '_egnyteRequest' as never).mockResolvedValue({
data: [
{
protectawesomecustomer: {
storage_stats,
},
},
],
} as never)
await expect(egnyte.getCustomerProtectPlanUsage('AWESOMECUSTOMER')).resolves.toEqual(
storage_stats,
)
})

it('gets all protect plans', async () => {
const data = [
{
protectawesomecustomer: {
storage_stats: { Used: 100, Unused: 200, Available: 100 },
},
},
]
jest.spyOn(egnyte, '_authenticate' as never).mockResolvedValue({
authCookie: 'authCookie',
csrfToken: 'csrfToken',
} as never)
jest.spyOn(egnyte, '_egnyteRequest' as never).mockResolvedValue({ data } as never)
await expect(egnyte.getAllProtectPlans()).resolves.toEqual(data)
})
})
})
62 changes: 55 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import qs from 'querystring'
import axios, { AxiosRequestConfig } from 'axios'
import cheerio from 'cheerio'

interface EgnyteCustomer {
export interface EgnyteCustomer {
customerEgnyteId: string
planId: string
powerUsers: {
Expand All @@ -19,27 +19,37 @@ interface EgnyteCustomer {
}
}

interface EgnyteUpdateResponse {
export interface EgnyteUpdateResponse {
result: string
message: string
}

interface EgnyteConfig {
export interface StorageStats {
Used: number
Unused: number
Available: number
}

export interface EgnyteConfig {
/** the egnyte resellers portal username */
username: string
/** the egnyte resellers portal password */
password: string
/**
* Protect planId
*/
protectPlanId?: number
/** timeout threshold in milliseconds */
timeoutMs?: number
forceLicenseChange?: boolean
}

interface UpdateCustomer {
export interface UpdateCustomer {
powerUsers?: { total?: number }
storageGB?: { total?: number }
}

class Egnyte {
export default class Egnyte {
private readonly _config: EgnyteConfig
private readonly httpConfig: AxiosRequestConfig
private resellerId: string
Expand Down Expand Up @@ -282,6 +292,46 @@ class Egnyte {
return results
}

/**
* retrieves protect plan usage for a customer
* @returns object containing the available usage data or null if the customer does not have protect
*/
async getCustomerProtectPlanUsage(egnyteTenantId: string): Promise<StorageStats | null> {
const { authCookie, csrfToken } = await this._authenticate()
const { data: protectUsage } = await this._egnyteRequest(
`/msp/usage_stats/${this.resellerId}/${this._config.protectPlanId}/`,
{
headers: { cookie: authCookie, 'X-CSRFToken': csrfToken },
},
)

const id = `protect${egnyteTenantId.toLowerCase()}`

for (const entry of protectUsage) {
if (Object.keys(entry)[0] === id) {
return entry[id].storage_stats
}
}

return null
}

/**
* retrieves all protect plans
* @returns array containing the available plans
*/
async getAllProtectPlans(): Promise<StorageStats[]> {
const { authCookie, csrfToken } = await this._authenticate()
const { data: protectUsage } = await this._egnyteRequest(
`/msp/usage_stats/${this.resellerId}/${this._config.protectPlanId}/`,
{
headers: { cookie: authCookie, 'X-CSRFToken': csrfToken },
},
)

return protectUsage
}

/**
* Updates a plan's power user licensing count. This will result in a billing change from egnyte. Must be increased in increments of 5
* @param planId the planId to update licensing for
Expand Down Expand Up @@ -436,5 +486,3 @@ class Egnyte {
}
}
}

export = Egnyte

0 comments on commit 1c5ebf7

Please sign in to comment.