createDataSource.vue 15.1 KB
Newer Older
K
khadgarmage 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
L
ligang 已提交
17 18 19
<template>
  <div class="datasource-popup-model">
    <div class="top-p">
G
i18n  
gongzijian 已提交
20
      <span>{{item ? `${$t('Edit')}` : `${$t('Create')}`}}{{`${$t('Datasource')}`}}</span>
L
ligang 已提交
21 22 23 24
    </div>
    <div class="content-p">
      <div class="create-datasource-model">
        <m-list-box-f>
25
          <template slot="name"><strong>*</strong>{{$t('Datasource')}}</template>
L
ligang 已提交
26 27 28 29
          <template slot="content">
            <x-radio-group v-model="type" size="small">
              <x-radio :label="'MYSQL'">MYSQL</x-radio>
              <x-radio :label="'POSTGRESQL'">POSTGRESQL</x-radio>
G
gongzijian 已提交
30
              <x-radio :label="'HIVE'">HIVE/IMPALA</x-radio>
L
ligang 已提交
31
              <x-radio :label="'SPARK'">SPARK</x-radio>
B
Baoqi 已提交
32
              <x-radio :label="'CLICKHOUSE'">CLICKHOUSE</x-radio>
B
Baoqi 已提交
33
              <x-radio :label="'ORACLE'">ORACLE</x-radio>
B
Baoqi 已提交
34
              <x-radio :label="'SQLSERVER'">SQLSERVER</x-radio>
B
bao liang 已提交
35
              <x-radio :label="'DB2'" class="radio-label-last" >DB2</x-radio>
L
ligang 已提交
36 37 38 39
            </x-radio-group>
          </template>
        </m-list-box-f>
        <m-list-box-f>
40
          <template slot="name"><strong>*</strong>{{$t('Datasource Name')}}</template>
L
ligang 已提交
41 42 43 44
          <template slot="content">
            <x-input
                    type="input"
                    v-model="name"
45
                    maxlength="60"
G
i18n  
gongzijian 已提交
46
                    :placeholder="$t('Please enter datasource name')"
L
ligang 已提交
47 48 49 50 51
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
G
i18n  
gongzijian 已提交
52
          <template slot="name">{{$t('Description')}}</template>
L
ligang 已提交
53 54 55 56
          <template slot="content">
            <x-input
                    type="textarea"
                    v-model="note"
G
i18n  
gongzijian 已提交
57
                    :placeholder="$t('Please enter description')"
L
ligang 已提交
58 59 60 61 62
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
63
          <template slot="name"><strong>*</strong>{{$t('IP')}}</template>
L
ligang 已提交
64 65 66 67
          <template slot="content">
            <x-input
                    type="input"
                    v-model="host"
68
                    maxlength="60"
G
i18n  
gongzijian 已提交
69
                    :placeholder="$t('Please enter IP')"
L
ligang 已提交
70 71 72 73 74
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
75
          <template slot="name"><strong>*</strong>{{$t('Port')}}</template>
L
ligang 已提交
76 77 78 79
          <template slot="content">
            <x-input
                    type="input"
                    v-model="port"
G
i18n  
gongzijian 已提交
80
                    :placeholder="$t('Please enter port')"
L
ligang 已提交
81 82 83 84
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
85
        <m-list-box-f :class="{hidden:showPrincipal}">
86
          <template slot="name"><strong>*</strong>Principal</template>
87 88 89 90 91 92 93 94 95
          <template slot="content">
            <x-input
              type="input"
              v-model="principal"
              :placeholder="$t('Please enter Principal')"
              autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
L
ligang 已提交
96
        <m-list-box-f>
97
          <template slot="name"><strong>*</strong>{{$t('User Name')}}</template>
L
ligang 已提交
98 99 100 101
          <template slot="content">
            <x-input
                    type="input"
                    v-model="userName"
102
                    maxlength="60"
G
i18n  
gongzijian 已提交
103
                    :placeholder="$t('Please enter user name')"
