Skip to content

Commit b919953

Browse files
Add change for File Identifier, macro for Oauth 2 fields.
Add change for File Identifier, macro for Oauth 2 fields.
1 parent d0a8b64 commit b919953

10 files changed

Lines changed: 131 additions & 21 deletions

docs/GoogleDrive-batchsource.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ https://drive.google.com/drive/folders/1dyUEebJaFnWa3Z4n0BFMVAXQ7mfUH11g?resourc
1717
```
1818
Then the Directory Identifier would be `1dyUEebJaFnWa3Z4n0BFMVAXQ7mfUH11g`.
1919

20+
**File Identifier:** Identifier of the file.
21+
22+
This comes after `file/d/ or document/d/ or spreadsheets/d/` in the URL. For example, if the URL is
23+
```
24+
https://docs.google.com/file/d/17W3vOhBwe0i24OdVNsbz8rAMClzUitKeAbumTqWFrkows
25+
```
26+
Then the File Identifier would be `17W3vOhBwe0i24OdVNsbz8rAMClzUitKeAbumTqWFrkows`.
27+
Either Directory Identifier or File Identifier should have a value. Filters will not work
28+
while providing File Identifier.
29+
2030
**File Metadata Properties:** Properties that represent metadata of files.
2131
They will be a part of output structured record. Descriptions for properties can be view at
2232
[Drive API file reference](https://developers.google.com/drive/api/v3/reference/files).

docs/GoogleSheets-batchsource.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ https://drive.google.com/drive/folders/1dyUEebJaFnWa3Z4n0BFMVAXQ7mfUH11g?resourc
1717
```
1818
Then the Directory Identifier would be `1dyUEebJaFnWa3Z4n0BFMVAXQ7mfUH11g`.
1919

20+
**File Identifier:** Identifier of the spreadsheet file.
21+
22+
This comes after `spreadsheets/d/` in the URL. For example, if the URL is
23+
```
24+
https://docs.google.com/spreadsheets/d/17W3vOhBwe0i24OdVNsbz8rAMClzUitKeAbumTqWFrkows
25+
```
26+
Then the File Identifier would be `17W3vOhBwe0i24OdVNsbz8rAMClzUitKeAbumTqWFrkows`.
27+
Either Directory Identifier or File Identifier should have a value. Filters will not work
28+
while providing File Identifier.
29+
2030
### Filtering
2131

2232
**Filter:** Filter that can be applied to the files in the selected directory.

