简体   繁体   中英

Android Management API change Policy for device

I created 2 policies in my enterprise.

I would like to switch one device to the other policy without re-enroling it.

I tried to use android management enterprises devices.patch with following JSON

{ "policyName": "policy2" }

this link

When i execute this command i always get follwoing error Message:

{
 "error": {
  "code": 400,
  "message": "Illegal state transition from ACTIVE to DEVICE_STATE_UNSPECIFIED",
  "status": "INVALID_ARGUMENT"
 }
}

Does anybody know how to change policy for a device without wiping it?

It is indeed possible to change the policy of a device without re-enrolling it, and you're not far from the solution.

You get this error because you implicitly attempt to change other fields of the Device resource (in particular the state field) by not setting them in the resource that you send in devices.patch .

You have two options:

  1. Set the updateMask in devices.patch to "policyName" , to tell the API that you only want to change the policyName field.

  2. Call devices.get to get the current Device resource, and then send back the entire resource with only the policyName field changed in to devices.patch .

Using updateMask is preferable because it does an atomic read-modify-write.

As Fred mentioned, updateMask is the preferred way to go. Here's an example of how to use the updateMask approach in the Google colab

swap_result = androidmanagement.enterprises().devices().patch(
name='enterprises/ENTERPRISE_NAME/devices/DEVICE_ID', updateMask='policyName', body={ "policyName": "enterprises/ENTERPRISE_NAME/policies/NEW_POLICY_NAME"}).execute()

i foudn a solution for my problem:

{ "policyName": "policy2" , "state":"active"}

If you try this from management api test site then add updateMask as "policyName"

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM