From 30676411e45306802a614e9c00cb2560a005b658 Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Mon, 8 Jul 2024 08:11:48 -0400 Subject: [PATCH] Add Delete cni configuration API --- .../main/java/com/cloud/event/EventTypes.java | 1 + .../com/cloud/server/ManagementService.java | 9 +++ .../userdata/DeleteCniConfigurationCmd.java | 73 +++++++++++++++++++ .../cloud/server/ManagementServerImpl.java | 8 ++ ui/public/locales/en.json | 2 + ui/src/config/section/compute.js | 6 +- 6 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/org/apache/cloudstack/api/command/user/userdata/DeleteCniConfigurationCmd.java diff --git a/api/src/main/java/com/cloud/event/EventTypes.java b/api/src/main/java/com/cloud/event/EventTypes.java index d73bd056022..1a814658a03 100644 --- a/api/src/main/java/com/cloud/event/EventTypes.java +++ b/api/src/main/java/com/cloud/event/EventTypes.java @@ -290,6 +290,7 @@ public class EventTypes { //registering userdata events public static final String EVENT_REGISTER_USER_DATA = "REGISTER.USER.DATA"; public static final String EVENT_REGISTER_CNI_CONFIG = "REGISTER.CNI.CONFIG"; + public static final String EVENT_DELETE_CNI_CONFIG = "DELETE.CNI.CONFIG"; //register for user API and secret keys public static final String EVENT_REGISTER_FOR_SECRET_API_KEY = "REGISTER.USER.KEY"; diff --git a/api/src/main/java/com/cloud/server/ManagementService.java b/api/src/main/java/com/cloud/server/ManagementService.java index 2de6e4b9ffb..3b8fb2c5e4b 100644 --- a/api/src/main/java/com/cloud/server/ManagementService.java +++ b/api/src/main/java/com/cloud/server/ManagementService.java @@ -59,6 +59,7 @@ import org.apache.cloudstack.api.command.user.ssh.CreateSSHKeyPairCmd; import org.apache.cloudstack.api.command.user.ssh.DeleteSSHKeyPairCmd; import org.apache.cloudstack.api.command.user.ssh.ListSSHKeyPairsCmd; import org.apache.cloudstack.api.command.user.ssh.RegisterSSHKeyPairCmd; +import org.apache.cloudstack.api.command.user.userdata.DeleteCniConfigurationCmd; import org.apache.cloudstack.api.command.user.userdata.DeleteUserDataCmd; import org.apache.cloudstack.api.command.user.userdata.ListUserDataCmd; import org.apache.cloudstack.api.command.user.userdata.RegisterCniConfigurationCmd; @@ -387,6 +388,14 @@ public interface ManagementService { */ boolean deleteUserData(DeleteUserDataCmd cmd); + /** + * Deletes a userdata. + * + * @param cmd + * The api command class. + * @return True on success. False otherwise. + */ + boolean deleteCniConfiguration(DeleteCniConfigurationCmd cmd); /** * Search registered key pairs for the logged in user. * diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/userdata/DeleteCniConfigurationCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/userdata/DeleteCniConfigurationCmd.java new file mode 100644 index 00000000000..77dd514bbb0 --- /dev/null +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/userdata/DeleteCniConfigurationCmd.java @@ -0,0 +1,73 @@ +// 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. +package org.apache.cloudstack.api.command.user.userdata; + +import org.apache.cloudstack.acl.RoleType; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.response.SuccessResponse; +import org.apache.cloudstack.context.CallContext; +import org.apache.log4j.Logger; + +import com.cloud.user.Account; +import com.cloud.user.UserData; + + +@APICommand(name = "deleteCniConfiguration", description = "Deletes a CNI Configuration", responseObject = SuccessResponse.class, entityType = {UserData.class}, + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, since = "4.19", + authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User}) +public class DeleteCniConfigurationCmd extends DeleteUserDataCmd { + + public static final Logger s_logger = Logger.getLogger(DeleteUserDataCmd.class.getName()); + + + ///////////////////////////////////////////////////// + /////////////// API Implementation/////////////////// + ///////////////////////////////////////////////////// + + @Override + public void execute() { + boolean result = _mgr.deleteCniConfiguration(this); + if (result) { + SuccessResponse response = new SuccessResponse(getCommandName()); + response.setSuccess(result); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete userdata"); + } + } + + @Override + public long getEntityOwnerId() { + Account account = CallContext.current().getCallingAccount(); + Long domainId = this.getDomainId(); + String accountName = this.getAccountName(); + if ((account == null || _accountService.isAdmin(account.getId())) && (domainId != null && accountName != null)) { + Account userAccount = _responseGenerator.findAccountByNameDomain(accountName, domainId); + if (userAccount != null) { + return userAccount.getId(); + } + } + + if (account != null) { + return account.getId(); + } + + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked + } +} diff --git a/server/src/main/java/com/cloud/server/ManagementServerImpl.java b/server/src/main/java/com/cloud/server/ManagementServerImpl.java index 2c612165079..2fbafb823ab 100644 --- a/server/src/main/java/com/cloud/server/ManagementServerImpl.java +++ b/server/src/main/java/com/cloud/server/ManagementServerImpl.java @@ -520,6 +520,7 @@ import org.apache.cloudstack.api.command.user.template.RegisterTemplateCmd; import org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd; import org.apache.cloudstack.api.command.user.template.UpdateTemplatePermissionsCmd; import org.apache.cloudstack.api.command.user.userdata.BaseRegisterUserDataCmd; +import org.apache.cloudstack.api.command.user.userdata.DeleteCniConfigurationCmd; import org.apache.cloudstack.api.command.user.userdata.DeleteUserDataCmd; import org.apache.cloudstack.api.command.user.userdata.LinkUserDataToTemplateCmd; import org.apache.cloudstack.api.command.user.userdata.ListCniConfigurationCmd; @@ -4035,6 +4036,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe cmdList.add(LinkUserDataToTemplateCmd.class); cmdList.add(RegisterCniConfigurationCmd.class); cmdList.add(ListCniConfigurationCmd.class); + cmdList.add(DeleteCniConfigurationCmd.class); //object store APIs cmdList.add(AddObjectStoragePoolCmd.class); @@ -4833,6 +4835,12 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe return userDataDao.remove(userData.getId()); } + @Override + @ActionEvent(eventType = EventTypes.EVENT_DELETE_CNI_CONFIG, eventDescription = "CNI Configuration deletion") + public boolean deleteCniConfiguration(DeleteCniConfigurationCmd cmd) { + return deleteUserData(cmd); + } + @Override public Pair, Integer> listUserDatas(final ListUserDataCmd cmd, final boolean forCks) { final Long id = cmd.getId(); diff --git a/ui/public/locales/en.json b/ui/public/locales/en.json index 1ccf1457366..856fb0b72a5 100644 --- a/ui/public/locales/en.json +++ b/ui/public/locales/en.json @@ -1893,6 +1893,7 @@ "label.remove": "Remove", "label.remove.annotation": "Remove comment", "label.remove.bgp.peer": "Remove BGP peer", +"label.remove.cni.configuration": "Remove CNI configuration", "label.remove.egress.rule": "Remove egress rule", "label.remove.interface.route.table": "Remove Tungsten interface route table", "label.remove.ip.range": "Remove IP range", @@ -3362,6 +3363,7 @@ "message.password.reset.success": "Password has been reset successfully. Please login using your new credentials.", "message.path": "Path : ", "message.path.description": "NFS: exported path from the server. VMFS: /datacenter name/datastore name. SharedMountPoint: path where primary storage is mounted, such as /mnt/primary.", +"message.please.confirm.remove.cni.configuration": "Please confirm that you want to remove this CNI Configuration", "message.please.confirm.remove.ssh.key.pair": "Please confirm that you want to remove this SSH key pair.", "message.please.confirm.remove.user.data": "Please confirm that you want to remove this Userdata", "message.please.enter.valid.value": "Please enter a valid value.", diff --git a/ui/src/config/section/compute.js b/ui/src/config/section/compute.js index 8759bea992a..40e53409eba 100644 --- a/ui/src/config/section/compute.js +++ b/ui/src/config/section/compute.js @@ -1041,10 +1041,10 @@ export default { component: shallowRef(defineAsyncComponent(() => import('@/views/compute/RegisterUserData.vue'))) }, { - api: 'deleteUserData', + api: 'deleteCniConfiguration', icon: 'delete-outlined', - label: 'label.remove.user.data', - message: 'message.please.confirm.remove.user.data', + label: 'label.remove.cni.configuration', + message: 'message.please.confirm.remove.cni.configuration', dataView: true, args: ['id', 'account', 'domainid', 'projectid'], mapping: {