Search in sources :

Example 86 with AuthCredentialsServiceState

use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.

the class TestAWSSetupUtils method createAWSVMResource.

/**
 * Create a compute resource for an AWS instance.
 */
public static ComputeService.ComputeState createAWSVMResource(VerificationHost host, ComputeState computeHost, EndpointState endpointState, @SuppressWarnings("rawtypes") Class clazz, String vmName, String zoneId, String regionId, Set<String> tagLinks, AwsNicSpecs nicSpecs, boolean addNewSecurityGroup, Map<String, Object> awsTestContext, boolean persistDiskOnVmDelete, boolean withAdditionalDisks) throws Throwable {
    // Step 1: Create an auth credential to login to the VM
    AuthCredentialsServiceState auth = new AuthCredentialsServiceState();
    auth.type = DEFAULT_AUTH_TYPE;
    auth.userEmail = DEFAULT_COREOS_USER;
    auth.privateKey = TestUtils.loadTestResource(clazz, DEFAULT_COREOS_PRIVATE_KEY_FILE);
    auth = TestUtils.doPost(host, auth, AuthCredentialsServiceState.class, UriUtils.buildUri(host, AuthCredentialsService.FACTORY_LINK));
    // Step 2: Create a VM desc
    ComputeDescription awsVMDesc = new ComputeDescription();
    awsVMDesc.id = instanceType;
    awsVMDesc.name = vmName;
    awsVMDesc.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
    awsVMDesc.instanceType = instanceType;
    awsVMDesc.supportedChildren = new ArrayList<>();
    awsVMDesc.supportedChildren.add(ComputeType.DOCKER_CONTAINER.name());
    awsVMDesc.customProperties = new HashMap<>();
    awsVMDesc.customProperties.put(AWSConstants.AWS_SECURITY_GROUP, securityGroup);
    // set zone to east
    awsVMDesc.zoneId = zoneId;
    awsVMDesc.regionId = regionId;
    awsVMDesc.authCredentialsLink = auth.documentSelfLink;
    awsVMDesc.tenantLinks = endpointState.tenantLinks;
    awsVMDesc.endpointLink = endpointState.documentSelfLink;
    awsVMDesc.endpointLinks = new HashSet<String>();
    awsVMDesc.endpointLinks.add(endpointState.documentSelfLink);
    // set the create service to the aws instance service
    awsVMDesc.instanceAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_INSTANCE_ADAPTER);
    awsVMDesc.statsAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_STATS_ADAPTER);
    awsVMDesc = TestUtils.doPost(host, awsVMDesc, ComputeDescription.class, UriUtils.buildUri(host, ComputeDescriptionService.FACTORY_LINK));
    // Step 3: create boot disk
    List<String> vmDisks = new ArrayList<>();
    ImageState bootImage;
    {
        // Create PUBLIC image state
        bootImage = new ImageState();
        bootImage.id = imageId;
        bootImage.endpointType = endpointState.endpointType;
        bootImage.regionId = regionId;
        bootImage = TestUtils.doPost(host, bootImage, ImageState.class, UriUtils.buildUri(host, ImageService.FACTORY_LINK));
    }
    DiskState rootDisk = new DiskState();
    rootDisk.id = UUID.randomUUID().toString();
    rootDisk.documentSelfLink = rootDisk.id;
    rootDisk.name = DEFAULT_ROOT_DISK_NAME;
    rootDisk.bootOrder = 1;
    rootDisk.sourceImageReference = URI.create(imageId);
    rootDisk.imageLink = bootImage.documentSelfLink;
    rootDisk.bootConfig = new DiskState.BootConfig();
    rootDisk.bootConfig.label = DEFAULT_CONFIG_LABEL;
    DiskState.BootConfig.FileEntry file = new DiskState.BootConfig.FileEntry();
    file.path = DEFAULT_CONFIG_PATH;
    file.contents = TestUtils.loadTestResource(clazz, DEFAULT_USER_DATA_FILE);
    rootDisk.bootConfig.files = new DiskState.BootConfig.FileEntry[] { file };
    rootDisk.capacityMBytes = BOOT_DISK_SIZE_IN_MEBI_BYTES;
    // add custom properties to root disk from profile
    rootDisk.customProperties = new HashMap<>();
    rootDisk.customProperties.put(DEVICE_TYPE, "ebs");
    rootDisk.customProperties.put(VOLUME_TYPE, "io1");
    rootDisk.customProperties.put(IOPS, "500");
    rootDisk.regionId = regionId;
    rootDisk.endpointLink = endpointState.documentSelfLink;
    rootDisk.endpointLinks = new HashSet<String>();
    rootDisk.endpointLinks.add(endpointState.documentSelfLink);
    rootDisk.computeHostLink = endpointState.computeHostLink;
    rootDisk.tenantLinks = endpointState.tenantLinks;
    rootDisk = TestUtils.doPost(host, rootDisk, DiskService.DiskState.class, UriUtils.buildUri(host, DiskService.FACTORY_LINK));
    vmDisks.add(rootDisk.documentSelfLink);
    if (withAdditionalDisks) {
        List<DiskState> additionalDisks = getAdditionalDiskConfiguration(host, endpointState, computeHost, persistDiskOnVmDelete);
        for (DiskState additionalDisk : additionalDisks) {
            vmDisks.add(additionalDisk.documentSelfLink);
        }
    }
    // Create NIC States
    List<String> nicLinks = null;
    if (nicSpecs != null) {
        nicLinks = createAWSNicStates(host, computeHost, endpointState, awsVMDesc.name, nicSpecs, addNewSecurityGroup, awsTestContext).stream().map(nic -> nic.documentSelfLink).collect(Collectors.toList());
    }
    // Create compute state
    ComputeState resource;
    {
        resource = new ComputeState();
        resource.id = UUID.randomUUID().toString();
        resource.name = awsVMDesc.name;
        resource.type = ComputeType.VM_GUEST;
        resource.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
        resource.descriptionLink = awsVMDesc.documentSelfLink;
        resource.parentLink = computeHost.documentSelfLink;
        resource.resourcePoolLink = computeHost.resourcePoolLink;
        resource.networkInterfaceLinks = nicLinks;
        resource.diskLinks = vmDisks;
        resource.tagLinks = tagLinks;
        resource.regionId = awsVMDesc.regionId;
        resource.endpointLink = endpointState.documentSelfLink;
        resource.endpointLinks = new HashSet<String>();
        resource.endpointLinks.add(endpointState.documentSelfLink);
        resource.tenantLinks = endpointState.tenantLinks;
    }
    return TestUtils.doPost(host, resource, ComputeService.ComputeState.class, UriUtils.buildUri(host, ComputeService.FACTORY_LINK));
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) HashSet(java.util.HashSet)

