Skip to content

Commit

Permalink
[Google] Expose functions region in account
Browse files Browse the repository at this point in the history
  • Loading branch information
avano committed Nov 7, 2022
1 parent 5a2e133 commit 9b3033d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package software.tnb.google.cloud.functions.account;

import software.tnb.google.cloud.common.account.GoogleCloudAccount;

public class GoogleFunctionsAccount extends GoogleCloudAccount {
private String region = "us-central1";

public String region() {
return region;
}

public void setRegion(String region) {
this.region = region;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import software.tnb.common.account.AccountFactory;
import software.tnb.common.service.Service;
import software.tnb.common.service.ServiceFactory;
import software.tnb.google.cloud.common.account.GoogleCloudAccount;
import software.tnb.google.cloud.functions.account.GoogleFunctionsAccount;
import software.tnb.google.cloud.functions.validation.GoogleFunctionsValidation;
import software.tnb.google.storage.service.GoogleStorage;

Expand All @@ -23,15 +23,12 @@
public class GoogleFunctions implements Service {
private final GoogleStorage storage = ServiceFactory.create(GoogleStorage.class);

private GoogleCloudAccount account;
private GoogleFunctionsAccount account;
private GoogleFunctionsValidation validation;

public GoogleFunctions() {
}

public GoogleCloudAccount account() {
public GoogleFunctionsAccount account() {
if (account == null) {
account = AccountFactory.create(GoogleCloudAccount.class);
account = AccountFactory.create(GoogleFunctionsAccount.class);
}
return account;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package software.tnb.google.cloud.functions.validation;

import software.tnb.common.service.Validation;
import software.tnb.common.utils.WaitUtils;
import software.tnb.google.cloud.common.account.GoogleCloudAccount;
import software.tnb.google.cloud.functions.account.GoogleFunctionsAccount;
import software.tnb.google.storage.service.GoogleStorage;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -28,22 +29,20 @@
import java.util.Map;
import java.util.stream.Collectors;

public class GoogleFunctionsValidation {
public class GoogleFunctionsValidation implements Validation {
private static final Logger LOG = LoggerFactory.getLogger(GoogleFunctionsValidation.class);

private final GoogleCloudAccount account;
private final GoogleFunctionsAccount account;
private final CloudFunctionsServiceClient client;
private final GoogleStorage storage;

private final String defaultRegion = "us-central1";
private final String bucketPrefix = "function-";

// Save the function names, as the client needs CloudFunctionName (consists of project/location/name)
// So that the user doesn't need to specify the region in get/delete methods for example
// The CloudFunctionName is the key as it is always unique
private final Map<String, String> functionNames;

public GoogleFunctionsValidation(GoogleCloudAccount account, CloudFunctionsServiceClient client, GoogleStorage storage) {
public GoogleFunctionsValidation(GoogleFunctionsAccount account, CloudFunctionsServiceClient client, GoogleStorage storage) {
this.account = account;
this.client = client;
this.storage = storage;
Expand Down Expand Up @@ -91,15 +90,15 @@ public void createFunction(String name, String region, String runtime, String en
}

public void createFunction(String name, String runtime, String entryPoint, Path zipSource) {
createFunction(name, defaultRegion, runtime, entryPoint, zipSource);
createFunction(name, account.region(), runtime, entryPoint, zipSource);
}

public CloudFunction getFunction(String name) {
return client.getFunction(getFunctionName(name));
}

public List<CloudFunction> listFunctions() {
return listFunctions(defaultRegion);
return listFunctions(account.region());
}

public List<CloudFunction> listFunctions(String region) {
Expand All @@ -109,8 +108,8 @@ public List<CloudFunction> listFunctions(String region) {
return functions;
}

public String getUrl(String name) {
return getFunction(getFunctionName(name)).getHttpsTrigger().getUrl();
public String getUrl(String functionName) {
return getFunction(getFunctionName(functionName)).getHttpsTrigger().getUrl();
}

public void deleteFunction(String functionName) {
Expand Down

0 comments on commit 9b3033d

Please sign in to comment.