Search in sources :

Example 1 with Builder

use of com.google.api.services.compute.Compute.Builder in project cloudbreak by hortonworks.

the class TagsUtil method checkTagsGcp.

protected static void checkTagsGcp(ApplicationContext applicationContext, String applicationName, String projectId, String serviceAccountId, String p12File, String availabilityZone, Iterable<String> instanceIdList, Map<String, String> tagsToCheckMap) throws Exception {
    String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, p12File);
    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(serviceAccountId).setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE)).setServiceAccountPrivateKey(privateKey).build();
    Compute compute = new Builder(httpTransport, jsonFactory, null).setApplicationName(applicationName).setHttpRequestInitializer(googleCredential).build();
    Instances instances = compute.instances();
    for (String id : instanceIdList) {
        Get response = instances.get(projectId, availabilityZone, id);
        com.google.api.services.compute.model.Instance instance = response.execute();
        Tags gcpTags = instance.getTags();
        Map<String, String> extractedTags = new HashMap<>();
        List<String> tagList = gcpTags.getItems();
        for (String i : tagList) {
            String[] tmpTagList = i.split("-");
            if (tmpTagList.length > 1) {
                extractedTags.put(tmpTagList[0], tmpTagList[1]);
            }
        }
        checkTags(tagsToCheckMap, extractedTags);
        extractedTags.clear();
    }
}
Also used : PrivateKey(java.security.PrivateKey) HashMap(java.util.HashMap) Builder(com.google.api.services.compute.Compute.Builder) AmazonEC2ClientBuilder(com.amazonaws.services.ec2.AmazonEC2ClientBuilder) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Instances(com.google.api.services.compute.Compute.Instances) 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) Get(com.google.api.services.compute.Compute.Instances.Get) Tags(com.google.api.services.compute.model.Tags)

Example 2 with Builder

use of com.google.api.services.compute.Compute.Builder 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 Builder

use of com.google.api.services.compute.Compute.Builder in project cloudbreak by hortonworks.

the class GcpDeleteVpcTest method deleteNetwork.

@AfterSuite
@Parameters("vpcName")
public void deleteNetwork(@Optional("it-vpc") String vpcName) throws Exception {
    springTestContextPrepareTestInstance();
    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();
    Delete delete = compute.networks().delete(defaultProjectId, vpcName);
    Operation operation = delete.execute();
    if (operation.getHttpErrorStatusCode() != null) {
        throw new IllegalStateException("gcp operation failed: " + operation.getHttpErrorMessage());
    }
}
Also used : Delete(com.google.api.services.compute.Compute.Networks.Delete) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Builder(com.google.api.services.compute.Compute.Builder) Compute(com.google.api.services.compute.Compute) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Operation(com.google.api.services.compute.model.Operation) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Parameters(org.testng.annotations.Parameters) AfterSuite(org.testng.annotations.AfterSuite)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)3 HttpTransport (com.google.api.client.http.HttpTransport)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 Compute (com.google.api.services.compute.Compute)3 Builder (com.google.api.services.compute.Compute.Builder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 PrivateKey (java.security.PrivateKey)3 Operation (com.google.api.services.compute.model.Operation)2 HashMap (java.util.HashMap)2 Parameters (org.testng.annotations.Parameters)2 AmazonEC2ClientBuilder (com.amazonaws.services.ec2.AmazonEC2ClientBuilder)1 Instances (com.google.api.services.compute.Compute.Instances)1 Get (com.google.api.services.compute.Compute.Instances.Get)1 Networks (com.google.api.services.compute.Compute.Networks)1 Delete (com.google.api.services.compute.Compute.Networks.Delete)1 Insert (com.google.api.services.compute.Compute.Subnetworks.Insert)1 Network (com.google.api.services.compute.model.Network)1 Subnetwork (com.google.api.services.compute.model.Subnetwork)1 Tags (com.google.api.services.compute.model.Tags)1