L
ligang 已提交
104 105 106 107 108
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
G
i18n  
gongzijian 已提交
109
          <template slot="name">{{$t('Password')}}</template>
L
ligang 已提交
110 111 112 113
          <template slot="content">
            <x-input
                    type="password"
                    v-model="password"
G
i18n  
gongzijian 已提交
114
                    :placeholder="$t('Please enter your password')"
L
ligang 已提交
115 116 117 118 119
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
120
          <template slot="name"><strong :class="{hidden:showdDatabase}">*</strong>{{$t('Database Name')}}</template>
L
ligang 已提交
121 122 123 124
          <template slot="content">
            <x-input
                    type="input"
                    v-model="database"
125
                    maxlength="60"
G
i18n  
gongzijian 已提交
126
                    :placeholder="$t('Please enter database name')"
L
ligang 已提交
127 128 129 130
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
131 132 133 134 135 136 137 138 139
        <m-list-box-f v-if="showConnectType">
          <template slot="name"><strong>*</strong>{{$t('Oracle Connect Type')}}</template>
          <template slot="content">
            <x-radio-group v-model="connectType" size="small">
              <x-radio :label="'ORACLE_SERVICE_NAME'">{{$t('Oracle Service Name')}}</x-radio>
              <x-radio :label="'ORACLE_SID'">{{$t('Oracle SID')}}</x-radio>
            </x-radio-group>
          </template>
        </m-list-box-f>
L
ligang 已提交
140
        <m-list-box-f>
G
i18n  
gongzijian 已提交
141
          <template slot="name">{{$t('jdbc connect parameters')}}</template>
L
ligang 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154
          <template slot="content">
            <x-input
                    type="textarea"
                    v-model="other"
                    :autosize="{minRows:2}"
                    :placeholder="_rtOtherPlaceholder()"
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
      </div>
    </div>
    <div class="bottom-p">
G
i18n  
gongzijian 已提交
155 156 157
      <x-button type="text" @click="_close()"> {{$t('Cancel')}} </x-button>
      <x-button type="success" shape="circle" @click="_testConnect()" :loading="testLoading">{{testLoading ? 'Loading...' : $t('Test Connect')}}</x-button>
      <x-button type="primary" shape="circle" :loading="spinnerLoading" @click="_ok()">{{spinnerLoading ? 'Loading...' :item ? `${$t('Edit')}` : `${$t('Submit')}`}} </x-button>
L
ligang 已提交
158 159 160 161 162 163
    </div>
  </div>
</template>
<script>
  import i18n from '@/module/i18n'
  import store from '@/conf/home/store'
164
  import {isJson} from '@/module/util/util'
