Merge pull request #576 from danikov/vcloud-director-endpoints-and-predicates

Issue 830: vCloud director functions, endpoints and predicates
This commit is contained in:
Adrian Cole 2012-04-19 10:22:01 -07:00
commit d841d2e76a
28 changed files with 1718 additions and 0 deletions

View File

@ -0,0 +1,39 @@
/**
* 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.endpoints;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
* Related to a VCloud Catalog.
*
* @author Adrian Cole
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Qualifier
public @interface Catalog {
}

View File

@ -0,0 +1,39 @@
/**
* 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.endpoints;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
* Related to a VCloud Network.
*
* @author Adrian Cole
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Qualifier
public @interface Network {
}

View File

@ -0,0 +1,39 @@
/**
* 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.endpoints;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
* Related to a VCloud express Org.
*
* @author Adrian Cole
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Qualifier
public @interface Org {
}

View File

@ -0,0 +1,39 @@
/**
* 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.endpoints;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
* Related to a VCloud express Org List.
*
* @author Adrian Cole
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Qualifier
public @interface OrgList {
}

View File

@ -0,0 +1,39 @@
/**
* 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.endpoints;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
* Related to a VCloud express Task List.
*
* @author Adrian Cole
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Qualifier
public @interface TasksList {
}

View File

@ -0,0 +1,40 @@
/**
* 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.endpoints;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
* Represents a component related to vCloud.
*
* @see <a href="https://community.vcloudexpress.terremark.com/en-us/discussion_forums/f/60.aspx" />
* @author Adrian Cole
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Qualifier
public @interface VCloudLogin {
}

View File

@ -0,0 +1,39 @@
/**
* 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.endpoints;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
* Related to a VCloud express Catalog.
*
* @author Adrian Cole
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Qualifier
public @interface Vdc {
}

View File

@ -0,0 +1,81 @@
/**
* 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.functions;
import static com.google.common.collect.Iterables.filter;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Catalog;
import org.jclouds.vcloud.director.v1_5.domain.CatalogItem;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
/**
* @author danikov
*/
@Singleton
public class AllCatalogItemsInCatalog implements Function<Catalog, Iterable<? extends CatalogItem>> {
@Resource
public Logger logger = Logger.NULL;
private final VCloudDirectorAsyncClient aclient;
private final ExecutorService executor;
@Inject
AllCatalogItemsInCatalog(VCloudDirectorAsyncClient aclient, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
this.aclient = aclient;
this.executor = executor;
}
@Override
public Iterable<? extends CatalogItem> apply(Catalog from) {
Iterable<? extends CatalogItem> catalogItems = transformParallel(filter(from.getCatalogItems(), new Predicate<Reference>() {
@Override
public boolean apply(Reference input) {
return input.getType().equals(VCloudDirectorMediaType.CATALOG_ITEM);
}
}), new Function<Reference, Future<? extends CatalogItem>>() {
@Override
public Future<CatalogItem> apply(Reference from) {
return aclient.getCatalogClient().getCatalogItem(from.getHref());
}
}, executor, null, logger, "catalogItems in " + from.getHref());
return catalogItems;
}
}

View File

@ -0,0 +1,59 @@
/**
* 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.functions;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Catalog;
import org.jclouds.vcloud.director.v1_5.domain.CatalogItem;
import org.jclouds.vcloud.director.v1_5.domain.org.Org;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author danikov
*/
@Singleton
public class AllCatalogItemsInOrg implements Function<Org, Iterable<CatalogItem>> {
private final Function<Org, Iterable<Catalog>> allCatalogsInOrg;
private final Function<Catalog, Iterable<CatalogItem>> allCatalogItemsInCatalog;
@Inject
AllCatalogItemsInOrg(Function<Org, Iterable<Catalog>> allCatalogsInOrg,
Function<Catalog, Iterable<CatalogItem>> allCatalogItemsInCatalog) {
this.allCatalogsInOrg = allCatalogsInOrg;
this.allCatalogItemsInCatalog = allCatalogItemsInCatalog;
}
@Override
public Iterable<CatalogItem> apply(Org from) {
return Iterables.concat(Iterables.transform(allCatalogsInOrg.apply(from),
new Function<Catalog, Iterable<CatalogItem>>() {
@Override
public Iterable<CatalogItem> apply(Catalog from) {
return allCatalogItemsInCatalog.apply(from);
}
}));
}
}

View File

@ -0,0 +1,68 @@
/**
* 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.functions;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.concurrent.FutureIterables;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.domain.Catalog;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.org.AdminOrg;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import com.google.common.base.Function;
/**
* @author danikov
*/
@Singleton
public class AllCatalogsInOrg implements Function<AdminOrg, Iterable<? extends Catalog>> {
@Resource
public Logger logger = Logger.NULL;
private final VCloudDirectorAsyncClient aclient;
private final ExecutorService executor;
@Inject
AllCatalogsInOrg(VCloudDirectorAsyncClient aclient, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
this.aclient = aclient;
this.executor = executor;
}
@Override
public Iterable<? extends Catalog> apply(final AdminOrg org) {
Iterable<? extends Catalog> catalogs = FutureIterables.<Reference, Catalog>transformParallel(org.getCatalogs(),
new Function<Reference, Future<? extends Catalog>>() {
@Override
public Future<? extends Catalog> apply(Reference from) {
return aclient.getCatalogClient().getCatalog(from.getHref());
}
}, executor, null, logger, "catalogs in " + org.getName());
return catalogs;
}
}

View File

@ -0,0 +1,71 @@
/**
* 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.functions;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.Vdc;
import org.jclouds.vcloud.director.v1_5.domain.org.AdminOrg;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import com.google.common.base.Function;
/**
* @author danikov
*/
@Singleton
public class AllVdcsInOrg implements Function<AdminOrg, Iterable<? extends Vdc>> {
@Resource
public Logger logger = Logger.NULL;
private final VCloudDirectorAsyncClient aclient;
private final ExecutorService executor;
@Inject
AllVdcsInOrg(VCloudDirectorAsyncClient aclient, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
this.aclient = aclient;
this.executor = executor;
}
@Override
public Iterable<? extends Vdc> apply(final AdminOrg org) {
Iterable<? extends Vdc> catalogItems = transformParallel(org.getVdcs(),
new Function<Reference, Future<? extends Vdc>>() {
@Override
public Future<? extends Vdc> apply(Reference from) {
return aclient.getVdcClient().getVdc(from.getHref());
}
}, executor, null, logger, "vdcs in org " + org.getName());
return catalogItems;
}
}

View File