Example 87 with AuthCredentialsServiceState

use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.

the class AzureEndpointAdapterService method validate.

private BiConsumer<AuthCredentialsServiceState, BiConsumer<ServiceErrorResponse, Throwable>> validate(EndpointConfigRequest body) {
    return (credentials, callback) -> {
        try {
            Boolean shouldProvision = Boolean.parseBoolean(body.endpointProperties.get(AZURE_PROVISIONING_PERMISSION));
            validateEndpointUniqueness(credentials, body.checkForEndpointUniqueness, body.tenantLinks).thenCompose(aVoid -> validateCredentials(credentials)).thenCompose(subscription -> getPermissions(credentials)).thenCompose(permList -> verifyPermissions(permList, shouldProvision)).whenComplete((aVoid, e) -> {
                if (e == null) {
                    callback.accept(null, null);
                    return;
                }
                if (e instanceof CompletionException) {
                    e = e.getCause();
                }
                final LocalizableValidationException localizableExc;
                if (e instanceof LocalizableValidationException) {
                    localizableExc = (LocalizableValidationException) e;
                } else {
                    // Azure doesn't send us any meaningful status code to work with
                    localizableExc = new LocalizableValidationException(e, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE);
                }
                ServiceErrorResponse rsp = Utils.toServiceErrorResponse(localizableExc);
                rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
                callback.accept(rsp, localizableExc);
            });
        } catch (Throwable e) {
            logSevere(e);
            ServiceErrorResponse rsp = new ServiceErrorResponse();
            rsp.message = "Invalid Azure credentials";
            rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
            callback.accept(rsp, e);
        }
    };
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) QueryTask(com.vmware.xenon.services.common.QueryTask) PROVIDER_PERMISSIONS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_PERMISSIONS_URI) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubscriptionInner(com.microsoft.azure.management.resources.implementation.SubscriptionInner) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE) URI(java.net.URI) Permission(com.vmware.photon.controller.model.adapters.azure.model.permission.Permission) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) AUTHORIZATION_NAMESPACE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTHORIZATION_NAMESPACE) PROVIDER_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_REST_API_VERSION) CompletionException(java.util.concurrent.CompletionException) PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) STATUS_CODE_UNAUTHORIZED(com.vmware.xenon.common.Operation.STATUS_CODE_UNAUTHORIZED) Optional(java.util.Optional) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) PermissionList(com.vmware.photon.controller.model.adapters.azure.model.permission.PermissionList) PRIVATE_KEY_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEY_KEY) SubscriptionState(com.microsoft.azure.management.resources.SubscriptionState) USER_LINK_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.USER_LINK_KEY) HashMap(java.util.HashMap) PRIVATE_KEYID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEYID_KEY) SubscriptionClientImpl(com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl) ArrayList(java.util.ArrayList) ZONE_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ZONE_KEY) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) BiConsumer(java.util.function.BiConsumer) Retriever(com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils.Retriever) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) EndpointAdapterUtils(com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils) AZURE_TENANT_ID(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_TENANT_ID) SUPPORT_DATASTORES(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_DATASTORES) AZURE_PROVISIONING_PERMISSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_PROVISIONING_PERMISSION) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) REGION_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.REGION_KEY) SUPPORT_PUBLIC_IMAGES(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_PUBLIC_IMAGES) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) CompletionException(java.util.concurrent.CompletionException) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse)

