Skip to content

Commit

Permalink
Add optional environment variable for specifying a ccloud executable …
Browse files Browse the repository at this point in the history
…path (#58)

* fix: added verbose output for failed ccloud command execution

* feat: add optional environment variable for ccloud executable path

* docs: add optional ccloud path environment var
  • Loading branch information
tball-dev authored Feb 25, 2021
1 parent 309658d commit e537292
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion docs/confluent-cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ Additionally, you'll need to login to the `ccloud` tool. You can automate this b

Then, you can run `ccloud login` and it will run without a prompt. This is great for CI builds.

You can optionally specify a path to a `ccloud` executable:

* `CCLOUD_EXECUTABLE_PATH`: `/full/path/to/ccloud`

Otherwise, `ccloud` must be on your path.

## Validate

First, validate your state file is correct by running:
Expand Down Expand Up @@ -109,4 +115,4 @@ Congrats! You're now using `kafka-gitops` to manage your Confluent Cloud cluster

Welcome to GitOps!

[installation]: /installation.md
[installation]: /installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ConfluentCloudService {
private static org.slf4j.Logger log = LoggerFactory.getLogger(ConfluentCloudService.class);

private final ObjectMapper objectMapper;
private static final String ccloudExecutable;

public ConfluentCloudService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
Expand All @@ -22,10 +23,11 @@ public ConfluentCloudService(ObjectMapper objectMapper) {
public List<ServiceAccount> getServiceAccounts() {
log.info("Fetching service account list from Confluent Cloud via ccloud tool.");
try {
String result = execCmd(new String[]{"ccloud", "service-account", "list", "-o", "json"});
String result = execCmd(new String[]{ccloudExecutable, "service-account", "list", "-o", "json"});
return objectMapper.readValue(result, new TypeReference<List<ServiceAccount>>() {
});
} catch (IOException ex) {
log.info(ex.getMessage());
throw new ConfluentCloudException("There was an error listing Confluent Cloud service accounts. Are you logged in?");
}
}
Expand All @@ -35,7 +37,7 @@ public ServiceAccount createServiceAccount(String name, boolean isUser) {
try {
String serviceName = isUser ? String.format("user-%s", name) : name;
String description = isUser ? String.format("User: %s", name) : String.format("Service account: %s", name);
String result = execCmd(new String[]{"ccloud", "service-account", "create", serviceName, "--description", description, "-o", "json"});
String result = execCmd(new String[]{ccloudExecutable, "service-account", "create", serviceName, "--description", description, "-o", "json"});
return objectMapper.readValue(result, ServiceAccount.class);
} catch (IOException ex) {
throw new ConfluentCloudException(String.format("There was an error creating Confluent Cloud service account: %s.", name));
Expand All @@ -46,4 +48,9 @@ public static String execCmd(String[] cmd) throws java.io.IOException {
java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}

static {
ccloudExecutable = System.getenv("CCLOUD_EXECUTABLE_PATH") != null ? System.getenv("CCLOUD_EXECUTABLE_PATH") : "ccloud";
log.info("Using ccloud executable at: {}", ccloudExecutable);
}
}

0 comments on commit e537292

Please sign in to comment.