Skip to content

Commit

Permalink
Merge pull request #220 from laDok8/refractor
Browse files Browse the repository at this point in the history
refractor
  • Loading branch information
istraka authored Apr 18, 2024
2 parents 3f1bb55 + 24ea096 commit c86eec8
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sunstone.aws.annotation.AwsAutoResolve;
import sunstone.aws.annotation.AwsEc2Instance;
import sunstone.aws.annotation.AwsRds;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;
import sunstone.core.exceptions.IllegalArgumentSunstoneException;
import sunstone.core.exceptions.SunstoneCloudResourceException;
import sunstone.core.exceptions.SunstoneException;
Expand Down Expand Up @@ -76,8 +76,8 @@ <T> T get(Annotation injectionAnnotation, AwsSunstoneStore store, Class<T> clazz
getRepresentedInjectionAnnotation().getName(), injectionAnnotation.annotationType().getName()));
}
AwsEc2Instance vm = (AwsEc2Instance) injectionAnnotation;
String vmNameTag = SunstoneConfig.resolveExpressionToString(vm.nameTag());
String region = SunstoneConfig.resolveExpressionToString(vm.region());
String vmNameTag = SunstoneConfigResolver.resolveExpressionToString(vm.nameTag());
String region = SunstoneConfigResolver.resolveExpressionToString(vm.region());
Optional<Instance> awsEc2 = AwsUtils.findEc2InstanceByNameTag(store.getAwsEc2ClientOrCreate(region), vmNameTag);
return clazz.cast(awsEc2.orElseThrow(() -> new SunstoneCloudResourceException(format("Unable to find '%s' AWS EC2 instance in '%s' region.", vmNameTag, region))));
}
Expand All @@ -98,8 +98,8 @@ <T> T get(Annotation injectionAnnotation, AwsSunstoneStore store, Class<T> clazz
getRepresentedInjectionAnnotation().getName(), injectionAnnotation.annotationType().getName()));
}
AwsRds rds = (AwsRds) injectionAnnotation;
String vmNameTag = SunstoneConfig.resolveExpressionToString(rds.name());
String region = SunstoneConfig.resolveExpressionToString(rds.region());
String vmNameTag = SunstoneConfigResolver.resolveExpressionToString(rds.name());
String region = SunstoneConfigResolver.resolveExpressionToString(rds.region());
Optional<DBInstance> awsRds = AwsUtils.findRdsInstanceByNameTag(store.getAwsRdsClientOrCreate(region), vmNameTag);
return clazz.cast(awsRds.orElseThrow(() -> new SunstoneCloudResourceException(format("Unable to find '%s' AWS RDS instance in '%s' region.", vmNameTag, region))));
}
Expand Down
6 changes: 3 additions & 3 deletions aws/src/main/java/sunstone/aws/impl/AwsSunstoneDeployer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sunstone.aws.annotation.WithAwsCfTemplate;
import sunstone.aws.annotation.WithAwsCfTemplateRepeatable;
import sunstone.core.AbstractSunstoneCloudDeployer;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;
import sunstone.core.SunstoneExtension;
import sunstone.core.exceptions.IllegalArgumentSunstoneException;
import sunstone.core.exceptions.SunstoneException;
Expand Down Expand Up @@ -53,9 +53,9 @@ private void deployCfTemplate(WithAwsCfTemplate awsTemplateDefinition, AwsSunsto
AwsCloudFormationCloudDeploymentManager deploymentManager = store.getAwsCfDemploymentManagerOrCreate();

try {
String content = getResourceContent(SunstoneConfig.resolveExpressionToString(awsTemplateDefinition.template()));
String content = getResourceContent(SunstoneConfigResolver.resolveExpressionToString(awsTemplateDefinition.template()));
Map<String, String> parameters = getParameters(awsTemplateDefinition.parameters());
String region = SunstoneConfig.resolveExpressionToString(awsTemplateDefinition.region());
String region = SunstoneConfigResolver.resolveExpressionToString(awsTemplateDefinition.region());
if (region == null) {
throw new IllegalArgumentSunstoneException("Region for AWS template is not defined. It must be specified either "
+ "in the annotation or as Sunstone Config property.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.utils.SdkAutoCloseable;
import sunstone.aws.annotation.AwsAutoResolve;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;
import sunstone.core.api.SunstoneResourceInjector;
import sunstone.core.exceptions.IllegalArgumentSunstoneException;
import sunstone.core.exceptions.SunstoneException;
Expand Down Expand Up @@ -46,7 +46,7 @@ static Ec2Client resolveEc2ClientDI(AwsIdentifiableSunstoneResource.Identificati
Ec2Client client;
if (identification.type == AwsIdentifiableSunstoneResource.AUTO) {
AwsAutoResolve annotation = (AwsAutoResolve) identification.identification;
client = AwsUtils.getEC2Client(SunstoneConfig.resolveExpressionToString(annotation.region()));
client = AwsUtils.getEC2Client(SunstoneConfigResolver.resolveExpressionToString(annotation.region()));
} else {
throw new UnsupportedSunstoneOperationException("EC2 Client may be injected only with " + AwsIdentifiableSunstoneResource.AUTO);
}
Expand All @@ -57,7 +57,7 @@ static S3Client resolveS3ClientDI(AwsIdentifiableSunstoneResource.Identification
S3Client client;
if (identification.type == AwsIdentifiableSunstoneResource.AUTO) {
AwsAutoResolve annotation = (AwsAutoResolve) identification.identification;
client = AwsUtils.getS3Client(SunstoneConfig.resolveExpressionToString(annotation.region()));
client = AwsUtils.getS3Client(SunstoneConfigResolver.resolveExpressionToString(annotation.region()));
} else {
throw new UnsupportedSunstoneOperationException("EC2 Client may be injected only with " + AwsIdentifiableSunstoneResource.AUTO);
}
Expand All @@ -69,7 +69,7 @@ private RdsClient resolveRdsClientDI(AwsIdentifiableSunstoneResource.Identificat
RdsClient client;
if (identification.type == AwsIdentifiableSunstoneResource.AUTO) {
AwsAutoResolve annotation = (AwsAutoResolve) identification.identification;
client = AwsUtils.getRdsClient(SunstoneConfig.resolveExpressionToString(annotation.region()));
client = AwsUtils.getRdsClient(SunstoneConfigResolver.resolveExpressionToString(annotation.region()));
} else {
throw new UnsupportedSunstoneOperationException("RDS Client may be injected only with " + AwsIdentifiableSunstoneResource.AUTO);
}
Expand Down
8 changes: 4 additions & 4 deletions aws/src/main/java/sunstone/aws/impl/AwsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
import software.amazon.awssdk.services.rds.model.DescribeDbInstancesResponse;
import software.amazon.awssdk.services.s3.S3Client;

import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;

import java.util.Optional;

class AwsUtils {

private static AwsCredentialsProvider getCredentialsProvider() {
AwsBasicCredentials credentials = AwsBasicCredentials.create(SunstoneConfig.getString(AwsConfig.ACCESS_KEY_ID), SunstoneConfig.getString(AwsConfig.SECRET_ACCESS_KEY));
AwsBasicCredentials credentials = AwsBasicCredentials.create(SunstoneConfigResolver.getString(AwsConfig.ACCESS_KEY_ID), SunstoneConfigResolver.getString(AwsConfig.SECRET_ACCESS_KEY));
return StaticCredentialsProvider.create(credentials);
}

static boolean propertiesForAwsClientArePresent() {
return SunstoneConfig.unwrap().isPropertyPresent(AwsConfig.ACCESS_KEY_ID)
&& SunstoneConfig.unwrap().isPropertyPresent(AwsConfig.SECRET_ACCESS_KEY);
return SunstoneConfigResolver.unwrap().isPropertyPresent(AwsConfig.ACCESS_KEY_ID)
&& SunstoneConfigResolver.unwrap().isPropertyPresent(AwsConfig.SECRET_ACCESS_KEY);
}

static Region getAndCheckRegion(String regionStr) {
Expand Down
6 changes: 3 additions & 3 deletions aws/src/test/java/aws/cloudformation/AwsTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import software.amazon.awssdk.services.ec2.model.Filter;
import software.amazon.awssdk.services.ec2.model.KeyPairInfo;
import sunstone.aws.impl.AwsConfig;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;

import java.util.List;


public class AwsTestUtils {

private static AwsCredentialsProvider getCredentialsProvider() {
AwsBasicCredentials credentials = AwsBasicCredentials.create(SunstoneConfig.getString(AwsConfig.ACCESS_KEY_ID), SunstoneConfig.getString(AwsConfig.SECRET_ACCESS_KEY));
AwsBasicCredentials credentials = AwsBasicCredentials.create(SunstoneConfigResolver.getString(AwsConfig.ACCESS_KEY_ID), SunstoneConfigResolver.getString(AwsConfig.SECRET_ACCESS_KEY));
return StaticCredentialsProvider.create(credentials);
}

Expand All @@ -33,7 +33,7 @@ static Region getAndCheckRegion(String regionStr) {

public static Ec2Client getEC2Client() {
Ec2Client ec2Client = Ec2Client.builder()
.region(Region.of(SunstoneConfig.getString(AwsConfig.REGION)))
.region(Region.of(SunstoneConfigResolver.getString(AwsConfig.REGION)))
.credentialsProvider(getCredentialsProvider())
.build();
return ec2Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Set;
import java.util.UUID;

import static sunstone.core.SunstoneConfig.getValue;
import static sunstone.core.SunstoneConfigResolver.getValue;

/**
* Purpose: the class handles Azure template - deploy and undeploy the template to and from a stack.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sunstone.azure.annotation.AzurePgSqlServer;
import sunstone.azure.annotation.AzureVirtualMachine;
import sunstone.azure.annotation.AzureWebApplication;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;
import sunstone.core.exceptions.IllegalArgumentSunstoneException;
import sunstone.core.exceptions.SunstoneCloudResourceException;
import sunstone.core.exceptions.SunstoneException;
Expand Down Expand Up @@ -78,8 +78,8 @@ <T> T get(Annotation injectionAnnotation, AzureSunstoneStore store, Class<T> cla
getRepresentedInjectionAnnotation().getName(), injectionAnnotation.annotationType().getName()));
}
AzureVirtualMachine vm = (AzureVirtualMachine) injectionAnnotation;
String vmName = SunstoneConfig.resolveExpressionToString(vm.name());
String vmGroup = SunstoneConfig.resolveExpressionToString(vm.group());
String vmName = SunstoneConfigResolver.resolveExpressionToString(vm.name());
String vmGroup = SunstoneConfigResolver.resolveExpressionToString(vm.group());
Optional<VirtualMachine> azureVM = AzureUtils.findAzureVM(store.getAzureArmClientOrCreate(), vmName, vmGroup);
return clazz.cast(azureVM.orElseThrow(() -> new SunstoneCloudResourceException(format("Unable to find '%s' Azure VM in '%s' resource group.", vmName, vmGroup))));
}
Expand All @@ -106,8 +106,8 @@ <T> T get(Annotation injectionAnnotation, AzureSunstoneStore store, Class<T> cla
getRepresentedInjectionAnnotation().getName(), injectionAnnotation.annotationType().getName()));
}
AzureWebApplication webApp = (AzureWebApplication) injectionAnnotation;
String appName = SunstoneConfig.resolveExpressionToString(webApp.name());
String appGroup = SunstoneConfig.resolveExpressionToString(webApp.group());
String appName = SunstoneConfigResolver.resolveExpressionToString(webApp.name());
String appGroup = SunstoneConfigResolver.resolveExpressionToString(webApp.group());
Optional<WebApp> azureWebApp = AzureUtils.findAzureWebApp(store.getAzureArmClientOrCreate(), appName, appGroup);
return clazz.cast(azureWebApp.orElseThrow(() -> new SunstoneCloudResourceException(format("Unable to find '%s' Azure Web App in '%s' resource group.", appName, appGroup))));
}
Expand All @@ -133,8 +133,8 @@ <T> T get(Annotation injectionAnnotation, AzureSunstoneStore store, Class<T> cla
getRepresentedInjectionAnnotation().getName(), injectionAnnotation.annotationType().getName()));
}
AzureAppServicePlan plan = (AzureAppServicePlan) injectionAnnotation;
String planName = SunstoneConfig.resolveExpressionToString(plan.name());
String planGroup = SunstoneConfig.resolveExpressionToString(plan.group());
String planName = SunstoneConfigResolver.resolveExpressionToString(plan.name());
String planGroup = SunstoneConfigResolver.resolveExpressionToString(plan.group());
Optional<AppServicePlan> azureWebApp = AzureUtils.findAzureAppServicePlan(store.getAzureArmClientOrCreate(), planName, planGroup);
return clazz.cast(azureWebApp.orElseThrow(() -> new SunstoneCloudResourceException(format("Unable to find '%s' Azure App Service plan in '%s' resource group.", planName, planGroup))));
}
Expand All @@ -159,8 +159,8 @@ <T> T get(Annotation injectionAnnotation, AzureSunstoneStore store, Class<T> cla
getRepresentedInjectionAnnotation().getName(), injectionAnnotation.annotationType().getName()));
}
AzurePgSqlServer pgsqlServer = (AzurePgSqlServer) injectionAnnotation;
String serverName = SunstoneConfig.resolveExpressionToString(pgsqlServer.name());
String serverGroup = SunstoneConfig.resolveExpressionToString(pgsqlServer.group());
String serverName = SunstoneConfigResolver.resolveExpressionToString(pgsqlServer.name());
String serverGroup = SunstoneConfigResolver.resolveExpressionToString(pgsqlServer.group());
Optional<Server> azureWebApp = AzureUtils.findAzurePgSqlServer(store.getAzurePgSqlManagerOrCreate(), serverName, serverGroup);
return clazz.cast(azureWebApp.orElseThrow(() -> new SunstoneCloudResourceException(format("Unable to find '%s' Azure PostgreSql Server in '%s' resource group.", serverName, serverGroup))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sunstone.azure.annotation.WithAzureArmTemplate;
import sunstone.azure.annotation.WithAzureArmTemplateRepeatable;
import sunstone.core.AbstractSunstoneCloudDeployer;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;
import sunstone.core.SunstoneExtension;
import sunstone.core.exceptions.IllegalArgumentSunstoneException;
import sunstone.core.exceptions.SunstoneException;
Expand Down Expand Up @@ -51,13 +51,13 @@ private void deployArmTemplate(WithAzureArmTemplate armTemplateDefinition, Azure

String content = null;
try {
content = getResourceContent(SunstoneConfig.resolveExpressionToString(armTemplateDefinition.template()));
String group = SunstoneConfig.resolveExpressionToString(armTemplateDefinition.group());
content = getResourceContent(SunstoneConfigResolver.resolveExpressionToString(armTemplateDefinition.template()));
String group = SunstoneConfigResolver.resolveExpressionToString(armTemplateDefinition.group());
if (group == null) {
throw new IllegalArgumentSunstoneException("Resource group for Azure ARM template is not defined. "
+ "It must be specified either in the annotation or as Sunstone Config property.");
}
String region = SunstoneConfig.resolveExpressionToString(armTemplateDefinition.region());
String region = SunstoneConfigResolver.resolveExpressionToString(armTemplateDefinition.region());
if (region == null) {
throw new IllegalArgumentSunstoneException("Region for Azure ARM template is not defined. It must be specified either "
+ "in the annotation or as Sunstone Config property.");
Expand Down
22 changes: 11 additions & 11 deletions azure/src/main/java/sunstone/azure/impl/AzureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.azure.resourcemanager.postgresqlflexibleserver.PostgreSqlManager;
import com.azure.resourcemanager.postgresqlflexibleserver.models.Server;
import org.slf4j.Logger;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -26,33 +26,33 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static sunstone.core.SunstoneConfig.getValue;
import static sunstone.core.SunstoneConfigResolver.getValue;

public class AzureUtils {
static Logger LOGGER = AzureLogger.DEFAULT;
static AzureResourceManager getResourceManager() {
return AzureResourceManager
.authenticate(getCredentials(), new AzureProfile(AzureEnvironment.AZURE))
.withSubscription(SunstoneConfig.getString(AzureConfig.SUBSCRIPTION_ID));
.withSubscription(SunstoneConfigResolver.getString(AzureConfig.SUBSCRIPTION_ID));
}
static PostgreSqlManager getPgsqlManager() {
return PostgreSqlManager
.authenticate(getCredentials(), new AzureProfile(SunstoneConfig.getString(AzureConfig.TENANT_ID), SunstoneConfig.getString(AzureConfig.SUBSCRIPTION_ID), AzureEnvironment.AZURE));
.authenticate(getCredentials(), new AzureProfile(SunstoneConfigResolver.getString(AzureConfig.TENANT_ID), SunstoneConfigResolver.getString(AzureConfig.SUBSCRIPTION_ID), AzureEnvironment.AZURE));
}

private static TokenCredential getCredentials() {
return new ClientSecretCredentialBuilder()
.tenantId(SunstoneConfig.getString(AzureConfig.TENANT_ID))
.clientId(SunstoneConfig.getString(AzureConfig.APPLICATION_ID))
.clientSecret(SunstoneConfig.getString(AzureConfig.PASSWORD))
.tenantId(SunstoneConfigResolver.getString(AzureConfig.TENANT_ID))
.clientId(SunstoneConfigResolver.getString(AzureConfig.APPLICATION_ID))
.clientSecret(SunstoneConfigResolver.getString(AzureConfig.PASSWORD))
.build();
}

static boolean propertiesForArmClientArePresent() {
return SunstoneConfig.unwrap().isPropertyPresent(AzureConfig.SUBSCRIPTION_ID)
&& SunstoneConfig.unwrap().isPropertyPresent(AzureConfig.TENANT_ID)
&& SunstoneConfig.unwrap().isPropertyPresent(AzureConfig.APPLICATION_ID)
&& SunstoneConfig.unwrap().isPropertyPresent(AzureConfig.PASSWORD);
return SunstoneConfigResolver.unwrap().isPropertyPresent(AzureConfig.SUBSCRIPTION_ID)
&& SunstoneConfigResolver.unwrap().isPropertyPresent(AzureConfig.TENANT_ID)
&& SunstoneConfigResolver.unwrap().isPropertyPresent(AzureConfig.APPLICATION_ID)
&& SunstoneConfigResolver.unwrap().isPropertyPresent(AzureConfig.PASSWORD);
}

static Optional<VirtualMachine> findAzureVM(AzureResourceManager arm, String name, String resourceGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import sunstone.azure.impl.AzureConfig;
import sunstone.core.SunstoneConfig;
import sunstone.core.SunstoneConfigResolver;

public class AzureTestUtils {
public static AzureResourceManager getResourceManager() {
return AzureResourceManager
.authenticate(getCredentials(), new AzureProfile(AzureEnvironment.AZURE))
.withSubscription(SunstoneConfig.getString(AzureConfig.SUBSCRIPTION_ID));
.withSubscription(SunstoneConfigResolver.getString(AzureConfig.SUBSCRIPTION_ID));
}

private static TokenCredential getCredentials() {
return new ClientSecretCredentialBuilder()
.tenantId(SunstoneConfig.getString(AzureConfig.TENANT_ID))
.clientId(SunstoneConfig.getString(AzureConfig.APPLICATION_ID))
.clientSecret(SunstoneConfig.getString(AzureConfig.PASSWORD))
.tenantId(SunstoneConfigResolver.getString(AzureConfig.TENANT_ID))
.clientId(SunstoneConfigResolver.getString(AzureConfig.APPLICATION_ID))
.clientSecret(SunstoneConfigResolver.getString(AzureConfig.PASSWORD))
.build();
}
}
Loading

0 comments on commit c86eec8

Please sign in to comment.