diff --git a/pom.xml b/pom.xml index fdcbc5e..c3462c2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 es.upv.i3m.grycap im-java-api - 0.4.4 + 0.4.5 IM Java API Java client for the REST API of the IM @@ -20,6 +20,7 @@ 2.10.3 2.17 3.10.4 + 0.7.7.201606060606 @@ -131,7 +132,7 @@ org.jacoco jacoco-maven-plugin - 0.7.5.201505241946 + ${jacoco-maven-plugin.version} default-prepare-agent diff --git a/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java b/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java index 4847509..089372a 100644 --- a/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java +++ b/src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java @@ -16,6 +16,7 @@ package es.upv.i3m.grycap.im; +import es.upv.i3m.grycap.im.auth.AuthorizationHeader; import es.upv.i3m.grycap.im.exceptions.ImClientException; import es.upv.i3m.grycap.im.exceptions.ToscaContentTypeNotSupportedException; import es.upv.i3m.grycap.im.lang.ImMessages; @@ -38,8 +39,7 @@ import java.util.List; /** - * This class offers the user an API to communicate with the Infrastructure - * Manager. + * This class offers the user an API to communicate with the Infrastructure Manager. */ public class InfrastructureManager { @@ -75,18 +75,32 @@ public InfrastructureManager(final String targetUrl, final Path authFile) * @param authorizationHeader * : string with the authorization content */ - public InfrastructureManager(final String targetUrl, - final String authorizationHeader) throws ImClientException { + public InfrastructureManager(final String targetUrl, final String authorizationHeader) + throws ImClientException { imClient = new ImClient(targetUrl, authorizationHeader); } + /** + * Create a new IM client. + * + * @param targetUrl + * : the URL where the REST API of the IM is defined + * @param authorizationHeader + * : {@link AuthorizationHeader} with the authorization content + */ + public InfrastructureManager(final String targetUrl, + final AuthorizationHeader authorizationHeader) throws ImClientException { + String serializedAuthorizationHeader = authorizationHeader.serialize(); + imClient = new ImClient(targetUrl, serializedAuthorizationHeader); + } + private ImClient getImClient() { return imClient; } /** - * Create and configure an infrastructure with the requirements specified in - * the document of the body contents.
+ * Create and configure an infrastructure with the requirements specified in the document of the + * body contents.
* If success, it is returned the URI of the new infrastructure. * * @param infrastructureDefinition @@ -103,29 +117,27 @@ public InfrastructureUri createInfrastructure(String infrastructureDefinition, } /** - * Return a list of URIs referencing the infrastructures associated to the IM - * user. + * Return a list of URIs referencing the infrastructures associated to the IM user. */ public InfrastructureUris getInfrastructureList() throws ImClientException { return getImClient().get(PATH_INFRASTRUCTURES, InfrastructureUris.class); } /** - * Return a list of URIs referencing the virtual machines associated to the - * infrastructure with ID 'infId'. + * Return a list of URIs referencing the virtual machines associated to the infrastructure with ID + * 'infId'. * * @param infId * : infrastructure id */ - public InfrastructureUris getInfrastructureInfo(String infId) - throws ImClientException { + public InfrastructureUris getInfrastructureInfo(String infId) throws ImClientException { return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, InfrastructureUris.class); } /** - * Return information about the virtual machine with ID vmId associated to the - * infrastructure with ID infId. + * Return information about the virtual machine with ID vmId associated to the infrastructure with + * ID infId. * * @param infId * : infrastructure id @@ -133,16 +145,14 @@ public InfrastructureUris getInfrastructureInfo(String infId) * : virtual machine id * @return : POJO with the vm info. */ - public VirtualMachineInfo getVmInfo(String infId, String vmId) - throws ImClientException { - return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId - + PATH_SEPARATOR + PATH_VMS + PATH_SEPARATOR + vmId, - VirtualMachineInfo.class); + public VirtualMachineInfo getVmInfo(String infId, String vmId) throws ImClientException { + return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + PATH_VMS + PATH_SEPARATOR + vmId, VirtualMachineInfo.class); } /** - * Return property 'propertyName' from to the virtual machine with ID 'vmId' - * associated to the infrastructure with ID 'infId'. + * Return property 'propertyName' from to the virtual machine with ID 'vmId' associated to the + * infrastructure with ID 'infId'. * * @param infId * : infrastructure id @@ -151,75 +161,71 @@ public VirtualMachineInfo getVmInfo(String infId, String vmId) * @param vmProperty * : VM property to retrieve from the virtual machine */ - public Property getVmProperty(String infId, String vmId, - VmProperties vmProperty) throws ImClientException { - return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId - + PATH_SEPARATOR + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR - + vmProperty.toString(), Property.class); + public Property getVmProperty(String infId, String vmId, VmProperties vmProperty) + throws ImClientException { + return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR + vmProperty.toString(), + Property.class); } /** - * Return the contextualization log associated to the infrastructure with ID - * 'infId'. + * Return the contextualization log associated to the infrastructure with ID 'infId'. * * @param infId * : infrastructure id */ - public Property getInfrastructureContMsg(String infId) - throws ImClientException { - return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId - + PATH_SEPARATOR + ImValues.CONTMSG, Property.class); + public Property getInfrastructureContMsg(String infId) throws ImClientException { + return getImClient().get( + PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.CONTMSG, + Property.class); } /** - * Return a json with the original RADL specified to create the infrastructure - * with ID 'infId'. + * Return a json with the original RADL specified to create the infrastructure with ID 'infId'. * * @param infId * : infrastructure id */ public Property getInfrastructureRadl(String infId) throws ImClientException { - return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId - + PATH_SEPARATOR + ImValues.RADL, Property.class); + return getImClient().get( + PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RADL, + Property.class); } /** * Return a JSON with the infrastructure and virtual machines states.
- * The JSON has a 'vm_states' array describing the states of all the machines - * of the infrastructure and a 'state' that describes the general state of the - * infrastructure ( e.g. {"vm_states": {"144838424585": "running"}, "state": - * "running"} ). + * The JSON has a 'vm_states' array describing the states of all the machines of the + * infrastructure and a 'state' that describes the general state of the infrastructure ( e.g. + * {"vm_states": {"144838424585": "running"}, "state": "running"} ). * * @param infId * : infrastructure id * @return : json with the infrastructure state */ - public InfrastructureState getInfrastructureState(String infId) - throws ImClientException { - return getImClient().get(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId - + PATH_SEPARATOR + ImValues.STATE, InfrastructureState.class); + public InfrastructureState getInfrastructureState(String infId) throws ImClientException { + return getImClient().get( + PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.STATE, + InfrastructureState.class); } /** - * Undeploy the virtual machines associated to the infrastructure with ID - * 'infId'. + * Undeploy the virtual machines associated to the infrastructure with ID 'infId'. * * @param infId * : infrastructure id */ public void destroyInfrastructure(String infId) throws ImClientException { - getImClient().delete(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, - String.class); + getImClient().delete(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, String.class); } /** - * Add the resources specified in the body contents to the infrastructure with - * ID 'infId'. The RADL restrictions are the same as in - * - * RPC-XML AddResource
+ * Add the resources specified in the body contents to the infrastructure with ID 'infId'. The + * RADL restrictions are the same as in + * RPC-XML + * AddResource
* If success, it is returned a list of URIs of the new virtual machines.
- * The context parameter is optional and is a flag to specify if the - * contextualization step will be launched just after the VM addition.
+ * The context parameter is optional and is a flag to specify if the contextualization step will + * be launched just after the VM addition.
* If not specified the contextualization flag is set to True. * * @param infId @@ -229,31 +235,28 @@ public void destroyInfrastructure(String infId) throws ImClientException { * @param bodyContentType * : set the body content type. Can be RADL, RADL_JSON or TOSCA. * @param context - * : flag to specify if the contextualization step will be launched - * just after the VM addition + * : flag to specify if the contextualization step will be launched just after the VM + * addition * @return : list of URIs of the new virtual machines */ public InfrastructureUris addResource(String infId, String radlFile, - BodyContentType bodyContentType, boolean... context) - throws ImClientException { + BodyContentType bodyContentType, boolean... context) throws ImClientException { RestParameter restParameter = createCallParameters(context); - return getImClient().post(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, - radlFile, bodyContentType.getValue(), InfrastructureUris.class, - restParameter); + return getImClient().post(PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId, radlFile, + bodyContentType.getValue(), InfrastructureUris.class, restParameter); } private static RestParameter createCallParameters(boolean... context) { return (context != null && context.length > 0) - ? new Parameter(REST_PARAMETER_NAME_CONTEXT, context[0]) - : new NoParameter(); + ? new Parameter(REST_PARAMETER_NAME_CONTEXT, context[0]) : new NoParameter(); } /** - * Undeploy the virtual machine with ID 'vmId' associated to the - * infrastructure with ID 'infId'.
- * The context parameter is optional and is a flag to specify if the - * contextualization step will be launched just after the VM addition.
+ * Undeploy the virtual machine with ID 'vmId' associated to the infrastructure with ID 'infId'. + *
+ * The context parameter is optional and is a flag to specify if the contextualization step will + * be launched just after the VM addition.
* As default the contextualization flag is set to True. * * @param infId @@ -261,22 +264,21 @@ private static RestParameter createCallParameters(boolean... context) { * @param vmIds * : list of virtual machine ids * @param context - * : flag to specify if the contextualization step will be launched - * just after the VM addition + * : flag to specify if the contextualization step will be launched just after the VM + * addition * @throws ImClientException * : exception in the IM client */ - public void removeResource(String infId, List vmIds, - boolean... context) throws ImClientException { + public void removeResource(String infId, List vmIds, boolean... context) + throws ImClientException { String ids = StringUtils.join(vmIds, ","); removeResource(infId, ids, context); } /** - * Undeploy the virtual machine with ID vmId associated to the infrastructure - * with ID 'infId'.
- * The context parameter is optional and is a flag to specify if the - * contextualization step will be launched just after the VM addition.
+ * Undeploy the virtual machine with ID vmId associated to the infrastructure with ID 'infId'.
+ * The context parameter is optional and is a flag to specify if the contextualization step will + * be launched just after the VM addition.
* As default the contextualization flag is set to True. * * @param infId @@ -284,49 +286,46 @@ public void removeResource(String infId, List vmIds, * @param vmId * : virtual machine id * @param context - * : flag to specify if the contextualization step will be launched - * just after the VM addition + * : flag to specify if the contextualization step will be launched just after the VM + * addition */ public void removeResource(String infId, String vmId, boolean... context) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + PATH_VMS + PATH_SEPARATOR + vmId; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS + + PATH_SEPARATOR + vmId; RestParameter restParameter = createCallParameters(context); getImClient().delete(path, String.class, restParameter); } /** - * Stop (but do not undeploy) all the virtual machines associated to the - * infrastructure with ID 'infId'. They can be resumed by the - * 'startInfrastructure' method. + * Stop (but do not undeploy) all the virtual machines associated to the infrastructure with ID + * 'infId'. They can be resumed by the 'startInfrastructure' method. * * @param infId * : infrastructure id */ public void stopInfrastructure(String infId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + ImValues.STOP; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.STOP; getImClient().put(path, String.class); } /** - * Resume all the virtual machines associated to the infrastructure with ID - * 'infId', previously stopped with the 'stopInfrastructure' method. + * Resume all the virtual machines associated to the infrastructure with ID 'infId', previously + * stopped with the 'stopInfrastructure' method. * * @param infId * : infrastructure id */ public void startInfrastructure(String infId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + ImValues.START; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.START; getImClient().put(path, String.class); } /** - * Perform the 'stop' action in the virtual machine with ID 'vmId' associated - * to the infrastructure with ID 'infId'. + * Perform the 'stop' action in the virtual machine with ID 'vmId' associated to the + * infrastructure with ID 'infId'. * * @param infId * : infrastructure id @@ -335,14 +334,14 @@ public void startInfrastructure(String infId) throws ImClientException { */ public void stopVm(String infId, String vmId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.STOP; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS + + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.STOP; getImClient().put(path, String.class); } /** - * Perform the 'start' action in the virtual machine with ID 'vmId' associated - * to the infrastructure with ID 'infId'. + * Perform the 'start' action in the virtual machine with ID 'vmId' associated to the + * infrastructure with ID 'infId'. * * @param infId * : infrastructure id @@ -350,18 +349,16 @@ public void stopVm(String infId, String vmId) throws ImClientException { * : virtual machine id */ public void startVm(String infId, String vmId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + PATH_VMS + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.START; + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS + + PATH_SEPARATOR + vmId + PATH_SEPARATOR + ImValues.START; getImClient().put(path, String.class); } /** - * Change the features of the virtual machine with ID 'vmId' in the - * infrastructure with with ID 'infId', specified by the RADL document - * specified in the body contents.
+ * Change the features of the virtual machine with ID 'vmId' in the infrastructure with with ID + * 'infId', specified by the RADL document specified in the body contents.
* Return a RADL with information about the virtual machine, like - * - * GetVMInfo. + * GetVMInfo. * * @param infId * : infrastructure id @@ -378,10 +375,9 @@ public VirtualMachineInfo alterVm(String infId, String vmId, String radlFile, BodyContentType bodyContentType) throws ImClientException { // The content type must not be TOSCA failIfToscaContentType(bodyContentType); - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + PATH_VMS + PATH_SEPARATOR + vmId; - return getImClient().put(path, radlFile, bodyContentType.getValue(), - VirtualMachineInfo.class); + String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + PATH_VMS + + PATH_SEPARATOR + vmId; + return getImClient().put(path, radlFile, bodyContentType.getValue(), VirtualMachineInfo.class); } /** @@ -390,38 +386,34 @@ public VirtualMachineInfo alterVm(String infId, String vmId, String radlFile, * @param bodyContentType * : type of the content sent in the body of the message */ - private void failIfToscaContentType(BodyContentType bodyContentType) - throws ImClientException { + private void failIfToscaContentType(BodyContentType bodyContentType) throws ImClientException { if (bodyContentType.equals(BodyContentType.TOSCA)) { - ImJavaApiLogger.severe(InfrastructureManager.class, - ImMessages.EXCEPTION_TOSCA_NOT_SUPPORTED); + ImJavaApiLogger.severe(InfrastructureManager.class, ImMessages.EXCEPTION_TOSCA_NOT_SUPPORTED); throw new ToscaContentTypeNotSupportedException(); } } /** - * Perform the reconfigure action in all the virtual machines in the the - * infrastructure with ID 'infID'.
+ * Perform the reconfigure action in all the virtual machines in the the infrastructure with ID + * 'infID'.
* This method starts the contextualization process again.
- * To reconfigure the VMs see the reconfigure methods that include the 'radl' - * parameter. + * To reconfigure the VMs see the reconfigure methods that include the 'radl' parameter. * * @param infId * : infrastructure id */ public void reconfigure(String infId) throws ImClientException { - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + ImValues.RECONFIGURE; + String path = + PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RECONFIGURE; getImClient().put(path, String.class); } /** - * Perform the reconfigure action in all the virtual machines of the - * infrastructure with ID 'infID.' It updates the configuration of the - * infrastructure as indicated in the 'radlFile'. The RADL restrictions are - * the same as in - * - * RPC-XML Reconfigure
+ * Perform the reconfigure action in all the virtual machines of the infrastructure with ID + * 'infID.' It updates the configuration of the infrastructure as indicated in the 'radlFile'. The + * RADL restrictions are the same as in + * RPC-XML + * Reconfigure
* If no RADL is specified, the contextualization process is started again. * * @param infId @@ -431,22 +423,21 @@ public void reconfigure(String infId) throws ImClientException { * @param bodyContentType * : set the body content type. Can be RADL or RADL_JSON */ - public void reconfigure(String infId, String radlFile, - BodyContentType bodyContentType) throws ImClientException { + public void reconfigure(String infId, String radlFile, BodyContentType bodyContentType) + throws ImClientException { // The content type must not be TOSCA failIfToscaContentType(bodyContentType); - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + ImValues.RECONFIGURE; + String path = + PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RECONFIGURE; getImClient().put(path, radlFile, bodyContentType.getValue(), String.class); } /** - * Perform the reconfigure action in all the virtual machines of the - * infrastructure with ID 'infID.' It updates the configuration of the - * infrastructure as indicated in the 'radlFile'. The RADL restrictions are - * the same as in - * - * RPC-XML Reconfigure
+ * Perform the reconfigure action in all the virtual machines of the infrastructure with ID + * 'infID.' It updates the configuration of the infrastructure as indicated in the 'radlFile'. The + * RADL restrictions are the same as in + * RPC-XML + * Reconfigure
* If no RADL is specified, the contextualization process is started again. * * @param infId @@ -456,20 +447,17 @@ public void reconfigure(String infId, String radlFile, * @param bodyContentType * : set the body content type. Can be RADL or RADL_JSON * @param vmList - * : comma separated list of IDs of the VMs to reconfigure. If not - * specified all the VMs will be reconfigured. + * : comma separated list of IDs of the VMs to reconfigure. If not specified all the VMs + * will be reconfigured. */ - public void reconfigure(String infId, String radlFile, - BodyContentType bodyContentType, List vmList) - throws ImClientException { + public void reconfigure(String infId, String radlFile, BodyContentType bodyContentType, + List vmList) throws ImClientException { // The content type must not be TOSCA failIfToscaContentType(bodyContentType); - RestParameter parameters = - new Parameter(REST_PARAMETER_NAME_VMLIST, vmList); - String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR - + ImValues.RECONFIGURE; - getImClient().put(path, radlFile, bodyContentType.getValue(), String.class, - parameters); + RestParameter parameters = new Parameter(REST_PARAMETER_NAME_VMLIST, vmList); + String path = + PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + ImValues.RECONFIGURE; + getImClient().put(path, radlFile, bodyContentType.getValue(), String.class, parameters); } /** @@ -477,11 +465,9 @@ public void reconfigure(String infId, String radlFile, * * @param infId * : infrastructure id - * @return : InfrastructureStatus class with an internal map containing the - * outputs + * @return : InfrastructureStatus class with an internal map containing the outputs */ - public InfOutputValues getInfrastructureOutputs(String infId) - throws ImClientException { + public InfOutputValues getInfrastructureOutputs(String infId) throws ImClientException { String path = PATH_INFRASTRUCTURES + PATH_SEPARATOR + infId + PATH_SEPARATOR + REST_PARAMETER_INFRASTRUCTURE_OUTPUTS; return getImClient().get(path, InfOutputValues.class); diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java b/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java new file mode 100644 index 0000000..e704e60 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java @@ -0,0 +1,48 @@ +package es.upv.i3m.grycap.im.auth; + +import es.upv.i3m.grycap.im.auth.credential.Credential; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class AuthorizationHeader { + + private static final String ERROR_MESSAGE = "Credentials must not be null"; + private List> credentials = new ArrayList<>(); + + public List> getCredentials() { + return credentials; + } + + /** + * Sets the credentials information. + */ + public void setCredentialsAuthInfos(List> credentials) { + if (credentials == null) { + throw new IllegalArgumentException(ERROR_MESSAGE); + } + this.credentials = credentials; + } + + public void addCredential(Credential credential) { + credentials.add(credential); + } + + /** + * Returns a string with the credentials information. + */ + public String serialize() { + StringBuilder sb = new StringBuilder(); + Iterator> it = credentials.iterator(); + while (it.hasNext()) { + String serializedAuthInfo = it.next().serialize(); + sb.append(serializedAuthInfo); + if (it.hasNext()) { + sb.append("\\n"); + } + } + return sb.toString(); + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java new file mode 100644 index 0000000..dd30f64 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java @@ -0,0 +1,59 @@ +package es.upv.i3m.grycap.im.auth.credential; + +public abstract class AbstractCredential> + implements Credential { + + private String id; + + protected > AbstractCredential( + B builder) { + id = builder.getId(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Override + public String serialize() { + return serialize(null).toString(); + } + + protected StringBuilder serialize(StringBuilder sb) { + if (sb == null) { + sb = new StringBuilder(); + } + if (!isNullOrEmpty(id)) { + sb.append("id = ").append(id).append(" ; "); + } + sb.append("type = ").append(getServiceType().getValue()); + return sb; + } + + //@formatter:off + public abstract static class AbstractCredentialBuilder + , T extends AbstractCredential> + implements CredentialBuilder { + //@formatter:on + + private String id; + + public String getId() { + return id; + } + + @SuppressWarnings("unchecked") + public B withId(String id) { + this.id = id; + return (B) this; + } + } + + public static boolean isNullOrEmpty(String string) { + return (string == null) || string.isEmpty(); + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java new file mode 100644 index 0000000..2dfcfff --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java @@ -0,0 +1,49 @@ +package es.upv.i3m.grycap.im.auth.credential; + +public abstract class AbstractTokenCredential> + extends AbstractCredential { + + protected String token; + + protected > AbstractTokenCredential( + B builder) { + super(builder); + setToken(builder.getToken()); + } + + public String getToken() { + return token; + } + + private void setToken(String token) { + if (isNullOrEmpty(token)) { + throw new IllegalArgumentException("token must not be blank"); + } + this.token = token; + } + + @Override + protected StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; token = ").append(token); + return sb; + } + + //@formatter:off + public abstract static class AbstractTokenCredentialBuilder + , T extends AbstractTokenCredential> + extends AbstractCredentialBuilder { + //@formatter:on + private String token; + + @SuppressWarnings("unchecked") + public B withToken(String token) { + this.token = token; + return (B) this; + } + + public String getToken() { + return token; + } + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java new file mode 100644 index 0000000..8af35d6 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java @@ -0,0 +1,81 @@ +package es.upv.i3m.grycap.im.auth.credential; + +//@formatter:off +public abstract class AbstractUsernamePasswordCredential> + extends AbstractCredential { + //@formatter:on + + private String username; + private String password; + + //@formatter:off + protected > + AbstractUsernamePasswordCredential(B builder) { + //@formatter:on + + super(builder); + username = builder.getUsername(); + password = builder.getPassword(); + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + if (!isNullOrEmpty(username)) { + sb.append(" ; username = ").append(username); + } + if (!isNullOrEmpty(password)) { + sb.append(" ; password = ").append(password); + } + return sb; + } + + //@formatter:off + public abstract static class AbstractUsernamePasswordCredentialBuilder + , + T extends AbstractUsernamePasswordCredential> + extends AbstractCredentialBuilder { + //@formatter:on + + private String username; + private String password; + + @SuppressWarnings("unchecked") + public B withUsername(String username) { + this.username = username; + return (B) this; + } + + @SuppressWarnings("unchecked") + public B withPassword(String password) { + this.password = password; + return (B) this; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java new file mode 100644 index 0000000..ea5a157 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java @@ -0,0 +1,14 @@ +package es.upv.i3m.grycap.im.auth.credential; + +public interface Credential> { + + public ServiceType getServiceType(); + + public String serialize(); + + public interface CredentialBuilder { + + public T build(); + + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java new file mode 100644 index 0000000..f1d9912 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java @@ -0,0 +1,28 @@ +package es.upv.i3m.grycap.im.auth.credential; + +public class DummyCredential extends AbstractCredential { + + protected DummyCredential(DummyCredentialBuilder builder) { + super(builder); + } + + @Override + public ServiceType getServiceType() { + return ServiceType.DUMMY; + } + + public static DummyCredentialBuilder getBuilder() { + return new DummyCredentialBuilder(); + } + + public static class DummyCredentialBuilder extends + AbstractCredentialBuilder { + + @Override + public DummyCredential build() { + return new DummyCredential(this); + } + + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java new file mode 100644 index 0000000..537246f --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java @@ -0,0 +1,32 @@ +package es.upv.i3m.grycap.im.auth.credential; + +public enum ServiceType { + + //@formatter:off + INFRASTRUCTURE_MANAGER("InfrastructureManager"), + VMRC("VMRC"), + DUMMY("Dummy"), + OPENNEBULA("OpenNebula"), + EC2("EC2"), + FOG_BOW("FogBow"), + OPENSTACK("OpenStack"), + OCCI("OCCI"), + LIB_CLOUD("LibCloud"), + DOCKER("Docker"), + GCE("GCE"), + AZURE("Azure"), + KUBERNETES("Kubernetes"), + LIB_VIRT("LibVirt"); + //@formatter:on + + private final String value; + + ServiceType(String value) { + this.value = value; + } + + public final String getValue() { + return value; + } + +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java new file mode 100644 index 0000000..c584973 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/im/ImCredential.java @@ -0,0 +1,64 @@ +package es.upv.i3m.grycap.im.auth.credential.im; + +import es.upv.i3m.grycap.im.auth.credential.AbstractTokenCredential; +import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public final class ImCredential { + + public static class ImTokenCredential + extends AbstractTokenCredential { + + protected ImTokenCredential(ImTokenCredentialBuilder builder) { + super(builder); + } + + @Override + public ServiceType getServiceType() { + return ServiceType.INFRASTRUCTURE_MANAGER; + } + + public static ImTokenCredentialBuilder getBuilder() { + return new ImTokenCredentialBuilder(); + } + + public static class ImTokenCredentialBuilder extends + AbstractTokenCredentialBuilder { + + @Override + public ImTokenCredential build() { + return new ImTokenCredential(this); + } + + } + } + + public static class ImUsernamePasswordCredential + extends AbstractUsernamePasswordCredential { + @Override + public ServiceType getServiceType() { + return ServiceType.INFRASTRUCTURE_MANAGER; + } + + protected ImUsernamePasswordCredential( + ImUsernamePasswordCredentialBuilder builder) { + super(builder); + } + + public static ImUsernamePasswordCredentialBuilder getBuilder() { + return new ImUsernamePasswordCredentialBuilder(); + } + + //@formatter:off + public static class ImUsernamePasswordCredentialBuilder extends + AbstractUsernamePasswordCredentialBuilder { + //@formatter:on + + @Override + public ImUsernamePasswordCredential build() { + return new ImUsernamePasswordCredential(this); + } + } + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java new file mode 100644 index 0000000..fa6bd00 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/occi/OcciCredential.java @@ -0,0 +1,85 @@ +package es.upv.i3m.grycap.im.auth.credential.occi; + +import es.upv.i3m.grycap.im.auth.credential.AbstractCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class OcciCredential extends AbstractCredential { + + private String host; + private String proxy; + + protected OcciCredential(OcciCredentialBuilder builder) { + super(builder); + setHost(builder.getHost()); + setProxy(builder.getProxy()); + } + + @Override + public ServiceType getServiceType() { + return ServiceType.OCCI; + } + + public String getHost() { + return host; + } + + private void setHost(String host) { + if (isNullOrEmpty(host)) { + throw new IllegalArgumentException("host must not be blank"); + } + this.host = host; + } + + public String getProxy() { + return proxy; + } + + private void setProxy(String proxy) { + if (isNullOrEmpty(proxy)) { + throw new IllegalArgumentException("proxy must not be blank"); + } + this.proxy = proxy; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; host = ").append(host); + sb.append(" ; proxy = ").append(proxy.replaceAll("\r?\n", "\\n")); + return sb; + } + + public static OcciCredentialBuilder getBuilder() { + return new OcciCredentialBuilder(); + } + + public static class OcciCredentialBuilder + extends AbstractCredentialBuilder { + + private String host; + private String proxy; + + public OcciCredentialBuilder withHost(String host) { + this.host = host; + return this; + } + + public OcciCredentialBuilder withProxy(String proxy) { + this.proxy = proxy; + return this; + } + + public String getHost() { + return host; + } + + public String getProxy() { + return proxy; + } + + @Override + public OcciCredential build() { + return new OcciCredential(this); + } + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java new file mode 100644 index 0000000..a52e1dd --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaTokenCredential.java @@ -0,0 +1,63 @@ +package es.upv.i3m.grycap.im.auth.credential.opennebula; + +import es.upv.i3m.grycap.im.auth.credential.AbstractTokenCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class OpenNebulaTokenCredential + extends AbstractTokenCredential { + + private String host; + + protected OpenNebulaTokenCredential( + OpennebulaTokenCredentialBuilder builder) { + super(builder); + setHost(builder.getHost()); + } + + public String getHost() { + return host; + } + + private void setHost(String host) { + if (isNullOrEmpty(host)) { + throw new IllegalArgumentException("Host must not be blank"); + } + this.host = host; + } + + @Override + public ServiceType getServiceType() { + return ServiceType.OPENNEBULA; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; host = ").append(host); + return sb; + } + + public static OpennebulaTokenCredentialBuilder getBuilder() { + return new OpennebulaTokenCredentialBuilder(); + } + + public static class OpennebulaTokenCredentialBuilder extends + AbstractTokenCredentialBuilder { + + private String host; + + public String getHost() { + return host; + } + + public OpennebulaTokenCredentialBuilder withHost(String host) { + this.host = host; + return this; + } + + @Override + public OpenNebulaTokenCredential build() { + return new OpenNebulaTokenCredential(this); + } + } +} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java new file mode 100644 index 0000000..59980c1 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/opennebula/OpenNebulaUserPwdCredential.java @@ -0,0 +1,66 @@ +package es.upv.i3m.grycap.im.auth.credential.opennebula; + +import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class OpenNebulaUserPwdCredential + extends AbstractUsernamePasswordCredential { + + private String host; + + public String getHost() { + return host; + } + + private void setHost(String host) { + if (isNullOrEmpty(host)) { + throw new IllegalArgumentException("host must not be blank"); + } + this.host = host; + } + + @Override + public ServiceType getServiceType() { + return ServiceType.OPENNEBULA; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; host = ").append(host); + return sb; + } + + protected OpenNebulaUserPwdCredential( + OpennebulaUserPwdCredentialBuilder builder) { + super(builder); + setHost(builder.getHost()); + } + + public static OpennebulaUserPwdCredentialBuilder getBuilder() { + return new OpennebulaUserPwdCredentialBuilder(); + } + + //@formatter:off + public static class OpennebulaUserPwdCredentialBuilder extends + AbstractUsernamePasswordCredentialBuilder { + //@formatter:on + + private String host; + + public String getHost() { + return host; + } + + public OpennebulaUserPwdCredentialBuilder withHost(String host) { + this.host = host; + return this; + } + + @Override + public OpenNebulaUserPwdCredential build() { + return new OpenNebulaUserPwdCredential(this); + } + } +} \ No newline at end of file diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackAuthVersion.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackAuthVersion.java new file mode 100644 index 0000000..e63cb7d --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackAuthVersion.java @@ -0,0 +1,19 @@ +package es.upv.i3m.grycap.im.auth.credential.openstack; + +public enum OpenstackAuthVersion { + + //@formatter:off + PASSWORD_2_0("2.0_password"), + PASSWORD_3_X("3.X_password"); + //@formatter:on + + private final String value; + + OpenstackAuthVersion(String value) { + this.value = value; + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java new file mode 100644 index 0000000..d69e148 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/openstack/OpenstackCredential.java @@ -0,0 +1,198 @@ +package es.upv.i3m.grycap.im.auth.credential.openstack; + +import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class OpenstackCredential + extends AbstractUsernamePasswordCredential { + + private OpenstackAuthVersion authVersion = OpenstackAuthVersion.PASSWORD_2_0; + private String tenant; + private String host; + private String baseUrl; + private String serviceRegion; + private String serviceName; + private String authToken; + + protected OpenstackCredential(OpenstackCredentialBuilder builder) { + super(builder); + authVersion = builder.getAuthVersion(); + tenant = builder.getTenant(); + setHost(builder.getHost()); + baseUrl = builder.getBaseUrl(); + serviceRegion = builder.getServiceRegion(); + serviceName = builder.getServiceName(); + authToken = builder.getAuthToken(); + } + + @Override + public ServiceType getServiceType() { + return ServiceType.OPENSTACK; + } + + public OpenstackAuthVersion getAuthVersion() { + return authVersion; + } + + public void setAuthVersion(OpenstackAuthVersion authVersion) { + this.authVersion = authVersion; + } + + public String getTenant() { + return tenant; + } + + public void setTenant(String tenant) { + this.tenant = tenant; + } + + public String getHost() { + return host; + } + + private void setHost(String host) { + if (isNullOrEmpty(host)) { + throw new IllegalArgumentException("Host must not be blank"); + } + this.host = host; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getServiceRegion() { + return serviceRegion; + } + + public void setServiceRegion(String serviceRegion) { + this.serviceRegion = serviceRegion; + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public String getAuthToken() { + return authToken; + } + + public void setAuthToken(String authToken) { + this.authToken = authToken; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; host = ").append(host); + if (authVersion != OpenstackAuthVersion.PASSWORD_2_0) { + sb.append(" ; OpenstackAuthVersion = ").append(authVersion.getValue()); + } + if (!isNullOrEmpty(baseUrl)) { + sb.append(" ; base_url = ").append(baseUrl); + } + if (!isNullOrEmpty(serviceRegion)) { + sb.append(" ; service_region = ").append(serviceRegion); + } + if (!isNullOrEmpty(serviceName)) { + sb.append(" ; service_name = ").append(serviceName); + } + if (!isNullOrEmpty(authToken)) { + sb.append(" ; auth_token = ").append(authToken); + } + return sb; + } + + public static OpenstackCredentialBuilder getBuilder() { + return new OpenstackCredentialBuilder(); + } + + public static class OpenstackCredentialBuilder extends + AbstractUsernamePasswordCredentialBuilder { + + private OpenstackAuthVersion authVersion; + private String tenant; + private String host; + private String baseUrl; + private String serviceRegion; + private String serviceName; + private String authToken; + + public OpenstackAuthVersion getAuthVersion() { + return authVersion; + } + + public OpenstackCredentialBuilder + withAuthVersion(OpenstackAuthVersion authVersion) { + this.authVersion = authVersion; + return this; + } + + public String getTenant() { + return tenant; + } + + public OpenstackCredentialBuilder withTenant(String tenant) { + this.tenant = tenant; + return this; + } + + public String getHost() { + return host; + } + + public OpenstackCredentialBuilder withHost(String host) { + this.host = host; + return this; + } + + public String getBaseUrl() { + return baseUrl; + } + + public OpenstackCredentialBuilder withBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + return this; + } + + public String getServiceRegion() { + return serviceRegion; + } + + public OpenstackCredentialBuilder withServiceRegion(String serviceRegion) { + this.serviceRegion = serviceRegion; + return this; + } + + public String getServiceName() { + return serviceName; + } + + public OpenstackCredentialBuilder withServiceName(String serviceName) { + this.serviceName = serviceName; + return this; + } + + public String getAuthToken() { + return authToken; + } + + public OpenstackCredentialBuilder withAuthToken(String authToken) { + this.authToken = authToken; + return this; + } + + @Override + public OpenstackCredential build() { + return new OpenstackCredential(this); + } + } +} diff --git a/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java b/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java new file mode 100644 index 0000000..506e964 --- /dev/null +++ b/src/main/java/es/upv/i3m/grycap/im/auth/credential/vmrc/VmrcCredential.java @@ -0,0 +1,64 @@ +package es.upv.i3m.grycap.im.auth.credential.vmrc; + +import es.upv.i3m.grycap.im.auth.credential.AbstractUsernamePasswordCredential; +import es.upv.i3m.grycap.im.auth.credential.ServiceType; + +public class VmrcCredential + extends AbstractUsernamePasswordCredential { + + private String host; + + protected VmrcCredential(VmrcCredentialBuilder builder) { + super(builder); + setHost(builder.getHost()); + + } + + @Override + public ServiceType getServiceType() { + return ServiceType.VMRC; + } + + public String getHost() { + return host; + } + + private void setHost(String host) { + if (isNullOrEmpty(host)) { + throw new IllegalArgumentException("host must not be blank"); + } + this.host = host; + } + + @Override + public StringBuilder serialize(StringBuilder sb) { + sb = super.serialize(sb); + sb.append(" ; host = ").append(host); + return sb; + } + + public static VmrcCredentialBuilder getBuilder() { + return new VmrcCredentialBuilder(); + } + + public static class VmrcCredentialBuilder extends + AbstractUsernamePasswordCredentialBuilder { + + private String host; + + public VmrcCredentialBuilder withHost(String host) { + this.host = host; + return this; + } + + public String getHost() { + return host; + } + + @Override + public VmrcCredential build() { + return new VmrcCredential(this); + } + + } +} diff --git a/src/test/java/es/upv/i3m/grycap/im/InfrastructureManagerTest.java b/src/test/java/es/upv/i3m/grycap/im/InfrastructureManagerTest.java index 23604ec..6324f03 100644 --- a/src/test/java/es/upv/i3m/grycap/im/InfrastructureManagerTest.java +++ b/src/test/java/es/upv/i3m/grycap/im/InfrastructureManagerTest.java @@ -20,6 +20,7 @@ import es.upv.i3m.grycap.file.NoNullOrEmptyFile; import es.upv.i3m.grycap.file.Utf8File; import es.upv.i3m.grycap.im.exceptions.FileException; +import es.upv.i3m.grycap.im.exceptions.ImClientErrorException; import es.upv.i3m.grycap.im.exceptions.ImClientException; import es.upv.i3m.grycap.im.exceptions.ToscaContentTypeNotSupportedException; import es.upv.i3m.grycap.im.pojo.InfOutputValues; @@ -110,12 +111,23 @@ public static void setRestClient() { * Creates a new infrastructure. */ @Before - public void createInfrastructure() throws ImClientException { - InfrastructureUri newInfrastructureUri = getIm() - .createInfrastructure(readFile(TOSCA_FILE_PATH), BodyContentType.TOSCA); - String uri = newInfrastructureUri.getUri(); - Assert.assertEquals(false, uri.isEmpty()); - setInfrastructureId(newInfrastructureUri.getInfrastructureId()); + public void createInfrastructure() { + try { + InfrastructureUri newInfrastructureUri = getIm().createInfrastructure( + readFile(TOSCA_FILE_PATH), BodyContentType.TOSCA); + String uri = newInfrastructureUri.getUri(); + Assert.assertEquals(false, uri.isEmpty()); + setInfrastructureId(newInfrastructureUri.getInfrastructureId()); + } catch (ImClientException exception) { + if (exception instanceof ImClientErrorException) { + System.out.println( + "Message: " + ((ImClientErrorException) exception).getMessage()); + System.out.println( + "Message: " + ((ImClientErrorException) exception).getCause()); + System.out.println("Message: " + ((ImClientErrorException) exception) + .getResponseError().getFormattedErrorMessage()); + } + } } private String readFile(String filePath) throws FileException { @@ -214,6 +226,7 @@ public void testInfrastructureOutputs() throws ImClientException { Assert.assertEquals("http://10.0.0.1:8080", galaxyUrl); } + @SuppressWarnings("unchecked") @Test public void testInfrastructureNestedOutputs() throws ImClientException { String infId = getInfrastructureId(); diff --git a/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java new file mode 100644 index 0000000..2d2aaaf --- /dev/null +++ b/src/test/java/es/upv/i3m/grycap/im/auth/AuthorizationHeaderTest.java @@ -0,0 +1,107 @@ +package es.upv.i3m.grycap.im.auth; + +import es.upv.i3m.grycap.ImTestWatcher; +import es.upv.i3m.grycap.im.auth.credential.Credential; +import es.upv.i3m.grycap.im.auth.credential.DummyCredential; +import es.upv.i3m.grycap.im.auth.credential.im.ImCredential.ImUsernamePasswordCredential; +import es.upv.i3m.grycap.im.auth.credential.occi.OcciCredential; +import es.upv.i3m.grycap.im.auth.credential.opennebula.OpenNebulaTokenCredential; +import es.upv.i3m.grycap.im.auth.credential.opennebula.OpenNebulaUserPwdCredential; +import es.upv.i3m.grycap.im.auth.credential.openstack.OpenstackAuthVersion; +import es.upv.i3m.grycap.im.auth.credential.openstack.OpenstackCredential; +import es.upv.i3m.grycap.im.auth.credential.vmrc.VmrcCredential; +import es.upv.i3m.grycap.im.exceptions.ImClientException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; + +public class AuthorizationHeaderTest extends ImTestWatcher { + + private static final String DUMMY_CREDS = "id = dummy ; type = Dummy"; + private static final String IM_CREDS = + "type = InfrastructureManager ; username = imuser01 ; password = pwd"; + private static final String VMRC_CREDS = + "type = VMRC ; username = demo ; password = pwd ; host = host"; + private static final String OST_CREDS = + "id = ost ; type = OpenStack ; username = usr ; password = pwd ; host = host ; service_region = region"; + private static final String OCCI_CREDS = + "id = occi ; type = OCCI ; host = host ; proxy = proxy"; + private static final String ONE_UP_CREDS = + "id = one ; type = OpenNebula ; username = usr ; password = pwd ; host = host"; + private static final String ONE_TK_CREDS = + "id = one ; type = OpenNebula ; token = token ; host = host"; + + private static AuthorizationHeader ah; + + @BeforeClass + public static void createAuthorizationHeader() { + ah = new AuthorizationHeader(); + } + + @Before + public void clearAuthorizationHeader() { + ah.setCredentialsAuthInfos(new ArrayList>()); + } + + @Test + public void testDummyCredentials() throws ImClientException { + Credential cred = DummyCredential.getBuilder().withId("dummy").build(); + ah.addCredential(cred); + Assert.assertEquals(DUMMY_CREDS, ah.serialize()); + } + + @Test + public void testImCredentials() throws ImClientException { + Credential cred = ImUsernamePasswordCredential.getBuilder() + .withUsername("imuser01").withPassword("pwd").build(); + ah.addCredential(cred); + Assert.assertEquals(IM_CREDS, ah.serialize()); + } + + @Test + public void testVmrcCredentials() throws ImClientException { + Credential cred = VmrcCredential.getBuilder().withUsername("demo") + .withPassword("pwd").withHost("host").build(); + ah.addCredential(cred); + Assert.assertEquals(VMRC_CREDS, ah.serialize()); + } + + @Test + public void testOpenStackCredentials() throws ImClientException { + Credential cred = OpenstackCredential.getBuilder().withId("ost") + .withUsername("usr").withPassword("pwd").withTenant("tenant") + .withServiceRegion("region").withHost("host") + .withAuthVersion(OpenstackAuthVersion.PASSWORD_2_0).build(); + ah.addCredential(cred); + Assert.assertEquals(OST_CREDS, ah.serialize()); + } + + @Test + public void testOpenNebulaUserPassCredentials() throws ImClientException { + Credential cred = OpenNebulaUserPwdCredential.getBuilder().withId("one") + .withUsername("usr").withPassword("pwd").withHost("host").build(); + ah.addCredential(cred); + Assert.assertEquals(ONE_UP_CREDS, ah.serialize()); + } + + @Test + public void testOpenNebulaTokenCredentials() throws ImClientException { + Credential cred = OpenNebulaTokenCredential.getBuilder().withId("one") + .withToken("token").withHost("host").build(); + ah.addCredential(cred); + Assert.assertEquals(ONE_TK_CREDS, ah.serialize()); + } + + @Test + public void testOcciCredentials() throws ImClientException { + Credential cred = OcciCredential.getBuilder().withId("occi") + .withHost("host").withProxy("proxy").build(); + ah.addCredential(cred); + Assert.assertEquals(OCCI_CREDS, ah.serialize()); + } + +} diff --git a/src/test/resources/tosca/elastic-cluster.yaml b/src/test/resources/tosca/elastic-cluster.yaml index 77b2c92..ea809b6 100644 --- a/src/test/resources/tosca/elastic-cluster.yaml +++ b/src/test/resources/tosca/elastic-cluster.yaml @@ -7,6 +7,7 @@ description: > TOSCA test for launching a Virtual Elastic Cluster. It will launch a single front-end that will be in change of managing the elasticity using the specified LRMS (torque, sge, slurm and condor) workload. + topology_template: node_templates: @@ -21,14 +22,17 @@ topology_template: torque_front_end: type: tosca.nodes.indigo.LRMS.FrontEnd.Torque + properties: + wn_ips: { get_attribute: [ torque_wn, private_address ] } requirements: - host: torque_server torque_server: type: tosca.nodes.indigo.Compute - properties: - public_ip: yes capabilities: + endpoint: + properties: + network_name: PUBLIC host: properties: num_cpus: 1 @@ -39,6 +43,8 @@ topology_template: wn_node: type: tosca.nodes.indigo.LRMS.WorkerNode.Torque + properties: + front_end_ip: { get_attribute: [ torque_server, private_address, 0 ] } capabilities: wn: properties: @@ -49,8 +55,6 @@ topology_template: torque_wn: type: tosca.nodes.indigo.Compute - properties: - public_ip: no capabilities: scalable: properties: @@ -69,4 +73,4 @@ topology_template: cluster_ip: value: { get_attribute: [ torque_server, public_address, 0 ] } cluster_creds: - value: { get_attribute: [ torque_server, credential, 0 ] } + value: { get_attribute: [ torque_server, endpoint, credential, 0 ] } \ No newline at end of file diff --git a/src/test/resources/tosca/galaxy_tosca.yaml b/src/test/resources/tosca/galaxy_tosca.yaml index 177ef19..79b1997 100644 --- a/src/test/resources/tosca/galaxy_tosca.yaml +++ b/src/test/resources/tosca/galaxy_tosca.yaml @@ -1,31 +1,16 @@ tosca_definitions_version: tosca_simple_yaml_1_0 -repositories: - indigo_repository: - description: INDIGO Custom types repository - url: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/ - imports: - - indigo_custom_types: - file: custom_types.yaml - repository: indigo_repository + - indigo_custom_types: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yaml description: > TOSCA test for launching a Galaxy Server also configuring the bowtie2 tool using Galaxy Tool Shed. + topology_template: node_templates: - bowtie2_galaxy_tool: - type: tosca.nodes.indigo.GalaxyShedTool - properties: - name: bowtie2 - owner: devteam - tool_panel_section_id: ngs_mapping - requirements: - - host: galaxy - galaxy: type: tosca.nodes.indigo.GalaxyPortal requirements: @@ -39,14 +24,19 @@ topology_template: galaxy_server: type: tosca.nodes.indigo.Compute - properties: - public_ip: yes capabilities: + endpoint: + properties: + network_name: PUBLIC + ports: + http_port: + protocol: tcp + source: 8080 # Host container properties host: - properties: - num_cpus: 1 - mem_size: 1 GB + properties: + num_cpus: 1 + mem_size: 1 GB # Guest Operating System properties os: properties: @@ -59,4 +49,4 @@ topology_template: galaxy_url: value: { concat: [ 'http://', get_attribute: [ galaxy_server, public_address, 0 ], ':8080' ] } cluster_creds: - value: { get_attribute: [ galaxy_server, credential, 0 ] } + value: { get_attribute: [ galaxy_server, endpoint, credential, 0 ] } diff --git a/src/test/resources/tosca/galaxy_tosca_2_nodes.yaml b/src/test/resources/tosca/galaxy_tosca_2_nodes.yaml index deba347..bb374a9 100644 --- a/src/test/resources/tosca/galaxy_tosca_2_nodes.yaml +++ b/src/test/resources/tosca/galaxy_tosca_2_nodes.yaml @@ -1,31 +1,16 @@ tosca_definitions_version: tosca_simple_yaml_1_0 -repositories: - indigo_repository: - description: INDIGO Custom types repository - url: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/ - imports: - - indigo_custom_types: - file: custom_types.yaml - repository: indigo_repository + - indigo_custom_types: https://raw.githubusercontent.com/indigo-dc/tosca-types/master/custom_types.yaml description: > TOSCA test for launching a Galaxy Server also configuring the bowtie2 tool using Galaxy Tool Shed. + topology_template: node_templates: - bowtie2_galaxy_tool: - type: tosca.nodes.indigo.GalaxyShedTool - properties: - name: bowtie2 - owner: devteam - tool_panel_section_id: ngs_mapping - requirements: - - host: galaxy - galaxy: type: tosca.nodes.indigo.GalaxyPortal requirements: @@ -39,17 +24,22 @@ topology_template: galaxy_server: type: tosca.nodes.indigo.Compute - properties: - public_ip: yes capabilities: + endpoint: + properties: + network_name: PUBLIC + ports: + http_port: + protocol: tcp + source: 8080 scalable: properties: count: 2 # Host container properties host: - properties: - num_cpus: 1 - mem_size: 1 GB + properties: + num_cpus: 1 + mem_size: 1 GB # Guest Operating System properties os: properties: