org.jclouds.googlecomputeengine.compute.functions.NetworkToSecurityGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of google-compute-engine Show documentation
Show all versions of google-compute-engine Show documentation
jclouds components to access GoogleCompute
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.googlecomputeengine.compute.functions;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.compute.domain.SecurityGroup;
import org.jclouds.compute.domain.SecurityGroupBuilder;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.googlecomputeengine.GoogleComputeEngineApi;
import org.jclouds.googlecomputeengine.config.UserProject;
import org.jclouds.googlecomputeengine.domain.Firewall;
import org.jclouds.googlecomputeengine.domain.Network;
import org.jclouds.googlecomputeengine.options.ListOptions;
import org.jclouds.logging.Logger;
import org.jclouds.net.domain.IpPermission;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
/**
* A function for transforming a GCE-specific Network into a generic
* SecurityGroup object.
*
* @author Andrew Bayer
*/
public class NetworkToSecurityGroup implements Function {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final Function> firewallToPerms;
private final GoogleComputeEngineApi api;
private final Supplier project;
@Inject
public NetworkToSecurityGroup(Function> firewallToPerms,
GoogleComputeEngineApi api,
@UserProject Supplier project) {
this.firewallToPerms = firewallToPerms;
this.api = api;
this.project = project;
}
@Override
public SecurityGroup apply(Network network) {
SecurityGroupBuilder builder = new SecurityGroupBuilder();
builder.id(network.getName());
builder.providerId(network.getId());
builder.name(network.getName());
builder.uri(network.getSelfLink());
ImmutableSet.Builder permBuilder = ImmutableSet.builder();
ListOptions options = new ListOptions.Builder().filter("network eq .*/" + network.getName());
for (Firewall fw : api.getFirewallApiForProject(project.get()).list(options).concat()) {
permBuilder.addAll(firewallToPerms.apply(fw));
}
builder.ipPermissions(permBuilder.build());
return builder.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy