Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.hetzner.HetznerRegion;
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.oci.OciRegion;
import com.yanchware.fractal.sdk.domain.livesystem.service.dtos.ProviderType;
import com.yanchware.fractal.sdk.domain.values.ResourceGroupId;
import com.yanchware.fractal.sdk.utils.CollectionUtils;
import com.yanchware.fractal.sdk.utils.SerializationUtils;
import lombok.Getter;
Expand All @@ -38,7 +39,7 @@ public abstract class BaseEnvironment implements Environment, Validatable {

private final Map<String, Object> parameters;
private String name;
private Collection<UUID> resourceGroups;
private Collection<ResourceGroupId> resourceGroups;
private Map<String, String> tags;
private Collection<Secret> secrets;
private CiCdProfile defaultCiCdProfile;
Expand All @@ -51,7 +52,7 @@ public String getName() {
}

@Override
public Collection<UUID> getResourceGroups() {
public Collection<ResourceGroupId> getResourceGroups() {
return resourceGroups;
}

Expand Down Expand Up @@ -162,11 +163,11 @@ public B withName(String name) {
return builder;
}

public B withResourceGroup(UUID resourceGroupId) {
public B withResourceGroup(ResourceGroupId resourceGroupId) {
return withResourceGroups(List.of(resourceGroupId));
}

public B withResourceGroups(Collection<UUID> resourceGroups) {
public B withResourceGroups(Collection<ResourceGroupId> resourceGroups) {
if (CollectionUtils.isBlank(resourceGroups)) {
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yanchware.fractal.sdk.domain.environment;

import com.yanchware.fractal.sdk.domain.livesystem.service.dtos.EnvironmentDto;
import com.yanchware.fractal.sdk.domain.values.ResourceGroupId;

import java.util.Collection;
import java.util.Map;
Expand All @@ -9,7 +10,7 @@
public interface Environment {
EnvironmentIdValue getId();
String getName();
Collection<UUID> getResourceGroups();
Collection<ResourceGroupId> getResourceGroups();
Map<String, String> getTags();
Map<String, Object> getParameters();
EnvironmentDto toDto();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.gcp.GcpRegion;
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.hetzner.HetznerRegion;
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.oci.OciRegion;
import com.yanchware.fractal.sdk.domain.values.ResourceGroupId;

import java.util.Collection;
import java.util.Map;
Expand All @@ -23,14 +24,14 @@ EnvironmentResponse create(
EnvironmentIdValue managementEnvironmentId,
EnvironmentIdValue environmentId,
String name,
Collection<UUID> resourceGroups,
Collection<ResourceGroupId> resourceGroups,
Map<String, Object> parameters) throws InstantiatorException;

EnvironmentResponse update(
EnvironmentIdValue managementEnvironmentId,
EnvironmentIdValue environmentId,
String name,
Collection<UUID> resourceGroups,
Collection<ResourceGroupId> resourceGroups,
Map<String, Object> parameters,
String defaultCiCdProfileShortName) throws InstantiatorException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.hetzner.HetznerRegion;
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.oci.OciRegion;
import com.yanchware.fractal.sdk.domain.livesystem.service.dtos.ProviderType;
import com.yanchware.fractal.sdk.domain.values.ResourceGroupId;
import com.yanchware.fractal.sdk.utils.HttpUtils;
import com.yanchware.fractal.sdk.utils.StringHelper;
import io.github.resilience4j.retry.RetryRegistry;
Expand Down Expand Up @@ -44,7 +45,7 @@ public EnvironmentResponse create(
EnvironmentIdValue managementEnvironmentId,
EnvironmentIdValue environmentId,
String name,
Collection<UUID> resourceGroups,
Collection<ResourceGroupId> resourceGroups,
Map<String, Object> parameters) throws InstantiatorException {
return executeRequestWithRetries(
"createEnvironment",
Expand All @@ -57,7 +58,7 @@ public EnvironmentResponse create(
serializeSafely(new CreateEnvironmentRequest(
managementEnvironmentId,
name,
resourceGroups,
resourceGroups.stream().map(ResourceGroupId::toString).toList(),
parameters))),
new int[]{201},
EnvironmentResponse.class);
Expand All @@ -68,7 +69,7 @@ public EnvironmentResponse update(
EnvironmentIdValue managementEnvironmentId,
EnvironmentIdValue environmentId,
String name,
Collection<UUID> resourceGroups,
Collection<ResourceGroupId> resourceGroups,
Map<String, Object> parameters,
String defaultCiCdProfileShortName) throws InstantiatorException {
return executeRequestWithRetries(
Expand All @@ -79,7 +80,7 @@ public EnvironmentResponse update(
HttpUtils.buildPutRequest(
getEnvironmentsUri(environmentId),
sdkConfiguration,
serializeSafely(new UpdateEnvironmentRequest(managementEnvironmentId, name, resourceGroups, parameters, defaultCiCdProfileShortName))),
serializeSafely(new UpdateEnvironmentRequest(managementEnvironmentId, name, resourceGroups.stream().map(ResourceGroupId::toString).toList(), parameters, defaultCiCdProfileShortName))),
new int[]{200},
EnvironmentResponse.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
public record CreateEnvironmentRequest(
EnvironmentIdValue managementEnvironmentId,
String name,
Collection<UUID> resourceGroups,
Collection<String> resourceGroups,
Map<String, Object> parameters) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public record UpdateEnvironmentRequest(
EnvironmentIdValue managementEnvironmentId,
String name,
Collection<UUID> resourceGroups,
Collection<String> resourceGroups,
Map<String, Object> parameters,
String defaultCiCdProfileShortName) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

public record EnvironmentResponse(
EnvironmentIdDto managementEnvironmentId,
EnvironmentIdDto id,
String name,
Collection<UUID> resourceGroups,
Collection<String> resourceGroups,
Map<String, Object> parameters,
String defaultCiCdProfileShortName,
String status,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.yanchware.fractal.sdk.domain.values;

import com.fasterxml.jackson.annotation.JsonValue;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.UUID;

public record ResourceGroupId(
@NotNull ResourceGroupType resourceGroupType,
@NotNull UUID ownerId,
@NotNull String shortName)
{
public static ResourceGroupId fromString(String s) {
var rdIdArr = s.split("/");
if (rdIdArr.length != 3) {
throw new IllegalArgumentException("Invalid resource group id");
}

return new ResourceGroupId(ResourceGroupType.valueOf(rdIdArr[0].toUpperCase()), UUID.fromString(rdIdArr[1]), rdIdArr[2]);
}

@NotNull
@Override
@JsonValue
public String toString() {
return String.format("%s/%s/%s", resourceGroupType.getValue(), ownerId.toString(), shortName);
}

@Override
public boolean equals(Object o) {
if (o == this){
return true;
}
if (!(o instanceof ResourceGroupId)) {
return false;
}

ResourceGroupId other = (ResourceGroupId)o;
return this.resourceGroupType.getValue().equals(other.resourceGroupType.getValue())
&& this.ownerId.toString().equals(other.ownerId.toString())
&& this.shortName.equals(other.shortName);
}

@Override
public int hashCode() {
return Objects.hash(resourceGroupType.getValue(), ownerId.toString(), shortName);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.yanchware.fractal.sdk.domain.values;

import com.fasterxml.jackson.annotation.JsonValue;

public enum ResourceGroupType {
PERSONAL("Personal"),
ORGANIZATIONAL("Organizational");

private final String value;

ResourceGroupType(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}

@Override
public String toString() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.yanchware.fractal.sdk.domain.exceptions.InstantiatorException;
import com.yanchware.fractal.sdk.domain.livesystem.service.dtos.EnvironmentIdDto;
import com.yanchware.fractal.sdk.domain.livesystem.service.dtos.EnvironmentTypeDto;
import com.yanchware.fractal.sdk.domain.values.ResourceGroupId;
import org.junit.jupiter.api.Test;

import java.util.Date;
Expand All @@ -19,14 +20,14 @@ class EnvironmentAggregateTest {
public void noErrors_When_ManagingSecrets() throws InstantiatorException {
var mockedEnvironmentService = mock(EnvironmentService.class);
var aggregate = new EnvironmentAggregate(mockedEnvironmentService);

var ownerId = UUID.randomUUID();
var envId = new EnvironmentIdValue(
EnvironmentType.PERSONAL,
UUID.randomUUID(),
ownerId,
"production-001");
var managementEnvironment = ManagementEnvironment.builder()
.withId(envId)
.withResourceGroup(UUID.randomUUID())
.withResourceGroup(ResourceGroupId.fromString(String.format("Personal/%s/rg", ownerId)))
.build();

aggregate.setManagementEnvironment(managementEnvironment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.azure.AzureRegion;
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.gcp.GcpRegion;
import com.yanchware.fractal.sdk.domain.livesystem.paas.providers.oci.OciRegion;
import com.yanchware.fractal.sdk.domain.values.ResourceGroupId;
import com.yanchware.fractal.sdk.utils.LocalSdkConfiguration;
import com.yanchware.fractal.sdk.utils.StringHandler;
import io.github.resilience4j.retry.RetryRegistry;
Expand Down Expand Up @@ -41,14 +42,15 @@ void setUp(WireMockRuntimeInfo wmRuntimeInfo) {
.build();

var sdkConfiguration = new LocalSdkConfiguration(wmRuntimeInfo.getHttpBaseUrl());
var ownerId = UUID.randomUUID();
mockEnvironment = ManagementEnvironment.builder()
.withId(new EnvironmentIdValue(
EnvironmentType.PERSONAL,
UUID.randomUUID(),
ownerId,
"test-env"
))
.withName("Test Environment")
.withResourceGroup(UUID.randomUUID())
.withResourceGroup(ResourceGroupId.fromString(String.format("Personal/%s/rg", ownerId)))
.build();

environmentService = new RestEnvironmentService(httpClient, sdkConfiguration, RetryRegistry.ofDefaults());
Expand Down Expand Up @@ -102,7 +104,7 @@ void fetchEnvironment_should_ReturnEnvironmentIdDto() throws Exception {
assertThat(response.id().ownerId()).isEqualTo(mockEnvironment.getId().ownerId());
assertThat(response.id().shortName()).isEqualTo(mockEnvironment.getId().shortName());
assertThat(response.name()).isEqualTo(mockEnvironment.getName());
assertThat(response.resourceGroups()).containsExactlyElementsOf(mockEnvironment.getResourceGroups());
assertThat(response.resourceGroups()).containsExactlyElementsOf(mockEnvironment.getResourceGroups().stream().map(ResourceGroupId::toString).toList());
assertThat(response.parameters()).isEqualTo(mockEnvironment.toDto().parameters());

}
Expand Down
Loading
Loading