Search in sources :

Example 1 with Network

use of com.google.api.services.compute.model.Network in project google-cloud-java by GoogleCloudPlatform.

the class NetworkInfo method toPb.

Network toPb() {
    Network networkPb = configuration.toPb();
    if (generatedId != null) {
        networkPb.setId(new BigInteger(generatedId));
    }
    if (creationTimestamp != null) {
        networkPb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp));
    }
    networkPb.setName(networkId.getNetwork());
    networkPb.setDescription(description);
    networkPb.setSelfLink(networkId.getSelfLink());
    return networkPb;
}
Also used : Network(com.google.api.services.compute.model.Network) BigInteger(java.math.BigInteger)

Example 2 with Network

use of com.google.api.services.compute.model.Network in project cloudbreak by hortonworks.

the class GcpCreateVirtualNetworkTest method createNetwork.

@Test
@Parameters({ "networkName", "description", "publicInAccount", "resourceGroupName", "vpcName", "vpcSubnet", "subnetCIDR", "networkType" })
public void createNetwork(String networkName, @Optional("") String description, @Optional("false") boolean publicInAccount, @Optional("europe-west1") String subnetRegion, @Optional("it-vpc") String vpcName, @Optional("it-vpc-subnet") String vpcSubnet, @Optional("10.0.36.0/24") String subnetCIDR, NetworkType networkType) throws Exception {
    String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, defaultP12File);
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(defaultServiceAccountId).setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE)).setServiceAccountPrivateKey(privateKey).build();
    Compute compute = new Builder(httpTransport, jsonFactory, null).setApplicationName(defaultName).setHttpRequestInitializer(googleCredential).build();
    Network gcpNetwork = new Network();
    gcpNetwork.setName(vpcName);
    if (!LAGACY_NETWORK.equals(networkType)) {
        gcpNetwork.setAutoCreateSubnetworks(false);
    }
    Networks.Insert networkInsert = compute.networks().insert(defaultProjectId, gcpNetwork);
    Operation networkInsertResponse = networkInsert.execute();
    if (networkInsertResponse.getHttpErrorStatusCode() != null) {
        throw new IllegalStateException("gcp network operation failed: " + networkInsertResponse.getHttpErrorMessage());
    }
    waitOperation(compute, networkInsertResponse);
    if (EXISTING_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
        Subnetwork gcpSubnet = new Subnetwork();
        gcpSubnet.setName(vpcSubnet);
        gcpSubnet.setIpCidrRange(subnetCIDR);
        gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", defaultProjectId, vpcName));
        Insert subNetworkInsert = compute.subnetworks().insert(defaultProjectId, subnetRegion, gcpSubnet);
        Operation subNetInsertResponse = subNetworkInsert.execute();
        if (subNetInsertResponse.getHttpErrorStatusCode() != null) {
            throw new IllegalStateException("gcp subnetwork operation failed: " + subNetInsertResponse.getHttpErrorMessage());
        }
    }
    NetworkRequest networkRequest = new NetworkRequest();
    networkRequest.setName(networkName);
    networkRequest.setDescription(description);
    if (NEW_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
        networkRequest.setSubnetCIDR(subnetCIDR);
    }
    Map<String, Object> map = new HashMap<>();
    map.put("networkId", vpcName);
    if (EXISTING_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
        map.put("subnetId", vpcSubnet);
    }
    networkRequest.setParameters(map);
    networkRequest.setCloudPlatform("GCP");
    String id = getCloudbreakClient().networkEndpoint().postPrivate(networkRequest).getId().toString();
    getItContext().putContextParam(CloudbreakITContextConstants.NETWORK_ID, id, true);
}
Also used : Networks(com.google.api.services.compute.Compute.Networks) PrivateKey(java.security.PrivateKey) Subnetwork(com.google.api.services.compute.model.Subnetwork) HashMap(java.util.HashMap) Builder(com.google.api.services.compute.Compute.Builder) NetworkRequest(com.sequenceiq.cloudbreak.api.model.NetworkRequest) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Subnetworks.Insert) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) Compute(com.google.api.services.compute.Compute) Network(com.google.api.services.compute.model.Network) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test)

Example 3 with Network

use of com.google.api.services.compute.model.Network in project google-cloud-java by GoogleCloudPlatform.

the class HttpComputeRpc method listNetworks.

