-1

My enterprise has 2 policies: POLICY_1 and POLICY_2. My device was provisioned with the POLICY_1, but I would like to change it to POLICY_2.

I'm using the Java lib 'com.google.apis:google-api-services-androidmanagement:v1-rev20201012-1.30.10'.

I'm trying to change its policy with the following code:

private void updateMyDevicePolicy(String enterpriseName, String deviceName) throws IOException {
    Device device = getMyDevice(deviceName);
    String policyName = enterpriseName + "/policies/POLICY_2";
    device.setPolicyName(policyName);
    device.setAppliedPolicyName(policyName);
    Device deviceUpdated = androidManagementClient.enterprises().devices().patch(deviceName, device).execute();
    
    System.out.println("DEVICE policy updated = " + deviceUpdated);
}

The problem is: The patch command does not work. The device policy remains "POLICY_1".

Please, how can I change the device policy?

2 Answers2

0

This is very much possible and I do it regularly too. Use the Device Patch request and pass in the PolicyId that needs to be applied to the device.

The name of the policy applied to the device, in the form enterprises/{enterpriseId}/policies/{policyId}. This field can be modified by a patch request. You can specify only the policyId when calling enterprises.devices.patch, as long as the policyId doesn’t contain any slashes. The rest of the policy name is inferred. Ref

Here's a overview of my C# code.

  1. Create the Device Request body with the policy that needs to be applied to it.
Google.Apis.AndroidManagement.v1.Data.Device deviceBody = new Device()
{
    Name = device.DeviceId,
    PolicyName = POLICY_2,
    State = "ACTIVE"
};
  1. Invoke the Device Patch request
EnterprisesResource.DevicesResource.PatchRequest attachNewPolicy = 
new EnterprisesResource.DevicesResource.PatchRequest(service, deviceBody, device.DeviceId);
attachNewPolicy.Execute();

And this should apply the new policy to the device.

Sudhu
  • 565
  • 8
  • 24
0

Updating the policy of a device can be done using devices().patch(). However, it is necessary to set the updateMask in devices().patch() to which field will be updated, which in this case is the policyName, to tell that you only want to change the that certain field.

Below is the implementation of updateMask using different language:

androidmanagement.enterprises().devices().patch(
    name=device_name,
     updateMask="policyName",
    body=json.loads(devices_json)
).execute()

In this code, the “policyName” was updated from enterpriseName + "/policies/POLICY_1" to enterpriseName + "/policies/POLICY_2"

Vega
  • 27,856
  • 27
  • 95
  • 103
TJ Domingo
  • 91
  • 5