@ -0,0 +1,54 @@
/**
* 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.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.get;
import java.util.Set;
import javax.annotation.Resource;
import javax.inject.Singleton;
import org.jclouds.dmtf.ovf.Network;
import org.jclouds.dmtf.ovf.NetworkSection;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import com.google.common.base.Function;
@Singleton
public class DefaultNetworkNameInTemplate implements Function<VAppTemplate, String> {
@Resource
protected Logger logger = Logger.NULL;
private SectionForVAppTemplate<NetworkSection> networkSelector =
new SectionForVAppTemplate<NetworkSection>(NetworkSection.class);
@Override
public String apply(VAppTemplate vAppTemplate) {
checkArgument(vAppTemplate != null, "vAppTemplate was null!");
vAppTemplate.getSections();
Set<Network> networks = networkSelector.apply(vAppTemplate).getNetworks();
checkArgument(networks.size() > 0, "no networks found in vAppTemplate %s", vAppTemplate);
if (networks.size() > 1)
logger.warn("multiple networks found for %s, choosing first from: %s", vAppTemplate.getName(), networks);
return get(networks, 0).getName();
}
}

View File

@ -0,0 +1,78 @@
/**
* 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.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates.nameEquals;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.org.AdminOrg;
import org.jclouds.vcloud.director.v1_5.endpoints.Catalog;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameAndCatalogNameToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, AdminOrg>> orgMap;
private final Supplier<Reference> defaultOrg;
private final Supplier<Reference> defaultCatalog;
@Inject
public OrgNameAndCatalogNameToEndpoint(Supplier<Map<String, AdminOrg>> orgMap,
@org.jclouds.vcloud.director.v1_5.endpoints.Org Supplier<Reference> defaultOrg,
@Catalog Supplier<Reference> defaultCatalog) {
this.orgMap = orgMap;
this.defaultOrg = defaultOrg;
this.defaultCatalog = defaultCatalog;
}
@SuppressWarnings("unchecked")
public URI apply(Object from) {
Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args");
Object org = Iterables.get(orgCatalog, 0);
Object catalog = Iterables.get(orgCatalog, 1);
if (org == null && catalog == null)
return defaultCatalog.get().getHref();
else if (org == null)
org = defaultOrg.get().getName();
try {
Set<Reference> catalogs = checkNotNull(orgMap.get().get(org)).getCatalogs();
return catalog == null ? Iterables.getLast(catalogs).getHref() :
Iterables.find(catalogs, nameEquals((String)catalog)).getHref();
} catch (NullPointerException e) {
throw new NoSuchElementException(org + "/" + catalog + " not found in " + orgMap.get());
}
}
}

View File

@ -0,0 +1,77 @@
/**
* 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.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates.nameEquals;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.org.AdminOrg;
import org.jclouds.vcloud.director.v1_5.endpoints.Vdc;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameAndVdcNameToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, AdminOrg>> orgNameToVdcEndpoint;
private final Supplier<Reference> defaultOrg;
private final Supplier<Reference> defaultVdc;
@Inject
public OrgNameAndVdcNameToEndpoint(Supplier<Map<String, AdminOrg>> orgNameToVDCEndpoint,
@org.jclouds.vcloud.director.v1_5.endpoints.Org Supplier<Reference> defaultOrg, @Vdc Supplier<Reference> defaultVdc) {
this.orgNameToVdcEndpoint = orgNameToVDCEndpoint;
this.defaultOrg = defaultOrg;
this.defaultVdc = defaultVdc;
}
@SuppressWarnings("unchecked")
public URI apply(Object from) {
Iterable<Object> orgVdc = (Iterable<Object>) checkNotNull(from, "args");
Object org = Iterables.get(orgVdc, 0);
Object vdc = Iterables.get(orgVdc, 1);
if (org == null && vdc == null)
return defaultVdc.get().getHref();
else if (org == null)
org = defaultOrg.get().getName();
try {
Set<Reference> vdcs = checkNotNull(orgNameToVdcEndpoint.get().get(org)).getVdcs();
return vdc == null ? Iterables.getLast(vdcs).getHref() :
Iterables.find(vdcs, nameEquals((String)vdc)).getHref();
} catch (NullPointerException e) {
throw new NoSuchElementException(org + "/" + vdc + " not found in " + orgNameToVdcEndpoint.get());
}
}
}

View File

@ -0,0 +1,78 @@
/**
* 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.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates.nameEquals;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.Catalog;
import org.jclouds.vcloud.director.v1_5.endpoints.Org;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameCatalogNameItemNameToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, Map<String, Catalog>>> orgCatalogMap;
private final Supplier<Reference> defaultOrg;
private final Supplier<Reference> defaultCatalog;
@Inject
public OrgNameCatalogNameItemNameToEndpoint(
Supplier<Map<String, Map<String, Catalog>>> orgCatalogMap,
@Org Supplier<Reference> defaultOrg,
@org.jclouds.vcloud.director.v1_5.endpoints.Catalog Supplier<Reference> defaultCatalog) {
this.orgCatalogMap = orgCatalogMap;
this.defaultOrg = defaultOrg;
this.defaultCatalog = defaultCatalog;
}
@SuppressWarnings("unchecked")
public URI apply(Object from) {
Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args");
Object org = Iterables.get(orgCatalog, 0);
Object catalog = Iterables.get(orgCatalog, 1);
Object catalogItem = Iterables.get(orgCatalog, 2);
if (org == null)
org = defaultOrg.get().getName();
if (catalog == null)
catalog = defaultCatalog.get().getName();
try {
Map<String, Catalog> catalogs = checkNotNull(orgCatalogMap.get().get(org));
return Iterables.find(catalogs.get(catalog).getCatalogItems(), nameEquals((String)catalogItem)).getHref();
} catch (NullPointerException e) {
throw new NoSuchElementException(org + "/" + catalog + "/" + catalogItem + " not found in "
+ orgCatalogMap.get());
}
}
}

View File

@ -0,0 +1,87 @@
/**
* 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.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.CatalogItem;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.endpoints.Catalog;
import org.jclouds.vcloud.director.v1_5.endpoints.Org;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameCatalogNameVAppTemplateNameToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, Map<String, Map<String, CatalogItem>>>> orgCatalogItemMap;
private final Supplier<Reference> defaultOrg;
private final Supplier<Reference> defaultCatalog;
@Inject
public OrgNameCatalogNameVAppTemplateNameToEndpoint(
Supplier<Map<String, Map<String, Map<String, CatalogItem>>>> orgCatalogItemMap,
@Org Supplier<Reference> defaultOrg, @Catalog Supplier<Reference> defaultCatalog) {
this.orgCatalogItemMap = orgCatalogItemMap;
this.defaultOrg = defaultOrg;
this.defaultCatalog = defaultCatalog;
}
@SuppressWarnings("unchecked")
public URI apply(Object from) {
Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args");
Object org = Iterables.get(orgCatalog, 0);
Object catalog = Iterables.get(orgCatalog, 1);
Object catalogItem = Iterables.get(orgCatalog, 2);
if (org == null)
org = defaultOrg.get().getName();
if (catalog == null)
catalog = defaultCatalog.get().getName();
Map<String, Map<String, Map<String, CatalogItem>>> orgCatalogItemMap = this.orgCatalogItemMap.get();
if (!orgCatalogItemMap.containsKey(org))
throw new NoSuchElementException("org: " + org + " not found in " + orgCatalogItemMap.keySet());
Map<String, Map<String, CatalogItem>> catalogs = orgCatalogItemMap.get(org);
if (!catalogs.containsKey(catalog))
throw new NoSuchElementException("catalog: " + org + "/" + catalog + " not found in " + catalogs.keySet());
Map<String, CatalogItem> catalogMap = catalogs.get(catalog);
if (!catalogMap.containsKey(catalogItem))
throw new NoSuchElementException("item: " + org + "/" + catalog + "/" + catalogItem + " not found in "
+ catalogMap.keySet());
CatalogItem item = catalogMap.get(catalogItem);
return checkNotNull(item.getEntity(), "item: " + org + "/" + catalog + "/" + catalogItem + " has no entity")
.getHref();
}
}

View File

@ -0,0 +1,59 @@
/**
* 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.functions;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.endpoints.Org;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, Reference>> orgNameToEndpointSupplier;
private final Supplier<Reference> defaultOrg;
@Inject
public OrgNameToEndpoint(@Org Supplier<Map<String, Reference>> orgNameToEndpointSupplier,
@Org Supplier<Reference> defaultOrg) {
this.orgNameToEndpointSupplier = orgNameToEndpointSupplier;
this.defaultOrg = defaultOrg;
}
public URI apply(Object from) {
try {
Map<String, Reference> orgNameToEndpoint = orgNameToEndpointSupplier.get();
return from == null ? defaultOrg.get().getHref() : orgNameToEndpoint.get(from).getHref();
} catch (NullPointerException e) {
throw new NoSuchElementException("org " + from + " not found in " + orgNameToEndpointSupplier.get().keySet());
}
}
}

View File

@ -0,0 +1,64 @@
/**
* 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.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.org.Org;
import org.jclouds.vcloud.director.v1_5.endpoints.TasksList;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameToTasksListEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, Org>> orgMap;
private final Supplier<Reference> defaultTasksList;
@Inject
public OrgNameToTasksListEndpoint(Supplier<Map<String, Org>> orgMap,
@TasksList Supplier<Reference> defaultTasksList) {
this.orgMap = orgMap;
this.defaultTasksList = defaultTasksList;
}
public URI apply(Object from) {
Object org = from;
if (org == null)
return defaultTasksList.get().getHref();
try {
return checkNotNull(orgMap.get().get(org)).getHref();
} catch (NullPointerException e) {
throw new NoSuchElementException(org + " not found in " + orgMap.get());
}
}
}

View File

@ -0,0 +1,59 @@
/**
* 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.functions;
import static org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates.nameEquals;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.endpoints.Org;
import org.jclouds.vcloud.director.v1_5.endpoints.Vdc;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameVdcNameNetworkNameToEndpoint extends OrgNameVdcNameResourceNameToEndpoint {
@Inject
public OrgNameVdcNameNetworkNameToEndpoint(
Supplier<Map<String, Map<String, org.jclouds.vcloud.director.v1_5.domain.Vdc>>> orgVdcMap,
@Org Supplier<Reference> defaultOrg, @Vdc Supplier<Reference> defaultVdc) {
super(orgVdcMap, defaultOrg, defaultVdc);
}
protected URI getEndpointOfResourceInVdc(Object org, Object Vdc, Object resource,
org.jclouds.vcloud.director.v1_5.domain.Vdc VdcObject) {
Reference resourceEntity = Iterables.find(VdcObject.getAvailableNetworks(), nameEquals((String)Vdc));
if (resourceEntity == null)
throw new NoSuchElementException("network " + resource + " in Vdc " + Vdc + ", org " + org + " not found in "
+ VdcObject.getAvailableNetworks());
return resourceEntity.getHref();
}
}

View File

@ -0,0 +1,59 @@
/**
* 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.functions;
import static org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates.nameEquals;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.endpoints.Org;
import org.jclouds.vcloud.director.v1_5.endpoints.Vdc;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
/**
*
* @author danikov
*/
@Singleton
public class OrgNameVdcNameResourceEntityNameToEndpoint extends OrgNameVdcNameResourceNameToEndpoint {
@Inject
public OrgNameVdcNameResourceEntityNameToEndpoint(
Supplier<Map<String, Map<String, org.jclouds.vcloud.director.v1_5.domain.Vdc>>> orgVdcMap,
@Org Supplier<Reference> defaultOrg, @Vdc Supplier<Reference> defaultVdc) {
super(orgVdcMap, defaultOrg, defaultVdc);
}
protected URI getEndpointOfResourceInVdc(Object org, Object Vdc, Object resource,
org.jclouds.vcloud.director.v1_5.domain.Vdc VdcObject) {
Reference resourceEntity = Iterables.find(VdcObject.getResourceEntities(), nameEquals((String)resource));
if (resourceEntity == null)
throw new NoSuchElementException("entity " + resource + " in Vdc " + Vdc + ", org " + org + " not found in "
+ VdcObject.getResourceEntities());
return resourceEntity.getHref();
}
}

