codekingpro/portable-devtools
114k
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10import React from 'react';11import pgAdmin from 'sources/pgadmin';12import { getNodeAjaxOptions, getNodeListById } from 'pgbrowser/node_ajax';13import {BigAnimalClusterSchema, BigAnimalDatabaseSchema, BigAnimalClusterTypeSchema} from './biganimal_schema.ui';14import SchemaView from '../../../../static/js/SchemaView';15import url_for from 'sources/url_for';16import getApiInstance from '../../../../static/js/api_instance';17import { isEmptyString } from 'sources/validators';18import PropTypes from 'prop-types';19import gettext from 'sources/gettext';20 21const axiosApi = getApiInstance();22 23// BigAnimal Cluster Type24export function BigAnimalClusterType(props) {25 const [bigAnimalClusterType, setBigAnimalClusterType] = React.useState();26 27 React.useMemo(() => {28 const bigAnimalClusterTypeSchema = new BigAnimalClusterTypeSchema({29 projects: ()=>getNodeAjaxOptions('biganimal_projects', pgAdmin.Browser.Nodes['server'], props.nodeInfo, props.nodeData, {30 useCache:false,31 cacheNode: 'server',32 customGenerateUrl: ()=>{33 return url_for('biganimal.projects');34 }35 }),36 providers: (project)=>getNodeAjaxOptions('biganimal_providers', pgAdmin.Browser.Nodes['server'], props.nodeInfo, props.nodeData, {37 useCache:false,38 cacheNode: 'server',39 customGenerateUrl: ()=>{40 return url_for('biganimal.providers', {'project_id':project});41 }42 }),43 }, {44 nodeInfo: props.nodeInfo,45 nodeData: props.nodeData,46 hostIP: props.hostIP,47 });48 setBigAnimalClusterType(bigAnimalClusterTypeSchema);49 }, [props.cloudProvider]);50 51 return <SchemaView52 formType={'dialog'}53 getInitData={() => { /*This is intentional (SonarQube)*/ }}54 viewHelperProps={{ mode: 'create' }}55 schema={bigAnimalClusterType}56 showFooter={false}57 isTabView={false}58 onDataChange={(isChanged, changedData) => {59 props.setBigAnimalClusterTypeData(changedData);60 }}61 />;62}63BigAnimalClusterType.propTypes = {64 nodeInfo: PropTypes.object,65 nodeData: PropTypes.object,66 cloudProvider: PropTypes.string,67 setBigAnimalClusterTypeData: PropTypes.func,68 hostIP: PropTypes.string69};70 71 72// BigAnimal Instance73export function BigAnimalInstance(props) {74 const [bigAnimalInstance, setBigAnimalInstance] = React.useState();75 76 React.useMemo(() => {77 const bigAnimalSchema = new BigAnimalClusterSchema({78 regions: ()=>getNodeAjaxOptions('biganimal_regions', pgAdmin.Browser.Nodes['server'], props.nodeInfo, props.nodeData, {79 useCache:false,80 cacheNode: 'server',81 customGenerateUrl: ()=>{82 return url_for('biganimal.regions');83 }84 }),85 instance_types: (region_id, provider_id)=>{86 if (isEmptyString(region_id)) return [];87 return getNodeAjaxOptions('biganimal_instance_types', pgAdmin.Browser.Nodes['server'],88 props.nodeInfo, props.nodeData, {89 useCache:false,90 cacheNode: 'server',91 customGenerateUrl: ()=>{92 return url_for('biganimal.instance_types', {'region_id': region_id || 0, 'provider_id': provider_id || 0});93 }94 });95 },96 volume_types: (region_id, provider_id)=>{97 if (isEmptyString(region_id)) return [];98 return getNodeAjaxOptions('biganimal_volume_types', pgAdmin.Browser.Nodes['server'],99 props.nodeInfo, props.nodeData, {100 useCache:false,101 cacheNode: 'server',102 customGenerateUrl: ()=>{103 return url_for('biganimal.volume_types', {'region_id': region_id || 0, 'provider_id': provider_id || 0});104 }105 });106 },107 volume_properties: (region_id, provider_id, volume_type)=>{108 if (isEmptyString(region_id) || isEmptyString(volume_type)) return [];109 return getNodeAjaxOptions('biganimal_volume_properties', pgAdmin.Browser.Nodes['server'],110 props.nodeInfo, props.nodeData, {111 useCache:false,112 cacheNode: 'server',113 customGenerateUrl: ()=>{114 return url_for('biganimal.volume_properties', {'region_id': region_id || 0, 'provider_id': provider_id || 0, 'volume_type': volume_type || ''});115 }116 });117 },118 }, {119 nodeInfo: props.nodeInfo,120 nodeData: props.nodeData,121 hostIP: props.hostIP,122 provider: props.bigAnimalClusterTypeData.provider,123 });124 setBigAnimalInstance(bigAnimalSchema);125 }, [props.cloudProvider]);126 127 return <SchemaView128 formType={'dialog'}129 getInitData={() => { /*This is intentional (SonarQube)*/ }}130 viewHelperProps={{ mode: 'create' }}131 schema={bigAnimalInstance}132 showFooter={false}133 isTabView={false}134 onDataChange={(isChanged, changedData) => {135 props.setBigAnimalInstanceData(changedData);136 }}137 />;138}139BigAnimalInstance.propTypes = {140 nodeInfo: PropTypes.object,141 nodeData: PropTypes.object,142 cloudProvider: PropTypes.string,143 setBigAnimalInstanceData: PropTypes.func,144 hostIP: PropTypes.string,145 bigAnimalClusterTypeData: PropTypes.object,146};147 148 149// BigAnimal Database150export function BigAnimalDatabase(props) {151 const [bigAnimalDatabase, setBigAnimalDatabase] = React.useState();152 153 React.useMemo(() => {154 const bigAnimalDBSchema = new BigAnimalDatabaseSchema({155 db_types: ()=>getNodeAjaxOptions('biganimal_db_types', pgAdmin.Browser.Nodes['server'], props.nodeInfo, props.nodeData, {156 useCache:false,157 cacheNode: 'server',158 customGenerateUrl: ()=>{159 return url_for('biganimal.db_types');160 }161 }),162 db_versions: (cluster_type, pg_type)=>getNodeAjaxOptions('biganimal_db_versions', pgAdmin.Browser.Nodes['server'], props.nodeInfo, props.nodeData, {163 useCache:false,164 cacheNode: 'server',165 customGenerateUrl: ()=>{166 return url_for('biganimal.db_versions', {'cluster_type': cluster_type || 'single', 'pg_type': pg_type || 'pg'});167 }168 }),169 server_groups: ()=>getNodeListById(pgAdmin.Browser.Nodes['server_group'], props.nodeInfo, props.nodeData),170 }, {171 gid: props.nodeInfo['server_group']._id,172 cluster_type: props.bigAnimalClusterTypeData.cluster_type,173 });174 setBigAnimalDatabase(bigAnimalDBSchema);175 }, [props.cloudProvider]);176 177 return <SchemaView178 formType={'dialog'}179 getInitData={() => { /*This is intentional (SonarQube)*/ }}180 viewHelperProps={{ mode: 'create' }}181 schema={bigAnimalDatabase}182 showFooter={false}183 isTabView={false}184 onDataChange={(isChanged, changedData) => {185 props.setBigAnimalDatabaseData(changedData);186 }}187 />;188}189BigAnimalDatabase.propTypes = {190 nodeInfo: PropTypes.object,191 nodeData: PropTypes.object,192 cloudProvider: PropTypes.string,193 setBigAnimalDatabaseData: PropTypes.func,194 bigAnimalClusterTypeData: PropTypes.object195};196 197 198export function validateBigAnimal() {199 return new Promise((resolve, reject)=>{200 let _url = url_for('biganimal.verification');201 axiosApi.get(_url)202 .then((res) => {203 if (res.data.data) {204 resolve(res.data.data);205 }206 })207 .catch((error) => {208 reject(new Error(`Error while fetching EDB BigAnimal verification URI: ${error.response.data.errormsg}`));209 });210 });211}212 213function createData(name, value) {214 if (typeof(value) == 'boolean') {215 value = (value === true) ? 'True' : 'False';216 }217 return { name, value };218}219 220export function getBigAnimalSummary(cloud, bigAnimalClusterTypeData, bigAnimalInstanceData, bigAnimalDatabaseData) {221 const rows1 = [222 createData(gettext('Cloud'), cloud),223 createData(gettext('Instance name'), bigAnimalInstanceData.name),224 createData(gettext('Region'), bigAnimalInstanceData.region),225 createData(gettext('Cluster type'), bigAnimalInstanceData.cloud_type),226 createData(gettext('Public IPs'), bigAnimalInstanceData.biganimal_public_ip),227 ];228 229 let instance_size = bigAnimalInstanceData.instance_size.split('||');230 231 const rows2 = [232 createData(gettext('Cluster type'), bigAnimalClusterTypeData.cluster_type),233 createData(gettext('No. of Standby Replicas'), bigAnimalClusterTypeData.replicas),234 createData(gettext('Provider'), bigAnimalClusterTypeData.provider),235 ];236 237 const rows3 = [238 createData(gettext('Instance type'), bigAnimalInstanceData.instance_type),239 createData(gettext('Instance series'), bigAnimalInstanceData.instance_series),240 createData(gettext('Instance size'), instance_size[0]),241 ];242 243 const rows4 = [244 createData(gettext('Volume type'), bigAnimalInstanceData.volume_type),245 createData(gettext('Volume size'), bigAnimalInstanceData.volume_size)246 ];247 if(bigAnimalClusterTypeData.provider.includes('aws')){248 rows4.push(createData(gettext('Volume IOPS'), bigAnimalInstanceData.volume_IOPS));249 rows4.push(createData(gettext('Disk Throuhgput'), bigAnimalInstanceData.disk_throughput));250 }251 252 const rows5 = [253 createData(gettext('Password'), 'xxxxxxx'),254 createData(gettext('Database Type'), bigAnimalDatabaseData.database_type),255 createData(gettext('Database Version'), bigAnimalDatabaseData.postgres_version),256 ];257 258 return [rows1, rows2, rows3, rows4, rows5];259}260 261export function validateBigAnimalStep2(cloudTypeDetails) {262 let isError = false;263 if (isEmptyString(cloudTypeDetails.cluster_type) || isEmptyString(cloudTypeDetails.provider) ||264 (cloudTypeDetails.cluster_type == 'ha' && cloudTypeDetails.replicas == 0)265 ) {266 isError = true;267 }268 269 return isError;270}271 272export function validateBigAnimalStep3(cloudDetails) {273 let isError = false;274 if (isEmptyString(cloudDetails.name) ||275 isEmptyString(cloudDetails.region) || isEmptyString(cloudDetails.instance_type) ||276 isEmptyString(cloudDetails.instance_series)|| isEmptyString(cloudDetails.instance_size) ||277 isEmptyString(cloudDetails.volume_type) || (!cloudDetails.provider.includes('aws') && isEmptyString(cloudDetails.volume_properties)) ) {278 isError = true;279 }280 return isError;281}282 283export function validateBigAnimalStep4(cloudDBDetails, nodeInfo) {284 let isError = false;285 if (isEmptyString(cloudDBDetails.password) ||286 isEmptyString(cloudDBDetails.database_type) || isEmptyString(cloudDBDetails.postgres_version)) {287 isError = true;288 }289 290 if (isEmptyString(cloudDBDetails.gid)) cloudDBDetails.gid = nodeInfo['server_group']._id;291 return isError;292}293 