> + * @return The virtual machine this volume is attached to, or null if it
> is
> + * not attached.
> + */
> + public VirtualMachine getVirtualMachine() {
> + if ( ATTACHED == valueOf(target.getState()) ) {
> + RESTLink vmLink =
> checkNotNull(target.searchLink(ParentLinkName.VIRTUAL_MACHINE),
> + ValidationErrors.MISSING_REQUIRED_LINK + " " +
> ParentLinkName.VIRTUAL_MACHINE);
> + vmLink.setType(VirtualMachineWithNodeExtendedDto.BASE_MEDIA_TYPE);
> + HttpResponse response = context.getApi().get(vmLink);
> +
> + ParseXMLWithJAXB<VirtualMachineWithNodeExtendedDto> parser = new
> ParseXMLWithJAXB<VirtualMachineWithNodeExtendedDto>(
> + context.utils().xml(),
> + TypeLiteral.get(VirtualMachineWithNodeExtendedDto.class));
> + return wrap(context, VirtualMachine.class, parser.apply(response));
> + }
> + return null;
In jclouds we try to avoid returning `null`, and we rely on Guava's `Optional`
instead (see
[UsingAndAvoidingNullExplained](http://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained)).
In this concrete case, however, I think it should be better to change the
method to fail if the volume is not attached (callers should take care of
checking that before calling this method), by replacing the first conditional
by something like:
```java
checkState(ATTACHED == valueOf(target.getState()), "volume is not attached to a
vm");
```
---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-labs/pull/31/files#r7259189