RoutingRule.vue 10.7 KB
Newer Older
M
min 已提交
1 2 3 4 5 6
<!--
  - 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
N
nzomkxia 已提交
7
  - the License.  You may obtain a copy of the License at
M
min 已提交
8
  -
N
nzomkxia 已提交
9
  -     http://www.apache.org/licenses/LICENSE-2.0
M
min 已提交
10
  -
N
nzomkxia 已提交
11 12 13 14 15
  - 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.
M
min 已提交
16 17 18 19
  -->

<template>
  <v-container grid-list-xl fluid >
20
      <v-layout row wrap>
M
min 已提交
21
        <v-flex xs12 >
N
nzomkxia 已提交
22
          <search v-model="filter" :submit="submit" label="Search Routing Rule by service name"></search>
23 24
        </v-flex>
      </v-layout>
B
beiwei30 已提交
25 26 27

    <v-flex lg12>
      <v-card>
B
beiwei30 已提交
28 29
        <v-toolbar flat color="transparent" class="elevation-0">
          <v-toolbar-title><span class="headline">Search Result</span></v-toolbar-title>
B
beiwei30 已提交
30 31 32 33 34 35 36 37 38
          <v-spacer></v-spacer>
          <v-btn outline color="primary" @click.stop="openDialog" class="mb-2">CREATE</v-btn>
        </v-toolbar>

        <v-card-text class="pa-0">
          <v-data-table
            :headers="headers"
            :items="routingRules"
            hide-actions
B
beiwei30 已提交
39
            class="elevation-0"
B
beiwei30 已提交
40 41 42
          >
            <template slot="items" slot-scope="props">
              <td class="text-xs-left">{{ props.item.service }}</td>
N
nzomkxia 已提交
43
              <td class="text-xs-left">{{ props.item.group }}</td>
B
beiwei30 已提交
44
              <td class="text-xs-left">{{ props.item.priority }}</td>
N
nzomkxia 已提交
45
              <td class="text-xs-left">{{ props.item.enabled }}</td>
M
Ma.Jinkai 已提交
46
              <td class="text-xs-center px-0">
N
nzomkxia 已提交
47
                <v-tooltip bottom v-for="op in operations" :key="op.id">
N
nzomkxia 已提交
48 49
                  <v-icon small class="mr-2" slot="activator" @click="itemOperation(op.icon(props.item), props.item)">
                    {{op.icon(props.item)}}
B
beiwei30 已提交
50
                  </v-icon>
N
nzomkxia 已提交
51
                  <span>{{op.tooltip(props.item)}}</span>
B
beiwei30 已提交
52
                </v-tooltip>
B
beiwei30 已提交
53 54 55 56 57 58 59
              </td>
            </template>
          </v-data-table>
        </v-card-text>
      </v-card>
    </v-flex>

60
    <v-dialog   v-model="dialog" width="800px" persistent >
M
min 已提交
61 62
      <v-card>
        <v-card-title class="justify-center">
B
beiwei30 已提交
63
          <span class="headline">Create New Routing Rule</span>
M
min 已提交
64 65
        </v-card-title>
        <v-card-text >
M
min 已提交
66
          <v-text-field
B
beiwei30 已提交
67 68
            label="Service Unique ID"
            hint="A service ID in form of service:version, version is optional"
N
nzomkxia 已提交
69
            :rules="[required]"
M
min 已提交
70
            v-model="service"
M
min 已提交
71 72
          ></v-text-field>
          <v-text-field
B
beiwei30 已提交
73 74
            label="Application Name"
            hint="Application name the service belongs to"
M
min 已提交
75
            v-model="application"
M
min 已提交
76
          ></v-text-field>
B
beiwei30 已提交
77 78

          <v-subheader class="pa-0 mt-3">RULE CONTENT</v-subheader>
N
nzomkxia 已提交
79
          <ace-editor v-model="ruleText" :readonly="readonly"></ace-editor>