L
ligang 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178
  import mPopup from '@/module/components/popup/popup'
  import mListBoxF from '@/module/components/listBoxF/listBoxF'

  export default {
    name: 'create-datasource',
    data () {
      return {
        store,
        // btn loading
        spinnerLoading: false,
        // Data source type
        type: 'MYSQL',
        // name
        name: '',
B
break60 已提交
179
        // description
L
ligang 已提交
180 181 182 183 184 185 186
        note: '',
        // host
        host: '',
        // port
        port: '',
        // data storage name
        database: '',
187 188
        // principal
        principal:'',
L
ligang 已提交
189 190 191 192
        // database username
        userName: '',
        // Database password
        password: '',
193 194
        // Database connect type
        connectType: '',
L
ligang 已提交
195 196 197
        // Jdbc connection parameter
        other: '',
        // btn test loading
198 199
        testLoading: false,
        showPrincipal: true,
200
        showdDatabase: false,
201
        showConnectType: false,
202 203
        isShowPrincipal:true,
        prePortMapper:{}
L
ligang 已提交
204 205 206 207 208
      }
    },
    props: {
      item: Object
    },
209

L
ligang 已提交
210 211
    methods: {
      _rtOtherPlaceholder () {
G
i18n  
gongzijian 已提交
212
        return `${i18n.$t('Please enter format')} {"key1":"value1","key2":"value2"...} ${i18n.$t('connection parameter')}`
L
ligang 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      },
      /**
       * submit
       */
      _ok () {
        if (this._verification()) {
          this._verifName().then(res => {
            this._submit()
          })
        }
      },
      /**
       * close
       */
      _close () {
        this.$emit('close')
      },
      /**
       * return param
       */
      _rtParam () {
        return {
          type: this.type,
          name: this.name,
          note: this.note,
          host: this.host,
          port: this.port,
          database: this.database,
241
          principal: this.principal,
L
ligang 已提交
242 243
          userName: this.userName,
          password: this.password,
244
          connectType: this.connectType,
L
ligang 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
          other: this.other
        }
      },
      /**
       * test connect
       */
      _testConnect () {
        if (this._verification()) {
          this.testLoading = true
          this.store.dispatch('datasource/connectDatasources', this._rtParam()).then(res => {
            setTimeout(() => {
              this.$message.success(res.msg)
              this.testLoading = false
            }, 800)
          }).catch(e => {
            this.$message.error(e.msg || '')
            this.testLoading = false
          })
        }
      },
      /**
       * Verify that the data source name exists
       */
      _verifName () {
        return new Promise((resolve, reject) => {
          if (this.name === this.item.name) {
            resolve()
            return
          }
          this.store.dispatch('datasource/verifyName', { name: this.name }).then(res => {
            resolve()
          }).catch(e => {
            this.$message.error(e.msg || '')
            reject(e)
          })
        })
      },
      /**
       * verification
       */
      _verification () {
        if (!this.name) {
G
i18n  
gongzijian 已提交
287
          this.$message.warning(`${i18n.$t('Please enter resource name')}`)
L
ligang 已提交
288 289 290
          return false
        }
        if (!this.host) {
G
i18n  
gongzijian 已提交
291
          this.$message.warning(`${i18n.$t('Please enter IP/hostname')}`)
L
ligang 已提交
292 293 294
          return false
        }
        if (!this.port) {
G
i18n  
gongzijian 已提交
295
          this.$message.warning(`${i18n.$t('Please enter port')}`)
L
ligang 已提交
296 297 298
          return false
        }
        if (!this.userName) {
G
i18n  
gongzijian 已提交
299
          this.$message.warning(`${i18n.$t('Please enter user name')}`)
L
ligang 已提交
300 301 302
          return false
        }

303
        if (!this.database && this.showdDatabase == false) {
G
i18n  
gongzijian 已提交
304
          this.$message.warning(`${i18n.$t('Please enter database name')}`)
L
ligang 已提交
305 306 307 308
          return false
        }
        if (this.other) {
          if (!isJson(this.other)) {
G
i18n  
gongzijian 已提交
309
            this.$message.warning(`${i18n.$t('jdbc connection parameters is not a correct JSON format')}`)
L
ligang 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            return false
          }
        }

        return true
      },
      /**
       * submit => add/update
       */
      _submit () {
        this.spinnerLoading = true
        let param = this._rtParam()
        // edit
        if (this.item) {
          param.id = this.item.id
        }
        this.store.dispatch(`datasource/${this.item ? `updateDatasource` : `createDatasources`}`, param).then(res => {
          this.$message.success(res.msg)
          this.spinnerLoading = false
          this.$emit('onUpdate')
        }).catch(e => {
          this.$message.error(e.msg || '')
          this.spinnerLoading = false
        })
      },
      /**
       * Get modified data
       */
338 339
      _getEditDatasource() {
        this.store.dispatch('datasource/getEditDatasource', {id: this.item.id}).then(res => {
L
ligang 已提交
340 341 342 343
          this.type = res.type
          this.name = res.name
          this.note = res.note
          this.host = res.host
344 345 346 347 348 349 350

          //When in Editpage, Prevent default value overwrite backfill value
          let that = this;
          setTimeout(() => {
            this.port = res.port
          },0)

351
          this.principal = res.principal
L
ligang 已提交
352 353 354
          this.database = res.database
          this.userName = res.userName
          this.password = res.password
355
          this.connectType = res.connectType
L
ligang 已提交
356 357 358 359
          this.other = JSON.stringify(res.other) === '{}' ? '' : JSON.stringify(res.other)
        }).catch(e => {
          this.$message.error(e.msg || '')
        })
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
      },
      /**
       * Set default port for each type.
       */
      _setDefaultValues(value) {

        //Default type is MYSQL
        let type = this.type || 'MYSQL'

        let defaultPort = this._getDefaultPort(type)

        //Backfill the previous input from memcache
        let mapperPort = this.prePortMapper[type]

        this.port = mapperPort || defaultPort

      },

      /**
       * Get default port by type
       */
      _getDefaultPort(type) {
        var defaultPort = ''
        switch (type) {
          case 'MYSQL':
            defaultPort = '3306'
            break
          case 'POSTGRESQL':
            defaultPort = '5432'
            break
          case 'HIVE':
            defaultPort = '10000'
            break
          case 'SPARK':
            defaultPort = '10015'
            break
          case 'CLICKHOUSE':
            defaultPort = '8123'
            break
          case 'ORACLE':
            defaultPort = '1521'
            break
          case 'SQLSERVER':
            defaultPort = '1433'
            break
          case 'DB2':
            defaultPort = '50000'
            break
          default:
            break

        }
        return defaultPort
      },
L
ligang 已提交
414 415 416 417 418 419
    },
    created () {
      // Backfill
      if (this.item.id) {
        this._getEditDatasource()
      }
420

421 422
      this._setDefaultValues()

423 424 425 426 427 428 429 430
    },
    watch: {
      type(value){
        if(value == 'POSTGRESQL') {
          this.showdDatabase = true;
        } else {
          this.showdDatabase = false;
        }
431

432
        if (value== 'ORACLE') {
433 434 435 436
          this.showConnectType = true;
          this.connectType = 'ORACLE_SERVICE_NAME'
        } else {
          this.showConnectType = false;
437
          this.connectType = ''
438
        }
439

440 441 442
        //Set default port for each type datasource
        this._setDefaultValues(value)

443 444 445 446 447 448 449 450 451 452 453 454
        return new Promise((resolve, reject) => {
          this.store.dispatch('datasource/getKerberosStartupState').then(res => {
            this.isShowPrincipal=res
            if((value =='HIVE'||value == 'SPARK')&&this.isShowPrincipal== true){
              this.showPrincipal = false
            }else{
              this.showPrincipal = true
            }
          }).catch(e => {
            this.$message.error(e.msg || '')
            reject(e)
          })
455
        })
456 457 458 459 460 461 462
      },
      /**
       * Cache the previous input port for each type datasource
       * @param value
       */
      port(value){
        this.prePortMapper[this.type] = value
463
      }
L
ligang 已提交
464
    },
B
bao liang 已提交
465

L
ligang 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    mounted () {
    },
    components: { mPopup, mListBoxF }
  }
</script>

<style lang="scss" rel="stylesheet/scss">
  .datasource-popup-model {
    background: #fff;
    border-radius: 3px;

    .top-p {
      height: 70px;
      line-height: 70px;
      border-radius: 3px 3px 0 0;
      padding: 0 20px;
      >span {
        font-size: 20px;
      }
    }
    .bottom-p {
      text-align: right;
      height: 72px;
      line-height: 72px;
      border-radius:  0 0 3px 3px;
      padding: 0 20px;
    }
    .content-p {
G
gongzijian 已提交
494
      min-width: 850px;
L
ligang 已提交
495
      min-height: 100px;
496 497 498 499 500 501 502 503
      .list-box-f {
        .text {
          width: 166px;
        }
        .cont {
          width: calc(100% - 186px);
        }
      }
L
ligang 已提交
504
    }
B
bao liang 已提交
505 506 507
    .radio-label-last {
      margin-left: 0px !important;
    }
L
ligang 已提交
508
  }
B
bao liang 已提交
509 510


L
ligang 已提交
511
</style>