Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.openstack4j.api.network;

import org.openstack4j.api.AbstractTest;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.network.Resource;
import org.openstack4j.openstack.networking.domain.NeutronResourceTag;
import org.testng.annotations.Test;

import java.util.List;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

/**
* Test cases for (Neutron) Resource Tag based Services
*
* @author bboyHan
*/
@Test(suiteName="NeutronTagTests")
public class NeutronTagTests extends AbstractTest {

private static final String JSON_SECURITY_GROUP_TAGS = "/network/tags.json";

@Test
public void listTags() throws Exception {
respondWith(JSON_SECURITY_GROUP_TAGS);
NeutronResourceTag sgTags = osv3().networking().resourceTags().list(Resource.NETWORK, "1");
List<String> tags = sgTags.getTags();
assertEquals(2, tags.size());
assertEquals("tag1", tags.get(0));
}

@Test
public void replaceTags() {
String jsonResponse = "{\"tags\": [\"newTag1\", \"newTag2\"]}";
respondWith(200, jsonResponse);
NeutronResourceTag sgTags = new NeutronResourceTag();
sgTags.addTag("newTag1");
sgTags.addTag("newTag2");
NeutronResourceTag newTags = osv3().networking().resourceTags().replace(Resource.NETWORK, "1", sgTags);

assertEquals(sgTags.getTags(), newTags.getTags());
}

@Test
public void deleteAllTags() {
respondWith(204);
ActionResponse delete = osv3().networking().resourceTags().deleteAll(Resource.NETWORK, "1");
System.out.println(delete.getCode());
assertTrue(delete.isSuccess());
}

@Test
public void checkTag() {
respondWith(204);
ActionResponse check = osv3().networking().resourceTags().check(Resource.NETWORK, "1", "tag1");
assertTrue(check.isSuccess());
}

@Test
public void addTag() {
respondWith(204);
ActionResponse check = osv3().networking().resourceTags().addSingle(Resource.NETWORK, "1", "tag");
assertTrue(check.isSuccess());
}

@Test
public void deleteTag() {
respondWith(204);
ActionResponse delete = osv3().networking().resourceTags().delete(Resource.NETWORK, "1", "tag1");
assertTrue(delete.isSuccess());
}

@Override
protected Service service() {
return Service.NETWORK;
}

}
3 changes: 3 additions & 0 deletions core-test/src/main/resources/network/tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tags": ["tag1", "tag2"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public interface NetworkingService extends RestService {
*/
SecurityGroupService securitygroup();

/**
*
* @return the (Neutron) Resource Tag Service API
*/
NeutronResourceTagService resourceTags();

/**
*
* @return the Security Group Rule Service API
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.openstack4j.api.networking;

import org.openstack4j.common.RestService;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.network.Resource;
import org.openstack4j.openstack.networking.domain.NeutronResourceTag;

/**
* @author bboyHan
*/
public interface NeutronResourceTagService extends RestService {

/**
* Confirms a given tag is set on the resource.
*
* @param resource resource type
* @param resourceId resource id
* @param tag tag
* @return the ActionResponse
*/
ActionResponse check(Resource resource, String resourceId, String tag);

/**
* Obtains the (Neutron) resource tags for a resource.
*
* @param resource resource type
* @param resourceId resource id
* @return the NeutronResourceTag
*/
NeutronResourceTag list(Resource resource, String resourceId);

/**
* Add tag to the resource.
*
* @param resourceId resource id
* @param tag tag
* @return the ActionResponse
*/
ActionResponse addSingle(Resource resource, String resourceId, String tag);

/**
* reset all tags from the resource
*
* @param resourceId resource id
* @param tags tags
* @return the NeutronResourceTag
*/
NeutronResourceTag replace(Resource resource, String resourceId, NeutronResourceTag tags);

/**
* Delete tag from the resource.
*
* @param resourceId resource id
* @param tag tag
* @return the ActionResponse
*/
ActionResponse delete(Resource resource, String resourceId, String tag);

/**
* Remove all tags from the resource
*
* @param resourceId resource id
* @return the ActionResponse
*/
ActionResponse deleteAll(Resource resource, String resourceId);

}
36 changes: 36 additions & 0 deletions core/src/main/java/org/openstack4j/model/network/Resource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.openstack4j.model.network;