80

M
min 已提交
81 82 83
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
N
nzomkxia 已提交
84 85
          <v-btn color="blue darken-1" flat @click.native="closeDialog">Close</v-btn>
          <v-btn color="blue darken-1" flat @click.native="saveItem">Save</v-btn>
M
min 已提交
86 87 88
        </v-card-actions>
      </v-card>
    </v-dialog>
N
nzomkxia 已提交
89

N
nzomkxia 已提交
90 91 92 93 94 95
    <v-dialog v-model="warn" persistent max-width="500px">
      <v-card>
        <v-card-title class="headline">{{this.warnTitle}}</v-card-title>
        <v-card-text >{{this.warnText}}</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
N
nzomkxia 已提交
96 97
          <v-btn color="green darken-1" flat @click.native="closeWarn">CANCLE</v-btn>
          <v-btn color="green darken-1" flat @click.native="deleteItem(warnStatus)">CONFIRM</v-btn>
N
nzomkxia 已提交
98 99 100
        </v-card-actions>
      </v-card>
    </v-dialog>
N
nzomkxia 已提交
101

M
min 已提交
102 103 104 105
  </v-container>

</template>
<script>
N
nzomkxia 已提交
106
  import yaml from 'js-yaml'
N
nzomkxia 已提交
107 108
  import AceEditor from '@/components/public/AceEditor'
  import Search from '@/components/public/Search'
N
nzomkxia 已提交
109
  import operations from '@/api/operation'
