|
| 1 | +/*----------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE file in the project root for license information. |
| 4 | + *-----------------------------------------------------------------------------------------------*/ |
| 5 | +import * as k8s from 'vscode-kubernetes-tools-api'; |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { vsCommand } from '../vscommand'; |
| 8 | +import { createSandboxAPI, SBSignupResponse } from '../openshift/sandbox'; |
| 9 | +import ClusterViewLoader from '../webview/cluster/clusterViewLoader'; |
| 10 | +import path = require('path'); |
| 11 | + |
| 12 | +const sandboxAPI = createSandboxAPI(); |
| 13 | + |
| 14 | +class RedHatCloudItem extends vscode.TreeItem { |
| 15 | + |
| 16 | +} |
| 17 | + |
| 18 | +class RedHatTreeDataProvier implements vscode.TreeDataProvider<RedHatCloudItem> { |
| 19 | + private eventEmitter: vscode.EventEmitter<RedHatCloudItem> = new vscode.EventEmitter<RedHatCloudItem>() |
| 20 | + readonly onDidChangeTreeData: vscode.Event<RedHatCloudItem> = this.eventEmitter.event |
| 21 | + |
| 22 | + getTreeItem(element: RedHatCloudItem): vscode.TreeItem | Thenable<vscode.TreeItem> { |
| 23 | + return element |
| 24 | + } |
| 25 | + |
| 26 | + getChildren(element?: RedHatCloudItem): vscode.ProviderResult<RedHatCloudItem[]> { |
| 27 | + if (!element) { |
| 28 | + return this.getTopLevelItems(); |
| 29 | + } |
| 30 | + return []; |
| 31 | + } |
| 32 | + |
| 33 | + getParent?(element: RedHatCloudItem): unknown { |
| 34 | + throw new Error('Method not implemented.'); |
| 35 | + } |
| 36 | + |
| 37 | + resolveTreeItem?(item: vscode.TreeItem, element: unknown, token: vscode.CancellationToken): vscode.ProviderResult<vscode.TreeItem> { |
| 38 | + throw new Error('Method not implemented.'); |
| 39 | + } |
| 40 | + |
| 41 | + private async getTopLevelItems(): Promise<RedHatCloudItem[]> { |
| 42 | + const sessionCheck = await vscode.authentication.getSession('redhat-account-auth', ['openid'], { createIfNone: false }); |
| 43 | + if (sessionCheck) { |
| 44 | + const sandboxItem = await this.buildDevSandboxItem(); |
| 45 | + const openshiftLocalItem = this.buildOpenshiftLocalItem(); |
| 46 | + return [sandboxItem, openshiftLocalItem]; |
| 47 | + } |
| 48 | + const loginItem = new RedHatCloudItem('Sign in to Red Hat'); |
| 49 | + loginItem.iconPath = new vscode.ThemeIcon('account'); |
| 50 | + loginItem.tooltip = 'Sign in to Red Hat'; |
| 51 | + loginItem.command = { |
| 52 | + command: 'cloud.redhat.login', |
| 53 | + title: 'Sign in to Red Hat', |
| 54 | + } |
| 55 | + |
| 56 | + const signUpItem = new RedHatCloudItem('Create Red Hat Account'); |
| 57 | + signUpItem.tooltip = 'Create Red Hat Account'; |
| 58 | + signUpItem.iconPath = new vscode.ThemeIcon('add'); |
| 59 | + signUpItem.command = { |
| 60 | + command: '_openshift.open.signup', |
| 61 | + title: 'Create Red Hat Account', |
| 62 | + tooltip: 'Create Red Hat Account' |
| 63 | + } |
| 64 | + return [loginItem, signUpItem]; |
| 65 | + } |
| 66 | + |
| 67 | + private async buildDevSandboxItem() { |
| 68 | + const signupStatus = await RedHatTreeDataProvier.getSandboxSignupStatus(); |
| 69 | + const sandboxItem = new RedHatCloudItem('Developer Sandbox'); |
| 70 | + sandboxItem.tooltip = 'Get 30-days free access to a shared OpenShift and Kubernetes cluster.'; |
| 71 | + sandboxItem.iconPath = vscode.Uri.file(path.join(__dirname, '..', '..', '..', 'images', 'title', 'logo.svg')); |
| 72 | + if (!signupStatus) { |
| 73 | + sandboxItem.contextValue = 'openshift.sandbox.status.none'; |
| 74 | + } |
| 75 | + else if (signupStatus.status.ready) { |
| 76 | + sandboxItem.contextValue = 'openshift.sandbox.status.ready'; |
| 77 | + } |
| 78 | + sandboxItem.command = { |
| 79 | + command: 'openshift.sandbox.open.setup', |
| 80 | + title: 'Set up Developer Sandbox', |
| 81 | + } |
| 82 | + return sandboxItem; |
| 83 | + } |
| 84 | + |
| 85 | + private buildOpenshiftLocalItem() { |
| 86 | + const openshiftLocalItem = new RedHatCloudItem('Openshift Local'); |
| 87 | + openshiftLocalItem.tooltip = 'Provision OpenShift 4 cluster to your local computer.'; |
| 88 | + openshiftLocalItem.iconPath = new vscode.ThemeIcon('vm'); |
| 89 | + openshiftLocalItem.command = { |
| 90 | + command: 'openshift.local.open.setup', |
| 91 | + title: 'Install OpenShift Local', |
| 92 | + } |
| 93 | + return openshiftLocalItem; |
| 94 | + } |
| 95 | + |
| 96 | + @vsCommand('openshift.sandbox.signup') |
| 97 | + static async signupForSandbox(): Promise<string> { |
| 98 | + const authSession = await vscode.authentication.getSession('redhat-account-auth', ['openid'], { createIfNone: false }); |
| 99 | + if (authSession) { |
| 100 | + // eslint-disable-next-line dot-notation |
| 101 | + const signupResponse = await sandboxAPI.signUp(authSession['idToken'] as string); |
| 102 | + if (!signupResponse) { |
| 103 | + return 'Sign up request for OpenShift Sandbox failed, please try again.'; |
| 104 | + } |
| 105 | + await RedHatTreeDataProvier.refreshView(); |
| 106 | + |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @vsCommand('openshift.local.open.setup') |
| 111 | + static async openCrCWizard(): Promise<void> { |
| 112 | + const webViewPanel: vscode.WebviewPanel = await ClusterViewLoader.loadView('Add OpenShift Cluster'); |
| 113 | + await webViewPanel.webview.postMessage({ action: 'openCluster', param: 'crc' }); |
| 114 | + } |
| 115 | + |
| 116 | + @vsCommand('openshift.sandbox.open.setup') |
| 117 | + static async openSandboxWizard(): Promise<void> { |
| 118 | + const webViewPanel: vscode.WebviewPanel = await ClusterViewLoader.loadView('Add OpenShift Cluster'); |
| 119 | + await webViewPanel.webview.postMessage({ action: 'openCluster', param: 'sandbox' }); |
| 120 | + } |
| 121 | + |
| 122 | + @vsCommand('cloud.redhat.login', false) |
| 123 | + static async loginToRedHatCloud(): Promise<void> { |
| 124 | + const session = await vscode.authentication.getSession('redhat-account-auth', ['openid'], { createIfNone: true }); |
| 125 | + if (session) { |
| 126 | + await RedHatTreeDataProvier.refreshView(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @vsCommand('_openshift.open.signup', false) |
| 131 | + static async signupForRedHatAccount(): Promise<void> { |
| 132 | + return vscode.commands.executeCommand('vscode.open', 'https://red.ht/3MkQ54W'); |
| 133 | + } |
| 134 | + |
| 135 | + @vsCommand('openshift.sandbox.open.dashboard', false) |
| 136 | + static async openDashboard(): Promise<void> { |
| 137 | + const sandboxStatus = await RedHatTreeDataProvier.getSandboxSignupStatus(); |
| 138 | + if (sandboxStatus) { |
| 139 | + return vscode.commands.executeCommand('vscode.open', sandboxStatus.consoleURL); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static async getSandboxSignupStatus(): Promise<SBSignupResponse | undefined> { |
| 144 | + const authSession = await vscode.authentication.getSession('redhat-account-auth', ['openid'], { createIfNone: false }); |
| 145 | + if (authSession) { |
| 146 | + // eslint-disable-next-line dot-notation |
| 147 | + return await sandboxAPI.getSignUpStatus(authSession['idToken'] as string); |
| 148 | + } |
| 149 | + return undefined; |
| 150 | + } |
| 151 | + private static async refreshView() { |
| 152 | + const cloudExplorer = await k8s.extension.cloudExplorer.v1; |
| 153 | + if (cloudExplorer.available) { |
| 154 | + cloudExplorer.api.refresh(); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +class RedHatCloudProvider implements k8s.CloudExplorerV1.CloudProvider { |
| 160 | + getKubeconfigYaml(cluster: any): Promise<string> { |
| 161 | + throw new Error('Method not implemented.'); |
| 162 | + } |
| 163 | + readonly cloudName = 'Red Hat OpenShift'; |
| 164 | + readonly treeDataProvider = new RedHatTreeDataProvier(); |
| 165 | +} |
| 166 | + |
| 167 | +export const REDHAT_CLOUD_PROVIDER = new RedHatCloudProvider(); |
0 commit comments