| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.aws.ec2.util; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import com.google.common.base.CaseFormat; |
| 26 | import com.google.common.collect.ImmutableMap; |
| 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.Iterables; |
| 29 | import com.google.common.collect.Maps; |
| 30 | import com.google.common.collect.Sets; |
| 31 | |
| 32 | /** |
| 33 | * @author grkvlt@apache.org |
| 34 | */ |
| 35 | public class TagFilters { |
| 36 | public static enum FilterName { |
| 37 | KEY, RESOURCE_ID, RESOURCE_TYPE, VALUE; |
| 38 | |
| 39 | public String value() { |
| 40 | return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, name()); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public String toString() { |
| 45 | return value(); |
| 46 | } |
| 47 | |
| 48 | public static FilterName fromValue(String name) { |
| 49 | try { |
| 50 | return valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(name, "name"))); |
| 51 | } catch (IllegalArgumentException e) { |
| 52 | return null; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public static enum ResourceType { |
| 58 | CUSTOMER_GATEWAY, DHCP_OPTIONS, IMAGE, INSTANCE, INTERNET_GATEWAY, NETWORK_ACL, RESERVED_INSTANCES, ROUTE_TABLE, SECURITY_GROUP, SNAPSHOT, SPOT_INSTANCES_REQUEST, SUBNET, VOLUME, VPC, VPN_CONNECTION, VPN_GATEWAY; |
| 59 | |
| 60 | public String value() { |
| 61 | return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, name()); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String toString() { |
| 66 | return value(); |
| 67 | } |
| 68 | |
| 69 | public static ResourceType fromValue(String name) { |
| 70 | try { |
| 71 | return valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(name, "name"))); |
| 72 | } catch (IllegalArgumentException e) { |
| 73 | return null; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | protected final Map<FilterName, Iterable<?>> map; |
| 79 | |
| 80 | protected TagFilters() { |
| 81 | map = Maps.<FilterName, Iterable<?>> newLinkedHashMap(); |
| 82 | } |
| 83 | |
| 84 | public static TagFilters filters() { |
| 85 | return new TagFilters(); |
| 86 | } |
| 87 | |
| 88 | public Map<FilterName, Iterable<?>> build() { |
| 89 | return ImmutableMap.copyOf(map); |
| 90 | } |
| 91 | |
| 92 | public TagFilters resourceId(String resourceId) { |
| 93 | put(FilterName.RESOURCE_ID, resourceId); |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | public TagFilters key(String key) { |
| 98 | put(FilterName.KEY, key); |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | public TagFilters keys(String... keys) { |
| 103 | put(FilterName.KEY, ImmutableSet.<String> copyOf(keys)); |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | public TagFilters keys(Iterable<String> keys) { |
| 108 | putAll(FilterName.KEY, ImmutableSet.<String> copyOf(keys)); |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | public TagFilters value(String value) { |
| 113 | put(FilterName.VALUE, value); |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | public TagFilters values(String... values) { |
| 118 | putAll(FilterName.VALUE, ImmutableSet.<String> copyOf(values)); |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | public TagFilters values(Iterable<String> values) { |
| 123 | putAll(FilterName.VALUE, ImmutableSet.<String> copyOf(values)); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | public TagFilters keyContains(String key) { |
| 128 | return key(String.format("*%s*", key)); |
| 129 | } |
| 130 | |
| 131 | public TagFilters valueContains(String value) { |
| 132 | return value(String.format("*%s*", value)); |
| 133 | } |
| 134 | |
| 135 | public TagFilters resourceIdContains(String value) { |
| 136 | return resourceId(String.format("*%s*", value)); |
| 137 | } |
| 138 | |
| 139 | public TagFilters keyStartsWith(String key) { |
| 140 | return key(String.format("%s*", key)); |
| 141 | } |
| 142 | |
| 143 | public TagFilters valueStartsWith(String value) { |
| 144 | return value(String.format("%s*", value)); |
| 145 | } |
| 146 | |
| 147 | public TagFilters resourceIdStartsWith(String value) { |
| 148 | return resourceId(String.format("%s*", value)); |
| 149 | } |
| 150 | |
| 151 | public TagFilters keyEndsWith(String key) { |
| 152 | return key(String.format("*%s", key)); |
| 153 | } |
| 154 | |
| 155 | public TagFilters valueEndsWith(String value) { |
| 156 | return value(String.format("*%s", value)); |
| 157 | } |
| 158 | |
| 159 | public TagFilters resourceIdEndsWith(String value) { |
| 160 | return resourceId(String.format("*%s", value)); |
| 161 | } |
| 162 | |
| 163 | public TagFilters keyValuePair(String key, String value) { |
| 164 | return key(key).value(value); |
| 165 | } |
| 166 | |
| 167 | public TagFilters keyValueSet(String key, Iterable<String> values) { |
| 168 | return key(key).values(values); |
| 169 | } |
| 170 | |
| 171 | public TagFilters keyValueSet(String key, String... values) { |
| 172 | return key(key).values(values); |
| 173 | } |
| 174 | |
| 175 | public TagFilters anyKey() { |
| 176 | putAll(FilterName.KEY, ImmutableSet.<String> of()); |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | public TagFilters anyValue() { |
| 181 | putAll(FilterName.VALUE, ImmutableSet.<String> of()); |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | public TagFilters anyResourceId() { |
| 186 | putAll(FilterName.RESOURCE_TYPE, ImmutableSet.<String> of()); |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | public TagFilters anyResourceType() { |
| 191 | putAll(FilterName.RESOURCE_TYPE, ImmutableSet.<ResourceType> of()); |
| 192 | return this; |
| 193 | } |
| 194 | |
| 195 | public TagFilters resourceType(ResourceType resourceType) { |
| 196 | put(FilterName.RESOURCE_TYPE, resourceType); |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | public TagFilters customerGateway() { |
| 201 | put(FilterName.RESOURCE_TYPE, ResourceType.CUSTOMER_GATEWAY); |
| 202 | return this; |
| 203 | } |
| 204 | |
| 205 | public TagFilters dhcpOptions() { |
| 206 | put(FilterName.RESOURCE_TYPE, ResourceType.DHCP_OPTIONS); |
| 207 | return this; |
| 208 | } |
| 209 | |
| 210 | public TagFilters image() { |
| 211 | put(FilterName.RESOURCE_TYPE, ResourceType.IMAGE); |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | public TagFilters instance() { |
| 216 | put(FilterName.RESOURCE_TYPE, ResourceType.INSTANCE); |
| 217 | return this; |
| 218 | } |
| 219 | |
| 220 | public TagFilters internetGateway() { |
| 221 | put(FilterName.RESOURCE_TYPE, ResourceType.INTERNET_GATEWAY); |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | public TagFilters networkAcl() { |
| 226 | put(FilterName.RESOURCE_TYPE, ResourceType.NETWORK_ACL); |
| 227 | return this; |
| 228 | } |
| 229 | |
| 230 | public TagFilters reservedInstance() { |
| 231 | put(FilterName.RESOURCE_TYPE, ResourceType.RESERVED_INSTANCES); |
| 232 | return this; |
| 233 | } |
| 234 | |
| 235 | public TagFilters routeTable() { |
| 236 | put(FilterName.RESOURCE_TYPE, ResourceType.ROUTE_TABLE); |
| 237 | return this; |
| 238 | } |
| 239 | |
| 240 | public TagFilters securityGroup() { |
| 241 | put(FilterName.RESOURCE_TYPE, ResourceType.SECURITY_GROUP); |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | public TagFilters snapshot() { |
| 246 | put(FilterName.RESOURCE_TYPE, ResourceType.SNAPSHOT); |
| 247 | return this; |
| 248 | } |
| 249 | |
| 250 | public TagFilters instancesRequest() { |
| 251 | put(FilterName.RESOURCE_TYPE, ResourceType.SPOT_INSTANCES_REQUEST); |
| 252 | return this; |
| 253 | } |
| 254 | |
| 255 | public TagFilters subnet() { |
| 256 | put(FilterName.RESOURCE_TYPE, ResourceType.SUBNET); |
| 257 | return this; |
| 258 | } |
| 259 | |
| 260 | public TagFilters volume() { |
| 261 | put(FilterName.RESOURCE_TYPE, ResourceType.VOLUME); |
| 262 | return this; |
| 263 | } |
| 264 | |
| 265 | public TagFilters vpc() { |
| 266 | put(FilterName.RESOURCE_TYPE, ResourceType.VPC); |
| 267 | return this; |
| 268 | } |
| 269 | |
| 270 | public TagFilters vpnConnection() { |
| 271 | put(FilterName.RESOURCE_TYPE, ResourceType.VPN_CONNECTION); |
| 272 | return this; |
| 273 | } |
| 274 | |
| 275 | public TagFilters vpnGateway() { |
| 276 | put(FilterName.RESOURCE_TYPE, ResourceType.VPN_GATEWAY); |
| 277 | return this; |
| 278 | } |
| 279 | |
| 280 | private void put(FilterName key, Object value) { |
| 281 | putAll(key, Sets.newHashSet(value)); |
| 282 | } |
| 283 | |
| 284 | private void putAll(FilterName key, Iterable<?> values) { |
| 285 | if (values == null || Iterables.isEmpty(values)) { |
| 286 | // If we add an empty or null set of values, replace the value in the |
| 287 | // map entirely |
| 288 | map.put(key, ImmutableSet.of()); |
| 289 | } else { |
| 290 | // Add the values, to a new set if required |
| 291 | if (!map.containsKey(key)) { |
| 292 | map.put(key, Sets.newHashSet()); |
| 293 | } |
| 294 | Iterable<?> entries = map.get(key); |
| 295 | map.put(key, Iterables.concat(entries, values)); |
| 296 | } |
| 297 | } |
| 298 | } |