Search in sources :

Example 1 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class AzureInstanceService method enableMonitoring.

private void enableMonitoring(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
    Operation readFile = Operation.createGet(null).setCompletion((o, e) -> {
        if (e != null) {
            handleError(ctx, e);
            return;
        }
        AzureDiagnosticSettings azureDiagnosticSettings = o.getBody(AzureDiagnosticSettings.class);
        String vmName = ctx.vmName;
        String azureInstanceId = ctx.vmId;
        String storageAccountName = ctx.storageAccountName;
        // Replace the resourceId and storageAccount keys with correct values
        azureDiagnosticSettings.getProperties().getPublicConfiguration().getDiagnosticMonitorConfiguration().getMetrics().setResourceId(azureInstanceId);
        azureDiagnosticSettings.getProperties().getPublicConfiguration().setStorageAccount(storageAccountName);
        ApplicationTokenCredentials credentials = ctx.azureSdkClients.credentials;
        URI uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(UriUtils.buildUri(AzureUtils.getAzureBaseUri()), azureInstanceId, AzureConstants.DIAGNOSTIC_SETTING_ENDPOINT, AzureConstants.DIAGNOSTIC_SETTING_AGENT), AzureConstants.QUERY_PARAM_API_VERSION, AzureConstants.DIAGNOSTIC_SETTING_API_VERSION);
        Operation operation = Operation.createPut(uri);
        operation.setBody(azureDiagnosticSettings);
        operation.addRequestHeader(Operation.ACCEPT_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
        operation.addRequestHeader(Operation.CONTENT_TYPE_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
        try {
            operation.addRequestHeader(Operation.AUTHORIZATION_HEADER, AzureConstants.AUTH_HEADER_BEARER_PREFIX + credentials.getToken(AzureUtils.getAzureBaseUri()));
        } catch (Exception ex) {
            handleError(ctx, ex);
            return;
        }
        logFine(() -> String.format("Enabling monitoring on the VM [%s]", vmName));
        operation.setCompletion((op, er) -> {
            if (er != null) {
                handleError(ctx, er);
                return;
            }
            logFine(() -> String.format("Successfully enabled monitoring on the VM [%s]", vmName));
            handleAllocation(ctx, nextStage);
        });
        sendRequest(operation);
    });
    String fileUri = getClass().getResource(AzureConstants.DIAGNOSTIC_SETTINGS_JSON_FILE_NAME).getFile();
    File jsonPayloadFile = new File(fileUri);
    try {
        FileUtils.readFileAndComplete(readFile, jsonPayloadFile);
    } catch (Exception e) {
        handleError(ctx, e);
    }
}
Also used : AzureDiagnosticSettings(com.vmware.photon.controller.model.adapters.azure.model.diagnostics.AzureDiagnosticSettings) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) File(java.io.File) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudException(com.microsoft.azure.CloudException)

Example 2 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class AzurePowerServiceTest method setUp.

