Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple client types for base implementations #149

Merged
merged 3 commits into from
Aug 1, 2024
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {

implementation enforcedPlatform('com.azure:azure-sdk-bom:1.2.25')
implementation 'com.azure.resourcemanager:azure-resourcemanager:2.40.0'
implementation 'com.azure.resourcemanager:azure-resourcemanager-communication:2.1.0'
implementation 'com.azure:azure-security-keyvault-certificates'
implementation 'com.azure:azure-security-keyvault-keys'
implementation 'com.azure:azure-security-keyvault-secrets'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gyro/azure/AbstractAzureCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public AzureResourceManager getResourceManagerClient() {
getCredential()));
}

return AzureResource.createClient((AzureCredentials) credentials);
return AzureResource.createClient(AzureResourceManager.class, (AzureCredentials) credentials);
}

public TokenCredential getTokenCredential() {
Expand Down
57 changes: 45 additions & 12 deletions src/main/java/gyro/azure/AzureCredentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.communication.CommunicationManager;
import com.psddev.dari.util.ObjectUtils;
import com.psddev.dari.util.StringUtils;
import gyro.core.GyroException;
Expand Down Expand Up @@ -60,7 +62,7 @@ public void setLogLevel(String logLevel) {
this.logLevel = logLevel;
}

public AzureResourceManager createClient() {
public <T> T createClient(Class<T> clientClass) {
Properties properties;

try (InputStream input = openInput(getCredentialFilePath())) {
Expand All @@ -80,21 +82,52 @@ public AzureResourceManager createClient() {

String subscription = ObjectUtils.to(String.class, properties.get("subscription"));

AzureProfile azureProfile = new AzureProfile(tenant, subscription, com.azure.core.management.AzureEnvironment.AZURE);
AzureProfile azureProfile = new AzureProfile(tenant, subscription, AzureEnvironment.AZURE);

try {
AzureResourceManager.Authenticated authenticated = AzureResourceManager
.configure()
.withHttpClient(new OkHttpAsyncHttpClientBuilder().build())
.authenticate(credential, azureProfile);
if (clientClass.getSimpleName().equals("CommunicationManager")) {
try {
CommunicationManager client = CommunicationManager
.configure()
.withHttpClient(new OkHttpAsyncHttpClientBuilder().build())
.authenticate(credential, azureProfile);

if (clientClass.isInstance(client)) {
return clientClass.cast(client);
}

return StringUtils.isBlank(subscription)
? authenticated.withDefaultSubscription()
: authenticated.withSubscription(subscription);
throw new GyroException(
String.format("Unable to create %s client", clientClass.getSimpleName()));

} catch (Exception error) {
throw new GyroException(error.getMessage(), error);
} catch (Exception error) {
throw new GyroException(error.getMessage(), error);
}

} else if (clientClass.getSimpleName().equals("AzureResourceManager")) {
try {
AzureResourceManager.Authenticated authenticated = AzureResourceManager
.configure()
.withHttpClient(new OkHttpAsyncHttpClientBuilder().build())
.authenticate(credential, azureProfile);


AzureResourceManager client = StringUtils.isBlank(subscription)
? authenticated.withDefaultSubscription()
: authenticated.withSubscription(subscription);

if (clientClass.isInstance(client)) {
return clientClass.cast(client);
}

throw new GyroException(
String.format("Unable to create %s client", clientClass.getSimpleName()));

} catch (Exception error) {
throw new GyroException(error.getMessage(), error);
}

} else {
throw new UnsupportedOperationException(
String.format("The following client type is not available: %s", clientClass.getSimpleName()));
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/gyro/azure/AzureFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import java.util.stream.Collectors;

import com.azure.core.credential.TokenCredential;
import com.azure.resourcemanager.AzureResourceManager;
import com.psddev.dari.util.TypeDefinition;
import gyro.core.finder.Finder;

public abstract class AzureFinder<M, R extends AzureResource> extends Finder<R> {
protected abstract List<M> findAllAzure(AzureResourceManager client);
public abstract class AzureFinder<C, M, R extends AzureResource> extends Finder<R> {
protected abstract List<M> findAllAzure(C client);

protected abstract List<M> findAzure(AzureResourceManager client, Map<String, String> filters);
protected abstract List<M> findAzure(C client, Map<String, String> filters);

@Override
public List<R> find(Map<String, Object> filters) {
Expand All @@ -44,8 +44,12 @@ public List<R> findAll() {
.collect(Collectors.toList());
}

private AzureResourceManager newClient() {
return AzureResource.createClient(credentials(AzureCredentials.class));
private C newClient() {
@SuppressWarnings("unchecked")
Class<C> clientClass = (Class<C>) TypeDefinition.getInstance(getClass())
.getInferredGenericTypeArgumentClass(AzureFinder.class, 0);

return AzureResource.createClient(clientClass, credentials(AzureCredentials.class));
}

protected TokenCredential getTokenCredential() {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/gyro/azure/AzureResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.azure.core.credential.TokenCredential;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.communication.CommunicationManager;
import gyro.core.resource.Resource;

public abstract class AzureResource extends Resource {
Expand All @@ -26,12 +27,12 @@ protected String getRegion() {
return credentials(AzureCredentials.class).getRegion();
}

public static AzureResourceManager createClient(AzureCredentials credentials) {
return credentials.createClient();
public static <T> T createClient(Class<T> clientClass, AzureCredentials credentials) {
return credentials.createClient(clientClass);
}

protected AzureResourceManager createClient() {
return AzureResource.createClient(credentials(AzureCredentials.class));
protected <T> T createClient(Class<T> clientClass) {
return AzureResource.createClient(clientClass, credentials(AzureCredentials.class));
}

public static TokenCredential getTokenCredential(AzureCredentials credentials) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
@Type("active-directory-group")
public class ActiveDirectoryGroupFinder
extends AzureFinder<ActiveDirectoryGroup, ActiveDirectoryGroupResource> {
extends AzureFinder<AzureResourceManager, ActiveDirectoryGroup, ActiveDirectoryGroupResource> {

private String name;
private String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void copyFrom(ActiveDirectoryGroup group) {

@Override
public boolean refresh() {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ActiveDirectoryGroup group = client.accessManagement().activeDirectoryGroups().getById(getId());

Expand All @@ -110,7 +110,7 @@ public boolean refresh() {

@Override
public void create(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ActiveDirectoryGroup group = client.accessManagement().activeDirectoryGroups()
.define(getName())
Expand All @@ -127,7 +127,7 @@ public void update(GyroUI ui, State state, Resource current, Set<String> changed

@Override
public void delete(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

client.accessManagement().activeDirectoryGroups().deleteById(getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
@Type("active-directory-user")
public class ActiveDirectoryUserFinder
extends AzureFinder<ActiveDirectoryUser, ActiveDirectoryUserResource> {
extends AzureFinder<AzureResourceManager, ActiveDirectoryUser, ActiveDirectoryUserResource> {

private String id;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void copyFrom(ActiveDirectoryUser user) {

@Override
public boolean refresh() {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ActiveDirectoryUser user = client.accessManagement().activeDirectoryUsers().getById(getId());

Expand All @@ -156,7 +156,7 @@ public boolean refresh() {

@Override
public void create(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ActiveDirectoryUser activeDirectoryUser = client.accessManagement().activeDirectoryUsers()
.define(getName())
Expand All @@ -175,7 +175,7 @@ public void update(GyroUI ui, State state, Resource current, Set<String> changed

@Override
public void delete(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

client.accessManagement().activeDirectoryUsers().deleteById(getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* application: $(external-query azure::application {})
*/
@Type("application")
public class ApplicationFinder extends AzureFinder<ActiveDirectoryApplication, ApplicationResource> {
public class ApplicationFinder extends AzureFinder<AzureResourceManager, ActiveDirectoryApplication, ApplicationResource> {

private String id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void copyFrom(ActiveDirectoryApplication model) {

@Override
public boolean refresh() {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ActiveDirectoryApplication application = client.accessManagement()
.activeDirectoryApplications()
Expand All @@ -188,7 +188,7 @@ public boolean refresh() {

@Override
public void create(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ActiveDirectoryApplication.DefinitionStages.WithCreate withCreate = client.accessManagement()
.activeDirectoryApplications()
Expand All @@ -215,7 +215,7 @@ public void create(GyroUI ui, State state) throws Exception {
@Override
public void update(
GyroUI ui, State state, Resource current, Set<String> changedFieldNames) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ActiveDirectoryApplication application = client.accessManagement()
.activeDirectoryApplications()
Expand Down Expand Up @@ -258,7 +258,7 @@ public void update(

@Override
public void delete(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

client.accessManagement().activeDirectoryApplications().deleteById(getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void copyFrom(RoleAssignment roleAssignment) {
setScope(roleAssignment.scope());
setId(roleAssignment.id());

AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);
setRole(client.accessManagement().roleDefinitions().getById(roleAssignment.roleDefinitionId()).roleName());

ActiveDirectoryUser user = null;
Expand Down Expand Up @@ -180,7 +180,7 @@ public void copyFrom(RoleAssignment roleAssignment) {

@Override
public boolean refresh() {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

RoleAssignment roleAssignment = client.accessManagement().roleAssignments().getByScope(getScope(), getName());

Expand All @@ -195,7 +195,7 @@ public boolean refresh() {

@Override
public void create(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

if (Stream.of(getPrincipalId(), getUser(), getGroup()).filter(Objects::nonNull).count() > 1) {
throw new GyroException("Only one of 'principal-id' or 'user' or 'group' is allowed.");
Expand Down Expand Up @@ -254,7 +254,7 @@ public void update(GyroUI ui, State state, Resource current, Set<String> changed

@Override
public void delete(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

client.accessManagement().roleAssignments().deleteById(getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* service-principal: $(external-query azure::service-principal {})
*/
@Type("service-principal")
public class ServicePrincipalFinder extends AzureFinder<ServicePrincipal, ServicePrincipalResource> {
public class ServicePrincipalFinder extends AzureFinder<AzureResourceManager, ServicePrincipal, ServicePrincipalResource> {

private String id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void copyFrom(ServicePrincipal model) {

@Override
public boolean refresh() {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ServicePrincipal servicePrincipal = client.accessManagement().servicePrincipals().getByName(getName());

Expand All @@ -113,7 +113,7 @@ public boolean refresh() {

@Override
public void create(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

ServicePrincipal servicePrincipal = client.accessManagement().servicePrincipals()
.define(getName())
Expand All @@ -131,7 +131,7 @@ public void update(

@Override
public void delete(GyroUI ui, State state) throws Exception {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

client.accessManagement().servicePrincipals().deleteById(getId());
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/gyro/azure/cdn/CdnEndpointResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public boolean refresh() {

@Override
public void create(GyroUI ui, State state) {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

CdnProfileResource parent = (CdnProfileResource) parent();

Expand Down Expand Up @@ -432,7 +432,7 @@ public void create(GyroUI ui, State state) {

@Override
public void update(GyroUI ui, State state, Resource current, Set<String> changedProperties) {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

CdnProfileResource parent = (CdnProfileResource) parent();

Expand Down Expand Up @@ -524,7 +524,7 @@ public void update(GyroUI ui, State state, Resource current, Set<String> changed

@Override
public void delete(GyroUI ui, State state) {
AzureResourceManager client = createClient();
AzureResourceManager client = createClient(AzureResourceManager.class);

CdnProfileResource parent = (CdnProfileResource) parent();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gyro/azure/cdn/CdnProfileFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* cdn-profile: $(external-query azure::cdn-profile {})
*/
@Type("cdn-profile")
public class CdnProfileFinder extends AzureFinder<CdnProfile, CdnProfileResource> {
public class CdnProfileFinder extends AzureFinder<AzureResourceManager, CdnProfile, CdnProfileResource> {

private String id;

Expand Down
Loading
Loading