View File

@ -0,0 +1,79 @@
/**
* 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.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.inject.Inject;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.Vdc;
import org.jclouds.vcloud.director.v1_5.endpoints.Org;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
/**
*
* @author danikov
*/
public abstract class OrgNameVdcNameResourceNameToEndpoint implements Function<Object, URI>{
protected final Supplier<Map<String, Map<String, org.jclouds.vcloud.director.v1_5.domain.Vdc>>> orgVdcMap;
protected final Supplier<Reference> defaultOrg;
protected final Supplier<Reference> defaultVdc;
@Inject
public OrgNameVdcNameResourceNameToEndpoint(
Supplier<Map<String, Map<String, org.jclouds.vcloud.director.v1_5.domain.Vdc>>> orgVdcMap,
@Org Supplier<Reference> defaultOrg, @org.jclouds.vcloud.director.v1_5.endpoints.Vdc Supplier<Reference> defaultVdc) {
this.orgVdcMap = orgVdcMap;
this.defaultOrg = defaultOrg;
this.defaultVdc = defaultVdc;
}
@SuppressWarnings("unchecked")
public URI apply(Object from) {
Iterable<Object> orgVdc = (Iterable<Object>) checkNotNull(from, "args");
Object org = Iterables.get(orgVdc, 0);
Object Vdc = Iterables.get(orgVdc, 1);
Object resource = Iterables.get(orgVdc, 2);
if (org == null)
org = defaultOrg.get().getName();
if (Vdc == null)
Vdc = defaultVdc.get().getName();
Map<String, Map<String, org.jclouds.vcloud.director.v1_5.domain.Vdc>> orgToVdcs = orgVdcMap.get();
checkState(orgToVdcs != null, "could not get map of org name to Vdcs!");
Map<String, org.jclouds.vcloud.director.v1_5.domain.Vdc> Vdcs = orgToVdcs.get(org);
if (Vdcs == null)
throw new NoSuchElementException("org " + org + " not found in " + orgToVdcs.keySet());
org.jclouds.vcloud.director.v1_5.domain.Vdc VdcObject = Vdcs.get(Vdc);
if (VdcObject == null)
throw new NoSuchElementException("Vdc " + Vdc + " in org " + org + " not found in " + Vdcs.keySet());
return getEndpointOfResourceInVdc(org, Vdc, resource, VdcObject);
}
protected abstract URI getEndpointOfResourceInVdc(Object org, Object Vdc, Object resource, Vdc VdcObject);
}