/**
* The resource of Neutron
*
* @author bboyHan
*/
public enum Resource {

SECURITY_GROUP("security-groups"),
NETWORK("networks"),
SUBNET("subnets"),
PORT("ports"),
ROUTER("routers"),
SUBNET_POOL("subnetpools"),
FLOATING_IP("floatingips"),
QOS_POLICY("policies"),
TRUNK("trunks");

private final String value;

Resource(String value) {
this.value = value;
}

public static String forValue(Resource resource) {
if (resource != null) {
for (Resource s : Resource.values()) {
if (s.name().equalsIgnoreCase(resource.name()))
return s.value;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ protected <R> Invocation<R> get(Class<R> returnType, String... path) {
return builder(returnType, path, HttpMethod.GET);
}

protected Invocation<ActionResponse> getWithResponse(String... path) {
return builder(ActionResponse.class, path, HttpMethod.GET);
}

protected <R> Invocation<R> post(Class<R> returnType, String... path) {
return builder(returnType, path, HttpMethod.POST);
}
Expand All @@ -68,6 +72,10 @@ protected <R> Invocation<R> put(Class<R> returnType, String... path) {
return builder(returnType, path, HttpMethod.PUT);
}

protected Invocation<ActionResponse> putWithResponse(String... path) {
return builder(ActionResponse.class, path, HttpMethod.PUT);
}

protected <R> Invocation<R> patch(Class<R> returnType, String... path) {
return builder(returnType, path, HttpMethod.PATCH);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.openstack4j.openstack.networking.domain;

import com.google.common.base.MoreObjects;
import org.openstack4j.model.ModelEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Neutron Resource Tags Entity
*
* @author bboyHan
*/
public class NeutronResourceTag implements ModelEntity {

private static final long serialVersionUID = 1L;

private List<String> tags = new ArrayList<>();

public List<String> getTags() {
return tags;
}

public void setTags(List<String> tags) {
this.tags = tags;
}

public void addTag(String... tags) {
this.tags.addAll(Arrays.asList(tags));
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).omitNullValues().add("tags", tags).toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package org.openstack4j.openstack.networking.internal;

import org.openstack4j.api.Apis;
import org.openstack4j.api.networking.AvailabilityZoneService;
import org.openstack4j.api.networking.NetFloatingIPService;
import org.openstack4j.api.networking.NetworkService;
import org.openstack4j.api.networking.NetworkingService;
import org.openstack4j.api.networking.PortService;
import org.openstack4j.api.networking.RouterService;
import org.openstack4j.api.networking.SecurityGroupRuleService;
import org.openstack4j.api.networking.SecurityGroupService;
import org.openstack4j.api.networking.SubnetService;
import org.openstack4j.api.networking.TrunkService;
import org.openstack4j.api.networking.*;
import org.openstack4j.api.networking.ext.AgentService;
import org.openstack4j.api.networking.ext.FirewallAsService;
import org.openstack4j.api.networking.ext.LbaasV2Service;
Expand Down Expand Up @@ -73,6 +64,14 @@ public SecurityGroupService securitygroup() {
return Apis.get(SecurityGroupService.class);
}

/**
* {@inheritDoc}
*/
@Override
public NeutronResourceTagService resourceTags() {
return Apis.get(NeutronResourceTagService.class);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.openstack4j.openstack.networking.internal;

import org.openstack4j.api.networking.NeutronResourceTagService;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.network.Resource;
import org.openstack4j.openstack.networking.domain.NeutronResourceTag;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Allows users to set (Neutron) Resource tags on their resources.
*
* @author bboyHan
*/
public class NeutronResourceTagServiceImpl extends BaseNetworkingServices implements NeutronResourceTagService {

@Override
public ActionResponse check(Resource resource, String resourceId, String tag) {
checkNotNull(resourceId);
checkNotNull(tag);
checkNotNull(resource);
String resourceType = checkNotNull(Resource.forValue(resource));
return getWithResponse(uri("/%s/%s/tags/%s", resourceType ,resourceId, tag)).execute();
}

/**
* {@inheritDoc}
*/
@Override
public NeutronResourceTag list(Resource resource, String securityGroupId) {
checkNotNull(securityGroupId);
checkNotNull(resource);
String resourceType = checkNotNull(Resource.forValue(resource));
return get(NeutronResourceTag.class, uri("/%s/%s/tags", resourceType, securityGroupId)).execute();
}

/**
* {@inheritDoc}
*/
@Override
public ActionResponse addSingle(Resource resource, String securityGroupId, String tag) {
checkNotNull(securityGroupId);
checkNotNull(tag);
checkNotNull(resource);
String resourceType = checkNotNull(Resource.forValue(resource));
return putWithResponse(uri("/%s/%s/tags/%s", resourceType, securityGroupId, tag)).execute();
}

/**
* {@inheritDoc}
*/
@Override
public NeutronResourceTag replace(Resource resource, String securityGroupId, NeutronResourceTag tags) {
checkNotNull(securityGroupId);
checkNotNull(tags);
checkNotNull(resource);
String resourceType = checkNotNull(Resource.forValue(resource));
return put(NeutronResourceTag.class, uri("/%s/%s/tags", resourceType, securityGroupId)).entity(tags).execute();
}

/**
* {@inheritDoc}
*/
@Override
public ActionResponse delete(Resource resource, String securityGroupId, String tag) {
checkNotNull(securityGroupId);
checkNotNull(tag);
checkNotNull(resource);
String resourceType = checkNotNull(Resource.forValue(resource));
return deleteWithResponse(uri("/%s/%s/tags/%s", resourceType, securityGroupId, tag)).execute();
}

/**
* {@inheritDoc}
*/
@Override
public ActionResponse deleteAll(Resource resource, String securityGroupId) {
checkNotNull(securityGroupId);
checkNotNull(resource);
String resourceType = checkNotNull(Resource.forValue(resource));
return deleteWithResponse(uri("/%s/%s/tags", resourceType, securityGroupId)).execute();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public void initialize() {
bind(NetworkIPAvailabilityService.class, NetworkIPAvailabilityServiceImpl.class);
bind(TrunkService.class, TrunkServiceImpl.class);
bind(ServerActionsService.class, NovaServerActionsService.class);
bind(NeutronResourceTagService.class, NeutronResourceTagServiceImpl.class);
bind(PortForwardingService.class, PortForwardingServiceImpl.class);
}

Expand Down