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

SPOTAUT-9153 Implementation of EMR Scaler Routes #133

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.spotinst.sdkjava.enums.AwsMrScalerStateEnum;
import com.spotinst.sdkjava.model.*;
import com.spotinst.sdkjava.model.api.mrScaler.aws.*;
import com.spotinst.sdkjava.model.requests.mrScaler.aws.*;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -33,6 +34,22 @@ public static void main(String[] args) throws IOException {
updateMrScaler(mrScalerAwsClient, mrScalerId);
System.out.println("Update Success: " + mrScalerId);

System.out.print("List Instances");
listMrScalerInstances(mrScalerAwsClient, mrScalerId);
System.out.println("List All Instances Success");

System.out.print("List MrScaler Cluster");
listMrScaler(mrScalerAwsClient, mrScalerId);
System.out.println("List MrScaler Cluster Success");

System.out.print("Scale Up MrScaler Cluster");
scaleUpMrScaler(mrScalerAwsClient, mrScalerId, 2);
System.out.println("Scale Up MrScaler Cluster Success");

System.out.print("Scale Down MrScaler Cluster");
scaleDownMrScaler(mrScalerAwsClient, mrScalerId, 2);
System.out.println("Scale Down MrScaler Cluster Success");

System.out.println("Delete MrScaler");
deleteMrScaler(mrScalerAwsClient, mrScalerId);
System.out.println("Delete Success: " + mrScalerId);
Expand All @@ -41,8 +58,8 @@ public static void main(String[] args) throws IOException {
ApiMrScalerAws mrScalerForOperator = buildApiMrScalerAws();
ApiMrScalerOperatorAwsResponse mrScalerOperator =
createMrScaleOperator(mrScalerAwsClient, mrScalerForOperator);
AwsMrScalerStateEnum mrScalerState = mrScalerOperator.getState();
String mrScalerOperatorId = mrScalerOperator.getMrScalerId();
AwsMrScalerStateEnum mrScalerState = mrScalerOperator.getState();
String mrScalerOperatorId = mrScalerOperator.getMrScalerId();

System.out.println("Update MrScaler cached in MrScaler Operator");
updateMrScaler(mrScalerAwsClient, mrScalerOperatorId);
Expand All @@ -54,7 +71,6 @@ public static void main(String[] args) throws IOException {
System.out.println("Delete MrScaler cached in MrScaler Operator");
deleteMrScaler(mrScalerAwsClient, mrScalerOperatorId);
System.out.println("Delete Success: " + mrScalerOperatorId);

}

public static String createMrScaler(SpotinstMrScalerAwsClient mrScalerAwsClient) {
Expand Down Expand Up @@ -274,6 +290,47 @@ public static void updateMrScaler(SpotinstMrScalerAwsClient mrScalerAwsClient, S
System.out.println("Update Status: " + mrScalerUpdateResponse);
}

public static void listMrScalerInstances(SpotinstMrScalerAwsClient mrScalerAwsClient, String mrScalerId) {
System.out.print("List Instances");
ApiMrScalerListInstancesRequest.Builder listMrScalerInstancesBuilder = ApiMrScalerListInstancesRequest.Builder.get();
ApiMrScalerListInstancesRequest listMrScalerInstances = listMrScalerInstancesBuilder.setMrScalerId(mrScalerId).build();

System.out.println("Sending Request");
List<ApiMrScalerListInstancesAws> listRes = mrScalerAwsClient.listMrScalerInstances(listMrScalerInstances);
System.out.println(JsonMapper.toJson(listRes));
}

public static void listMrScaler(SpotinstMrScalerAwsClient mrScalerAwsClient, String mrScalerId) {
System.out.print("List MrScaler");
ApiMrScalerListMrScalersRequest.Builder listMrScalersBuilder = ApiMrScalerListMrScalersRequest.Builder.get();
ApiMrScalerListMrScalersRequest listMrScalers = listMrScalersBuilder.setMrScalerId(mrScalerId).build();

System.out.println("Sending Request");
List<ApiMrScalerListScalersAws> listRes = mrScalerAwsClient.listMrScalers(listMrScalers);
System.out.println(JsonMapper.toJson(listRes));

}

public static void scaleUpMrScaler(SpotinstMrScalerAwsClient mrScalerAwsClient, String mrScalerId, Integer adjustment){
System.out.print("Scale Up MrScaler");
ApiMrScalerScaleUpRequest.Builder scalerUpMrScalersBuilder = ApiMrScalerScaleUpRequest.Builder.get();
ApiMrScalerScaleUpRequest scalerScaleUpRequest = scalerUpMrScalersBuilder.setMrScalerId(mrScalerId).setAdjustment(adjustment).build();

System.out.println("Sending Request");
List<ApiMrScalerScaleUpAws> scalerUpRes = mrScalerAwsClient.scaleUpMrScaler(scalerScaleUpRequest);
System.out.println(JsonMapper.toJson(scalerUpRes));
}

public static void scaleDownMrScaler(SpotinstMrScalerAwsClient mrScalerAwsClient, String mrScalerId, Integer adjustment){
System.out.print("Scale Down MrScaler");
ApiMrScalerScaleDownRequest.Builder scalerDownMrScalersBuilder = ApiMrScalerScaleDownRequest.Builder.get();
ApiMrScalerScaleDownRequest scalerScaleDownRequest = scalerDownMrScalersBuilder.setMrScalerId(mrScalerId).setAdjustment(adjustment).build();

System.out.println("Sending Request");
List<ApiMrScalerScaleDownAws> scalerDownRes = mrScalerAwsClient.scaleDownMrScaler(scalerScaleDownRequest);
System.out.println(JsonMapper.toJson(scalerDownRes));
}

public static void deleteMrScaler(SpotinstMrScalerAwsClient mrScalerAwsClient, String mrScalerId) {
ApiMrScalerAwsDeleteRequest.Builder deleteBuilder = ApiMrScalerAwsDeleteRequest.Builder.get();
ApiMrScalerAwsDeleteRequest delete = deleteBuilder.setMrScalerId(mrScalerId).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.spotinst.sdkjava.model;

import com.spotinst.sdkjava.exception.SpotinstNotSupportedException;
import com.spotinst.sdkjava.model.api.mrScaler.aws.ApiMrScalerListInstancesAws;

import java.util.List;

public interface ISpotinstMrScalerListInstancesRepo extends IRepository<ApiMrScalerListInstancesAws, Void, String> {
default RepoGenericResponse<List<ApiMrScalerListInstancesAws>> listMrScalerInstances(String mrScalerId, String authToken, String account){
throw new SpotinstNotSupportedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.spotinst.sdkjava.model;

import com.spotinst.sdkjava.exception.SpotinstNotSupportedException;
import com.spotinst.sdkjava.model.api.mrScaler.aws.ApiMrScalerListScalersAws;

import java.util.List;

public interface ISpotinstMrScalerListScalersRepo extends IRepository<ApiMrScalerListScalersAws, Void, String> {
default RepoGenericResponse<List<ApiMrScalerListScalersAws>> listMrScalers(String mrScalerId, String authToken, String account){
throw new SpotinstNotSupportedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.spotinst.sdkjava.model;

import com.spotinst.sdkjava.exception.SpotinstNotSupportedException;
import com.spotinst.sdkjava.model.api.mrScaler.aws.ApiMrScalerScaleDownAws;

import java.util.List;

public interface ISpotinstMrScalerScaleDownRepo extends IRepository<ApiMrScalerScaleDownAws, Void, String> {
default RepoGenericResponse<List<ApiMrScalerScaleDownAws>> scaleDownMrScaler(String mrScalerId, Integer adjustment, String authToken, String account){
throw new SpotinstNotSupportedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.spotinst.sdkjava.model;

import com.spotinst.sdkjava.exception.SpotinstNotSupportedException;
import com.spotinst.sdkjava.model.api.mrScaler.aws.ApiMrScalerScaleUpAws;

import java.util.List;

public interface ISpotinstMrScalerScaleUpRepo extends IRepository<ApiMrScalerScaleUpAws, Void, String> {
default RepoGenericResponse<List<ApiMrScalerScaleUpAws>> scaleUpMrScaler(String mrScalerId, Integer adjustment, String authToken, String account){
throw new SpotinstNotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.spotinst.sdkjava.exception.HttpError;
import com.spotinst.sdkjava.exception.SpotinstHttpException;
import com.spotinst.sdkjava.model.api.mrScaler.aws.*;
import com.spotinst.sdkjava.model.requests.mrScaler.aws.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -13,10 +14,15 @@ public class SpotinstMrScalerAwsClient {
private static final Logger LOGGER = LoggerFactory.getLogger(SpotinstSpectrumClient.class);

//region Members
private String authToken;
private String account;
private ISpotinstMrScalerAwsRepo spotinstMrScalerRepo;
private ISpotinstMrScalerOperatorAwsRepo spotinstMrScalerOperatorAwsRepo;
private String authToken;
private String account;
private Integer adjustment;
private ISpotinstMrScalerAwsRepo spotinstMrScalerRepo;
private ISpotinstMrScalerOperatorAwsRepo spotinstMrScalerOperatorAwsRepo;
private ISpotinstMrScalerListInstancesRepo sportMrScalerListInstancesRepo;
private ISpotinstMrScalerListScalersRepo spotinstMrScalerListScalersRepo;
private ISpotinstMrScalerScaleUpRepo spotinstMrScalerScaleUpRepo;
private ISpotinstMrScalerScaleDownRepo spotinstMrScalerScaleDownRepo;

public ISpotinstMrScalerAwsRepo getSpotinstMrScalerRepo() {
return this.spotinstMrScalerRepo;
Expand All @@ -34,6 +40,38 @@ public void setSpotinstMrScalerOperatorAwsRepo() {
this.spotinstMrScalerOperatorAwsRepo = SpotinstRepoManager.getInstance().getSpotinstMrScalerOperatorAwsRepo();
}

public ISpotinstMrScalerListInstancesRepo getSpotinstMrScalerListInstancesRepo(){
return this.sportMrScalerListInstancesRepo;
}

public void setSportMrScalerListInstancesRepo(){
this.sportMrScalerListInstancesRepo = SpotinstRepoManager.getInstance().getSpotinstMrScalerListInstancesRepo();
}

public ISpotinstMrScalerListScalersRepo getSpotinstMrScalerListScalersRepo(){
return this.spotinstMrScalerListScalersRepo;
}

public void setSpotinstMrScalerListScalersRepo(){
this.spotinstMrScalerListScalersRepo = SpotinstRepoManager.getInstance().getSpotinstMrScalerListScalersRepo();
}

public ISpotinstMrScalerScaleUpRepo getSpotinstMrScalerScaleUpRepo(){
return this.spotinstMrScalerScaleUpRepo;
}

public void setSpotinstMrScalerScaleUpRepo(){
this.spotinstMrScalerScaleUpRepo = SpotinstRepoManager.getInstance().getSpotinstMrScalerScaleUpRepo();
}

public ISpotinstMrScalerScaleDownRepo getSpotinstMrScalerScaleDownRepo(){
return this.spotinstMrScalerScaleDownRepo;
}

public void setSpotinstMrScalerScaleDownRepo(){
this.spotinstMrScalerScaleDownRepo = SpotinstRepoManager.getInstance().getSpotinstMrScalerScaleDownRepo();
}

/**
* This Object is used to store the users account and token information and then make requests to the
* AWS MrScaler endpoints.
Expand All @@ -47,6 +85,10 @@ public SpotinstMrScalerAwsClient(String authToken, String account) {

setSpotinstMrScalerRepo();
setSpotinstMrScalerOperatorAwsRepo();
setSportMrScalerListInstancesRepo();
setSpotinstMrScalerListScalersRepo();
setSpotinstMrScalerScaleUpRepo();
setSpotinstMrScalerScaleDownRepo();
}


Expand Down Expand Up @@ -157,6 +199,109 @@ public ApiMrScalerAws getMrScaler(ApiMrScalerAwsGetRequest mrScalerAwsGetRequest
return retVal;
}

/**
* This method is used to Get a list of all instances and instances groups in the cluster
*
*
* @return a list of instances
*/
public List<ApiMrScalerListInstancesAws> listMrScalerInstances(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return BL object list

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client class shouldn't have any reference of Api level objects.

ApiMrScalerListInstancesRequest mrScalerListInstancesRequest) {
List<ApiMrScalerListInstancesAws> retVal;
String clusterToGet = mrScalerListInstancesRequest.getMrScalerId();
RepoGenericResponse<List<ApiMrScalerListInstancesAws>> mrScalerListInstances = getSpotinstMrScalerListInstancesRepo().listMrScalerInstances(clusterToGet, authToken, account);

if(mrScalerListInstances.isRequestSucceed()){
retVal = mrScalerListInstances.getValue();
}
else {
List<HttpError> httpExceptions = mrScalerListInstances.getHttpExceptions();
HttpError httpException = httpExceptions.get(0);
LOGGER.error(String.format("Error encountered while attempting to get a list of all instances and instances groups in the cluster. Code: %s. Message: %s.",
httpException.getCode(), httpException.getMessage()));
throw new SpotinstHttpException(httpException.getMessage());
}

return retVal;
}

/**
* This method is used to Scaler cluster
*
*
* @return a list of Scaler cluster
*/
public List<ApiMrScalerListScalersAws> listMrScalers(ApiMrScalerListMrScalersRequest mrScalerListScalersRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the ApiMrScalerListMrScalersRequest object. We can directly pass the MrScalerId.

List<ApiMrScalerListScalersAws> retVal;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client class shouldn't have any reference of Api level objects.

String clusterToGet = mrScalerListScalersRequest.getMrScalerId();
RepoGenericResponse<List<ApiMrScalerListScalersAws>> mrScalerListScalers = getSpotinstMrScalerListScalersRepo().listMrScalers(clusterToGet, authToken, account);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client class shouldn't have any reference of Api level objects.


if(mrScalerListScalers.isRequestSucceed()){
retVal = mrScalerListScalers.getValue();
}
else {
List<HttpError> httpExceptions = mrScalerListScalers.getHttpExceptions();
HttpError httpException = httpExceptions.get(0);
LOGGER.error(String.format("Error encountered while attempting to get a list of Scaler cluster. Code: %s. Message: %s.",
httpException.getCode(), httpException.getMessage()));
throw new SpotinstHttpException(httpException.getMessage());
}

return retVal;
}

/**
* This method is used to Scale Up the cluster
*
*
* @return a list of instances
*/
public List<ApiMrScalerScaleUpAws> scaleUpMrScaler(ApiMrScalerScaleUpRequest mrScalerScaleUpRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the ApiMrScalerScaleUpRequest object. We can directly pass the MrScalerId and adjustment

List<ApiMrScalerScaleUpAws> retVal;
String clusterToGet = mrScalerScaleUpRequest.getMrScalerId();
Integer adjustment = mrScalerScaleUpRequest.getAdjustment();
RepoGenericResponse<List<ApiMrScalerScaleUpAws>> mrScalerScaleUp = getSpotinstMrScalerScaleUpRepo().scaleUpMrScaler(clusterToGet, adjustment, authToken, account);

if(mrScalerScaleUp.isRequestSucceed()){
retVal = mrScalerScaleUp.getValue();
}
else {
List<HttpError> httpExceptions = mrScalerScaleUp.getHttpExceptions();
HttpError httpException = httpExceptions.get(0);
LOGGER.error(String.format("Error encountered while attempting to Scale Up the cluster. Code: %s. Message: %s.",
httpException.getCode(), httpException.getMessage()));
throw new SpotinstHttpException(httpException.getMessage());
}

return retVal;
}

/**
* This method is used to Scale Down the cluster
*
*
* @return a list of instances
*/
public List<ApiMrScalerScaleDownAws> scaleDownMrScaler(ApiMrScalerScaleDownRequest mrScalerScaleDownRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as previous function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client class shouldn't have any reference of Api level objects.

List<ApiMrScalerScaleDownAws> retVal;
String clusterToGet = mrScalerScaleDownRequest.getMrScalerId();
Integer adjustment = mrScalerScaleDownRequest.getAdjustment();
RepoGenericResponse<List<ApiMrScalerScaleDownAws>> mrScalerScaleDown = getSpotinstMrScalerScaleDownRepo().scaleDownMrScaler(clusterToGet, adjustment, authToken, account);

if(mrScalerScaleDown.isRequestSucceed()){
retVal = mrScalerScaleDown.getValue();
}
else {
List<HttpError> httpExceptions = mrScalerScaleDown.getHttpExceptions();
HttpError httpException = httpExceptions.get(0);
LOGGER.error(String.format("Error encountered while attempting to Scale Down the cluster. Code: %s. Message: %s.",
httpException.getCode(), httpException.getMessage()));
throw new SpotinstHttpException(httpException.getMessage());
}

return retVal;
}

/**
* This method is used to get all AWS MrScaler Clusters
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.spotinst.sdkjava.model;

import com.spotinst.sdkjava.model.repo.elastigroup.azure.v3.SpotinstElastigroupRepoAzure;
import com.spotinst.sdkjava.model.repo.mrScaler.aws.*;
import com.spotinst.sdkjava.model.repo.ocean.gke.SpotOceanGkeLaunchSpecRepo;
import com.spotinst.sdkjava.model.repo.aws.managedInstance.SpotAwsManagedInstanceRepo;
import com.spotinst.sdkjava.model.repo.ocean.ecs.SpotOceanEcsLaunchSpecRepo;
Expand Down Expand Up @@ -37,7 +38,11 @@ class SpotinstRepoManager {
private ISpotOceanEcsLaunchSpecRepo spotOceanEcsLaunchSpecRepo;
private ISpotStorageAzureVolumeRepo spotStorageAzureVolumeRepo;
private ISpotAwsManagedInstanceRepo spotAwsManagedInstanceRepo;
private ISpotK8sVngRepo spotK8sVngRepo;
private ISpotK8sVngRepo spotK8sVngRepo;
private ISpotinstMrScalerListInstancesRepo spotinstMrScalerListInstancesRepo;
private ISpotinstMrScalerListScalersRepo spotinstMrScalerListScalersRepo;
private ISpotinstMrScalerScaleUpRepo spotinstMrScalerScaleUpRepo;
private ISpotinstMrScalerScaleDownRepo spotinstMrScalerScaleDownRepo;

//endregion

Expand Down Expand Up @@ -65,6 +70,10 @@ private SpotinstRepoManager() {
this.spotStorageAzureVolumeRepo = new SpotStorageAzureVolumeRepo();
this.spotAwsManagedInstanceRepo = new SpotAwsManagedInstanceRepo();
this.spotK8sVngRepo = new K8sVngRepo();
this.spotinstMrScalerListInstancesRepo = new SpotinstMrScalerListInstancesRepo();
this.spotinstMrScalerListScalersRepo = new SpotinstMrScalerListScalersRepo();
this.spotinstMrScalerScaleUpRepo = new SpotinstMrScalerScaleUpRepo();
this.spotinstMrScalerScaleDownRepo = new SpotinstMrScalerScaleDownRepo();
}
//endregion

Expand Down Expand Up @@ -222,6 +231,23 @@ public ISpotK8sVngRepo getK8sVngRepo() {
public void setK8sVngRepo(ISpotK8sVngRepo k8sVngRepo) {
this.spotK8sVngRepo = k8sVngRepo;
}

public ISpotinstMrScalerListInstancesRepo getSpotinstMrScalerListInstancesRepo() {
return spotinstMrScalerListInstancesRepo;
}

public ISpotinstMrScalerListScalersRepo getSpotinstMrScalerListScalersRepo(){
return spotinstMrScalerListScalersRepo;
}

public ISpotinstMrScalerScaleUpRepo getSpotinstMrScalerScaleUpRepo(){
return spotinstMrScalerScaleUpRepo;
}

public ISpotinstMrScalerScaleDownRepo getSpotinstMrScalerScaleDownRepo(){
return spotinstMrScalerScaleDownRepo;
}

//endregion
}

Loading