0

I need to move an organization from one to another. I have organization units like below:

      Domain.com
          -> Grade 12
                 - Botany
                 - Mathematics
          -> Grade 11
                 - Zoology

I need to move 'Botany' from Grade 12 to Grade 11. This is my code:

        List<String> oua = new ArrayList<String>();
        oua.add("Grade 12");
        oua.add("Botany");
        OrgUnit org = service.orgunits().get("my_customer", oua).execute();

        org.setParentOrgUnitPath("/Grade 11");
        OrgUnit org = service.orgunits().update("my_customer", oua, org).execute();

I have followed this answer. But it couldn't helpful for me. I can update the name of 'Botany'. But I couldn't move the organization unit. I am getting this error:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "message" : "Parent Org unit not found",
    "reason" : "notFound"
  } ],
  "message" : "Parent Org unit not found"
}

What causes for this error and how to solve this?

EDIT 1: Here is my full code:

public class Gsuite {
    private static Logger logger = LoggerFactory.getLogger(Gsuite.class);


    private static final String APPLICATION_NAME = "Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";


    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER,DirectoryScopes.ADMIN_DIRECTORY_GROUP,
            DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER, DirectoryScopes.ADMIN_DIRECTORY_ORGUNIT, DirectoryScopes.ADMIN_DIRECTORY_USER_SECURITY);

    private static final String CREDENTIALS_FILE_PATH = "/credentials.json";


    /**
     * Creates an authorized Credential object.
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */
    protected static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = Gsuite.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }
    public Directory directoryApiCall() throws GeneralSecurityException, IOException {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Directory service = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
            .setApplicationName(APPLICATION_NAME)
            .build();
        return service;
    }

     public void updateOrgUnit() throws GeneralSecurityException, IOException {
            Gsuite api = new Gsuite();
            Directory service = api.directoryApiCall();

            List<String> oua = new ArrayList<String>();
            oua.add("Grade 12");
            oua.add("Botany");
            OrgUnit org = service.orgunits().get("my_customer", oua).execute();
            logger.debug("Parent Organizational unit path before moving : " + org.getParentOrgUnitPath());
            org.setParentOrgUnitPath("/Grade 11");
            logger.debug("Parent Organizational unit path after moving : " + org.getParentOrgUnitPath());

            service.orgunits().update("my_customer", oua, org).execute();

        }
     public static void main(String... args) throws GeneralSecurityException, IOException{

            Gsuite dapi = new Gsuite();
            dapi.updateOrgUnit();

   }
}   
Satheeshkumar
  • 75
  • 3
  • 12
  • Did you try to specify the `parentOrgUnitId` instead of `parentOrgUnitPath`? Also, are you the domain admin, if not: do you have the permissions to access the orgunit "/Grade 11"? – ziganotschka Dec 17 '19 at 16:56
  • Yes. I have tried parentOrgUnitId instead of parentOrgUnitPath. When I try with parentOrgUnitId, I cannot set the parentOrgUnitPath. i.e., Even after setting the parent organization unit path using parentOrgUnitPath (.setParentOrgUnitId("abcdef")), it doesn't changed. When I debug I can see the same organization path, nothing changed. – Satheeshkumar Dec 18 '19 at 04:15
  • Yes I am the domain admin. – Satheeshkumar Dec 18 '19 at 04:19
  • I am not sure if I understood your answer correctly. When you use the option `org.setParentOrgUnitId("abcdef")`, you do not need `org.setParentOrgUnitPath("/Grade 11");` It is just important that you use the correct `parentOrgUnitId`. – ziganotschka Dec 18 '19 at 09:27
  • @ziganotschka I have debugged the code. I added System.out.println() above and below the ```org.setParentOrgUnitPath("/Grade 11");``` line in above code. So I can see the changes in parentOrgUnitPath in the console. So the problem should be in the last line of the above code – Satheeshkumar Dec 18 '19 at 10:32
  • I also used ```org.setParentOrgUnitId("abcdef")``` instead of ```org.setParentOrgUnitPath```. Then I debugged again. I cannot see the changes in stacktrace in the console. When I use path, parentOrgUnitPath has been changed but not updated. And when I use id instead of path, the path is not changed. – Satheeshkumar Dec 18 '19 at 10:37
  • Comparing your code to the post you refer to, you assigned the command `service.orgunits().update("my_customer", oua, org).execute();` to the variable `org`. See if you get rid of the error by omitting it. – ziganotschka Dec 18 '19 at 10:38
  • Still getting same error? – Satheeshkumar Dec 18 '19 at 11:17
  • May I see your full code including libraries, authorization flow etc.? Make sure not to include confidential data. – ziganotschka Dec 20 '19 at 09:11
  • @zignotschka I have added full code. Please refer it. – Satheeshkumar Dec 21 '19 at 06:06
  • 1
    It seems to be a bug, see here: https://issuetracker.google.com/36759435 I would recommend you provide your information, so it can be worked on. – ziganotschka Dec 25 '19 at 13:36

0 Answers0