Copilot commented on code in PR #12013:
URL: https://github.com/apache/cloudstack/pull/12013#discussion_r2591982741


##########
ui/src/views/network/ImportNetworkACL.vue:
##########
@@ -0,0 +1,380 @@
+// 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 v-ctrl-enter="handleSubmit">
+    <a-spin :spinning="loading">
+      <a-form
+        :ref="formRef"
+        :model="form"
+        :rules="rules"
+        layout="vertical"
+        @finish="handleSubmit">
+
+        <div class="info-section" style="margin-bottom: 24px; padding: 16px; 
background: #fafafa; border-radius: 4px;">
+          <a-descriptions :column="1" size="small">
+            <a-descriptions-item :label="$t('label.acl.id')">
+              <span style="font-family: monospace;">{{ resource.id }}</span>
+            </a-descriptions-item>
+            <a-descriptions-item :label="$t('label.add.acl.name')">
+              <strong>{{ resource.name }}</strong>
+            </a-descriptions-item>
+          </a-descriptions>
+        </div>
+
+         <a-form-item name="file" ref="file">
+          <template #label>
+            <tooltip-label :title="$t('label.rules.file')" 
:tooltip="$t('label.rules.file.to.import')"/>
+          </template>
+          <a-upload-dragger
+            :multiple=false
+            :fileList="fileList"
+            @remove="handleRemove"
+            :beforeUpload="beforeUpload"
+            @change="handleChange"
+            v-model:value="form.file">
+            <p class="ant-upload-drag-icon">
+              <cloud-upload-outlined />
+            </p>
+            <p class="ant-upload-text" v-if="fileList.length === 0">
+              {{ $t('label.rules.file.import.description') }}
+            </p>
+          </a-upload-dragger>
+        </a-form-item>
+
+        <a-form-item v-if="csvData.length > 0" 
:label="$t('label.csv.preview')">
+          <div class="csv-preview">
+            <a-table
+                :columns="columns"
+                :dataSource="csvData"
+                :pagination="{ pageSize: 5 }"
+                :scroll="{ x: true }"
+                size="small">
+
+                <template #action="{ record }">
+                  <a-tag :color="record.action && record.action.toLowerCase() 
=== 'allow' ? 'green' : 'red'">
+                    {{ record.action ? record.action.toUpperCase() : 'N/A' }}
+                  </a-tag>
+                </template>
+
+                <template #traffictype="{ record }">
+                  <a-tag :color="record.traffictype && 
record.traffictype.toLowerCase() === 'ingress' ? 'blue' : 'orange'">
+                    {{ record.traffictype ? record.traffictype.toUpperCase() : 
'N/A' }}
+                  </a-tag>
+                </template>
+
+            </a-table>
+          </div>
+        </a-form-item>
+
+        <div :span="24" class="action-button">
+          <a-button class="button-cancel" @click="closeAction">{{ 
$t('label.cancel') }}</a-button>
+          <a-button
+            class="button-submit"
+            ref="submit"
+            type="primary"
+            :loading="loading"
+            :disabled="csvData.length === 0"
+            @click="handleSubmit">{{ $t('label.import') }}</a-button>
+        </div>
+
+      </a-form>
+    </a-spin>
+  </div>
+</template>
+
+<script>
+import { ref, reactive, toRaw } from 'vue'
+// import { Tag } from 'ant-design-vue'

Review Comment:
   Remove commented-out import statement that is not being used.
   ```suggestion
   
   ```