@Before
public void setUp() throws Exception {
    try {
        /*
             * Init Class-specific (shared between test runs) vars.
             *
             * NOTE: Ultimately this should go to @BeforeClass, BUT BasicReusableHostTestCase.HOST
             * is not accessible.
             */
        if (computeHost == null) {
            PhotonModelServices.startServices(this.host);
            PhotonModelMetricServices.startServices(this.host);
            PhotonModelAdaptersRegistryAdapters.startServices(this.host);
            PhotonModelTaskServices.startServices(this.host);
            AzureAdaptersTestUtils.startServicesSynchronouslyAzure(this.host);
            this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
            this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
            // TODO: VSYM-992 - improve test/fix arbitrary timeout
            this.host.setTimeoutSeconds(1200);
            // Create a resource pool where the VM will be housed
            ResourcePoolState resourcePool = createDefaultResourcePool(this.host);
            AuthCredentialsServiceState authCredentials = createDefaultAuthCredentials(this.host, this.clientID, this.clientKey, this.subscriptionId, this.tenantId);
            endpointState = createDefaultEndpointState(this.host, authCredentials.documentSelfLink);
            // create a compute host for the Azure
            computeHost = createDefaultComputeHost(this.host, resourcePool.documentSelfLink, endpointState);
        }
        if (!this.isMock) {
            ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(this.clientID, this.tenantId, this.clientKey, AzureEnvironment.AZURE);
            this.computeManagementClient = new ComputeManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
        }
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeManagementClientImpl(com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) Before(org.junit.Before)

Example 3 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class TestAzureEnumerationTask method setUp.

@Override
@Before
public void setUp() throws Exception {
    try {
        /*
             * Init Class-specific (shared between test runs) vars.
             *
             * NOTE: Ultimately this should go to @BeforeClass, BUT BasicReusableHostTestCase.HOST
             * is not accessible.
             */
        if (computeHost == null) {
            PhotonModelServices.startServices(this.host);
            PhotonModelMetricServices.startServices(this.host);
            PhotonModelTaskServices.startServices(this.host);
            PhotonModelAdaptersRegistryAdapters.startServices(this.host);
            AzureAdaptersTestUtils.startServicesSynchronouslyAzure(this.host);
            this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
            this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
            // TODO: VSYM-992 - improve test/fix arbitrary timeout
            this.host.setTimeoutSeconds(600);
            // Create a resource pool where the VMs will be housed
            ResourcePoolState resourcePool = createDefaultResourcePool(this.host);
            AuthCredentialsServiceState authCredentials = createDefaultAuthCredentials(this.host, this.clientID, this.clientKey, this.subscriptionId, this.tenantId);
            endpointState = createDefaultEndpointState(this.host, authCredentials.documentSelfLink);
            // create a compute host for the Azure
            computeHost = createDefaultComputeHost(this.host, resourcePool.documentSelfLink, endpointState);
            endpointState.computeHostLink = computeHost.documentSelfLink;
            this.host.waitForResponse(Operation.createPatch(this.host, endpointState.documentSelfLink).setBody(endpointState));
        }
        azureVMName = azureVMName == null ? generateName(azureVMNamePrefix) : azureVMName;
        this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
        AzureUtils.setAzureClientMock(this.isAzureClientMock);
        AzureUtils.setAzureMockHost(this.azureMockEndpointReference);
        if (!this.isMock) {
            if (AzureUtils.isAzureClientMock()) {
                AzureEnvironment azureEnv = AzureEnvironment.AZURE;
                azureEnv.endpoints().put(AzureEnvironment.Endpoint.ACTIVE_DIRECTORY.toString(), AzureUtils.getAzureBaseUri());
                ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(this.clientID, this.tenantId, this.clientKey, azureEnv);
                this.computeManagementClient = new ComputeManagementClientImpl(AzureUtils.getAzureBaseUri(), credentials).withSubscriptionId(this.subscriptionId);
                this.resourceManagementClient = new ResourceManagementClientImpl(AzureUtils.getAzureBaseUri(), credentials).withSubscriptionId(this.subscriptionId);
                this.networkManagementClient = new NetworkManagementClientImpl(AzureUtils.getAzureBaseUri(), credentials).withSubscriptionId(this.subscriptionId);
            } else {
                ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(this.clientID, this.tenantId, this.clientKey, AzureEnvironment.AZURE);
                this.computeManagementClient = new ComputeManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
                ;
                this.resourceManagementClient = new ResourceManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
                ;
                this.networkManagementClient = new NetworkManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
                ;
            }
        }
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : NetworkManagementClientImpl(com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeManagementClientImpl(com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl) AzureEnvironment(com.microsoft.azure.AzureEnvironment) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) ResourceManagementClientImpl(com.microsoft.azure.management.resources.implementation.ResourceManagementClientImpl) Before(org.junit.Before)

Example 4 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class AzureSubnetTaskServiceTest method setUp.

@Override
@Before
public void setUp() throws Throwable {
    CommandLineArgumentParser.parseFromProperties(this);
    if (computeHost == null) {
        PhotonModelServices.startServices(this.host);
        PhotonModelTaskServices.startServices(this.host);
        PhotonModelAdaptersRegistryAdapters.startServices(this.host);
        AzureAdaptersTestUtils.startServicesSynchronouslyAzure(this.host);
        this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
        // TODO: VSYM-992 - improve test/fix arbitrary timeout
        this.host.setTimeoutSeconds(600);
        ResourcePoolState resourcePool = createDefaultResourcePool(this.host);
        AuthCredentialsServiceState authCredentials = createDefaultAuthCredentials(this.host, this.clientID, this.clientKey, this.subscriptionId, this.tenantId);
        endpointState = createDefaultEndpointState(this.host, authCredentials.documentSelfLink);
        // create a compute host for the Azure
        computeHost = createDefaultComputeHost(this.host, resourcePool.documentSelfLink, endpointState);
    }
    if (!this.isMock) {
        ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(this.clientID, this.tenantId, this.clientKey, AzureEnvironment.AZURE);
        NetworkManagementClientImpl networkManagementClient = new NetworkManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
        ResourceManagementClientImpl resourceManagementClient = new ResourceManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
        this.vNetClient = networkManagementClient.virtualNetworks();
        this.rgOpsClient = resourceManagementClient.resourceGroups();
        this.subnetsClient = networkManagementClient.subnets();
        ResourceGroupInner rg = new ResourceGroupInner();
        rg.withName(this.rgName);
        rg.withLocation(this.regionId);
        this.rgOpsClient.createOrUpdate(this.rgName, rg);
        VirtualNetworkInner vNet = new VirtualNetworkInner();
        // Azure's custom serializers don't handle well collections constructed with
        // Collections.singletonList(), so initialize an ArrayList
        AddressSpace addressSpace = new AddressSpace();
        List<String> cidrs = new ArrayList<>();
        cidrs.add(AZURE_DEFAULT_VPC_CIDR);
        addressSpace.withAddressPrefixes(cidrs);
        vNet.withAddressSpace(addressSpace);
        vNet.withLocation(this.regionId);
        this.vNetClient.createOrUpdate(this.rgName, this.vNetName, vNet);
    }
    ResourceGroupState rgState = createDefaultResourceGroupState(this.host, this.rgName, computeHost, endpointState, ResourceGroupStateType.AzureResourceGroup);
    this.networkState = createNetworkState(rgState.documentSelfLink);
}
Also used : NetworkManagementClientImpl(com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) VirtualNetworkInner(com.microsoft.azure.management.network.implementation.VirtualNetworkInner) ResourceGroupInner(com.microsoft.azure.management.resources.implementation.ResourceGroupInner) AddressSpace(com.microsoft.azure.management.network.AddressSpace) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) AzureTestUtil.createDefaultResourceGroupState(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultResourceGroupState) ArrayList(java.util.ArrayList) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) ResourceManagementClientImpl(com.microsoft.azure.management.resources.implementation.ResourceManagementClientImpl) Before(org.junit.Before)

Example 5 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class AzureLifecycleOperationServiceTest method setUp.

@Before
public void setUp() throws Exception {
    try {
        /*
             * Init Class-specific (shared between test runs) vars.
             *
             * NOTE: Ultimately this should go to @BeforeClass, BUT BasicReusableHostTestCase.HOST
             * is not accessible.
             */
        if (computeHost == null) {
            PhotonModelServices.startServices(this.host);
            PhotonModelAdaptersRegistryAdapters.startServices(this.host);
            PhotonModelMetricServices.startServices(this.host);
            PhotonModelTaskServices.startServices(this.host);
            AzureAdaptersTestUtils.startServicesSynchronouslyAzure(this.host);
            this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
            this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
            // TODO: VSYM-992 - improve test/fix arbitrary timeout
            this.host.setTimeoutSeconds(1200);
            // Create a resource pool where the VM will be housed
            ResourcePoolState resourcePool = createDefaultResourcePool(this.host);
            AuthCredentialsServiceState authCredentials = createDefaultAuthCredentials(this.host, this.clientID, this.clientKey, this.subscriptionId, this.tenantId);
            endpointState = createDefaultEndpointState(this.host, authCredentials.documentSelfLink);
            // create a compute host for the Azure
            computeHost = createDefaultComputeHost(this.host, resourcePool.documentSelfLink, endpointState);
        }
        if (!this.isMock) {
            ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(this.clientID, this.tenantId, this.clientKey, AzureEnvironment.AZURE);
            this.computeManagementClient = new ComputeManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
        }
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeManagementClientImpl(com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) Before(org.junit.Before)

Aggregations

ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)27 File (java.io.File)9 Azure (com.microsoft.azure.management.Azure)8 RestClient (com.microsoft.rest.RestClient)7 Before (org.junit.Before)7 AzureResponseBuilder (com.microsoft.azure.AzureResponseBuilder)6 AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)6 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)5 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)5 AzureEnvironment (com.microsoft.azure.AzureEnvironment)4 ComputeManagementClientImpl (com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl)4 IOException (java.io.IOException)4 NetworkManagementClientImpl (com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl)3 ResourceManagementClientImpl (com.microsoft.azure.management.resources.implementation.ResourceManagementClientImpl)3 Parameters (org.testng.annotations.Parameters)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Authenticated (com.microsoft.azure.management.Azure.Authenticated)2 DataLakeStoreAccountManagementClientImpl (com.microsoft.azure.management.datalake.store.implementation.DataLakeStoreAccountManagementClientImpl)2 DataLakeStoreAccount (com.microsoft.azure.management.datalake.store.models.DataLakeStoreAccount)2 NetworkRequest (com.sequenceiq.cloudbreak.api.model.NetworkRequest)2