Skip to content

Commit 5181a86

Browse files
committed
CDAP-14477 Remove bucket from GCS Create/Delete action
1 parent 83f53e8 commit 5181a86

File tree

6 files changed

+32
-59
lines changed

6 files changed

+32
-59
lines changed

docs/GCSBucketCreate-action.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ Properties
3030
**Project ID**: Google Cloud Project ID, which uniquely identifies a project.
3131
It can be found on the Dashboard in the Google Cloud Platform Console.
3232

33-
**Bucket Name**: Bucket to create objects in.
34-
35-
**Objects to Create**: Objects to create in the bucket.
33+
**Objects to Create**: Comma separated list of objects to create.
3634

3735
**Fail if Object Exists**: Whether to fail the pipeline if an object already exists.
3836

docs/GCSBucketDelete-action.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ Properties
3030
**Project ID**: The Google Cloud Project ID, which uniquely identifies a project.
3131
It can be found on the Dashboard in the Google Cloud Platform Console.
3232

33-
**Bucket Name**: The bucket to delete objects from.
34-
35-
**Objects to Delete**: Objects to delete from the bucket.
33+
**Objects to Delete**: Comma separated list of objects to delete.
3634

3735
**Service Account File Path**: Path on the local file system of the service account key used for
3836
authorization. Can be set to 'auto-detect' when running on a Dataproc cluster.

src/main/java/co/cask/gcp/gcs/actions/GCSBucketCreate.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import co.cask.cdap.etl.api.action.Action;
2424
import co.cask.cdap.etl.api.action.ActionContext;
2525
import co.cask.gcp.common.GCPConfig;
26+
import co.cask.gcp.gcs.GCSConfigHelper;
2627
import org.apache.hadoop.conf.Configuration;
2728
import org.apache.hadoop.fs.FileSystem;
2829
import org.apache.hadoop.fs.Path;
@@ -31,7 +32,9 @@
3132

3233
import java.io.IOException;
3334
import java.util.ArrayList;
35+
import java.util.Arrays;
3436
import java.util.List;
37+
import java.util.stream.Collectors;
3538

3639

3740
/**
@@ -58,18 +61,12 @@ public void run(ActionContext context) throws Exception {
5861
String projectId = config.getProject();
5962
configuration.set("fs.gs.project.id", projectId);
6063

61-
configuration.set("fs.gs.system.bucket", config.bucket);
6264
configuration.setBoolean("fs.gs.impl.disable.cache", true);
6365
configuration.setBoolean("fs.gs.metadata.cache.enable", false);
6466

65-
String[] paths = config.paths.split(",");
6667
List<Path> gcsPaths = new ArrayList<>();
67-
for (String path : paths) {
68-
String gcsPath = String.format("gs://%s/%s", config.bucket, path);
69-
if (path.startsWith("/")) {
70-
gcsPath = String.format("gs://%s%s", config.bucket, path);
71-
}
72-
gcsPaths.add(new Path(gcsPath));
68+
for (String path : config.getPaths()) {
69+
gcsPaths.add(new Path(GCSConfigHelper.getPath(path)));
7370
}
7471

7572
FileSystem fs;
@@ -97,10 +94,10 @@ public void run(ActionContext context) throws Exception {
9794
throw e;
9895
}
9996
} else {
100-
if (config.failIfExists.equalsIgnoreCase("yes")) {
97+
if (config.getFailIfExists()) {
10198
rollback = true;
10299
throw new Exception(
103-
String.format("Object %s already exists")
100+
String.format("Object %s already exists", gcsPath)
104101
);
105102
}
106103
}
@@ -126,20 +123,22 @@ public void run(ActionContext context) throws Exception {
126123
* Config for the plugin.
127124
*/
128125
public final class Config extends GCPConfig {
129-
130-
@Name("bucket")
131-
@Description("Name of the bucket.")
132-
@Macro
133-
public String bucket;
134-
135126
@Name("paths")
136-
@Description("List of objects to be created within a bucket")
127+
@Description("Comma separated list of objects to be created.")
137128
@Macro
138-
public String paths;
129+
private String paths;
139130

140131
@Name("failIfExists")
141132
@Description("Fail if path exists.")
142133
@Macro
143-
public String failIfExists;
134+
private boolean failIfExists;
135+
136+
public List<String> getPaths() {
137+
return Arrays.stream(paths.split(",")).map(String::trim).collect(Collectors.toList());
138+
}
139+
140+
public boolean getFailIfExists() {
141+
return failIfExists;
142+
}
144143
}
145144
}

