nbafna04 commented on a change in pull request #11: URL: https://github.com/apache/airavata-mft/pull/11#discussion_r418771202
########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveMetadataCollector.java ########## @@ -0,0 +1,146 @@ +/* + * 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.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import org.apache.airavata.mft.core.ResourceMetadata; +import org.apache.airavata.mft.core.api.MetadataCollector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GDriveMetadataCollector implements MetadataCollector { + + private String resourceServiceHost; + private int resourceServicePort; + private String secretServiceHost; + private int secretServicePort; + boolean initialized = false; + + private static final Logger logger = LoggerFactory.getLogger(GDriveMetadataCollector.class); + + + @Override + public void init(String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) { + this.resourceServiceHost = resourceServiceHost; + this.resourceServicePort = resourceServicePort; + this.secretServiceHost = secretServiceHost; + this.secretServicePort = secretServicePort; + this.initialized = true; + } + + + private void checkInitialized() { + if (!initialized) { + throw new IllegalStateException("GDrive Metadata Collector is not initialized"); + } + } + + @Override + public ResourceMetadata getGetResourceMetadata(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + credential = credential.createScoped(scopes); + } + + Drive drive = new Drive.Builder(transport, jsonFactory, credential).build(); + ResourceMetadata metadata = new ResourceMetadata(); + FileList fileList = drive.files().list() + .setQ("name = '"+gdriveResource.getResourcePath()+"'") + .setFields("files(id,name,modifiedTime,md5Checksum,size,mimeType)") + .execute(); + + for (File f : fileList.getFiles()) { Review comment: There are two ways in which the file gets uploaded. 1) If the file has been created locally and uploaded via our implementation, it goes to the drive associated with the service account. 2) If the file exists in the user's drive, then it goes to the drive associated with the user's drive. In both these cases, we use the for loop to update all files with the same name. We use this logic as the user has no way to determine the file IDs to point to a unique file. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