@Override
public Tuple<String, Iterable<Network>> listNetworks(Map<Option, ?> options) {
    try {
        NetworkList networkList = compute.networks().list(this.options.getProjectId()).setFilter(Option.FILTER.getString(options)).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).setFields(Option.FIELDS.getString(options)).execute();
        Iterable<Network> networks = networkList.getItems();
        return Tuple.of(networkList.getNextPageToken(), networks);
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : NetworkList(com.google.api.services.compute.model.NetworkList) Network(com.google.api.services.compute.model.Network) IOException(java.io.IOException)

Example 4 with Network

use of com.google.api.services.compute.model.Network in project cloudbreak by hortonworks.

the class GcpPlatformResources method securityGroups.

@Override
public CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws IOException {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    Map<String, Set<CloudSecurityGroup>> result = new HashMap<>();
    if (compute != null) {
        FirewallList firewallList = compute.firewalls().list(projectId).execute();
        for (Firewall firewall : firewallList.getItems()) {
            Map<String, Object> properties = new HashMap<>();
            properties.put("network", getNetworkName(firewall));
            CloudSecurityGroup cloudSecurityGroup = new CloudSecurityGroup(firewall.getName(), firewall.getName(), properties);
            result.computeIfAbsent(region.value(), k -> new HashSet<>()).add(cloudSecurityGroup);
        }
    }
    return new CloudSecurityGroups(result);
}
Also used : FirewallList(com.google.api.services.compute.model.FirewallList) Arrays(java.util.Arrays) CloudVmTypes(com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes) CloudIpPools(com.sequenceiq.cloudbreak.cloud.model.CloudIpPools) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Cacheable(org.springframework.cache.annotation.Cacheable) HashMap(java.util.HashMap) Network(com.google.api.services.compute.model.Network) StringUtils(org.apache.commons.lang3.StringUtils) CloudGateWays(com.sequenceiq.cloudbreak.cloud.model.CloudGateWays) CloudNetworks(com.sequenceiq.cloudbreak.cloud.model.CloudNetworks) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) Strings(com.google.common.base.Strings) NetworkList(com.google.api.services.compute.model.NetworkList) Firewall(com.google.api.services.compute.model.Firewall) CloudSshKeys(com.sequenceiq.cloudbreak.cloud.model.CloudSshKeys) Service(org.springframework.stereotype.Service) Map(java.util.Map) RegionList(com.google.api.services.compute.model.RegionList) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) VmTypeMeta(com.sequenceiq.cloudbreak.cloud.model.VmTypeMeta) Subnetwork(com.google.api.services.compute.model.Subnetwork) PlatformResources(com.sequenceiq.cloudbreak.cloud.PlatformResources) VmTypeMetaBuilder(com.sequenceiq.cloudbreak.cloud.model.VmTypeMeta.VmTypeMetaBuilder) Region.region(com.sequenceiq.cloudbreak.cloud.model.Region.region) Set(java.util.Set) IOException(java.io.IOException) CloudAccessConfigs(com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfigs) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) VmType(com.sequenceiq.cloudbreak.cloud.model.VmType) MachineType(com.google.api.services.compute.model.MachineType) List(java.util.List) MachineTypeList(com.google.api.services.compute.model.MachineTypeList) CloudSecurityGroups(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroups) FirewallList(com.google.api.services.compute.model.FirewallList) Collections(java.util.Collections) Compute(com.google.api.services.compute.Compute) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) CloudSecurityGroup(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Firewall(com.google.api.services.compute.model.Firewall) CloudSecurityGroups(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroups) Compute(com.google.api.services.compute.Compute) CloudSecurityGroup(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup) HashSet(java.util.HashSet)

Example 5 with Network

use of com.google.api.services.compute.model.Network in project cloudbreak by hortonworks.

the class GcpPlatformResources method networks.

@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws Exception {
    Compute compute = GcpStackUtil.buildCompute(cloudCredential);
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    Map<String, Set<CloudNetwork>> result = new HashMap<>();
    Set<CloudNetwork> cloudNetworks = new HashSet<>();
    if (compute != null) {
        NetworkList networkList = compute.networks().list(projectId).execute();
        List<Subnetwork> subnetworkList = compute.subnetworks().list(projectId, region.value()).execute().getItems();
        for (Network network : networkList.getItems()) {
            Map<String, Object> properties = new HashMap<>();
            properties.put("gatewayIPv4", Strings.nullToEmpty(network.getGatewayIPv4()));
            properties.put("description", Strings.nullToEmpty(network.getDescription()));
            properties.put("IPv4Range", Strings.nullToEmpty(network.getIPv4Range()));
            properties.put("creationTimestamp", Strings.nullToEmpty(network.getCreationTimestamp()));
            Map<String, String> subnets = new HashMap<>();
            if (subnetworkList != null && network.getSubnetworks() != null) {
                for (Subnetwork subnetwork : subnetworkList) {
                    if (network.getSubnetworks().contains(subnetwork.getSelfLink())) {
                        subnets.put(subnetwork.getName(), subnetwork.getName());
                    }
                }
            }
            CloudNetwork cloudNetwork = new CloudNetwork(network.getName(), network.getId().toString(), subnets, properties);
            cloudNetworks.add(cloudNetwork);
        }
        result.put(region.value(), cloudNetworks);
    }
    return new CloudNetworks(result);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Subnetwork(com.google.api.services.compute.model.Subnetwork) HashMap(java.util.HashMap) CloudNetworks(com.sequenceiq.cloudbreak.cloud.model.CloudNetworks) NetworkList(com.google.api.services.compute.model.NetworkList) Compute(com.google.api.services.compute.Compute) Network(com.google.api.services.compute.model.Network) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) HashSet(java.util.HashSet)

Aggregations

Network (com.google.api.services.compute.model.Network)5 Compute (com.google.api.services.compute.Compute)3 NetworkList (com.google.api.services.compute.model.NetworkList)3 Subnetwork (com.google.api.services.compute.model.Subnetwork)3 HashMap (java.util.HashMap)3 CloudNetwork (com.sequenceiq.cloudbreak.cloud.model.CloudNetwork)2 CloudNetworks (com.sequenceiq.cloudbreak.cloud.model.CloudNetworks)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Builder (com.google.api.services.compute.Compute.Builder)1 Networks (com.google.api.services.compute.Compute.Networks)1 Insert (com.google.api.services.compute.Compute.Subnetworks.Insert)1 Firewall (com.google.api.services.compute.model.Firewall)1 FirewallList (com.google.api.services.compute.model.FirewallList)1 MachineType (com.google.api.services.compute.model.MachineType)1