use of com.vmware.xenon.common.ServiceErrorResponse in project photon-model by vmware.
the class VSphereEndpointAdapterService method doValidate.
private void doValidate(AuthCredentialsServiceState credentials, BiConsumer<ServiceErrorResponse, Throwable> callback, URI adapterManagementUri, String id) {
BasicConnection connection = createConnection(adapterManagementUri, credentials);
try {
// login and session creation
connection.connect();
if (id != null && !id.isEmpty()) {
// if a datacenter is configured also validate moref is OK
new GetMoRef(connection).entityProp(VimUtils.convertStringToMoRef(id), VimNames.PROPERTY_NAME);
}
callback.accept(null, null);
} catch (RuntimeFaultFaultMsg | InvalidPropertyFaultMsg | IllegalArgumentException e) {
ServiceErrorResponse r = Utils.toServiceErrorResponse(e);
r.statusCode = STATUS_CODE_BAD_REQUEST;
r.message = String.format("Error looking for datacenter for id '%s'", id);
callback.accept(r, e);
} catch (ConnectionException e) {
String msg = String.format("Cannot establish connection to %s", adapterManagementUri);
logWarning(msg);
callback.accept(null, e);
} finally {
closeQuietly(connection);
}
}
use of com.vmware.xenon.common.ServiceErrorResponse in project photon-model by vmware.
the class AzureInstanceService method validateAzureCredentials.
/**
* Validates azure credential by making an API call.
*/
private void validateAzureCredentials(final AzureInstanceContext ctx) {
if (ctx.computeRequest.isMockRequest) {
ctx.operation.complete();
return;
}
SubscriptionClientImpl subscriptionClient = new SubscriptionClientImpl(ctx.azureSdkClients.credentials);
subscriptionClient.subscriptions().getAsync(ctx.endpointAuth.userLink, new ServiceCallback<SubscriptionInner>() {
@Override
public void failure(Throwable e) {
// Azure doesn't send us any meaningful status code to work with
ServiceErrorResponse rsp = new ServiceErrorResponse();
rsp.message = "Invalid Azure credentials";
rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
ctx.operation.fail(e, rsp);
}
@Override
public void success(SubscriptionInner result) {
logFine(() -> String.format("Got subscription %s with id %s", result.displayName(), result.id()));
ctx.operation.complete();
}
});
}
use of com.vmware.xenon.common.ServiceErrorResponse in project photon-model by vmware.
the class AzureEndpointAdapterService method validate.
private BiConsumer<AuthCredentialsServiceState, BiConsumer<ServiceErrorResponse, Throwable>> validate(EndpointConfigRequest body) {
return (credentials, callback) -> {
try {
Boolean shouldProvision = Boolean.parseBoolean(body.endpointProperties.get(AZURE_PROVISIONING_PERMISSION));
validateEndpointUniqueness(credentials, body.checkForEndpointUniqueness, body.tenantLinks).thenCompose(aVoid -> validateCredentials(credentials)).thenCompose(subscription -> getPermissions(credentials)).thenCompose(permList -> verifyPermissions(permList, shouldProvision)).whenComplete((aVoid, e) -> {
if (e == null) {
callback.accept(null, null);
return;
}
if (e instanceof CompletionException) {
e = e.getCause();
}
final LocalizableValidationException localizableExc;
if (e instanceof LocalizableValidationException) {
localizableExc = (LocalizableValidationException) e;
} else {
// Azure doesn't send us any meaningful status code to work with
localizableExc = new LocalizableValidationException(e, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE);
}
ServiceErrorResponse rsp = Utils.toServiceErrorResponse(localizableExc);
rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
callback.accept(rsp, localizableExc);
});
} catch (Throwable e) {
logSevere(e);
ServiceErrorResponse rsp = new ServiceErrorResponse();
rsp.message = "Invalid Azure credentials";
rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
callback.accept(rsp, e);
}
};
}
Aggregations