'use strict' ; import { SERVER_TAG } from './constants' ; import Deploy from './Deploy' ; import ApiClient from './ApiClient' ; import Keychain from '../../keychain' ; import { Client } from 'providers/types/Client' ; import { Region } from 'providers/types/Region' ; import { Notify } from 'store/types/Notify' ; import { Server } from 'providers/types/Server' ; import { VPNCredentials } from 'providers/types/VPNCredentials' ; class ClientFacade implements Client { token : string ; apiClient : ApiClient ; constructor ( token ) { this . token = token ; this . apiClient = new ApiClient ( token ) ; } async getAccount ( ) { return await this . apiClient . getAccount ( ) ; } async createServer ( region , notify ) { const deploy = new Deploy ( this . apiClient , notify ) ; return await deploy . run ( region . slug ) ; } async readServerVPN ( server , notify ) { const deploy = new Deploy ( this . apiClient , notify ) ; const sshKeyPair = await Keychain . getSSHKeyPair ( server . name ) ; return await deploy . read ( server , sshKeyPair ) ; } async getServers ( ) { let droplets = await this . apiClient . getDropletsByTag ( SERVER_TAG ) ; return droplets . map ( ( droplet ) : Server => { const ipAddress = droplet . networks . v4 . filter ( ( ip ) => ip . type === 'public' ) ; return { provider : { id : 'digitalocean' , name : 'DigitalOcean' , } , uid : droplet . id , name : droplet . name , region : { name : droplet . region . name , slug : droplet . region . slug , available : droplet . region . available , } , ipv4Address : ipAddress [ 0 ] . ip_address , } ; } ) ; } async deleteServer ( server ) { this . apiClient . deleteDroplet ( server . uid ) ; const sshKeyPair = await Keychain . getSSHKeyPair ( server . name ) ; this . apiClient . deleteSshKey ( sshKeyPair . fingerprint ) ; } async getRegions ( ) { let regions = await this . apiClient . getRegions ( ) ; return regions . map ( ( region ) : Region => { return { name : region . name , slug : region . slug , available : region . available , } ; } ) ; } } export default ClientFacade ;