在 Softlayer 中删除要通知的用户时出错

Error deleting an user to notify in Softlayer

当我删除一个用户通知时,它returns错误:com.softlayer.api.ApiException$内部:所有被删除的对象必须有一个ID集。(代码:SoftLayer_Exception,状态: 500) 我已经设置了正确的 UserId 和 DeviceId,但它仍然 returns 同样的错误。 这是我的示例代码。

private void deleteUserTonotify(){

    // Define the hardware and user identifiers
    Long hardwareId = new Long(164420);
    Long userId = new Long(538047);

    // Define SoftLayer_User_Customer_Notification_Hardware service
    com.softlayer.api.service.user.customer.notification.Hardware.Service hardwareService = com.softlayer.api.service.user.customer.notification.Hardware
            .service(client);

    // Declare SoftLayer_User_Customer_Notification_Hardware List
    List<com.softlayer.api.service.user.customer.notification.Hardware> templateObjects = new ArrayList<com.softlayer.api.service.user.customer.notification.Hardware>();


    com.softlayer.api.service.user.customer.notification.Hardware templateObject = new com.softlayer.api.service.user.customer.notification.Hardware();
    templateObject.setUserId(userId);
    templateObject.setHardwareId(hardwareId);

    // Add templateObject to templateObjects
    templateObjects.add(templateObject);

    try {
        boolean result = hardwareService.deleteObjects(templateObjects);
        System.out.println(" delete result : " + result);

    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
}

数组中的每个对象都必须至少定义其 id 属性。 您可以在下一个 link 中找到更多信息: http://sldn.softlayer.com/reference/services/SoftLayer_User_Customer_Notification_Hardware http://sldn.softlayer.com/reference/services/SoftLayer_User_Customer_Notification_Hardware/deleteObjects

我已经在你的脚本中更改了它并且它有效:

package SoftLayer_Java_Scripts.Examples;

import java.util.ArrayList;
import java.util.List;

import com.softlayer.api.*;
import com.softlayer.api.service.user.customer.notification.Hardware;

public class EG
{
  private static String user = "set me";
  private static String apiKey = "set me";

  private static ApiClient client = new RestApiClient().withCredentials(user, apiKey);

  public static void main( String[] args )
  {
    deleteUserTonotify();
  }

  private static void deleteUserTonotify(){

    // Define the SoftLayer_User_Customer_Notification_Hardware id here
    Long userNotificationfHardwareId = new Long(280346);

    // Define SoftLayer_User_Customer_Notification_Hardware service
    Hardware.Service hardwareService = Hardware.service(client);

    // Declare SoftLayer_User_Customer_Notification_Hardware List
    List<Hardware> templateObjects = new ArrayList<Hardware>();

    // Creating a template object
    Hardware templateObject = new Hardware();

    templateObject.setId(userNotificationfHardwareId);

    // Add templateObject to templateObjects
    templateObjects.add(templateObject);

    try {
      boolean result = hardwareService.deleteObjects(templateObjects);
      System.out.println(" delete result : " + result);

    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
  }

  // This method can be used to retrieve and print 
  // the SoftLayer_User_Customer_Notification_Hardware ids by hardware id. 
    private static void printUserNotificationListByHardwareId(Long hardwareId, Long userId){
    Hardware.Service hardwareService = Hardware.service(client);

    try {
      List<Hardware> userNotificationByHardwareList = hardwareService.findByHardwareId(hardwareId);
      if(userNotificationByHardwareList.isEmpty()){
        System.out.println("There's no User Customer Notification for hardware id: " + hardwareId);
      }
      else{
        for(Hardware h : userNotificationByHardwareList){
          if((long)h.getUserId() == (long)userId){
            System.out.println(h.getId());
            break;
          }
        }
      }
    } catch (Exception e) {
      System.out.println("Error: " + e);
    }
  }
}