Example 88 with AuthCredentialsServiceState

use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method createComputeDescriptions.

/**
 * Creates relevant resources for given VMs.
 */
private void createComputeDescriptions(EnumerationContext ctx, ComputeEnumerationSubStages next) {
    if (ctx.virtualMachines.size() == 0 && ctx.regions.isEmpty()) {
        // nothing to create
        if (ctx.enumNextPageLink != null) {
            ctx.subStage = ComputeEnumerationSubStages.LISTVMS;
            handleSubStage(ctx);
            return;
        }
        logFine(() -> "No virtual machine found for creation.");
        ctx.subStage = ComputeEnumerationSubStages.PATCH_ADDITIONAL_FIELDS;
        handleSubStage(ctx);
        return;
    }
    logFine(() -> String.format("%d compute description with states to be created", ctx.virtualMachines.size()));
    Iterator<Entry<String, VirtualMachineInner>> iterator = ctx.virtualMachines.entrySet().iterator();
    Collection<Operation> opCollection = new ArrayList<>();
    while (iterator.hasNext()) {
        Entry<String, VirtualMachineInner> vmEntry = iterator.next();
        VirtualMachineInner virtualMachine = vmEntry.getValue();
        AuthCredentialsServiceState auth = new AuthCredentialsServiceState();
        if (virtualMachine.osProfile() != null) {
            auth.userEmail = virtualMachine.osProfile().adminUsername();
            auth.privateKey = virtualMachine.osProfile().adminPassword();
        }
        auth.documentSelfLink = UUID.randomUUID().toString();
        auth.tenantLinks = ctx.parentCompute.tenantLinks;
        auth.customProperties = new HashMap<>();
        if (ctx.request.endpointLink != null) {
            auth.customProperties.put(CUSTOM_PROP_ENDPOINT_LINK, ctx.request.endpointLink);
        }
        String authLink = UriUtils.buildUriPath(AuthCredentialsService.FACTORY_LINK, auth.documentSelfLink);
        Operation authOp = Operation.createPost(createInventoryUri(getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
        opCollection.add(authOp);
        // TODO VSYM-631: Match existing descriptions for new VMs discovered on Azure
        ComputeDescription computeDescription = new ComputeDescription();
        computeDescription.id = UUID.randomUUID().toString();
        computeDescription.name = virtualMachine.name();
        computeDescription.regionId = virtualMachine.location();
        computeDescription.authCredentialsLink = authLink;
        computeDescription.endpointLink = ctx.request.endpointLink;
        AdapterUtils.addToEndpointLinks(computeDescription, ctx.request.endpointLink);
        computeDescription.documentSelfLink = computeDescription.id;
        computeDescription.environmentName = ENVIRONMENT_NAME_AZURE;
        if (virtualMachine.hardwareProfile() != null && virtualMachine.hardwareProfile().vmSize() != null) {
            computeDescription.instanceType = virtualMachine.hardwareProfile().vmSize().toString();
        }
        computeDescription.instanceAdapterReference = ctx.parentCompute.description.instanceAdapterReference;
        computeDescription.statsAdapterReference = ctx.parentCompute.description.statsAdapterReference;
        computeDescription.diskAdapterReference = ctx.parentCompute.description.diskAdapterReference;
        computeDescription.computeHostLink = ctx.parentCompute.documentSelfLink;
        computeDescription.customProperties = new HashMap<>();
        computeDescription.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK);
        // TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-1268
        String resourceGroupName = getResourceGroupName(virtualMachine.id());
        computeDescription.customProperties.put(AZURE_RESOURCE_GROUP_NAME, resourceGroupName);
        computeDescription.tenantLinks = ctx.parentCompute.tenantLinks;
        Operation compDescOp = Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK).setBody(computeDescription);
        ctx.computeDescriptionIds.put(virtualMachine.name(), computeDescription.id);
        opCollection.add(compDescOp);
    }
    for (RegionInfo region : ctx.regions.values()) {
        ComputeDescription computeDescriptionForRegion = createComputeDescriptionForRegion(ctx, region);
        Operation compDescOp = Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK).setBody(computeDescriptionForRegion);
        ctx.computeDescriptionIds.put(region.regionId, computeDescriptionForRegion.id);
        opCollection.add(compDescOp);
    }
    OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
        if (exs != null) {
            exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
        }
        logFine(() -> "Continue on to updating disks.");
        ctx.subStage = next;
        handleSubStage(ctx);
    }).sendWith(this);
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) DISK_CONTROLLER_NUMBER(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DISK_CONTROLLER_NUMBER) Action1(rx.functions.Action1) StringUtils(org.apache.commons.lang3.StringUtils) Azure(com.microsoft.azure.management.Azure) Utils(com.vmware.xenon.common.Utils) Pair(org.apache.commons.lang3.tuple.Pair) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) OSDisk(com.microsoft.azure.management.compute.OSDisk) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) Set(java.util.Set) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) StorageAccountTypes(com.microsoft.azure.management.compute.StorageAccountTypes) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) InstanceViewStatus(com.microsoft.azure.management.compute.InstanceViewStatus) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) AZURE_DATA_DISK_CACHING(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_DATA_DISK_CACHING) ImageReferenceInner(com.microsoft.azure.management.compute.implementation.ImageReferenceInner) VirtualMachinesInner(com.microsoft.azure.management.compute.implementation.VirtualMachinesInner) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) RegionInfo(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse.RegionInfo) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) ArrayList(java.util.ArrayList) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) OperatingSystemTypes(com.microsoft.azure.management.compute.OperatingSystemTypes) BiConsumer(java.util.function.BiConsumer) AZURE_DIAGNOSTIC_STORAGE_ACCOUNT_LINK(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_DIAGNOSTIC_STORAGE_ACCOUNT_LINK) AZURE_STORAGE_ACCOUNT_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_ACCOUNT_URI) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) DataDisk(com.microsoft.azure.management.compute.DataDisk) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) ENVIRONMENT_NAME_AZURE(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ENVIRONMENT_NAME_AZURE) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) RegionEnumerationResponse(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QueryTask(com.vmware.xenon.services.common.QueryTask) AZURE_RESOURCE_GROUP_NAME(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_RESOURCE_GROUP_NAME) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) AZURE_MANAGED_DISK_TYPE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_MANAGED_DISK_TYPE) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) AzureUtils.injectOperationContext(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.injectOperationContext) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) AzureConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.getQueryResultLimit) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) AzureUtils.getResourceGroupName(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getResourceGroupName) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) Entry(java.util.Map.Entry) NetworkInterfacesInner(com.microsoft.azure.management.network.implementation.NetworkInterfacesInner) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) InstanceViewTypes(com.microsoft.azure.management.compute.InstanceViewTypes) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DiskService(com.vmware.photon.controller.model.resources.DiskService) AzureUtils.isDiskManaged(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.isDiskManaged) Default(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) AZURE_OSDISK_CACHING(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_OSDISK_CACHING) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) HashMap(java.util.HashMap) HashSet(java.util.HashSet) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) NetworkInterfaceIPConfigurationInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceIPConfigurationInner) Operation(com.vmware.xenon.common.Operation) Page(com.microsoft.azure.Page) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) NetworkInterfaceReferenceInner(com.microsoft.azure.management.compute.implementation.NetworkInterfaceReferenceInner) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) NetworkInterfaceInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceInner) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) RESOURCE_GROUP_NAME(com.vmware.photon.controller.model.ComputeProperties.RESOURCE_GROUP_NAME) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ArrayList(java.util.ArrayList) RegionInfo(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse.RegionInfo) Operation(com.vmware.xenon.common.Operation) Entry(java.util.Map.Entry) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner)

