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

Add support for communication and email services #148

Closed
wants to merge 3 commits into from
Closed
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
54 changes: 54 additions & 0 deletions examples/communication/communication-service.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
azure::resource-group resource-group-example
name: "resource-group-example-test"

tags: {
Name: "resource-group-example-test"
}
end

azure::identity identity-example
name: "identity-example-test"
resource-group: $(azure::resource-group resource-group-example)

tags: {
Name: "identity-example-test"
}
end

azure::email-service email-service-example
resource-group: $(azure::resource-group resource-group-example)
name: "example-email-test"
data-location: "United States"

tags: {
Name: "example-email-test"
}
end

azure::domain domain-example
resource-group: $(azure::resource-group resource-group-example)
email-service: $(azure::email-service email-service-example)
domain-management: "CustomerManaged"
name: "cloud.brightspot.dev"

tags: {
"example": "example"
}
end

azure::communication-service service-example
resource-group: $(azure::resource-group resource-group-example)
name: "service-example-test"
data-location: "United States"
domains: [
$(azure::domain domain-example)
]

identity
user-assigned-identity: [$(azure::identity identity-example)]
end

tags: {
Name: "service-example-test"
}
end
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
53 changes: 41 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,48 @@ 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 {
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);
}
}
}

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
15 changes: 12 additions & 3 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,20 @@ 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 <T> T createClient(Class<T> clientClass) {
return AzureResource.createClient(clientClass, credentials(AzureCredentials.class));
}

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

protected CommunicationManager createCommunicationClient() {
return AzureResource.createClient(CommunicationManager.class, 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 @@ -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 @@ -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 @@ -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
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
106 changes: 106 additions & 0 deletions src/main/java/gyro/azure/communication/CommunicationServiceFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2024, Perfect Sense, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gyro.azure.communication;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.azure.resourcemanager.communication.CommunicationManager;
import gyro.azure.AzureFinder;
import gyro.core.Type;

/**
* Query communication service.
*
* Example
* -------
*
* .. code-block:: gyro
*
* service: $(external-query azure::communication-service {})
*/
@Type("communication-service")
public class CommunicationServiceFinder extends
AzureFinder<CommunicationManager, com.azure.resourcemanager.communication.models.CommunicationServiceResource, CommunicationServiceResource> {

private String resourceGroup;
private String name;
private String id;

/**
* The resource group of the service
*/
public String getResourceGroup() {
return resourceGroup;
}

public void setResourceGroup(String resourceGroup) {
this.resourceGroup = resourceGroup;
}

/**
* The communication service
*/
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

/**
* The ID of the service
*/
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
protected List<com.azure.resourcemanager.communication.models.CommunicationServiceResource> findAllAzure(
CommunicationManager client) {
return client.communicationServices().list().stream().collect(Collectors.toList());
}

@Override
protected List<com.azure.resourcemanager.communication.models.CommunicationServiceResource> findAzure(
CommunicationManager client, Map<String, String> filters) {


if (filters.containsKey("id")) {
return Collections.singletonList(client.communicationServices().getById(filters.get("id")));
}

if (filters.containsKey("resource-group")) {
if (filters.containsKey("name")) {
return Collections.singletonList(client.communicationServices()
.getByResourceGroup(filters.get("resource-group"), filters.get("name")));
}

return client.communicationServices().listByResourceGroup(filters.get("resource-group")).stream()
.collect(Collectors.toList());
}

return Collections.emptyList();
}
}
Loading
Loading