Merge pull request #352 from grkvlt/vcloud-initial-domain-objects

Issue 830: vCloud initial domain objects
This commit is contained in:
Adrian Cole 2012-02-07 01:54:03 -08:00
commit 2305cdc61d
12 changed files with 836 additions and 492 deletions

View File

@ -1,124 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.equal;
import java.net.URI;
import java.util.Map;
import javax.xml.bind.annotation.XmlAttribute;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
/**
* Location of a Rest resource <xs:complexType name="ReferenceType">
*
* @author Adrian Cole
*
*/
public class BaseNamedResource<T extends BaseNamedResource<T>> extends BaseResource<T> {
public static <T extends BaseNamedResource<T>> Builder<T> builder() {
return new Builder<T>();
}
public Builder<T> toBuilder() {
return new Builder<T>().fromNamedResource(this);
}
public static class Builder<T extends BaseNamedResource<T>> extends BaseResource.Builder<T> {
protected String name;
/**
* @see BaseNamedResource#getName
*/
public Builder<T> name(String name) {
this.name = name;
return this;
}
public BaseNamedResource<T> build() {
return new BaseNamedResource<T>(href, type, name);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public Builder<T> fromBaseResource(BaseResource<T> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
public Builder<T> fromNamedResource(BaseNamedResource<T> in) {
return fromBaseResource(in).name(in.getName());
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public Builder<T> fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes)).name(attributes.get("name"));
}
}
@XmlAttribute
protected String name;
protected BaseNamedResource(URI href, String type, String name) {
super(href, type);
this.name = name;
}
protected BaseNamedResource() {
// For JAXB
}
/**
* The name of the referenced object, taken from the value of that object's name attribute.
* Action links do not include a name attribute.
*
* @return name;
*/
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (!super.equals(o))
return false;
BaseNamedResource<?> that = BaseNamedResource.class.cast(o);
return equal(name, that.name);
}
@Override
public int hashCode() {
return super.hashCode() + Objects.hashCode(name);
}
@Override
public ToStringHelper string() {
return super.string().add("name", name);
}
}

View File

@ -0,0 +1,200 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.*;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.*;
import java.net.URI;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
/**
* Basic entity type in the vCloud object model.
*
* Includes a name, an optional description, and an optional list of links
*
* <pre>
* &lt;xs:complexType name="EntityType"&gt;
* </pre>
*
* @author Adrian Cole
*/
public class Entity<T extends Entity<T>> extends Resource<T> {
public static <T extends Entity<T>> Builder<T> builder() {
return new Builder<T>();
}
@Override
public Builder<T> toBuilder() {
return new Builder<T>().fromEntity(this);
}
public static class Builder<T extends Entity<T>> extends Resource.Builder<T> {
protected String description;
protected TaskList tasks;
protected String name;
protected String id;
/**
* @see Entity#getName()
*/
public Builder<T> name(String name) {
this.name = name;
return this;
}
/**
* @see Entity#getDescription()
*/
public Builder<T> description(String description) {
this.description = description;
return this;
}
/**
* @see Entity#getId()
*/
public Builder<T> id(String id) {
this.id = id;
return this;
}
/**
* @see Entity#getTasks()
*/
public Builder<T> tasks(TaskList tasks) {
this.tasks = tasks;
return this;
}
@Override
public Entity<T> build() {
Entity<T> entity = new Entity<T>(href, name);
entity.setDescription(description);
entity.setTasks(tasks);
entity.setId(id);
entity.setType(type);
entity.setLinks(links);
return entity;
}
/**
* {@inheritDoc}
*/
@Override
public Builder<T> fromResource(Resource<T> in) {
return Builder.class.cast(super.fromResource(in));
}
public Builder<T> fromEntity(Entity<T> in) {
return fromResource(in).description(in.getDescription()).tasks(in.getTasks()).id(in.getId()).name(in.getName());
}
}
@XmlElement(namespace = NS, name = "Description")
protected String description;
@XmlElement(namespace = NS, name = "TasksInProgress")
protected TaskList tasks;
@XmlAttribute
protected String id;
@XmlAttribute
protected String name;
protected Entity(URI href, String name) {
super(href);
this.name = name;
}
protected Entity() {
// For JAXB
}
/**
* Optional description.
*/
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
/**
* A list of queued, running, or recently completed tasks associated with this entity.
*/
public TaskList getTasks() {
return tasks;
}
public void setTasks(TaskList tasks) {
this.tasks = tasks;
}
/**
* The resource identifier, expressed in URN format.
*
* The value of this attribute uniquely identifies the resource, persists for the life of the
* resource, and is never reused.
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* Contains the name of the the entity.
*
* The object type, specified as a MIME content type, of the object that the link references.
* This attribute is present only for links to objects. It is not present for links to actions.
*
* @return type definition, type, expressed as an HTTP Content-Type
*/
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (!super.equals(o))
return false;
Entity<?> that = Entity.class.cast(o);
return super.equals(that) && equal(this.id, that.id) && equal(this.description, that.description) && equal(this.tasks, that.tasks);
}
@Override
public int hashCode() {
return super.hashCode() + Objects.hashCode(description, tasks, id, name);
}
@Override
public ToStringHelper string() {
return super.string().add("description", description).add("tasks", tasks).add("id", id).add("name", name);
}
}