Example 89 with AuthCredentialsServiceState

use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.

the class TestProvisionAWSSecurityGroup method testDeleteAWSSecurityGroupWithDependency.

@Test
public void testDeleteAWSSecurityGroupWithDependency() throws Throwable {
    // create credentials
    Operation authResponse = new Operation();
    TestUtils.postCredentials(this.host, authResponse, this.privateKey, this.privateKeyId);
    AuthCredentialsServiceState creds = authResponse.getBody(AuthCredentialsServiceState.class);
    // create resource pool
    Operation poolResponse = new Operation();
    TestUtils.postResourcePool(this.host, poolResponse);
    ResourcePoolState pool = poolResponse.getBody(ResourcePoolState.class);
    // create sg service
    Operation securityGroupResponse = new Operation();
    SecurityGroupState initialSecurityGroupState = buildSecurityGroupState(creds, pool);
    TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, securityGroupResponse);
    SecurityGroupState securityGroupState = securityGroupResponse.getBody(SecurityGroupState.class);
    // set up security group task state
    ProvisionSecurityGroupTaskState task = new ProvisionSecurityGroupTaskState();
    task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.CREATE;
    task.securityGroupDescriptionLinks = Stream.of(securityGroupState.documentSelfLink).collect(Collectors.toSet());
    task.customProperties = new HashMap<>();
    task.customProperties.put(NETWORK_STATE_ID_PROP_NAME, this.vpcId);
    Operation provision = new Operation();
    provisionSecurityGroup(task, provision);
    ProvisionSecurityGroupTaskState ps = provision.getBody(ProvisionSecurityGroupTaskState.class);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
    securityGroupState = getServiceSynchronously(securityGroupState.documentSelfLink, SecurityGroupState.class);
    // provision machine on the newly created SG
    String vm = provisionAWSVMWithEC2Client(this.host, this.ec2client, EC2_LINUX_AMI, this.subnetId, securityGroupState.id);
    // reuse previous task, but switch to a delete
    task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.DELETE;
    Operation remove = new Operation();
    provisionSecurityGroup(task, remove);
    // delete the newly provisioned machine after a small delay
    Runnable deleteMachine = () -> {
        try {
            Thread.sleep(2000);
            deleteVMsUsingEC2Client(this.ec2client, this.host, Collections.singletonList(vm));
        } catch (Throwable t) {
            assertNotNull(t);
        }
    };
    deleteMachine.run();
    ProvisionSecurityGroupTaskState removeTask = remove.getBody(ProvisionSecurityGroupTaskState.class);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, removeTask.documentSelfLink));
    // verify security group state is gone
    try {
        getSecurityGroupState(securityGroupState.documentSelfLink);
    } catch (Exception ex) {
        assertTrue(ex instanceof ServiceNotFoundException);
    }
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) Operation(com.vmware.xenon.common.Operation) TimeoutException(java.util.concurrent.TimeoutException) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) CompletionException(java.util.concurrent.CompletionException) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 90 with AuthCredentialsServiceState