View File

@ -0,0 +1,92 @@
/**
* 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.functions;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.domain.org.Org;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Sets;
/**
* @author danikov
*/
@Singleton
public class OrgsForLocations implements Function<Iterable<Location>, Iterable<? extends Org>> {
@Resource
public Logger logger = Logger.NULL;
private final VCloudDirectorAsyncClient aclient;
private final ExecutorService executor;
@Inject
OrgsForLocations(VCloudDirectorAsyncClient aclient, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
this.aclient = aclient;
this.executor = executor;
}
/**
* Zones are assignable, but we want regions. so we look for zones, whose
* parent is region. then, we use a set to extract the unique set.
*/
@Override
public Iterable<? extends Org> apply(Iterable<Location> from) {
return transformParallel(Sets.newLinkedHashSet(transform(filter(from, new Predicate<Location>() {
@Override
public boolean apply(Location input) {
return input.getScope() == LocationScope.ZONE;
}
}), new Function<Location, URI>() {
@Override
public URI apply(Location from) {
return URI.create(from.getParent().getId());
}
})), new Function<URI, Future<? extends Org>>() {
@Override
public Future<? extends Org> apply(URI from) {
return aclient.getOrgClient().getOrg(from);
}
}, executor, null, logger, "organizations for uris");
}
}

