import * as accounts from '../../model/accounts' ; import * as digitalocean from '../../model/digitalocean' ; import * as gcp from '../../model/gcp' ; import * as server from '../../model/server' ; export class FakeDigitalOceanAccount implements digitalocean . Account { private servers : server . ManagedServer [ ] = [ ] ; constructor ( private accessToken = 'fake-access-token' ) { } getId ( ) { return 'account-id' ; } async getName ( ) { return 'fake-digitalocean-account-name' ; } async getStatus ( ) { return digitalocean . Status . ACTIVE ; } listServers ( ) { return Promise . resolve ( this . servers ) ; } listLocations ( ) { return Promise . resolve ( [ { cloudLocation : new digitalocean . Region ( 'AMS999' ) , available : true } , { cloudLocation : new digitalocean . Region ( 'FRA999' ) , available : false } ] ) ; } createServer ( region ) { const newServer = new FakeManagedServer ( region . id , false ) ; this . servers . push ( newServer ) ; return Promise . resolve ( newServer ) ; } getAccessToken ( ) { return this . accessToken ; } } export class FakeGcpAccount implements gcp . Account { constructor ( private refreshToken = 'fake-access-token' , private billingAccounts = [ ] , private locations = [ ] ) { } getId ( ) { return 'id' ; } async getName ( ) { return 'fake-gcp-account-name' ; } getRefreshToken ( ) { return this . refreshToken ; } createServer ( projectId , name , zone ) { return undefined ; } async listLocations ( projectId ) { return this . locations ; } async listServers ( projectId ) { return [ ] ; } async createProject ( id , billingAccountId ) { return { id : 'project-id' , name : 'project-name' , } ; } async isProjectHealthy ( projectId ) { return true ; } async listOpenBillingAccounts ( ) { return this . billingAccounts ; } async listProjects ( ) { return [ ] ; } } export class FakeServer implements server . Server { private name = 'serverName' ; private metricsId : string ; private metricsEnabled = false ; apiUrl : string ; constructor ( protected id ) { this . metricsId = Math . random ( ) . toString ( ) ; } getId ( ) { return this . id ; } getName ( ) { return this . name ; } setName ( name ) { this . name = name ; return Promise . resolve ( ) ; } getVersion ( ) { return '1.2.3' ; } listAccessKeys ( ) { return Promise . resolve ( [ ] ) ; } getMetricsEnabled ( ) { return this . metricsEnabled ; } setMetricsEnabled ( metricsEnabled ) { this . metricsEnabled = metricsEnabled ; return Promise . resolve ( ) ; } getMetricsId ( ) { return this . metricsId ; } isHealthy ( ) { return Promise . resolve ( true ) ; } getCreatedDate ( ) { return new Date ( ) ; } getDataUsage ( ) { return Promise . resolve ( new Map < server . AccessKeyId , number > ( ) ) ; } addAccessKey ( ) { return Promise . reject ( new Error ( 'FakeServer.addAccessKey not implemented' ) ) ; } renameAccessKey ( accessKeyId , name ) { return Promise . reject ( new Error ( 'FakeServer.renameAccessKey not implemented' ) ) ; } removeAccessKey ( accessKeyId ) { return Promise . reject ( new Error ( 'FakeServer.removeAccessKey not implemented' ) ) ; } setHostnameForAccessKeys ( hostname ) { return Promise . reject ( new Error ( 'FakeServer.setHostname not implemented' ) ) ; } getHostnameForAccessKeys ( ) { return 'fake-server' ; } getManagementApiUrl ( ) { return this . apiUrl || Math . random ( ) . toString ( ) ; } getPortForNewAccessKeys ( ) { return undefined ; } setPortForNewAccessKeys ( ) { return Promise . reject ( new Error ( 'FakeServer.setPortForNewAccessKeys not implemented' ) ) ; } setAccessKeyDataLimit ( accessKeyId , limit ) { return Promise . reject ( new Error ( 'FakeServer.setAccessKeyDataLimit not implemented' ) ) ; } removeAccessKeyDataLimit ( accessKeyId ) { return Promise . reject ( new Error ( 'FakeServer.removeAccessKeyDataLimit not implemented' ) ) ; } setDefaultDataLimit ( limit ) { return Promise . reject ( new Error ( 'FakeServer.setDefaultDataLimit not implemented' ) ) ; } removeDefaultDataLimit ( ) { return Promise . resolve ( ) ; } getDefaultDataLimit ( ) { return undefined ; } } export class FakeManualServer extends FakeServer implements server . ManualServer { constructor ( public manualServerConfig ) { super ( manualServerConfig . apiUrl ) ; } getManagementApiUrl ( ) { return this . manualServerConfig . apiUrl ; } forget ( ) { return Promise . reject ( new Error ( 'FakeManualServer.forget not implemented' ) ) ; } getCertificateFingerprint ( ) { return this . manualServerConfig . certSha256 ; } } export class FakeManualServerRepository implements server . ManualServerRepository { private servers : server . ManualServer [ ] = [ ] ; addServer ( config ) { const newServer = new FakeManualServer ( config ) ; this . servers . push ( newServer ) ; return Promise . resolve ( newServer ) ; } findServer ( config ) { return this . servers . find ( server => server . getManagementApiUrl ( ) === config . apiUrl ) ; } listServers ( ) { return Promise . resolve ( this . servers ) ; } } export class FakeManagedServer extends FakeServer implements server . ManagedServer { constructor ( id , private isInstalled = true ) { super ( id ) ; } async * monitorInstallProgress ( ) { yield 0.5 ; if ( ! this . isInstalled ) { await new Promise ( ( ) => { } ) ; } } getHost ( ) { return { getMonthlyOutboundTransferLimit : ( ) => ( { terabytes : 1 } ) , getMonthlyCost : ( ) => ( { usd : 5 } ) , getCloudLocation : ( ) => new digitalocean . Region ( 'AMS999' ) , delete : ( ) => Promise . resolve ( ) , getHostId : ( ) => 'fake-host-id' , } ; } } export class FakeCloudAccounts implements accounts . CloudAccounts { constructor ( private digitalOceanAccount = null , private gcpAccount = null ) { } connectDigitalOceanAccount ( accessToken ) { this . digitalOceanAccount = new FakeDigitalOceanAccount ( accessToken ) ; return this . digitalOceanAccount ; } connectGcpAccount ( refreshToken ) { this . gcpAccount = new FakeGcpAccount ( refreshToken ) ; return this . gcpAccount ; } disconnectDigitalOceanAccount ( ) { this . digitalOceanAccount = null ; } disconnectGcpAccount ( ) { this . gcpAccount = null ; } getDigitalOceanAccount ( ) { return this . digitalOceanAccount ; } getGcpAccount ( ) { return this . gcpAccount ; } }