##########
ui/src/views/network/ImportNetworkACL.vue:
##########
@@ -0,0 +1,380 @@
+// 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 v-ctrl-enter="handleSubmit">
+    <a-spin :spinning="loading">
+      <a-form
+        :ref="formRef"
+        :model="form"
+        :rules="rules"
+        layout="vertical"
+        @finish="handleSubmit">
+
+        <div class="info-section" style="margin-bottom: 24px; padding: 16px; 
background: #fafafa; border-radius: 4px;">
+          <a-descriptions :column="1" size="small">
+            <a-descriptions-item :label="$t('label.acl.id')">
+              <span style="font-family: monospace;">{{ resource.id }}</span>
+            </a-descriptions-item>
+            <a-descriptions-item :label="$t('label.add.acl.name')">
+              <strong>{{ resource.name }}</strong>
+            </a-descriptions-item>
+          </a-descriptions>
+        </div>
+
+         <a-form-item name="file" ref="file">
+          <template #label>
+            <tooltip-label :title="$t('label.rules.file')" 
:tooltip="$t('label.rules.file.to.import')"/>
+          </template>
+          <a-upload-dragger
+            :multiple=false
+            :fileList="fileList"
+            @remove="handleRemove"
+            :beforeUpload="beforeUpload"
+            @change="handleChange"
+            v-model:value="form.file">
+            <p class="ant-upload-drag-icon">
+              <cloud-upload-outlined />
+            </p>
+            <p class="ant-upload-text" v-if="fileList.length === 0">
+              {{ $t('label.rules.file.import.description') }}
+            </p>
+          </a-upload-dragger>
+        </a-form-item>
+
+        <a-form-item v-if="csvData.length > 0" 
:label="$t('label.csv.preview')">
+          <div class="csv-preview">
+            <a-table
+                :columns="columns"
+                :dataSource="csvData"
+                :pagination="{ pageSize: 5 }"
+                :scroll="{ x: true }"
+                size="small">
+
+                <template #action="{ record }">
+                  <a-tag :color="record.action && record.action.toLowerCase() 
=== 'allow' ? 'green' : 'red'">
+                    {{ record.action ? record.action.toUpperCase() : 'N/A' }}
+                  </a-tag>
+                </template>
+
+                <template #traffictype="{ record }">
+                  <a-tag :color="record.traffictype && 
record.traffictype.toLowerCase() === 'ingress' ? 'blue' : 'orange'">
+                    {{ record.traffictype ? record.traffictype.toUpperCase() : 
'N/A' }}
+                  </a-tag>
+                </template>
+
+            </a-table>
+          </div>
+        </a-form-item>
+
+        <div :span="24" class="action-button">
+          <a-button class="button-cancel" @click="closeAction">{{ 
$t('label.cancel') }}</a-button>
+          <a-button
+            class="button-submit"
+            ref="submit"
+            type="primary"
+            :loading="loading"
+            :disabled="csvData.length === 0"
+            @click="handleSubmit">{{ $t('label.import') }}</a-button>
+        </div>
+
+      </a-form>
+    </a-spin>
+  </div>
+</template>
+
+<script>
+import { ref, reactive, toRaw } from 'vue'
+// import { Tag } from 'ant-design-vue'
+import { postAPI } from '@/api'
+import TooltipLabel from '@/components/widgets/TooltipLabel'
+
+export default {
+  name: 'ImportNetworkACL',
+  components: {
+    TooltipLabel
+  },
+  props: {
+    resource: {
+      type: Object,
+      required: true
+    }
+  },
+  data () {
+    return {
+      loading: false,
+      fileList: [],
+      csvData: '',
+      csvFileType: ['.csv', 'text/csv', 
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
'application/vnd.ms-excel'],
+      columns: [
+        {
+          title: this.$t('label.protocol'),
+          dataIndex: 'protocol',
+          key: 'protocol',
+          width: 100
+        },
+        {
+          title: this.$t('label.action'),
+          dataIndex: 'action',
+          key: 'action',
+          width: 100,
+          slots: { customRender: 'action' }
+        },
+        {
+          title: this.$t('label.cidr'),
+          dataIndex: 'cidrlist',
+          width: 150,
+          ellipsis: true
+        },
+        {
+          title: this.$t('label.startport'),
+          dataIndex: 'startport',
+          width: 100
+        },
+        {
+          title: this.$t('label.endport'),
+          dataIndex: 'endport',
+          width: 100
+        },
+        {
+          title: this.$t('label.traffictype'),
+          dataIndex: 'traffictype',
+          key: 'traffictype',
+          width: 120,
+          slots: { customRender: 'traffictype' }
+        },
+        {
+          title: this.$t('label.number'),
+          dataIndex: 'number',
+          width: 80
+        },
+        {
+          title: this.$t('label.reason'),
+          dataIndex: 'reason',
+          ellipsis: true
+        }
+      ]
+    }
+  },
+  beforeCreate () {
+    this.apiParams = this.$getApiParams('importNetworkACL')
+  },
+  created () {
+    this.initForm()
+  },
+  methods: {
+    initForm () {
+      this.formRef = ref()
+      this.form = reactive({})
+      this.rules = reactive({
+        file: [
+          { required: true, message: 
this.$t('this.$message.error.required.input') },

Review Comment:
   The translation key 'this.$message.error.required.input' is malformed and 
will not resolve correctly. It should be 'message.error.required.input' without 
the 'this.' prefix.
   ```suggestion
             { required: true, message: this.$t('message.error.required.input') 
},
   ```



##########
ui/src/views/network/ImportNetworkACL.vue:
##########
@@ -0,0 +1,380 @@
+// 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 v-ctrl-enter="handleSubmit">
+    <a-spin :spinning="loading">
+      <a-form
+        :ref="formRef"
+        :model="form"
+        :rules="rules"
+        layout="vertical"
+        @finish="handleSubmit">
+
+        <div class="info-section" style="margin-bottom: 24px; padding: 16px; 
background: #fafafa; border-radius: 4px;">
+          <a-descriptions :column="1" size="small">
+            <a-descriptions-item :label="$t('label.acl.id')">
+              <span style="font-family: monospace;">{{ resource.id }}</span>
+            </a-descriptions-item>
+            <a-descriptions-item :label="$t('label.add.acl.name')">
+              <strong>{{ resource.name }}</strong>
+            </a-descriptions-item>
+          </a-descriptions>
+        </div>
+
+         <a-form-item name="file" ref="file">
+          <template #label>
+            <tooltip-label :title="$t('label.rules.file')" 
:tooltip="$t('label.rules.file.to.import')"/>
+          </template>
+          <a-upload-dragger
+            :multiple=false
+            :fileList="fileList"
+            @remove="handleRemove"
+            :beforeUpload="beforeUpload"
+            @change="handleChange"
+            v-model:value="form.file">
+            <p class="ant-upload-drag-icon">
+              <cloud-upload-outlined />
+            </p>
+            <p class="ant-upload-text" v-if="fileList.length === 0">
+              {{ $t('label.rules.file.import.description') }}
+            </p>
+          </a-upload-dragger>
+        </a-form-item>
+
+        <a-form-item v-if="csvData.length > 0" 
:label="$t('label.csv.preview')">
+          <div class="csv-preview">
+            <a-table
+                :columns="columns"
+                :dataSource="csvData"
+                :pagination="{ pageSize: 5 }"
+                :scroll="{ x: true }"
+                size="small">
+
+                <template #action="{ record }">
+                  <a-tag :color="record.action && record.action.toLowerCase() 
=== 'allow' ? 'green' : 'red'">
+                    {{ record.action ? record.action.toUpperCase() : 'N/A' }}
+                  </a-tag>
+                </template>
+
+                <template #traffictype="{ record }">
+                  <a-tag :color="record.traffictype && 
record.traffictype.toLowerCase() === 'ingress' ? 'blue' : 'orange'">
+                    {{ record.traffictype ? record.traffictype.toUpperCase() : 
'N/A' }}
+                  </a-tag>
+                </template>
+
+            </a-table>
+          </div>
+        </a-form-item>
+
+        <div :span="24" class="action-button">
+          <a-button class="button-cancel" @click="closeAction">{{ 
$t('label.cancel') }}</a-button>
+          <a-button
+            class="button-submit"
+            ref="submit"
+            type="primary"
+            :loading="loading"
+            :disabled="csvData.length === 0"
+            @click="handleSubmit">{{ $t('label.import') }}</a-button>
+        </div>
+
+      </a-form>
+    </a-spin>
+  </div>
+</template>
+
+<script>
+import { ref, reactive, toRaw } from 'vue'
+// import { Tag } from 'ant-design-vue'
+import { postAPI } from '@/api'
+import TooltipLabel from '@/components/widgets/TooltipLabel'
+
+export default {
+  name: 'ImportNetworkACL',
+  components: {
+    TooltipLabel
+  },
+  props: {
+    resource: {
+      type: Object,
+      required: true
+    }
+  },
+  data () {
+    return {
+      loading: false,
+      fileList: [],
+      csvData: '',
+      csvFileType: ['.csv', 'text/csv', 
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
'application/vnd.ms-excel'],
+      columns: [
+        {
+          title: this.$t('label.protocol'),
+          dataIndex: 'protocol',
+          key: 'protocol',
+          width: 100
+        },
+        {
+          title: this.$t('label.action'),
+          dataIndex: 'action',
+          key: 'action',
+          width: 100,
+          slots: { customRender: 'action' }
+        },
+        {
+          title: this.$t('label.cidr'),
+          dataIndex: 'cidrlist',
+          width: 150,
+          ellipsis: true
+        },
+        {
+          title: this.$t('label.startport'),
+          dataIndex: 'startport',
+          width: 100
+        },
+        {
+          title: this.$t('label.endport'),
+          dataIndex: 'endport',
+          width: 100
+        },
+        {
+          title: this.$t('label.traffictype'),
+          dataIndex: 'traffictype',
+          key: 'traffictype',
+          width: 120,
+          slots: { customRender: 'traffictype' }
+        },
+        {
+          title: this.$t('label.number'),
+          dataIndex: 'number',
+          width: 80
+        },
+        {
+          title: this.$t('label.reason'),
+          dataIndex: 'reason',
+          ellipsis: true
+        }
+      ]
+    }
+  },
+  beforeCreate () {
+    this.apiParams = this.$getApiParams('importNetworkACL')
+  },
+  created () {
+    this.initForm()
+  },
+  methods: {
+    initForm () {
+      this.formRef = ref()
+      this.form = reactive({})
+      this.rules = reactive({
+        file: [
+          { required: true, message: 
this.$t('this.$message.error.required.input') },
+          {
+            validator: this.checkCsvRulesFile,
+            message: this.$t('label.error.rules.file.import')
+          }
+        ]
+      })
+    },
+    beforeUpload (file) {
+      if (!this.csvFileType.includes(file.type)) {
+        return false
+      }
+      this.fileList = [file]
+      this.form.file = file
+      return false // Stop from uploading automatically
+    },
+    handleSubmit (e) {
+      e.preventDefault()
+      if (this.loading) return
+      this.formRef.value.validate().then(() => {
+        const values = toRaw(this.form)
+        const params = {}
+        for (const key in values) {
+          const input = values[key]
+          if (input === undefined) {
+            continue
+          }
+          if (key === 'file') {
+            continue
+          }
+          params[key] = input
+        }
+
+        if (this.csvData.length === 0) {
+          this.$message.error(this.$t('message.csv.no.data'))
+          return
+        }
+        this.importNetworkACL()
+      }).catch(error => {
+        this.formRef.value.scrollToField(error.errorFields[0].name)
+      })
+    },
+    handleRemove (file) {
+      const index = this.fileList.indexOf(file)
+      const newFileList = this.fileList.slice()
+      newFileList.splice(index, 1)
+      this.fileList = newFileList
+      this.form.file = undefined
+    },
+    handleChange (info) {
+      if (info.file.status === 'error') {
+        this.$notification.error({
+          message: this.$t('label.error.file.upload'),
+          description: this.$t('label.error.file.upload')
+        })
+      }
+    },
+    async checkCsvRulesFile (rule, value) {
+      if (!value || value === '') {
+        return Promise.resolve()
+      } else {
+        if (!this.csvFileType.includes(value.type)) {
+          return Promise.reject(rule.message)
+        }
+
+        try {
+          const validFile = await this.readCsvFile(value)
+          if (!validFile) {
+            return Promise.reject(rule.message)
+          } else {
+            return Promise.resolve()
+          }
+        } catch (reason) {
+          return Promise.reject(rule.message)
+        }
+      }
+    },
+    readCsvFile (file) {
+      return new Promise((resolve, reject) => {
+        if (window.FileReader) {
+          var reader = new FileReader()

Review Comment:
   Use 'const' or 'let' instead of 'var' for variable declaration to follow 
modern JavaScript best practices.
   ```suggestion
             const reader = new FileReader()
   ```



##########
api/src/main/java/com/cloud/event/EventTypes.java:
##########
@@ -582,6 +582,7 @@ public class EventTypes {
 
     // Network ACL
     public static final String EVENT_NETWORK_ACL_CREATE = "NETWORK.ACL.CREATE";
+    public static final String EVENT_NETWORK_ACL_IMPORT = "NETWORK.ACL.IMPORT";

Review Comment:
   The event type is defined as 'EVENT_NETWORK_ACL_IMPORT' but the 
ImportNetworkACLCmd uses 'EVENT_NETWORK_ACL_CREATE' at line 115. This should 
use the new 'EVENT_NETWORK_ACL_IMPORT' constant instead to properly distinguish 
import operations from create operations.



##########
server/src/main/java/com/cloud/network/vpc/NetworkACLServiceImpl.java:
##########
@@ -1061,6 +1064,111 @@ public NetworkACLItem 
moveRuleToTheTopInACLList(NetworkACLItem ruleBeingMoved) {
         return moveRuleToTheTop(ruleBeingMoved, allRules);
     }
 
+    @Override
+    public List<NetworkACLItem> importNetworkACLRules(ImportNetworkACLCmd cmd) 
throws ResourceUnavailableException {
+        long aclId = cmd.getAclId();
+        Map<Object, Object> rules = cmd.getRules();
+        List<NetworkACLItem> createdRules = new ArrayList<>();
+        List<String> errors = new ArrayList<>();
+        for (Map.Entry<Object, Object> entry : rules.entrySet()) {
+            try {
+                Map<String, Object> ruleMap = (Map<String, Object>) 
entry.getValue();
+                NetworkACLItem item = createACLRuleFromMap(ruleMap, aclId);
+                createdRules.add(item);
+            } catch (Exception ex) {
+                String error = "Failed to import rule at index " + 
entry.getKey() + ": " + ex.getMessage();
+                errors.add(error);
+                logger.error(error, ex);
+            }
+        }
+        // no rules got imported
+        if (createdRules.isEmpty() && !errors.isEmpty()) {
+            logger.error("Failed to import any ACL rules. Errors: {}", 
String.join("; ", errors));
+            throw new CloudRuntimeException("Failed to import any ACL rules.");
+        }
+
+        // apply ACL to network
+        if (!createdRules.isEmpty()) {
+            applyNetworkACL(aclId);
+        }
+        return createdRules;
+    }
+
+    private NetworkACLItem createACLRuleFromMap(Map<String, Object> ruleMap, 
long aclId) {
+        String protocol = (String) ruleMap.get(ApiConstants.PROTOCOL);
+        if (protocol == null || protocol.trim().isEmpty()) {
+            throw new InvalidParameterValueException("Protocol is required");
+        }
+        String action = (String) ruleMap.getOrDefault(ApiConstants.ACTION, 
"deny");
+        String trafficType = (String) 
ruleMap.getOrDefault(ApiConstants.TRAFFIC_TYPE, 
NetworkACLItem.TrafficType.Ingress);
+        String forDisplay = (String ) 
ruleMap.getOrDefault(ApiConstants.FOR_DISPLAY, "true");

Review Comment:
   Extra space between 'String' and the closing parenthesis ')' should be 
removed.
   ```suggestion
           String forDisplay = (String) 
ruleMap.getOrDefault(ApiConstants.FOR_DISPLAY, "true");
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to