sureshanaparti commented on a change in pull request #353:
URL: https://github.com/apache/cloudstack-primate/pull/353#discussion_r429894803



##########
File path: src/views/iam/ImportRole.vue
##########
@@ -0,0 +1,267 @@
+// 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.
+
+<template>
+  <div class="form-layout">
+    <a-spin :spinning="loading">
+      <a-form
+        :form="form"
+        @submit="handleSubmit"
+        layout="vertical">
+        <a-form-item :label="$t('rulesFile')">
+          <a-upload-dragger
+            :multiple="false"
+            :fileList="fileList"
+            :remove="handleRemove"
+            :beforeUpload="beforeUpload"
+            @change="handleChange"
+            v-decorator="['file', {
+              rules: [{ required: true, message: 'Please enter input' }]
+            }]">
+            <p class="ant-upload-drag-icon">
+              <a-icon type="cloud-upload" />
+            </p>
+            <p class="ant-upload-text" v-if="fileList.length === 0">
+              Click or drag rule defintions CVS file to import
+            </p>
+          </a-upload-dragger>
+        </a-form-item>
+        <a-form-item :label="$t('name')">
+          <a-input
+            v-decorator="['name', {
+              rules: [{ required: true, message: 'Please enter input' }]
+            }]"
+            :placeholder="$t('role.name')" />
+        </a-form-item>
+
+        <a-form-item :label="$t('description')">
+          <a-input
+            v-decorator="['description']"
+            :placeholder="$t('role.description')" />
+        </a-form-item>
+
+        <a-form-item :label="$t('type')">
+          <a-select
+            v-decorator="['type', {
+              rules: [{ required: true, message: 'Please select option'}]
+            }]"
+            :placeholder="$t('role.type')">
+            <a-select-option value="Admin">Admin</a-select-option>
+            <a-select-option value="DomainAdmin">DomainAdmin</a-select-option>
+            <a-select-option 
value="ResourceAdmin">ResourceAdmin</a-select-option>
+            <a-select-option value="User">User</a-select-option>
+          </a-select>
+        </a-form-item>
+
+        <a-form-item :label="$t('forced')">
+          <a-switch
+            v-decorator="['forced', {
+              initialValue: false
+            }]" />
+        </a-form-item>
+
+        <div :span="24" class="action-button">
+          <a-button @click="closeAction">{{ this.$t('Cancel') }}</a-button>
+          <a-button :loading="loading" type="primary" @click="handleSubmit">{{ 
this.$t('OK') }}</a-button>
+        </div>
+      </a-form>
+    </a-spin>
+  </div>
+</template>
+
+<script>
+import { api } from '@/api'
+
+export default {
+  name: 'ImportRole',
+  props: {
+    resource: {
+      type: Object,
+      required: true
+    },
+    action: {
+      type: Object,
+      required: true
+    }
+  },
+  data () {
+    return {
+      fileList: [],
+      loading: false
+    }
+  },
+  beforeCreate () {
+    this.form = this.$form.createForm(this)
+  },
+  methods: {
+    handleRemove (file) {
+      const index = this.fileList.indexOf(file)
+      const newFileList = this.fileList.slice()
+      newFileList.splice(index, 1)
+      this.fileList = newFileList
+    },
+    handleChange (info) {
+      if (info.file.status === 'error') {
+        this.$notification.error({
+          message: 'File Upload Failed',
+          description: 'File: ' + info.file.name + ' Upload Failed'
+        })
+      }
+    },
+    beforeUpload (file) {
+      if (file.type !== 'text/csv') {
+        alert('Only CSV file can be imported')
+        return false
+      }
+
+      this.fileList = [file]
+      return false
+    },
+    handleSubmit (e) {
+      e.preventDefault()
+      this.form.validateFields((err, values) => {
+        if (err) {
+          return
+        }
+        const params = {}
+        for (const key in values) {
+          const input = values[key]
+          if (input === undefined) {
+            continue
+          }
+          if (key === 'file') {
+            continue
+          }
+
+          params[key] = input
+        }
+
+        if (this.fileList.length !== 1) {
+          return
+        }
+
+        this.loadCSVFile(this.fileList[0]).then((rules) => {
+          rules.forEach(function (values, index) {
+            for (const key in values) {
+              params['rules[' + index + '].' + key] = values[key]
+            }
+          })
+
+          this.importRole(params)
+        })
+      })
+    },
+    closeAction () {
+      this.$emit('close-action')
+    },
+    importRole (params) {
+      this.loading = true
+      api('importRole', {}, 'POST', params).then(json => {
+        const role = json.importroleresponse.role
+        if (role) {
+          this.$emit('refresh-data')
+          this.$notification.success({
+            message: 'Import Role',
+            description: 'Sucessfully imported Role ' + params.name
+          })
+        }
+      }).catch(error => {
+        this.$notifyError(error)
+      }).finally(() => {
+        this.loading = false
+        this.closeAction()
+      })
+    },
+    csvToJson (csv) {
+      var lines = csv.split('\n')
+      var result = []
+      var headers = lines[0].split(',')
+
+      lines.map((line, indexLine) => {
+        if (indexLine < 1) return // Jump header line
+        var obj = {}
+        var currentline = line.split(',')
+
+        headers.map((header, indexHeader) => {
+          obj[header] = currentline[indexHeader]
+        })
+        result.push(obj)
+      })
+      result.pop() // remove the last item because undefined values
+
+      return result
+    },
+    loadCSVFile (file) {
+      return new Promise((resolve, reject) => {
+        if (window.FileReader) {
+          var reader = new FileReader()
+          reader.onload = (event) => {
+            var csv = event.target.result
+            // var lines = csv.split('\n')
+            // var result = []
+            // var headers = lines[0].split(',')
+
+            // lines.map((line, indexLine) => {
+            //  if (indexLine < 1) return
+            //  var obj = {}
+            //  var currentline = line.split(',')
+
+            //  headers.map((header, indexHeader) => {
+            //    obj[header] = currentline[indexHeader]
+            //  })
+            //  result.push(obj)
+            // })
+            // result.pop()
+

Review comment:
       > To be removed
   
   Removed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to