Search in sources :

Example 46 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class InstanceTemplateRequestToTemplateConverter method convert.

public Template convert(InstanceTemplateRequest source, CloudPlatform cloudPlatform, String accountId, String diskEncryptionSetId, String gcpKmsEncryptionKey, String awsKmsEncryptionKey) {
    Template template = new Template();
    template.setAccountId(accountId);
    template.setName(missingResourceNameGenerator.generateName(APIResourceType.TEMPLATE));
    template.setStatus(ResourceStatus.USER_MANAGED);
    setVolumesProperty(source.getAttachedVolumes(), template, cloudPlatform);
    template.setInstanceType(Objects.requireNonNullElse(source.getInstanceType(), defaultInstanceTypeProvider.getForPlatform(cloudPlatform.name())));
    Map<String, Object> attributes = new HashMap<>();
    if (cloudPlatform == CloudPlatform.AWS) {
        if (awsKmsEncryptionKey != null) {
            attributes.put(AwsInstanceTemplate.EBS_ENCRYPTION_ENABLED, Boolean.TRUE);
            attributes.put(InstanceTemplate.VOLUME_ENCRYPTION_KEY_TYPE, EncryptionType.CUSTOM.name());
            attributes.put(InstanceTemplate.VOLUME_ENCRYPTION_KEY_ID, awsKmsEncryptionKey);
        } else {
            attributes.put(AwsInstanceTemplate.EBS_ENCRYPTION_ENABLED, Boolean.TRUE);
            attributes.put(InstanceTemplate.VOLUME_ENCRYPTION_KEY_TYPE, EncryptionType.DEFAULT.name());
        }
    }
    Optional.ofNullable(source.getAws()).map(AwsInstanceTemplateParameters::getSpot).ifPresent(spotParameters -> {
        attributes.put(AwsInstanceTemplate.EC2_SPOT_PERCENTAGE, spotParameters.getPercentage());
        if (Objects.nonNull(spotParameters.getMaxPrice())) {
            attributes.put(AwsInstanceTemplate.EC2_SPOT_MAX_PRICE, spotParameters.getMaxPrice());
        }
    });
    if (cloudPlatform == CloudPlatform.AZURE) {
        if (diskEncryptionSetId != null) {
            attributes.put(AzureInstanceTemplate.DISK_ENCRYPTION_SET_ID, diskEncryptionSetId);
            attributes.put(AzureInstanceTemplate.MANAGED_DISK_ENCRYPTION_WITH_CUSTOM_KEY_ENABLED, true);
        }
        if (entitlementService.isAzureEncryptionAtHostEnabled(accountId)) {
            attributes.put(AzureInstanceTemplate.ENCRYPTION_AT_HOST_ENABLED, true);
        }
    }
    if (gcpKmsEncryptionKey != null && cloudPlatform == CloudPlatform.GCP) {
        attributes.put(GcpInstanceTemplate.VOLUME_ENCRYPTION_KEY_ID, gcpKmsEncryptionKey);
        attributes.put(GcpInstanceTemplate.VOLUME_ENCRYPTION_KEY_TYPE, EncryptionType.CUSTOM);
        attributes.put(GcpInstanceTemplate.KEY_ENCRYPTION_METHOD, KeyEncryptionMethod.KMS);
    }
    template.setAttributes(new Json(attributes));
    return template;
}
Also used : HashMap(java.util.HashMap) Json(com.sequenceiq.cloudbreak.common.json.Json) Template(com.sequenceiq.freeipa.entity.Template) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) AzureInstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.instance.AzureInstanceTemplate) AwsInstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.instance.AwsInstanceTemplate) GcpInstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.instance.GcpInstanceTemplate)

Example 47 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class StackToCloudStackConverter method buildGroupNetwork.

private GroupNetwork buildGroupNetwork(com.sequenceiq.freeipa.entity.Network network, InstanceGroup instanceGroup) {
    GroupNetwork groupNetwork = null;
    InstanceGroupNetwork instanceGroupNetwork = instanceGroup.getInstanceGroupNetwork();
    if (instanceGroupNetwork != null) {
        Json attributes = instanceGroupNetwork.getAttributes();
        Map<String, Object> params = attributes == null ? Collections.emptyMap() : attributes.getMap();
        Set<GroupSubnet> subnets = new HashSet<>();
        if (params != null) {
            List<String> subnetIds = (List<String>) params.getOrDefault(NetworkConstants.SUBNET_IDS, new ArrayList<>());
            for (String subnetId : subnetIds) {
                GroupSubnet groupSubnet = new GroupSubnet(subnetId);
                subnets.add(groupSubnet);
            }
        }
        groupNetwork = new GroupNetwork(network.getOutboundInternetTraffic(), subnets, params);
    }
    return groupNetwork;
}
Also used : InstanceGroupNetwork(com.sequenceiq.freeipa.entity.InstanceGroupNetwork) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Json(com.sequenceiq.cloudbreak.common.json.Json) InstanceGroupNetwork(com.sequenceiq.freeipa.entity.InstanceGroupNetwork) GroupSubnet(com.sequenceiq.cloudbreak.cloud.model.GroupSubnet) HashSet(java.util.HashSet)