View File

@ -0,0 +1,77 @@
/**
* 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.functions;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import static org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates.nameEquals;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.org.Org;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author danikov
*/
@Singleton
public class OrgsForNames implements Function<Iterable<String>, Iterable<? extends Org>> {
@Resource
public Logger logger = Logger.NULL;
private final VCloudDirectorAsyncClient aclient;
private final VCloudDirectorClient sclient;
private final ExecutorService executor;
@Inject
OrgsForNames(VCloudDirectorAsyncClient aclient,
VCloudDirectorClient sclient,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
this.aclient = aclient;
this.sclient = sclient;
this.executor = executor;
}
@Override
public Iterable<? extends Org> apply(Iterable<String> from) {
final Set<Reference> orgs = sclient.getOrgClient().getOrgList().getOrgs();
return transformParallel(from, new Function<String, Future<? extends Org>>() {
@Override
public Future<? extends Org> apply(String from) {
return aclient.getOrgClient().getOrg(Iterables.find(orgs, nameEquals(from)).getHref());
}
}, executor, null, logger, "organizations for names");
}
}

View File

@ -0,0 +1,39 @@
/*
* 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.functions;
import java.net.URI;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import com.google.common.base.Function;
/**
* @author danikov
*/
@Singleton
public class ReferenceToEndpoint implements Function<Reference, URI> {
@Override
public URI apply(Reference from) {
return from.getHref();
}
}

View File