View File

@ -0,0 +1,190 @@
package org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.*;
import static com.google.common.base.Preconditions.*;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import com.google.common.base.Objects;
/**
* The standard error message type used in the vCloud REST API.
*
* <pre>
* &lt;xs:complexType name="ErrorType"&gt;
* </pre>
*
* @author Adrian Cole
*/
@XmlRootElement(namespace = NS, name = "Error")
public class Error {
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromError(this);
}
public static class Builder {
protected String message;
protected int majorErrorCode;
protected String minorErrorCode;
protected String vendorSpecificErrorCode;
protected String stackTrace;
/**
* @see Error#getMessage()
*/
public Builder message(String message) {
this.message = message;
return this;
}
/**
* @see Error#getMajorErrorCode()
*/
public Builder majorErrorCode(int majorErrorCode) {
this.majorErrorCode = majorErrorCode;
return this;
}
/**
* @see Error#getMinorErrorCode()
*/
public Builder minorErrorCode(String minorErrorCode) {
this.minorErrorCode = minorErrorCode;
return this;
}
/**
* @see Error#getVendorSpecificErrorCode()
*/
public Builder vendorSpecificErrorCode(String vendorSpecificErrorCode) {
this.vendorSpecificErrorCode = vendorSpecificErrorCode;
return this;
}
/**
* @see Error#getStackTrace()
*/
public Builder stackTrace(String stackTrace) {
this.stackTrace = stackTrace;
return this;
}
public Error build() {
Error error = new Error(message, majorErrorCode, minorErrorCode);
error.setVendorSpecificErrorCode(vendorSpecificErrorCode);
error.setStackTrace(stackTrace);
return error;
}
public Builder fromError(Error in) {
return message(in.getMessage())
.majorErrorCode(in.getMajorErrorCode())
.minorErrorCode(in.getMinorErrorCode())
.vendorSpecificErrorCode(in.getVendorSpecificErrorCode())
.stackTrace(in.getStackTrace());
}
}
@XmlAttribute
protected String message;
@XmlAttribute
protected int majorErrorCode;
@XmlAttribute
protected String minorErrorCode;
@XmlAttribute
protected String vendorSpecificErrorCode;
@XmlAttribute
protected String stackTrace;
private Error(String message, int majorErrorCode, String minorErrorCode) {
this.message = checkNotNull(message, "message");
this.majorErrorCode = majorErrorCode;
this.minorErrorCode = checkNotNull(minorErrorCode, "minorErrorCode");
}
private Error() {
// For JAXB
}
/**
* An one line, human-readable message describing the error that occurred.
*/
public String getMessage() {
return message;
}
/**
* The class of the error. Matches the HTTP status code.
*/
public int getMajorErrorCode() {
return majorErrorCode;
}
/**
* Specific API error code.
*
* For example - can indicate that vApp power on failed by some reason.
*/
public String getMinorErrorCode() {
return minorErrorCode;
}
/**
* A vendor/implementation specific error code that point to specific
* modules/parts of the code and can make problem diagnostics easier.
*/
public String getVendorSpecificErrorCode() {
return vendorSpecificErrorCode;
}
public void setVendorSpecificErrorCode(String vendorSpecificErrorCode) {
this.vendorSpecificErrorCode = vendorSpecificErrorCode;
}
/**
* The stack trace of the exception which when examined might make problem
* diagnostics easier.
*/
public String getStackTrace() {
return stackTrace;
}
public void setStackTrace(String stackTrace) {
this.stackTrace = stackTrace;
}
@Override
public boolean equals(Object o) {
if (!super.equals(o))
return false;
Error that = (Error) o;
return equal(this.message, that.message) &&
equal(this.majorErrorCode, that.majorErrorCode) &&
equal(this.minorErrorCode, that.minorErrorCode) &&
equal(this.vendorSpecificErrorCode, that.vendorSpecificErrorCode) &&
equal(this.stackTrace, that.stackTrace);
}
@Override
public int hashCode() {
return super.hashCode() + Objects.hashCode(message, majorErrorCode, minorErrorCode, vendorSpecificErrorCode, stackTrace);
}
@Override
public String toString() {
return Objects.toStringHelper("")
.add("message", message)
.add("majorErrorCode", majorErrorCode)
.add("minorErrorCode", minorErrorCode)
.toString();
}
}