M
min 已提交
110
  export default {
M
min 已提交
111
    components: {
N
nzomkxia 已提交
112 113
      AceEditor,
      Search
M
min 已提交
114
    },
M
min 已提交
115 116
    data: () => ({
      dropdown_font: [ 'Service', 'App', 'IP' ],
N
nzomkxia 已提交
117
      ruleKeys: ['enabled', 'force', 'dynamic', 'runtime', 'group', 'version', 'rule', 'priority'],
118 119
      pattern: 'Service',
      filter: '',
M
min 已提交
120
      dialog: false,
N
nzomkxia 已提交
121
      warn: false,
N
nzomkxia 已提交
122
      updateId: '',
M
min 已提交
123 124
      application: '',
      service: '',
N
nzomkxia 已提交
125 126
      warnTitle: '',
      warnText: '',
N
nzomkxia 已提交
127
      warnStatus: {},
M
min 已提交
128
      height: 0,
N
nzomkxia 已提交
129
      operations: operations,
130 131
      routingRules: [
      ],
N
nzomkxia 已提交
132
      required: value => !!value || 'Service ID is required',
N
nzomkxia 已提交
133
      template:
N
nzomkxia 已提交
134
        'enabled: true/false\n' +
M
min 已提交
135 136 137 138 139
        'priority:\n' +
        'runtime: false/true\n' +
        'force: true/false\n' +
        'dynamic: true/false\n' +
        'conditions:\n' +
N
nzomkxia 已提交
140 141 142 143 144
        ' - \'=> host != 172.22.3.91\'\n' +
        ' - \'host != 10.20.153.10,10.20.153.11 =>\'\n' +
        ' - \'host = 10.20.153.10,10.20.153.11 =>\'\n' +
        ' - \'application != kylin => host != 172.22.3.95,172.22.3.96\'\n' +
        ' - \'method = find*,list*,get*,is* => host = 172.22.3.94,172.22.3.95,172.22.3.96\'',
N
nzomkxia 已提交
145
      ruleText: '',
N
nzomkxia 已提交
146
      readonly: false,
M
min 已提交
147 148
      headers: [
        {
N
nzomkxia 已提交
149 150
          text: 'Service Name',
          value: 'service',
B
beiwei30 已提交
151
          align: 'left'
152 153
        },
        {
N
nzomkxia 已提交
154 155
          text: 'Group',
          value: 'group',
B
beiwei30 已提交
156
          align: 'left'
N
nzomkxia 已提交
157

M
min 已提交
158 159
        },
        {
160 161
          text: 'Priority',
          value: 'priority',
B
beiwei30 已提交
162
          sortable: false
M
min 已提交
163 164
        },
        {
N
nzomkxia 已提交
165 166
          text: 'Enabled',
          value: 'enabled',
B
beiwei30 已提交
167
          sortable: false
M
min 已提交
168 169 170 171
        },
        {
          text: 'Operation',
          value: 'operation',
M
Ma.Jinkai 已提交
172 173
          sortable: false,
          width: '115px'
M
min 已提交
174 175 176 177
        }
      ]
    }),
    methods: {
M
min 已提交
178
      submit: function () {
N
nzomkxia 已提交
179
        this.search(this.filter, true)
180
      },
N
nzomkxia 已提交
181
      search: function (filter, rewrite) {
马金凯 已提交
182
        this.$axios.get('/rules/route/', {
N
nzomkxia 已提交
183 184 185 186 187 188 189 190 191
          params: {
            service: filter
          }
        }).then(response => {
          this.routingRules = response.data
          if (rewrite) {
            this.$router.push({path: 'routingRule', query: {service: filter}})
          }
        })
N
nzomkxia 已提交
192
      },
N
nzomkxia 已提交
193 194
      closeDialog: function () {
        this.ruleText = this.template
N
nzomkxia 已提交
195
        this.updateId = ''
N
nzomkxia 已提交
196
        this.service = ''
N
nzomkxia 已提交
197
        this.application = ''
N
nzomkxia 已提交
198
        this.dialog = false
N
nzomkxia 已提交
199
        this.readonly = false
N
nzomkxia 已提交
200
      },
M
min 已提交
201 202
      openDialog: function () {
        this.dialog = true
203
      },
N
nzomkxia 已提交
204 205 206 207 208 209 210 211 212 213 214
      openWarn: function (title, text) {
        this.warnTitle = title
        this.warnText = text
        this.warn = true
      },
      closeWarn: function () {
        this.warnTitle = ''
        this.warnText = ''
        this.warn = false
      },
      saveItem: function () {
N
nzomkxia 已提交
215
        let rule = yaml.safeLoad(this.ruleText)
N
nzomkxia 已提交
216 217 218
        if (rule.service === '') {
          return
        }
N
nzomkxia 已提交
219
        rule.service = this.service
N
nzomkxia 已提交
220 221 222 223 224
        if (this.updateId !== '') {
          if (this.updateId === 'close') {
            this.closeDialog()
          } else {
            rule.id = this.updateId
马金凯 已提交
225
            this.$axios.put('/rules/route/' + rule.id, rule)
N
nzomkxia 已提交
226 227 228 229 230 231 232
              .then(response => {
                if (response.data) {
                  this.search(this.service, true)
                }
                this.closeDialog()
              })
          }
N
nzomkxia 已提交
233
        } else {
马金凯 已提交
234
          this.$axios.post('/rules/route/', rule)
N
nzomkxia 已提交
235 236 237
            .then(response => {
              if (response.data) {
                this.search(this.service, true)
N
nzomkxia 已提交
238
                this.filter = this.service
N
nzomkxia 已提交
239 240 241
              }
              this.closeDialog()
            })
N
nzomkxia 已提交
242 243 244
            .catch(error => {
              console.log(error)
            })
N
nzomkxia 已提交
245
        }
N
nzomkxia 已提交
246
      },
N
nzomkxia 已提交
247 248 249
      itemOperation: function (icon, item) {
        switch (icon) {
          case 'visibility':
马金凯 已提交
250
            this.$axios.get('/rules/route/' + item.id)
N
nzomkxia 已提交
251 252
              .then(response => {
                let route = response.data
N
nzomkxia 已提交
253 254
                this.handleBalance(route, true)
                this.updateId = 'close'
N
nzomkxia 已提交
255 256 257
              })
            break
          case 'edit':
N
nzomkxia 已提交
258 259
            let id = {}
            id.id = item.id
马金凯 已提交
260
            this.$axios.get('/rules/route/' + item.id)
N
nzomkxia 已提交
261
              .then(response => {
N
nzomkxia 已提交
262
                let route = response.data
N
nzomkxia 已提交
263
                this.handleBalance(route, false)
N
nzomkxia 已提交
264
                this.updateId = item.id
N
nzomkxia 已提交
265 266 267
              })
            break
          case 'block':
N
nzomkxia 已提交
268 269 270 271
            this.openWarn(' Are you sure to block Routing Rule', 'service: ' + item.service)
            this.warnStatus.operation = 'disable'
            this.warnStatus.id = item.id
            break
N
nzomkxia 已提交
272
          case 'check_circle_outline':
N
nzomkxia 已提交
273 274
            this.openWarn(' Are you sure to enable Routing Rule', 'service: ' + item.service)
            this.warnStatus.operation = 'enable'
N
nzomkxia 已提交
275
            this.warnStatus.id = item.id
N
nzomkxia 已提交
276 277
            break
          case 'delete':
N
nzomkxia 已提交
278
            this.openWarn(' Are you sure to Delete Routing Rule', 'service: ' + item.service)
N
nzomkxia 已提交
279 280
            this.warnStatus.operation = 'delete'
            this.warnStatus.id = item.id
281
        }
M
min 已提交
282
      },
N
nzomkxia 已提交
283
      handleBalance: function (route, readonly) {
N
nzomkxia 已提交
284 285 286 287 288 289 290 291 292
        this.service = route.service
        delete route.service
        delete route.id
        delete route.app
        delete route.group
        this.ruleText = yaml.safeDump(route)
        this.readonly = readonly
        this.dialog = true
      },
M
min 已提交
293
      setHeight: function () {
M
min 已提交
294
        this.height = window.innerHeight * 0.5
N
nzomkxia 已提交
295
      },
N
nzomkxia 已提交
296
      deleteItem: function (warnStatus) {
N
nzomkxia 已提交
297 298 299
        let id = warnStatus.id
        let operation = warnStatus.operation
        if (operation === 'delete') {
马金凯 已提交
300
          this.$axios.delete('/rules/route/' + id)
N
nzomkxia 已提交
301 302 303 304
            .then(response => {
              this.warn = false
              this.search(this.filter, false)
            })
N
nzomkxia 已提交
305
        } else if (operation === 'disable') {
马金凯 已提交
306
          this.$axios.put('/rules/route/disable/' + id)
N
nzomkxia 已提交
307 308 309 310
            .then(response => {
              this.warn = false
              this.search(this.filter, false)
            })
N
nzomkxia 已提交
311
        } else if (operation === 'enable') {
马金凯 已提交
312
          this.$axios.put('/rules/route/enable/' + id)
N
nzomkxia 已提交
313 314 315 316 317
            .then(response => {
              this.warn = false
              this.search(this.filter, false)
            })
        }
M
min 已提交
318 319 320 321
      }
    },
    created () {
      this.setHeight()
N
nzomkxia 已提交
322 323
    },
    mounted: function () {
N
nzomkxia 已提交
324
      this.ruleText = this.template
N
nzomkxia 已提交
325
      let query = this.$route.query
N
nzomkxia 已提交
326
      let service = null
N
nzomkxia 已提交
327
      Object.keys(query).forEach(function (key) {
N
nzomkxia 已提交
328
        if (key === 'service') {
N
nzomkxia 已提交
329 330 331
          service = query[key]
        }
      })
N
nzomkxia 已提交
332
      if (service !== null) {
N
nzomkxia 已提交
333 334 335
        this.filter = service
        this.search(service, false)
      }
M
min 已提交
336 337 338 339
    }

  }
</script>