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,57 @@
package org.openstack4j.api.storage;

import com.google.common.collect.ImmutableMap;
import org.openstack4j.api.AbstractTest;
import org.openstack4j.model.storage.block.BlockQuotaSet;
import org.openstack4j.openstack.storage.block.domain.CinderBlockQuotaSet;
import org.testng.annotations.Test;

import java.util.Map;

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

@Test(suiteName = "Block Storage Tests")
public class VolumeTypeQuotaTests extends AbstractTest {

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

@Test
public void volumeTypeGigabytesQuota() throws Exception {
respondWith("/storage/v2/updateQuotaSetResponse.json");

String volumeTypeGigabytesQuotaKey = "gigabytes_ruby";
String volumeTypeSnapshotsQuotaKey = "snapshots_ruby";
String volumeTypeVolumesQuotaKey = "volumes_ruby";
String notAVolumeTypeQuotaKey = "i_dont_want_that_key";

BlockQuotaSet blockQuotaSet = new CinderBlockQuotaSet()
.toBuilder()
.volumeTypesQuotas(
ImmutableMap.of(
volumeTypeGigabytesQuotaKey, 100,
volumeTypeSnapshotsQuotaKey, 5,
volumeTypeVolumesQuotaKey, -1
)
)
.build();

blockQuotaSet = osv3().blockStorage().quotaSets().updateForTenant("1-2-3", blockQuotaSet);
String requestBody = server.takeRequest().getBody().readUtf8();

assertTrue(requestBody.contains("\"gigabytes_ruby\" : 100"));
assertTrue(requestBody.contains("\"snapshots_ruby\" : 5"));
assertTrue(requestBody.contains("\"volumes_ruby\" : -1"));

Map<String, Integer> quotas = blockQuotaSet.getVolumeTypesQuotas();

assertTrue(quotas.containsKey(volumeTypeGigabytesQuotaKey), "Should contain the ruby volume type gigabytes quota");
assertTrue(quotas.containsKey(volumeTypeSnapshotsQuotaKey), "Should contain the ruby volume type snapshots quota");
assertTrue(quotas.containsKey(volumeTypeVolumesQuotaKey), "Should contain the ruby volume type volumes quota");

assertFalse(quotas.containsKey(notAVolumeTypeQuotaKey), "Should not contain the " + notAVolumeTypeQuotaKey + " key");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"quota_set": {
"gigabytes_ruby": 100,
"snapshots_ruby": 5,
"volumes_ruby": -1,
"volumes": 5,
"i_dont_want_that_key": 88
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.openstack4j.model.ModelEntity;
import org.openstack4j.model.storage.block.builder.BlockQuotaSetBuilder;

import java.util.Map;

/**
* An OpenStack Quota-Set
*
Expand Down Expand Up @@ -33,4 +35,6 @@ public interface BlockQuotaSet extends ModelEntity, Buildable<BlockQuotaSetBuild
* @return the gigabytes
*/
int getGigabytes();

Map<String, Integer> getVolumeTypesQuotas();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.openstack4j.common.Buildable.Builder;
import org.openstack4j.model.storage.block.BlockQuotaSet;

import java.util.Map;

/**
* Builder for a QuotaSet model class.
*
Expand Down Expand Up @@ -32,4 +34,11 @@ public interface BlockQuotaSetBuilder extends Builder<BlockQuotaSetBuilder, Bloc
* @return space consumed in the Block Storage.
*/
BlockQuotaSetBuilder gigabytes(int gigabytes);

/**
* Quotas limits for each volume type
* @param volumeTypesQuotas
* @return volume types quota limits configured in the Block Storage.
*/
BlockQuotaSetBuilder volumeTypesQuotas(Map<String, Integer> volumeTypesQuotas);
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package org.openstack4j.openstack.storage.block.domain;

import static com.google.common.base.MoreObjects.toStringHelper;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import org.openstack4j.model.storage.block.BlockQuotaSet;
import org.openstack4j.model.storage.block.builder.BlockQuotaSetBuilder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.MoreObjects.toStringHelper;

/**
* An OpenStack Quota-Set
*
* @author Jeremy Unruh
* @author Manuel Mazzuola
*/
@JsonRootName("quota_set")
@JsonIgnoreProperties(ignoreUnknown=true)
public class CinderBlockQuotaSet implements BlockQuotaSet {

private static final long serialVersionUID = 1L;

private static final String VOLUME_TYPE_QUOTA_PREFIXES_REGEX = "(gigabytes|snapshots|volumes)_.*";

@JsonProperty
private String id;
@JsonProperty
Expand All @@ -29,6 +34,8 @@ public class CinderBlockQuotaSet implements BlockQuotaSet {
@JsonProperty
private int gigabytes;

private Map<String, Integer> volumeTypesQuotas = new HashMap<>();

public static BlockQuotaSetBuilder builder() {
return new BlockQuotaSetConcreteBuilder();
}
Expand Down Expand Up @@ -58,6 +65,19 @@ public int getGigabytes() {
return gigabytes;
}

@Override
@JsonAnyGetter
public Map<String, Integer> getVolumeTypesQuotas()
{
return volumeTypesQuotas;
}

@JsonAnySetter
private void setVolumeTypesQuotas(String key, Integer value)
{
if (key.matches( VOLUME_TYPE_QUOTA_PREFIXES_REGEX )) this.volumeTypesQuotas.put(key, value);
}

@Override
public String toString() {
return toStringHelper(this).add("snapshots", snapshots).add("volumes", volumes).add("gigabytes", gigabytes).toString();
Expand Down Expand Up @@ -104,6 +124,12 @@ public BlockQuotaSetBuilder gigabytes(int gigabytes) {
return this;
}

@Override
public BlockQuotaSetBuilder volumeTypesQuotas(Map<String, Integer> volumeTypesQuotas) {
model.volumeTypesQuotas = volumeTypesQuotas;
return this;
}

}

}