Search in sources :

Example 1 with ApplicationRet

use of com.microsoft.azuretools.authmanage.srvpri.entities.ApplicationRet in project azure-tools-for-java by Microsoft.

the class ApplicationStep method execute.

@Override
public void execute(Map<String, Object> params) throws IOException, InterruptedException {
    //System.out.println("ApplicationStep execute...");
    String displayName = params.get("displayName").toString();
    String homePage = params.get("homePage").toString();
    String identifierUri = params.get("identifierUri").toString();
    String password = params.get("password").toString();
    //UUID tenantId = (UUID) params.get("tenantId");
    String tenantId = CommonParams.getTenantId();
    graphRestHelper = new GraphRestHelper(tenantId);
    reporter = CommonParams.getReporter();
    reporter.report("displayName: " + displayName);
    reporter.report("homePage: " + homePage);
    reporter.report("identifierUri: " + identifierUri);
    reporter.report("password: " + password);
    reporter.report("Creating ADD Application...");
    ApplicationRet app = createAadApplication(displayName, homePage, new String[] { identifierUri }, password);
    reporter.report("Done.");
    reporter.report(String.format("Checking ADD Application availability..."));
    final int RETRY_QNTY = 5;
    final int SLEEP_SEC = 10;
    int retry_count = 0;
    while (retry_count < RETRY_QNTY) {
        ApplicationGet applicationGet = getAadApplication(app.appId);
        if (applicationGet.value.size() > 0) {
            reporter.report("Done.");
            break;
        }
        retry_count++;
        reporter.report(String.format("Not available. Will retry in %d sec...", SLEEP_SEC));
        Thread.sleep(SLEEP_SEC * 1000);
    }
    if ((retry_count >= RETRY_QNTY)) {
        String errorDetails = String.format("The AD Application (appId: %s) is not available after %s retries", app.appId, RETRY_QNTY);
        CommonParams.getStatusReporter().report(new Status(getName(), Status.Result.FAILED, errorDetails));
        throw new IOException(errorDetails);
    }
    params.put("appId", app.appId);
    params.put("appObjectId", app.objectId);
    params.put("status", "app");
    CommonParams.getStatusReporter().report(new Status(getName(), Status.Result.SUCCESSFUL, String.format("appId: %s; appObjectId: %s", app.appId, app.objectId)));
}
Also used : GraphRestHelper(com.microsoft.azuretools.authmanage.srvpri.rest.GraphRestHelper) ApplicationGet(com.microsoft.azuretools.authmanage.srvpri.entities.ApplicationGet) IOException(java.io.IOException) ApplicationRet(com.microsoft.azuretools.authmanage.srvpri.entities.ApplicationRet)

Example 2 with ApplicationRet

use of com.microsoft.azuretools.authmanage.srvpri.entities.ApplicationRet in project azure-tools-for-java by Microsoft.

the class ApplicationStep method createAadApplication.

// helpers
// Create AAD Application
private ApplicationRet createAadApplication(String displayName, String homePage, String[] identifierUris, String password) throws IOException {
    /*
            POST https://graph.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47/applications?api-version=1.6

            {
              "availableToOtherTenants": false,
              "displayName": "ShchAadApp",
              "homepage": "http://github.com/Microsoft/azure-tools-for-java",
              "identifierUris": [
                "http://github.com/Microsoft/azure-tools-for-java"
              ],
              "passwordCredentials": [
                {
                  "startDate": "2016-08-16T04:55:00.3381704Z",
                  "endDate": "2017-08-16T04:55:00.3381704Z",
                  "keyId": "61d97a80-147a-44d2-bccc-13ffda967071",
                  "value": "Zxcv1234"
                }
              ]
            }

        */
    Application app = new Application();
    app.displayName = displayName;
    app.homepage = homePage;
    app.identifierUris.addAll(Arrays.asList(identifierUris));
    PasswordCredentials pc = new PasswordCredentials();
    LocalDateTime dt = LocalDateTime.now(ZoneOffset.UTC);
    //pc.startDate = dt.format( DateTimeFormatter.ISO_DATE_TIME );
    pc.endDate = dt.plusYears(1).format(DateTimeFormatter.ISO_DATE_TIME);
    ;
    pc.keyId = UUID.randomUUID();
    pc.value = password;
    app.passwordCredentials.add(pc);
    ObjectMapper mapper = new ObjectMapper();
    String applicationJson = mapper.writeValueAsString(app);
    String resp = graphRestHelper.doPost("applications", null, applicationJson);
    ApplicationRet applicationRet = mapper.readValue(resp, ApplicationRet.class);
    return applicationRet;
}
Also used : LocalDateTime(java.time.LocalDateTime) PasswordCredentials(com.microsoft.azuretools.authmanage.srvpri.entities.PasswordCredentials) Application(com.microsoft.azuretools.authmanage.srvpri.entities.Application) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ApplicationRet(com.microsoft.azuretools.authmanage.srvpri.entities.ApplicationRet)

Aggregations

ApplicationRet (com.microsoft.azuretools.authmanage.srvpri.entities.ApplicationRet)2 Application (com.microsoft.azuretools.authmanage.srvpri.entities.Application)1 ApplicationGet (com.microsoft.azuretools.authmanage.srvpri.entities.ApplicationGet)1 PasswordCredentials (com.microsoft.azuretools.authmanage.srvpri.entities.PasswordCredentials)1 GraphRestHelper (com.microsoft.azuretools.authmanage.srvpri.rest.GraphRestHelper)1 IOException (java.io.IOException)1 LocalDateTime (java.time.LocalDateTime)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1