src/main/java/co/cask/gcp/gcs/actions/GCSBucketDelete.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import co.cask.cdap.etl.api.action.Action;
2424
import co.cask.cdap.etl.api.action.ActionContext;
2525
import co.cask.gcp.common.GCPConfig;
26+
import co.cask.gcp.gcs.GCSConfigHelper;
2627
import org.apache.hadoop.conf.Configuration;
2728
import org.apache.hadoop.fs.FileSystem;
2829
import org.apache.hadoop.fs.Path;
@@ -31,12 +32,13 @@
3132

3233
import java.io.IOException;
3334
import java.util.ArrayList;
35+
import java.util.Arrays;
3436
import java.util.List;
37+
import java.util.stream.Collectors;
3538

3639

3740
/**
38-
* This action plugin <code>GCSBucketDelete</code> provides a way to delete
39-
* directories within a given GCS bucket.
41+
* This action plugin <code>GCSBucketDelete</code> provides a way to delete directories within a given GCS bucket.
4042
*/
4143
@Plugin(type = Action.PLUGIN_TYPE)
4244
@Name(GCSBucketDelete.NAME)
@@ -59,17 +61,11 @@ public void run(ActionContext context) throws Exception {
5961
String projectId = config.getProject();
6062
configuration.set("fs.gs.project.id", projectId);
6163

62-
configuration.set("fs.gs.system.bucket", config.bucket);
6364
configuration.setBoolean("fs.gs.impl.disable.cache", true);
6465

65-
String[] paths = config.paths.split(",");
6666
List<Path> gcsPaths = new ArrayList<>();
67-
for (String path : paths) {
68-
String gcsPath = String.format("gs://%s/%s", config.bucket, path);
69-
if (path.startsWith("/")) {
70-
gcsPath = String.format("gs://%s%s", config.bucket, path);
71-
}
72-
gcsPaths.add(new Path(gcsPath));
67+
for (String path : config.getPaths()) {
68+
gcsPaths.add(new Path(GCSConfigHelper.getPath(path)));
7369
}
7470

7571
FileSystem fs;
@@ -98,15 +94,13 @@ public void run(ActionContext context) throws Exception {
9894
* Config for the plugin.
9995
*/
10096
public final class Config extends GCPConfig {
101-
102-
@Name("bucket")
103-
@Description("Name of the bucket.")
104-
@Macro
105-
public String bucket;
106-
10797
@Name("paths")
108-
@Description("List of objects to be deleted within a bucket")
98+
@Description("Comma separated list of objects to be deleted.")
10999
@Macro
110-
public String paths;
100+
private String paths;
101+
102+
public List<String> getPaths() {
103+
return Arrays.stream(paths.split(",")).map(String::trim).collect(Collectors.toList());
104+
}
111105
}
112106
}

widgets/GCSBucketCreate-action.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
"default": "auto-detect"
1616
}
1717
},
18-
{
19-
"widget-type": "textbox",
20-
"label": "Bucket Name",
21-
"name": "bucket",
22-
"widget-attributes" : {
23-
"placeholder": "Bucket to create objects in"
24-
}
25-
},
2618
{
2719
"name": "paths",
2820
"widget-type": "csv",

widgets/GCSBucketDelete-action.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
"default": "auto-detect"
1616
}
1717
},
18-
{
19-
"widget-type": "textbox",
20-
"label": "Bucket Name",
21-
"name": "bucket",
22-
"widget-attributes" : {
23-
"placeholder": "Bucket to delete objects from"
24-
}
25-
},
2618
{
2719
"name": "paths",
2820
"widget-type": "csv",

0 commit comments

Comments
 (0)