View File

@ -12,12 +12,15 @@ import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;
/** /**
* <xs:complexType name="LinkType"> * A link.
* *
* <pre>
* &lt;xs:complexType name="LinkType"&gt;
* </pre>
*
* @author Adrian Cole * @author Adrian Cole
*
*/ */
public class Link extends BaseNamedResource<Link> { public class Link extends Reference<Link> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Builder builder() { public static Builder builder() {
@ -32,71 +35,39 @@ public class Link extends BaseNamedResource<Link> {
return new Builder().fromLink(this); return new Builder().fromLink(this);
} }
public static class Builder extends BaseNamedResource.Builder<Link> { public static class Builder extends Reference.Builder<Link> {
protected String rel; protected String rel;
/** /**
* @see Link#getString * @see Link#getRel()
*/ */
public Builder rel(String rel) { public Builder rel(String rel) {
this.rel = rel; this.rel = rel;
return this; return this;
} }
@Override
public Link build() { public Link build() {
return new Link(href, type, name, rel); Link link = new Link(href, rel);
link.setId(id);
link.setName(name);
link.setType(type);
} }
public Builder fromLink(Link in) { public Builder fromLink(Link in) {
return fromNamedResource(in).rel(in.getRel()); return fromReference(in).rel(in.getRel());
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override public Builder fromReference(Reference<Link> in) {
public Builder fromBaseResource(BaseResource<Link> in) { return Builder.class.cast(super.fromReference(in));
return Builder.class.cast(super.fromBaseResource(in));
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public Builder fromNamedResource(BaseNamedResource<Link> in) {
return Builder.class.cast(super.fromNamedResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) { public Builder fromAttributes(Map<String, String> attributes) {
super.fromAttributes(attributes); super.fromAttributes(attributes);
rel(attributes.get("rel")); rel(attributes.get("rel"));
@ -107,8 +78,8 @@ public class Link extends BaseNamedResource<Link> {
@XmlAttribute @XmlAttribute
protected String rel; protected String rel;
private Link(URI href, String type, String name, String rel) { private Link(URI href, String rel) {
super(href, type, name); super(href);
this.rel = checkNotNull(rel, "rel"); this.rel = checkNotNull(rel, "rel");
} }
@ -133,7 +104,7 @@ public class Link extends BaseNamedResource<Link> {
if (!super.equals(o)) if (!super.equals(o))
return false; return false;
Link that = (Link) o; Link that = (Link) o;
return equal(this.rel, that.rel); return super.equals(that) && equal(this.rel, that.rel);
} }
@Override @Override

View File

@ -29,136 +29,81 @@ import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.jclouds.vcloud.director.v1_5.domain.Entity.Builder;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
/** /**
* Retrieves a list of organizations. * iRepresents an organization.
* *
* Unit of multi-tenancy and a top-level container. Contain vDCs, TasksList, Catalogs and Shared Network entities.
*
* <pre>
* &lt;xs:complexType name="OrgType"&gt;
* </pre>
*
* @author Adrian Cole * @author Adrian Cole
*/ */
@XmlRootElement(namespace = NS, name = "Org") @XmlRootElement(namespace = NS, name = "Org")
public class Org extends BaseNamedResource<Org> { public class Org extends Entity<Org> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@Override
public Builder toBuilder() { public Builder toBuilder() {
return new Builder().fromOrg(this); return new Builder().fromOrg(this);
} }
public static class Builder extends BaseNamedResource.Builder<Org> { public static class Builder extends Entity.Builder<Org> {
private String id;
private String description;
private String fullName; private String fullName;
private Set<Link> links = Sets.newLinkedHashSet();
/** /**
* @see Org#getId * @see Org#getFullName()
*/
public Builder id(String id) {
this.id = id;
return this;
}
/**
* @see Org#getDescription
*/
public Builder description(String description) {
this.description = description;
return this;
}
/**
* @see Org#getFullName
*/ */
public Builder fullName(String fullName) { public Builder fullName(String fullName) {
this.fullName = fullName; this.fullName = fullName;
return this; return this;
} }
/** @Override
* @see Org#getOrgs
*/
public Builder links(Set<Link> links) {
this.links = Sets.newLinkedHashSet(checkNotNull(links, "links"));
return this;
}
/**
* @see Org#getOrgs
*/
public Builder addLink(Link org) {
links.add(checkNotNull(org, "org"));
return this;
}
public Org build() { public Org build() {
return new Org(href, type, name, id, description, fullName, links); Org org = new Org(href, name, fullName);
org.setDescription(description);
org.setId(id);
org.setType(type);
org.setLinks(links);
org.setTasks(tasks);
return org;
}
@Override
public Builder fromEntity(Entity<Org> in) {
return Builder.class.cast(super.fromEntity(in));
} }
public Builder fromOrg(Org in) { public Builder fromOrg(Org in) {
return id(in.getId()).description(in.getDescription()).fullName(in.getFullName()).links(in.getLinks()); return fromEntity(in).fullName(in.getFullName());
} }
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
} }
private Org() { private Org() {
// For JAXB and builder use // For JAXB and builder use
} }
private Org(URI href, String type, String name, String id, String description, String fullName, Set<Link> links) { private Org(URI href, String name, String fullName) {
super(href, type, name); super(href, name);
this.id = id;
this.description = description;
this.fullName = fullName; this.fullName = fullName;
this.links = ImmutableSet.copyOf(links);
} }
@XmlAttribute
private String id;
@XmlElement(namespace = NS, name = "Description")
private String description;
@XmlElement(namespace = NS, name = "FullName") @XmlElement(namespace = NS, name = "FullName")
private String fullName; private String fullName;
@XmlElement(namespace = NS, name = "Link")
private Set<Link> links = Sets.newLinkedHashSet();
/**
*
* @return id of the org
*/
public String getId() {
return id;
}
/**
*
* @return description of the org
*/
public String getDescription() {
return description;
}
/** /**
* *
@ -168,29 +113,21 @@ public class Org extends BaseNamedResource<Org> {
return fullName; return fullName;
} }
/**
* TODO
*/
public Set<Link> getLinks() {
return ImmutableSet.copyOf(links);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (!super.equals(o))
return false; return false;
Org that = Org.class.cast(o); Org that = Org.class.cast(o);
return equal(id, that.id) && equal(description, that.description) && equal(fullName, that.fullName) return super.equals(that) && equal(fullName, that.fullName);
&& equal(links, that.links);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return super.hashCode() + Objects.hashCode(id, description, fullName, links); return super.hashCode() + Objects.hashCode(fullName);
} }
@Override @Override
public ToStringHelper string() { public ToStringHelper string() {
return super.string().add("id", id).add("description", description).add("fullName", fullName).add("links", links); return super.string().add("fullName", fullName);
} }
} }

View File

@ -1,69 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.vcloud.director.v1_5.domain;
import java.net.URI;
public class OrgLink extends BaseNamedResource<OrgLink> {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return Builder.class.cast(new Builder().fromNamedResource(this));
}
public static class Builder extends BaseNamedResource.Builder<OrgLink> {
@Override
public OrgLink build() {
return new OrgLink(href, type, name);
}
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
}
private OrgLink(URI href, String type, String name) {
super(href, type, name);
}
private OrgLink() {
// for JAXB
}
}

View File

@ -32,7 +32,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
/** /**
* Retrieves a list of organizations. * A list of organizations.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */

View File

@ -0,0 +1,219 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.equal;
import java.net.URI;
import java.util.Map;
import javax.xml.bind.annotation.XmlAttribute;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
/**
* A reference to a resource.
*
* Contains an href attribute and optional name and type attributes.
*
* <pre>
* &lt;xs:complexType name="ReferenceType"&gt;
* </pre>
*
* @author Adrian Cole
*/
public class Reference<T extends Reference<T>> {
public static <T extends Reference<T>> Builder<T> builder() {
return new Builder<T>();
}
public Builder<T> toBuilder() {
return new Builder<T>().fromReference(this);
}
public static class Builder<T extends Reference<T>> {
protected URI href;
protected String id;
protected String name;
protected String type;
/**
* @see Reference#getHref()
*/
public Builder<T> href(URI href) {
this.href = href;
return this;
}
/**
* @see Reference#getId()
*/
public Builder<T> id(String id) {
this.id = id;
return this;
}
/**
* @see Reference#getType()
*/
public Builder<T> type(String type) {
this.type = type;
return this;
}
/**
* @see Reference#getName()
*/
public Builder<T> name(String name) {
this.name = name;
return this;
}
public Reference<T> build() {
Reference<T> reference = new Reference<T>(href);
reference.setId(id);
reference.setName(name);
reference.setType(type);
return reference;
}
protected Builder<T> fromReference(Reference<T> in) {
return href(in.getHref()).id(in.getId()).name(in.getName()).type(in.getType());
}
protected Builder<T> fromAttributes(Map<String, String> attributes) {
return href(URI.create(attributes.get("href"))).id(attributes.get("id")).name(attributes.get("name")).type(attributes.get("type"));
}
}
@XmlAttribute
protected URI href;
@XmlAttribute
protected String id;
@XmlAttribute
protected String name;
@XmlAttribute
protected String type;
protected Reference(URI href) {
this.href = href;
}
protected Reference() {
// For JAXB
}
/**
* Contains the URI to the entity.
*
* An object reference, expressed in URL format. Because this URL includes the object identifier
* portion of the id attribute value, it uniquely identifies the object, persists for the life of
* the object, and is never reused. The value of the href attribute is a reference to a view of
* the object, and can be used to access a representation of the object that is valid in a
* particular context. Although URLs have a well-known syntax and a well-understood
* interpretation, a client should treat each href as an opaque string. The rules that govern how
* the server constructs href strings might change in future releases.
*
* @return an opaque reference and should never be parsed
*/
public URI getHref() {
return href;
}
/**
* The resource identifier, expressed in URN format.
*
* The value of this attribute uniquely identifies the resource, persists for the life of the
* resource, and is never reused.
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* Contains the name of the the entity.
*
* The object type, specified as a MIME content type, of the object that the link references.
* This attribute is present only for links to objects. It is not present for links to actions.
*
* @return type definition, type, expressed as an HTTP Content-Type
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* Contains the type of the the entity.
*
* The object type, specified as a MIME content type, of the object that the link references.
* This attribute is present only for links to objects. It is not present for links to actions.
*
* @return type definition, type, expressed as an HTTP Content-Type
*/
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* @see #getHref()
*/
public URI getURI() {
return getHref();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Reference<?> that = Reference.class.cast(o);
return equal(this.href, that.href) && equal(this.id, that.id) && equal(this.name, that.name) && equal(this.type, that.type);
}
@Override
public int hashCode() {
return Objects.hashCode(href, id, name, type);
}
@Override
public String toString() {
return string().toString();
}
protected ToStringHelper string() {
return Objects.toStringHelper("").add("href", href).add("id", id).add("name", name).add("type", type);
}
}

View File

@ -19,38 +19,59 @@
package org.jclouds.vcloud.director.v1_5.domain; package org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.equal; import static com.google.common.base.Objects.equal;
import static com.google.common.base.Preconditions.*;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.*;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import org.jclouds.vcloud.director.v1_5.domain.Task.Builder;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.Sets;
/** /**
* Location of a Rest resource * The base type for all objects in the vCloud model.
*
* Has an optional list of links and href and type attributes.
*
* <pre>
* &lt;xs:complexType name="ResourceType"&gt;
* </pre>
* *
* @author Adrian Cole * @author Adrian Cole
*
*/ */
public class BaseResource<T extends BaseResource<T>> { public class Resource<T extends Resource<T>> {
public static <T extends BaseResource<T>> Builder<T> builder() { public static <T extends Resource<T>> Builder<T> builder() {
return new Builder<T>(); return new Builder<T>();
} }
public Builder<T> toBuilder() { public Builder<T> toBuilder() {
return new Builder<T>().fromBaseResource(this); return new Builder<T>().fromResource(this);
} }
public static class Builder<T extends BaseResource<T>> { public static class Builder<T extends Resource<T>> {
protected String type;
protected URI href; protected URI href;
protected String type;
protected Set<Link> links = Sets.newLinkedHashSet();
/** /**
* @see BaseResource#getType * @see Reference#getHref()
*/
public Builder<T> href(URI href) {
this.href = href;
return this;
}
/**
* @see Reference#getType()
*/ */
public Builder<T> type(String type) { public Builder<T> type(String type) {
this.type = type; this.type = type;
@ -58,53 +79,51 @@ public class BaseResource<T extends BaseResource<T>> {
} }
/** /**
* @see BaseResource#getHref * @see Reference#getLinks()
*/ */
public Builder<T> href(URI href) { public Builder<T> links(Set<Link> links) {
this.href = href; this.links = Sets.newLinkedHashSet(checkNotNull(links, "links"));
return this; return this;
} }
public BaseResource<T> build() { /**
return new BaseResource<T>(href, type); * @see Reference#getLinks()
*/
public Builder<T> link(Link link) {
this.links.add(checkNotNull(link, "link"));
return this;
} }
protected Builder<T> fromBaseResource(BaseResource<T> in) { public Resource<T> build() {
return type(in.getType()).href(in.getHref()); Resource<T> reference = new Resource<T>(href);
reference.setType(type);
reference.setLinks(links);
return reference;
} }
protected Builder<T> fromAttributes(Map<String, String> attributes) { protected Builder<T> fromResource(Resource<T> in) {
return href(URI.create(attributes.get("href"))).type(attributes.get("type")); return href(in.getHref()).type(in.getType()).links(in.getLinks());
} }
} }
@XmlAttribute
protected String type;
@XmlAttribute @XmlAttribute
protected URI href; protected URI href;
@XmlAttribute
protected String type;
@XmlElement(namespace = NS, name = "Link")
protected Set<Link> links = Sets.newLinkedHashSet();
protected BaseResource(URI href, String type) { protected Resource(URI href) {
this.type = type;
this.href = href; this.href = href;
} }
protected BaseResource() { protected Resource() {
// For JAXB // For JAXB
} }
/** /**
* The object type, specified as a MIME content type, of the object that the link references. * Contains the URI to the entity.
* This attribute is present only for links to objects. It is not present for links to actions. *
*
* @return type definition, type, expressed as an HTTP Content-Type
*/
public String getType() {
return type;
}
/**
* An object reference, expressed in URL format. Because this URL includes the object identifier * An object reference, expressed in URL format. Because this URL includes the object identifier
* portion of the id attribute value, it uniquely identifies the object, persists for the life of * portion of the id attribute value, it uniquely identifies the object, persists for the life of
* the object, and is never reused. The value of the href attribute is a reference to a view of * the object, and is never reused. The value of the href attribute is a reference to a view of
@ -120,7 +139,38 @@ public class BaseResource<T extends BaseResource<T>> {
} }
/** /**
* @see #getHref * Contains the type of the the entity.
*
* The object type, specified as a MIME content type, of the object that the link references.
* This attribute is present only for links to objects. It is not present for links to actions.
*
* @return type definition, type, expressed as an HTTP Content-Type
*/
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* Set of optional links to an entity or operation associated with this object.
*/
public Set<Link>getLinks() {
return links;
}
public void setLinks(Set<Link> links) {
this.links = Sets.newLinkedHashSet(checkNotNull(links, "links"));
}
public void addLink(Link link) {
this.links.add(checkNotNull(link, "link"));
}
/**
* @see #getHref()
*/ */
public URI getURI() { public URI getURI() {
return getHref(); return getHref();
@ -132,13 +182,13 @@ public class BaseResource<T extends BaseResource<T>> {
return true; return true;
if (o == null || getClass() != o.getClass()) if (o == null || getClass() != o.getClass())
return false; return false;
BaseResource<?> that = BaseResource.class.cast(o); Resource<?> that = Resource.class.cast(o);
return equal(href, that.href) && equal(type, that.type); return equal(this.href, that.href) && equal(this.links, that.links) && equal(this.type, that.type);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(type, href); return Objects.hashCode(href, links, type);
} }
@Override @Override
@ -147,6 +197,6 @@ public class BaseResource<T extends BaseResource<T>> {
} }
protected ToStringHelper string() { protected ToStringHelper string() {
return Objects.toStringHelper("").add("href", href).add("type", type); return Objects.toStringHelper("").add("href", href).add("links", links).add("type", type);
} }
} }

View File

@ -58,7 +58,7 @@ public class Session {
private Set<Link> links = Sets.newLinkedHashSet(); private Set<Link> links = Sets.newLinkedHashSet();
/** /**
* @see Session#getUser * @see Session#getUser()
*/ */
public Builder user(String user) { public Builder user(String user) {
this.user = user; this.user = user;
@ -66,7 +66,7 @@ public class Session {
} }
/** /**
* @see Session#getOrg * @see Session#getOrg()
*/ */
public Builder org(String org) { public Builder org(String org) {
this.org = org; this.org = org;
@ -74,7 +74,7 @@ public class Session {
} }
/** /**
* @see Session#getHref * @see Session#getHref()
*/ */
public Builder href(URI href) { public Builder href(URI href) {
this.href = href; this.href = href;
@ -82,7 +82,7 @@ public class Session {
} }
/** /**
* @see Session#getLinks * @see Session#getLinks()
*/ */
public Builder links(Set<Link> links) { public Builder links(Set<Link> links) {
this.links = Sets.newLinkedHashSet(checkNotNull(links, "links")); this.links = Sets.newLinkedHashSet(checkNotNull(links, "links"));
@ -90,7 +90,7 @@ public class Session {
} }
/** /**
* @see Session#getLinks * @see Session#getLinks()
*/ */
public Builder addLink(Link link) { public Builder addLink(Link link) {
links.add(checkNotNull(link, "link")); links.add(checkNotNull(link, "link"));

View File

@ -26,7 +26,6 @@ import com.google.common.base.Objects;
* Session and its corresponding token * Session and its corresponding token
* *
* @author Adrian Cole * @author Adrian Cole
*
*/ */
public class SessionWithToken { public class SessionWithToken {

View File

@ -36,136 +36,107 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
/** /**
*
* Allows us to test a client via its side effects. * Allows us to test a client via its side effects.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "unit", singleThreaded = true, testName = "OrgClientExpectTest") @Test(groups = "unit", singleThreaded = true, testName = "OrgClientExpectTest")
public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest { public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest {
@Test
public void testWhenResponseIs2xxLoginReturnsValidOrgList() { public void testWhenResponseIs2xxLoginReturnsValidOrgList() {
HttpRequest orgListRequest = HttpRequest.builder()
.method("GET")
.endpoint(URI.create("http://localhost/api/org/"))
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "*/*")
.put("x-vcloud-authorization", token)
.build())
.build();
HttpRequest orgListRequest = HttpRequest.builder().method("GET") HttpResponse orgListResponse = HttpResponse.builder()
.endpoint(URI.create("http://localhost/api/org/")).headers( .statusCode(200)
ImmutableMultimap.<String, String> builder().put("Accept", "*/*").put("x-vcloud-authorization", .payload(payloadFromResourceWithContentType("/orglist.xml", VCloudDirectorMediaType.ORGLIST_XML + ";version=1.5"))
token).build()).build(); .build();
HttpResponse orgListResponse = HttpResponse.builder().statusCode(200) VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, orgListRequest, orgListResponse);
.payload(
payloadFromResourceWithContentType("/orglist.xml", VCloudDirectorMediaType.ORGLIST_XML
+ ";version=1.5")).build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, orgListRequest,
orgListResponse);
assertEquals(client.getOrgClient().getOrgList(), OrgList.builder().addOrg(
OrgLink.builder().type("application/vnd.vmware.vcloud.org+xml").name("JClouds").href(
URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build()).build()
assertEquals(client.getOrgClient().getOrgList(), OrgList.builder()
.addOrg(OrgLink.builder()
.type("application/vnd.vmware.vcloud.org+xml")
.name("JClouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build())
.build()
); );
} }
@Test @Test
public void testWhenResponseIs2xxLoginReturnsValidOrg() { public void testWhenResponseIs2xxLoginReturnsValidOrg() {
URI orgRef = URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"); URI orgRef = URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0");
HttpRequest orgRequest = HttpRequest.builder().method("GET").endpoint(orgRef).headers( HttpRequest orgRequest = HttpRequest.builder()
ImmutableMultimap.<String, String> builder().put("Accept", "*/*").put("x-vcloud-authorization", token) .method("GET")
.build()).build(); .endpoint(orgRef)
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "*/*")
.put("x-vcloud-authorization", token)
.build())
.build();
HttpResponse orgResponse = HttpResponse.builder().statusCode(200) HttpResponse orgResponse = HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResourceWithContentType("/org.xml", VCloudDirectorMediaType.ORG_XML + ";version=1.5")) .payload(payloadFromResourceWithContentType("/org.xml", VCloudDirectorMediaType.ORG_XML + ";version=1.5"))
.build(); .build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, orgRequest, orgResponse); VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, orgRequest, orgResponse);
assertEquals( assertEquals(client.getOrgClient().getOrg(orgRef), Org.builder()
client.getOrgClient().getOrg(orgRef), .name("JClouds")
Org .id("urn:vcloud:org:6f312e42-cd2b-488d-a2bb-97519cd57ed0")
.builder() .type(VCloudDirectorMediaType.ORG_XML)
.name("JClouds") .href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.id("urn:vcloud:org:6f312e42-cd2b-488d-a2bb-97519cd57ed0") .addLink(Link.builder()
.type(VCloudDirectorMediaType.ORG_XML) .rel("down")
.href( .type("application/vnd.vmware.vcloud.vdc+xml")
URI .name("Cluster01-JClouds")
.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0")) .href(URI.create("https://vcloudbeta.bluelock.com/api/vdc/d16d333b-e3c0-4176-845d-a5ee6392df07"))
.build())
.addLink( .addLink(Link.builder()
Link .rel("down")
.builder() .type("application/vnd.vmware.vcloud.tasksList+xml")
.rel("down") .href(URI.create("https://vcloudbeta.bluelock.com/api/tasksList/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.type("application/vnd.vmware.vcloud.vdc+xml") .build())
.name("Cluster01-JClouds") .addLink(Link.builder()
.href( .rel("down")
URI .type("application/vnd.vmware.vcloud.catalog+xml")
.create("https://vcloudbeta.bluelock.com/api/vdc/d16d333b-e3c0-4176-845d-a5ee6392df07")) .name("Public")
.build()) .href(URI.create("https://vcloudbeta.bluelock.com/api/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"))
.addLink( .build())
Link .addLink(Link.builder()
.builder() .rel("down")
.rel("down") .type("application/vnd.vmware.vcloud.controlAccess+xml")
.type("application/vnd.vmware.vcloud.tasksList+xml") .href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c/controlAccess/"))
.href( .build())
URI .addLink(Link.builder()
.create("https://vcloudbeta.bluelock.com/api/tasksList/6f312e42-cd2b-488d-a2bb-97519cd57ed0")) .rel("down")
.build()) .type("application/vnd.vmware.vcloud.orgNetwork+xml")
.addLink( .name("ilsolation01-Jclouds")
Link .href(URI.create("https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
.builder() .build())
.rel("down") .addLink(Link.builder()
.type("application/vnd.vmware.vcloud.catalog+xml") .rel("down")
.name("Public") .type("application/vnd.vmware.vcloud.orgNetwork+xml")
.href( .name("internet01-Jclouds")
URI .href(URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c"))
.create("https://vcloudbeta.bluelock.com/api/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c")) .build())
.build()) .addLink(Link.builder()
.addLink( .rel("down")
Link .type("application/vnd.vmware.vcloud.metadata+xml")
.builder() .href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/metadata"))
.rel("down") .build())
.type("application/vnd.vmware.vcloud.controlAccess+xml") .description("")
.href( .fullName("JClouds")
URI .build()
.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c/controlAccess/"))
.build())
.addLink(
Link
.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.orgNetwork+xml")
.name("ilsolation01-Jclouds")
.href(
URI
.create("https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
.build())
.addLink(
Link
.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.orgNetwork+xml")
.name("internet01-Jclouds")
.href(
URI
.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c"))
.build())
.addLink(
Link
.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.metadata+xml")
.href(
URI
.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/metadata"))
.build()).description("").fullName("JClouds").build()
); );
} }
} }