use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.

the class TestProvisionAWSSecurityGroup method setUp.

@Before
public void setUp() throws Exception {
    CommandLineArgumentParser.parseFromProperties(this);
    // ignore if any of the required properties are missing
    org.junit.Assume.assumeTrue(TestUtils.isNull(this.privateKey, this.privateKeyId, this.region, this.vpcId, this.subnetId));
    try {
        PhotonModelServices.startServices(this.host);
        PhotonModelMetricServices.startServices(this.host);
        PhotonModelTaskServices.startServices(this.host);
        PhotonModelAdaptersRegistryAdapters.startServices(this.host);
        AWSAdaptersTestUtils.startServicesSynchronously(this.host);
        // start the aws sg service
        this.host.startService(Operation.createPost(UriUtils.buildUri(this.host, AWSSecurityGroupService.class)), new AWSSecurityGroupService());
        this.provisionSecurityGroupFactory = UriUtils.buildUri(this.host, ProvisionSecurityGroupTaskService.FACTORY_LINK);
        this.netClient = new AWSNetworkClient(TestUtils.getClient(this.privateKeyId, this.privateKey, this.region, false));
        this.vpc = this.netClient.getVPC(this.vpcId);
        assertNotNull(this.vpc);
        AuthCredentialsServiceState creds = new AuthCredentialsServiceState();
        creds.privateKey = this.privateKey;
        creds.privateKeyId = this.privateKeyId;
        TestContext ec2WaitContext = new TestContext(1, Duration.ofSeconds(30L));
        AWSUtils.getEc2AsyncClient(creds, this.region, getExecutor()).exceptionally(t -> {
            ec2WaitContext.fail(t);
            throw new CompletionException(t);
        }).thenAccept(ec2Client -> {
            this.ec2client = ec2Client;
            ec2WaitContext.complete();
        });
        ec2WaitContext.await();
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) EC2_LINUX_AMI(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.EC2_LINUX_AMI) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) TestAWSSetupUtils.deleteVMsUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsUsingEC2Client) Date(java.util.Date) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TimeoutException(java.util.concurrent.TimeoutException) TestAWSSetupUtils.provisionAWSVMWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.provisionAWSVMWithEC2Client) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) Duration(java.time.Duration) After(org.junit.After) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) URI(java.net.URI) ProvisionSecurityGroupTaskService(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) List(java.util.List) Stream(java.util.stream.Stream) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) SecurityGroupInstanceRequest(com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest) NETWORK_STATE_ID_PROP_NAME(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.NETWORK_STATE_ID_PROP_NAME) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) HashMap(java.util.HashMap) AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) TenantService(com.vmware.xenon.services.common.TenantService) ArrayList(java.util.ArrayList) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) Before(org.junit.Before) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) Vpc(com.amazonaws.services.ec2.model.Vpc) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNull(org.junit.Assert.assertNull) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) TimeoutException(java.util.concurrent.TimeoutException) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) CompletionException(java.util.concurrent.CompletionException) Before(org.junit.Before)

Aggregations

AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)98 Operation (com.vmware.xenon.common.Operation)33 Before (org.junit.Before)28 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)25 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)19 UriUtils (com.vmware.xenon.common.UriUtils)18 URI (java.net.URI)18 List (java.util.List)18 HashMap (java.util.HashMap)17 CompletionException (java.util.concurrent.CompletionException)16 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)15 Utils (com.vmware.xenon.common.Utils)15 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)14 StatelessService (com.vmware.xenon.common.StatelessService)13 TimeUnit (java.util.concurrent.TimeUnit)13 Collections (java.util.Collections)12 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)11 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)11 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)10