Example 48 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class StackToCloudStackConverter method buildInstanceTemplate.

InstanceTemplate buildInstanceTemplate(Template template, String name, Long privateId, InstanceStatus status, String instanceImageId) {
    Json attributesJson = template.getAttributes();
    Map<String, Object> attributes = Optional.ofNullable(attributesJson).map(Json::getMap).orElseGet(HashMap::new);
    Json fromVault = template.getSecretAttributes() == null ? null : new Json(template.getSecretAttributes());
    Map<String, Object> secretAttributes = Optional.ofNullable(fromVault).map(Json::getMap).orElseGet(HashMap::new);
    Map<String, Object> fields = new HashMap<>();
    fields.putAll(attributes);
    fields.putAll(secretAttributes);
    List<Volume> volumes = new ArrayList<>();
    for (int i = 0; i < template.getVolumeCount(); i++) {
        // FIXME figure out volume mounting
        Volume volume = new Volume("/mnt/vol" + (i + 1), template.getVolumeType(), template.getVolumeSize(), CloudVolumeUsageType.GENERAL);
        volumes.add(volume);
    }
    return new InstanceTemplate(template.getInstanceType(), name, privateId, volumes, status, fields, template.getId(), instanceImageId, TemporaryStorage.ATTACHED_VOLUMES, 0L);
}
Also used : HashMap(java.util.HashMap) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) ArrayList(java.util.ArrayList) Json(com.sequenceiq.cloudbreak.common.json.Json) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 49 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class StackToCloudStackConverter method buildNetwork.

private Network buildNetwork(Stack stack) {
    com.sequenceiq.freeipa.entity.Network stackNetwork = stack.getNetwork();
    Network result = null;
    if (stackNetwork != null) {
        Subnet subnet = new Subnet(null);
        Json attributes = stackNetwork.getAttributes();
        Map<String, Object> params = attributes == null ? Collections.emptyMap() : attributes.getMap();
        result = new Network(subnet, stackNetwork.getNetworkCidrs(), stackNetwork.getOutboundInternetTraffic(), params);
    }
    return result;
}
Also used : InstanceGroupNetwork(com.sequenceiq.freeipa.entity.InstanceGroupNetwork) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Json(com.sequenceiq.cloudbreak.common.json.Json) GroupSubnet(com.sequenceiq.cloudbreak.cloud.model.GroupSubnet) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet)

Example 50 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class InstanceGroupNetworkRequestToInstanceGroupNetworkConverter method convert.

public InstanceGroupNetwork convert(String cloudPlatform, @Nonnull InstanceGroupNetworkRequest source) {
    InstanceGroupNetwork entity = new InstanceGroupNetwork();
    entity.setCloudPlatform(cloudPlatform);
    Map<String, Object> params = new HashMap<>();
    InstanceGroupAwsNetworkParameters aws = source.getAws();
    if (aws != null && aws.getSubnetIds() != null) {
        List<String> subnetIds = aws.getSubnetIds();
        params.put(NetworkConstants.SUBNET_IDS, subnetIds);
    }
    entity.setAttributes(new Json(params));
    return entity;
}
Also used : HashMap(java.util.HashMap) InstanceGroupAwsNetworkParameters(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.instance.aws.InstanceGroupAwsNetworkParameters) Json(com.sequenceiq.cloudbreak.common.json.Json) InstanceGroupNetwork(com.sequenceiq.freeipa.entity.InstanceGroupNetwork)

Aggregations

Json (com.sequenceiq.cloudbreak.common.json.Json)266 Test (org.junit.jupiter.api.Test)95 HashMap (java.util.HashMap)49 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)31 Template (com.sequenceiq.freeipa.entity.Template)26 AwsInstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.instance.AwsInstanceTemplate)25 List (java.util.List)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 AzureInstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.instance.AzureInstanceTemplate)23 Map (java.util.Map)22 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)21 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)21 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21 InstanceGroup (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)20 IOException (java.io.IOException)20 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)18 RestRequestDetails (com.sequenceiq.cloudbreak.structuredevent.event.rest.RestRequestDetails)16 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)16 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)14