Add multiple device dis-enrollment support #279

Merged
pahansith merged 2 commits from pramilaniroshan/device-mgt-core:rm-9970-dis into master 1 year ago

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.DisenrollRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@ -1279,6 +1280,49 @@ public interface DeviceManagementService {
@Size(max = 45)
String deviceId);
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("/disenroll")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT",
value = "Remove Multiple Devices Specified by Device IDs and Device Type",
notes = "Deletes multiple devices of the specified device type specified by their device IDs" +
" and returns the status of the dis-enrollment operation.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "dm:devices:delete")
})
},
nickname = "deleteMultipleDevicesByType"
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully deleted the devices.",
response = Device.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body")
}),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n " +
"Server error occurred while deleting devices.",
response = ErrorResponse.class)
})
Response disenrollMultipleDevices(@ApiParam(
name = "deviceTypeWithDeviceIds",
pramilaniroshan marked this conversation as resolved
Review

Change the value to match with the context

Change the value to match with the context
value = "Device type and corresponding device IDs for disenrollment",
required = true)
DisenrollRequest deviceTypeWithDeviceIds);
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/device-type/{type}/features")

@ -26,6 +26,7 @@ import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionMa
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.DisenrollRequest;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
@ -101,8 +102,9 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
@Path("/devices")
public class DeviceManagementServiceImpl implements DeviceManagementService {
@ -474,6 +476,64 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
}
@PUT
@Override
@Path("/disenroll")
public Response disenrollMultipleDevices(DisenrollRequest deviceTypeWithDeviceIds) {
if (deviceTypeWithDeviceIds == null) {
String errorMsg = "Invalid request. The request body must not be null.";
return Response.status(Response.Status.BAD_REQUEST).entity(errorMsg).build();
}
DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService();
List<DeviceIdentifier> successfullyDisenrolledDevices = new ArrayList<>();
List<DeviceIdentifier> failedToDisenrollDevices = new ArrayList<>();
Map<String, List<String>> list = deviceTypeWithDeviceIds.getDeviceTypeWithDeviceIds();
String deviceType;
List<String> deviceIds;
DeviceIdentifier deviceIdentifier;
Device persistedDevice;
boolean response;
for (Map.Entry<String, List<String>> entry : list.entrySet()) {
deviceType = entry.getKey();
deviceIds = entry.getValue();
for (String deviceId : deviceIds) {
deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
try {
persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true);
if (persistedDevice != null) {
response = deviceManagementProviderService.disenrollDevice(deviceIdentifier);
if (response) {
successfullyDisenrolledDevices.add(deviceIdentifier);
pramilaniroshan marked this conversation as resolved
Review

Better to put debug logs here to identify the cause of failing the disenrollment. If the persisted device returns null, it should be due to the user sent an invalid device identifiers.

Better to put debug logs here to identify the cause of failing the disenrollment. If the persisted device returns null, it should be due to the user sent an invalid device identifiers.
} else {
failedToDisenrollDevices.add(deviceIdentifier);
}
} else {
failedToDisenrollDevices.add(deviceIdentifier);
if(log.isDebugEnabled()){
String msg = "Error encountered while dis-enrolling device of type: " + deviceType + " with " + deviceId;
log.error(msg);
}
}
} catch (DeviceManagementException e) {
String msg = "Error encountered while dis-enrolling device of type: " + deviceType + " with " + deviceId;
log.error(msg, e);
failedToDisenrollDevices.add(deviceIdentifier);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
}
Map<String, List<DeviceIdentifier>> responseMap = new HashMap<>();
responseMap.put("successfullyDisenrollDevices", successfullyDisenrolledDevices);
responseMap.put("failedToDisenrollDevices", failedToDisenrollDevices);
return Response.status(Response.Status.OK).entity(responseMap).build();
}
@POST
@Override
@Path("/type/{deviceType}/id/{deviceId}/rename")

@ -0,0 +1,40 @@
/*
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
import java.util.Map;
@ApiModel(value = "DisenrollRequest", description = "Contains the multiple devices specified by device IDs")
public class DisenrollRequest {
@ApiModelProperty(name = "deviceTypeWithDeviceIds", value = "Contains the multiple devices specified by device IDs with type",
required = true)
private Map<String, List<String>> deviceTypeWithDeviceIds;
public Map<String, List<String>> getDeviceTypeWithDeviceIds() {
return deviceTypeWithDeviceIds;
}
public void setDeviceTypeWithDeviceIds(Map<String, List<String>> deviceTypeWithDeviceIds) {
this.deviceTypeWithDeviceIds = deviceTypeWithDeviceIds;
}
}
Loading…
Cancel
Save