src/main/java/io/cdap/plugin/google/common/GoogleAuthBaseConfig.java

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public abstract class GoogleAuthBaseConfig extends PluginConfig {
4949
public static final String REFRESH_TOKEN_LABEL = "Refresh token";
5050
public static final String ACCOUNT_FILE_PATH = "accountFilePath";
5151
public static final String DIRECTORY_IDENTIFIER = "directoryIdentifier";
52+
public static final String FILE_IDENTIFIER = "fileIdentifier";
5253
public static final String NAME_SERVICE_ACCOUNT_TYPE = "serviceAccountType";
5354
public static final String NAME_SERVICE_ACCOUNT_JSON = "serviceAccountJSON";
5455
public static final String SERVICE_ACCOUNT_FILE_PATH = "filePath";
@@ -77,16 +78,19 @@ public abstract class GoogleAuthBaseConfig extends PluginConfig {
7778
protected String serviceAccountType;
7879

7980
@Nullable
81+
@Macro
8082
@Name(CLIENT_ID)
8183
@Description("OAuth2 client id.")
8284
private String clientId;
8385

8486
@Nullable
87+
@Macro
8588
@Name(CLIENT_SECRET)
8689
@Description("OAuth2 client secret.")
8790
private String clientSecret;
8891

8992
@Nullable
93+
@Macro
9094
@Name(REFRESH_TOKEN)
9195
@Description("OAuth2 refresh token.")
9296
private String refreshToken;
@@ -108,12 +112,22 @@ public abstract class GoogleAuthBaseConfig extends PluginConfig {
108112
@Macro
109113
protected String serviceAccountJson;
110114

115+
@Nullable
116+
@Macro
111117
@Name(DIRECTORY_IDENTIFIER)
112118
@Description("Identifier of the folder. This comes after “folders/” in the URL. For example, if the URL was " +
113119
"“https://drive.google.com/drive/folders/1dyUEebJaFnWa3Z4n0BFMVAXQ7mfUH11g?resourcekey=0-XVijrJSp3E3gkdJp20MpCQ”, "
114120
+ "then the Directory Identifier would be “1dyUEebJaFnWa3Z4n0BFMVAXQ7mfUH11g”.")
115121
private String directoryIdentifier;
116122

123+
@Nullable
124+
@Macro
125+
@Name(FILE_IDENTIFIER)
126+
@Description("Identifier of the file. This comes after “file/d/ or spreadsheets/d/ or document/d/” in the URL. " +
127+
"For example, if the URL was “https://drive.google.com/file/d/16npTpL3ozkAzB5kLQ-oQD3IlTZhnnh2w1/view”, "
128+
+ "then the File Identifier would be “16npTpL3ozkAzB5kLQ-oQD3IlTZhnnh2w1”.")
129+
private String fileIdentifier;
130+
117131
/**
118132
* Returns the ValidationResult.
119133
*
@@ -122,7 +136,7 @@ public abstract class GoogleAuthBaseConfig extends PluginConfig {
122136
*/
123137
public ValidationResult validate(FailureCollector collector) {
124138
IdUtils.validateReferenceName(referenceName, collector);
125-
139+
checkIfDirectoryOrFileIdentifierExists(collector);
126140
ValidationResult validationResult = new ValidationResult();
127141
if (validateAuthType(collector)) {
128142
AuthType authType = getAuthType();
@@ -147,9 +161,10 @@ public ValidationResult validate(FailureCollector collector) {
147161
validateCredentials(collector, client);
148162

149163
// validate directory
150-
validateDirectoryIdentifier(collector, client);
151-
152-
validationResult.setDirectoryAccessible(true);
164+
validateDirectoryAndFileIdentifier(collector, client);
165+
if (!(containsMacro(FILE_IDENTIFIER) || containsMacro(DIRECTORY_IDENTIFIER))) {
166+
validationResult.setDirectoryOrFileAccessible(true);
167+
}
153168
} catch (Exception e) {
154169
collector.addFailure(
155170
String.format("Exception during authentication/directory properties check: %s.", e.getMessage()),
@@ -177,6 +192,9 @@ private boolean validateAuthType(FailureCollector collector) {
177192
}
178193

179194
private boolean validateOAuth2Properties(FailureCollector collector) {
195+
if (containsMacro(CLIENT_ID) || containsMacro(CLIENT_SECRET) || containsMacro(REFRESH_TOKEN)) {
196+
return false;
197+
}
180198
return checkPropertyIsSet(collector, clientId, CLIENT_ID, CLIENT_ID_LABEL)
181199
& checkPropertyIsSet(collector, clientSecret, CLIENT_SECRET, CLIENT_SECRET_LABEL)
182200
& checkPropertyIsSet(collector, refreshToken, REFRESH_TOKEN, REFRESH_TOKEN_LABEL);
@@ -222,9 +240,10 @@ private void validateCredentials(FailureCollector collector, GoogleDriveClient d
222240
}
223241
}
224242

225-
private void validateDirectoryIdentifier(FailureCollector collector, GoogleDriveClient driveClient)
243+
private void validateDirectoryAndFileIdentifier(FailureCollector collector, GoogleDriveClient driveClient)
226244
throws IOException {
227-
if (!containsMacro(DIRECTORY_IDENTIFIER)) {
245+
246+
if (directoryIdentifier != null && !containsMacro(DIRECTORY_IDENTIFIER)) {
228247
try {
229248
driveClient.isFolderAccessible(directoryIdentifier);
230249
} catch (GoogleJsonResponseException e) {
@@ -233,6 +252,26 @@ private void validateDirectoryIdentifier(FailureCollector collector, GoogleDrive
233252
.withStacktrace(e.getStackTrace());
234253
}
235254
}
255+
256+
if (fileIdentifier != null && !containsMacro(FILE_IDENTIFIER)) {
257+
try {
258+
driveClient.isFileAccessible(fileIdentifier);
259+
} catch (GoogleJsonResponseException e) {
260+
collector.addFailure(e.getDetails().getMessage(), "Provide an existing file identifier.")
261+
.withConfigProperty(FILE_IDENTIFIER)
262+
.withStacktrace(e.getStackTrace());
263+
}
264+
}
265+
}
266+
267+
protected void checkIfDirectoryOrFileIdentifierExists(FailureCollector collector) {
268+
if (directoryIdentifier == null && !containsMacro(DIRECTORY_IDENTIFIER) &&
269+
fileIdentifier == null && !containsMacro(FILE_IDENTIFIER)) {
270+
collector.addFailure("Both Directory Identifier and File Identifier can not be null.",
271+
"Provide either Directory Identifier or File Identifier.")
272+
.withConfigProperty(DIRECTORY_IDENTIFIER)
273+
.withConfigProperty(FILE_IDENTIFIER);
274+
}
236275
}
237276

238277
protected boolean checkPropertyIsSet(FailureCollector collector, String propertyValue, String propertyName,
@@ -256,6 +295,10 @@ public String getDirectoryIdentifier() {
256295
return directoryIdentifier;
257296
}
258297

298+
public String getFileIdentifier() {
299+
return fileIdentifier;
300+
}
301+
259302
public AuthType getAuthType() {
260303
return AuthType.fromValue(authType);
261304
}
@@ -283,6 +326,9 @@ public void setAccountFilePath(String accountFilePath) {
283326
public void setDirectoryIdentifier(String directoryIdentifier) {
284327
this.directoryIdentifier = directoryIdentifier;
285328
}
329+
public void setFileIdentifier(String fileIdentifier) {
330+
this.fileIdentifier = fileIdentifier;
331+
}
286332

287333
public void setClientId(String clientId) {
288334
this.clientId = clientId;

src/main/java/io/cdap/plugin/google/common/GoogleDriveClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ public void checkRootFolder() throws IOException {
113113
public void isFolderAccessible(String folderId) throws IOException {
114114
service.files().get(folderId).setSupportsAllDrives(true).execute();
115115
}
116+
117+
public void isFileAccessible(String fileId) throws IOException {
118+
service.files().get(fileId).setSupportsAllDrives(true).execute();
119+
}
116120
}

src/main/java/io/cdap/plugin/google/common/GoogleDriveFilteringClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public List<File> getFilesSummary(List<ExportedType> exportedTypes, int filesNum
7171
String nextToken = "";
7272
int retrievedFiles = 0;
7373
int actualFilesNumber = filesNumber;
74+
if (config.getFileIdentifier() != null) {
75+
files.add(service.files().get(config.getFileIdentifier()).setSupportsAllDrives(true).execute());
76+
return files;
77+
}
7478
Drive.Files.List request = service.files().list()
7579
.setSupportsAllDrives(true)
7680
.setIncludeItemsFromAllDrives(true)

src/main/java/io/cdap/plugin/google/common/ValidationResult.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
* Wrapper to save validation results. Is used to transfer validation results.
2121
*/
2222
public class ValidationResult {
23-
private boolean directoryAccessible = false;
23+
private boolean directoryOrFileAccessible = false;
2424

25-
public boolean isDirectoryAccessible() {
26-
return directoryAccessible;
25+
public boolean isDirectoryOrFileAccessible() {
26+
return directoryOrFileAccessible;
2727
}
2828

29-
public void setDirectoryAccessible(boolean directoryAccessible) {
30-
this.directoryAccessible = directoryAccessible;
29+
public void setDirectoryOrFileAccessible(boolean directoryOrFileAccessible) {
30+
this.directoryOrFileAccessible = directoryOrFileAccessible;
3131
}
3232
}

src/main/java/io/cdap/plugin/google/drive/source/GoogleDriveSourceConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ public static GoogleDriveSourceConfig of(JsonObject properties) throws IOExcepti
324324
googleDriveSourceConfig.setDirectoryIdentifier(
325325
properties.get(GoogleDriveSourceConfig.DIRECTORY_IDENTIFIER).getAsString());
326326
}
327+
if (properties.has(GoogleDriveSourceConfig.FILE_IDENTIFIER)) {
328+
googleDriveSourceConfig.setFileIdentifier(
329+
properties.get(GoogleDriveSourceConfig.FILE_IDENTIFIER).getAsString());
330+
}
327331
if (properties.has(GoogleDriveSourceConfig.FILTER)) {
328332
googleDriveSourceConfig.setFilter(properties.get(GoogleDriveSourceConfig.FILTER).getAsString());
329333
}

src/main/java/io/cdap/plugin/google/sheets/source/GoogleSheetsSourceConfig.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public class GoogleSheetsSourceConfig extends GoogleFilteringSourceConfig {
117117
@Name(NAME_SCHEMA)
118118
@Description("The schema of the table to read.")
119119
@Macro
120-
private transient Schema schema = null;
120+
private String schema;
121121

122122
@Name(FORMATTING)
123123
@Description("Output format for numeric sheet cells. " +
@@ -267,22 +267,26 @@ public GoogleSheetsSourceConfig(String referenceName) {
267267
* @param collector throws validation exception
268268
*/
269269
public Schema getSchema(FailureCollector collector) {
270-
if (schema == null && shouldGetSchema()) {
270+
Schema existingSchema = this.getSchema();
271+
if (existingSchema == null && shouldGetSchema()) {
271272
if (dataSchemaInfo.isEmpty()) {
272273
collector.addFailure("There are no headers to process.",
273274
"Perhaps no validation step was executed before schema generation.")
274275
.withConfigProperty(SCHEMA);
275276
}
276-
schema = SchemaBuilder.buildSchema(this, new ArrayList<>(dataSchemaInfo.values()));
277+
existingSchema = SchemaBuilder.buildSchema(this, new ArrayList<>(dataSchemaInfo.values()));
277278
}
278-
return schema;
279+
return existingSchema;
279280
}
280281

281282
private boolean shouldGetSchema() {
282283
return !containsMacro(SHEETS_TO_PULL) && !containsMacro(SHEETS_IDENTIFIERS) &&
283284
!containsMacro(COLUMN_NAMES_SELECTION) && !containsMacro(CUSTOM_COLUMN_NAMES_ROW) &&
284285
!containsMacro(LAST_DATA_COLUMN) && !containsMacro(NAME_SERVICE_ACCOUNT_TYPE) &&
285-
!containsMacro(ACCOUNT_FILE_PATH) && !containsMacro(NAME_SERVICE_ACCOUNT_JSON);
286+
!containsMacro(ACCOUNT_FILE_PATH) && !containsMacro(NAME_SERVICE_ACCOUNT_JSON) &&
287+
!containsMacro(CLIENT_ID) && !containsMacro(CLIENT_SECRET) &&
288+
!containsMacro(REFRESH_TOKEN) && !containsMacro(FILE_IDENTIFIER) &&
289+
!containsMacro(DIRECTORY_IDENTIFIER);
286290
}
287291

288292
/**
@@ -300,7 +304,8 @@ public ValidationResult validate(FailureCollector collector) {
300304
validateLastDataColumnIndexAndLastRowIndex(collector);
301305
validateSpreadsheetAndSheetFieldNames(collector);
302306

303-
if (collector.getValidationFailures().isEmpty() && validationResult.isDirectoryAccessible()) {
307+
if (collector.getValidationFailures().isEmpty() &&
308+
(validationResult.isDirectoryOrFileAccessible())) {
304309
GoogleDriveFilteringClient driveClient;
305310
GoogleSheetsSourceClient sheetsSourceClient;
306311
try {
@@ -315,7 +320,7 @@ public ValidationResult validate(FailureCollector collector) {
315320
spreadsheetsFiles = driveClient
316321
.getFilesSummary(Collections.singletonList(ExportedType.SPREADSHEETS), 1);
317322
} catch (ExecutionException | RetryException e) {
318-
collector.addFailure("Invalid search query, see https://developers.google.com/drive/api/v3/ref-search-terms",
323+
collector.addFailure(String.format("Failed while getting file schema due to reason : %s", e.getMessage()),
319324
null).withStacktrace(e.getStackTrace());
320325
return validationResult;
321326
}
@@ -1010,7 +1015,7 @@ public void setSheetsIdentifiers(String sheetsIdentifiers) {
10101015
}
10111016

10121017
public void setSchema(String schema) throws IOException {
1013-
this.schema = Schema.parseJson(schema);
1018+
this.schema = schema;
10141019
}
10151020

10161021
public void setFormatting(String formatting) {
@@ -1257,7 +1262,19 @@ public static GoogleSheetsSourceConfig of(JsonObject properties) throws IOExcept
12571262
googleSheetsSourceConfig.setDirectoryIdentifier(
12581263
properties.get(GoogleSheetsSourceConfig.DIRECTORY_IDENTIFIER).getAsString());
12591264
}
1260-
1265+
if (properties.has(GoogleSheetsSourceConfig.FILE_IDENTIFIER)) {
1266+
googleSheetsSourceConfig.setFileIdentifier(
1267+
properties.get(GoogleSheetsSourceConfig.FILE_IDENTIFIER).getAsString());
1268+
}
12611269
return googleSheetsSourceConfig;
12621270
}
1271+
@Nullable
1272+
public Schema getSchema() {
1273+
try {
1274+
return !this.containsMacro("schema") &&
1275+
!Strings.isNullOrEmpty(this.schema) ? Schema.parseJson(this.schema) : null;
1276+
} catch (Exception var2) {
1277+
throw new IllegalArgumentException("Invalid schema: " + var2.getMessage(), var2);
1278+
}
1279+
}
12631280
}

widgets/GoogleDrive-batchsource.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"label": "Directory Identifier",
1818
"name": "directoryIdentifier"
1919
},
20+
{
21+
"widget-type": "textbox",
22+
"label": "File Identifier",
23+
"name": "fileIdentifier"
24+
},
2025
{
2126
"name": "fileMetadataProperties",
2227
"widget-type": "multi-select",

widgets/GoogleSheets-batchsource.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"widget-type": "textbox",
1717
"label": "Directory Identifier",
1818
"name": "directoryIdentifier"
19+
},
20+
{
21+
"widget-type": "textbox",
22+
"label": "File Identifier",
23+
"name": "fileIdentifier"
1924
}
2025
]
2126
},
@@ -366,7 +371,12 @@
366371
]
367372
}
368373
],
369-
"outputs": [],
374+
"outputs": [
375+
{
376+
"name": "schema",
377+
"widget-type": "schema"
378+
}
379+
],
370380
"filters": [
371381
{
372382
"name": "Select modification date range",

0 commit comments

Comments
 (0)