@ -0,0 +1,37 @@
/*
* 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.functions;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import com.google.common.base.Function;
/**
* @author danikov
*/
@Singleton
public class ReferenceToName implements Function<Reference, String> {
@Override
public String apply(Reference from) {
return from.getName();
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.functions;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.dmtf.ovf.SectionType;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import com.google.common.base.Function;
import com.google.common.collect.Sets;
/**
* @author danikov
*/
@Singleton
public class SectionForVAppTemplate<S extends SectionType> implements Function<VAppTemplate, S> {
private final Class<? extends SectionType> sectionType;
@Inject
SectionForVAppTemplate(Class<S> sectionType) {
this.sectionType = sectionType;
}
@SuppressWarnings("unchecked")
@Override
public S apply(VAppTemplate from) {
Set<S> sections = Sets.newLinkedHashSet();
for (SectionType section : from.getSections()) {
if (sectionType.isAssignableFrom(section.getClass())) {
return (S)section;
}
}
return null;
}
}

View File

@ -0,0 +1,100 @@
/**
* 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.functions;
import static com.google.common.collect.Iterables.filter;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import static org.jclouds.util.Throwables2.propagateOrNull;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.concurrent.ExceptionParsingListenableFuture;
import org.jclouds.concurrent.Futures;
import org.jclouds.logging.Logger;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.CatalogItem;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
/**
* @author danikov
*/
@Singleton
public class VAppTemplatesForCatalogItems implements Function<Iterable<CatalogItem>, Iterable<? extends VAppTemplate>> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
public Logger logger = Logger.NULL;
private final VCloudDirectorAsyncClient aclient;
private final ExecutorService executor;
private final ReturnNullOnAuthorizationException returnNullOnAuthorizationException;
@Singleton
static class ReturnNullOnAuthorizationException implements Function<Exception, VAppTemplate> {
public VAppTemplate apply(Exception from) {
if (from instanceof AuthorizationException) {
return null;
}
return propagateOrNull(from);
}
}
@Inject
VAppTemplatesForCatalogItems(VCloudDirectorAsyncClient aclient,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor,
ReturnNullOnAuthorizationException returnNullOnAuthorizationException) {
this.aclient = aclient;
this.executor = executor;
this.returnNullOnAuthorizationException = returnNullOnAuthorizationException;
}
@Override
public Iterable<? extends VAppTemplate> apply(Iterable<CatalogItem> from) {
return transformParallel(filter(from, new Predicate<CatalogItem>() {
@Override
public boolean apply(CatalogItem input) {
return input.getEntity().getType().equals(VCloudDirectorMediaType.VAPP_TEMPLATE);
}
}), new Function<CatalogItem, Future<? extends VAppTemplate>>() {
@Override
public Future<? extends VAppTemplate> apply(CatalogItem from) {
return new ExceptionParsingListenableFuture<VAppTemplate>(Futures.makeListenable(VCloudDirectorAsyncClient.class
.cast(aclient).getVAppTemplateClient().getVAppTemplate(from.getEntity().getHref()), executor),
returnNullOnAuthorizationException);
}
}, executor, null, logger, "vappTemplates in");
}
}

View File

@ -0,0 +1,70 @@
/**
* 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.functions;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Iterables.filter;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.domain.CatalogItem;
import org.jclouds.vcloud.director.v1_5.domain.ResourceEntity.Status;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.domain.org.Org;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
/**
* @author danikov
*/
@Singleton
public class VAppTemplatesInOrg implements Function<Org, Iterable<VAppTemplate>> {
private final Function<Org, Iterable<CatalogItem>> allCatalogItemsInOrg;
private final Function<Iterable<CatalogItem>, Iterable<VAppTemplate>> vAppTemplatesForCatalogItems;
@Inject
VAppTemplatesInOrg(Function<Org, Iterable<CatalogItem>> allCatalogItemsInOrg,
Function<Iterable<CatalogItem>, Iterable<VAppTemplate>> vAppTemplatesForCatalogItems) {
this.allCatalogItemsInOrg = allCatalogItemsInOrg;
this.vAppTemplatesForCatalogItems = vAppTemplatesForCatalogItems;
}
@Override
public Iterable<VAppTemplate> apply(Org from) {
Iterable<CatalogItem> catalogs = allCatalogItemsInOrg.apply(from);
Iterable<VAppTemplate> vAppTemplates = vAppTemplatesForCatalogItems.apply(catalogs);
return filter(vAppTemplates, and(notNull(), new Predicate<VAppTemplate>(){
//TODO: test this
@Override
public boolean apply(VAppTemplate input) {
if (input == null)
return false;
return ImmutableSet.of(Status.RESOLVED, Status.POWERED_OFF).contains(input.getStatus());
}
}));
}
}