Skip to content

DA-365: Create Bucket on Capella #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ public CouchbaseClusterEntity getParent() {
return null;
}

public String getApiKey() {
return (savedCluster != null) ? savedCluster.getApiKey() : null;
}

public String getApiSecret() {
return (savedCluster != null) ? savedCluster.getApiSecret() : null;
}

@Override
public Stream<? extends CouchbaseClusterEntity> getChild(String name) {
return getChildren().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public static Set<String> listBucketNames(String clusterUrl, boolean ssl, String

}

public static SavedCluster saveDatabaseCredentials(String name, String url, String queryParams, boolean isSSL, String username, String password, String defaultBucket) {
public static SavedCluster saveDatabaseCredentials(String name, String url, String queryParams, boolean isSSL, String username, String password, String defaultBucket,String apiKey, String apiSecret) {
String key = username + ":" + name;
SavedCluster sc = new SavedCluster();
sc.setId(key);
Expand All @@ -563,6 +563,8 @@ public static SavedCluster saveDatabaseCredentials(String name, String url, Stri
sc.setUsername(username);
sc.setUrl(adjustClusterProtocol(url, isSSL));
sc.setDefaultBucket(defaultBucket);
sc.setApiKey(apiKey);
sc.setApiSecret(apiSecret);

Clusters clusters = ClustersStorage.getInstance().getValue();

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/couchbase/intellij/persistence/SavedCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class SavedCluster {

private Long inferCachePeriod;

private String apiKey;
private String apiSecret;

public String getId() {
return id;
}
Expand Down Expand Up @@ -181,4 +184,20 @@ public Boolean getReadOnly() {
public void setReadOnly(Boolean readOnly) {
this.readOnly = readOnly;
}

public String getApiKey() {
return apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public String getApiSecret() {
return apiSecret;
}

public void setApiSecret(String apiSecret) {
this.apiSecret = apiSecret;
}
}
101 changes: 67 additions & 34 deletions src/main/java/com/couchbase/intellij/tree/NewBucketCreationDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.json.JsonObject;
import com.couchbase.client.java.manager.bucket.*;
import com.couchbase.client.protostellar.admin.bucket.v1.EvictionMode;
import com.couchbase.intellij.database.ActiveCluster;
Expand Down Expand Up @@ -83,45 +84,77 @@ public void show(BiConsumer<Boolean, Throwable> listener) {

@Override
protected void doOKAction() {
Cluster cluster = ActiveCluster.getInstance().getCluster();
if (cluster == null) {
Messages.showMessageDialog("There is no active connection to run this query", "Couchbase Plugin Error", Messages.getErrorIcon());
}

BucketSettings bs = BucketSettings.create(bucketName.getText());
bs.bucketType(BucketType.valueOf(bucketTypes.getSelection().getActionCommand()));
bs.storageBackend(StorageBackend.of(storageBackends.getSelection().getActionCommand()));
bs.ramQuotaMB(Long.valueOf(memQuota.getValue().toString()));
if (enableReplicas.isSelected()) {
bs.numReplicas((Integer) replicaNum.getSelectedItem());
if (replicateViewIndexes.isSelected()) {
bs.replicaIndexes(true);
if (ActiveCluster.getInstance().isCapella()) {
JsonObject payload = JsonObject.create()
.put("name", this.bucketName.getText())
.put("type", BucketType.valueOf(bucketTypes.getSelection().getActionCommand()).name().toLowerCase())
.put("storageBackend", StorageBackend.of(storageBackends.getSelection().getActionCommand()).toString().toLowerCase())
.put("memoryAllocationInMb", Long.valueOf(memQuota.getValue().toString()))
.put("replicas", enableReplicas.isSelected() ? (Integer) replicaNum.getSelectedItem() : 1)
.put("replicaIndexes", enableReplicas.isSelected() && replicateViewIndexes.isSelected())
.put("flush", enableFlush.isSelected())
.put("timeToLiveInSeconds", enableTTL.isSelected() ? Long.valueOf(bucketTTL.getText()) : 0)
.put("bucketConflictResolution", ConflictResolutionType.valueOf(conflictResolutionType.getSelection().getActionCommand()).name().toLowerCase())
.put("durabilityLevel", DurabilityLevel.valueOf(minimumDurabilityLevel.getSelectedItem().toString()).name().toLowerCase())
.put("evictionPolicy", ejectionMethod.getSelection() != null ? EvictionPolicyType.valueOf(ejectionMethod.getSelection().getActionCommand()).name().toLowerCase() : "");

try {
String response = CouchbaseRestAPI.callPostSingleEndpoint(
"/v4/organizations/{organizationId}/projects/{projectId}/clusters/{clusterId}/buckets",
ActiveCluster.getInstance().getClusterURL(), payload.toString());
Messages.showMessageDialog(response, "Bucket Creation Response", Messages.getInformationIcon());
if (listener != null) {
listener.accept(false, null);
}
} catch (Exception e) {
Messages.showErrorDialog(e.getMessage(), "Failed to create bucket");
if (listener != null) {
listener.accept(false, e);
}
return;
}
} else {
Cluster cluster = ActiveCluster.getInstance().getCluster();
if (cluster == null) {
Messages.showMessageDialog("There is no active connection to run this query", "Couchbase Plugin Error",
Messages.getErrorIcon());
}
}
if (enableTTL.isSelected()) {
bs.maxExpiry(Duration.of(Long.valueOf(bucketTTL.getText()), ChronoUnit.SECONDS));
}
bs.compressionMode(CompressionMode.valueOf(compressionMode.getSelection().getActionCommand()));
bs.conflictResolutionType(ConflictResolutionType.valueOf(conflictResolutionType.getSelection().getActionCommand()));
if (ejectionMethod.getSelection() != null) {
bs.evictionPolicy(EvictionPolicyType.valueOf(ejectionMethod.getSelection().getActionCommand()));
}
bs.minimumDurabilityLevel(DurabilityLevel.values()[minimumDurabilityLevel.getSelectedIndex()]);
bs.flushEnabled(enableFlush.isSelected());

try {
cluster.buckets().createBucket(bs);
if (listener != null) {
listener.accept(false, null);
BucketSettings bs = BucketSettings.create(bucketName.getText());
bs.bucketType(BucketType.valueOf(bucketTypes.getSelection().getActionCommand()));
bs.storageBackend(StorageBackend.of(storageBackends.getSelection().getActionCommand()));
bs.ramQuotaMB(Long.valueOf(memQuota.getValue().toString()));
if (enableReplicas.isSelected()) {
bs.numReplicas((Integer) replicaNum.getSelectedItem());
if (replicateViewIndexes.isSelected()) {
bs.replicaIndexes(true);
}
}
} catch (Exception e) {
Messages.showErrorDialog(e.getMessage(), "Failed to create bucket");
if (listener != null) {
listener.accept(false, e);
if (enableTTL.isSelected()) {
bs.maxExpiry(Duration.of(Long.valueOf(bucketTTL.getText()), ChronoUnit.SECONDS));
}
return;
}
bs.compressionMode(CompressionMode.valueOf(compressionMode.getSelection().getActionCommand()));
bs.conflictResolutionType(
ConflictResolutionType.valueOf(conflictResolutionType.getSelection().getActionCommand()));
if (ejectionMethod.getSelection() != null) {
bs.evictionPolicy(EvictionPolicyType.valueOf(ejectionMethod.getSelection().getActionCommand()));
}
bs.minimumDurabilityLevel(DurabilityLevel.values()[minimumDurabilityLevel.getSelectedIndex()]);
bs.flushEnabled(enableFlush.isSelected());

try {
cluster.buckets().createBucket(bs);
if (listener != null) {
listener.accept(false, null);
}
} catch (Exception e) {
Messages.showErrorDialog(e.getMessage(), "Failed to create bucket");
if (listener != null) {
listener.accept(false, e);
}
return;
}
}
super.doOKAction();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void handleSaveConnection() {
ConnectionNodeDescriptor userObject = (ConnectionNodeDescriptor) clickedNode.getUserObject();
TreeActionHandler.deleteConnection(clickedNode, userObject, tree);
}
SavedCluster sc = DataLoader.saveDatabaseCredentials(connectionNameTextField.getText(), getBaseUrl(hostTextField.getText()), getQueryParams(hostTextField.getText()), enableSSLCheckBox.isSelected(), usernameTextField.getText(), String.valueOf(passwordField.getPassword()), defaultBucketTextField.getText().trim().isEmpty() ? null : defaultBucketTextField.getText());
SavedCluster sc = DataLoader.saveDatabaseCredentials(connectionNameTextField.getText(), getBaseUrl(hostTextField.getText()), getQueryParams(hostTextField.getText()), enableSSLCheckBox.isSelected(), usernameTextField.getText(), String.valueOf(passwordField.getPassword()), defaultBucketTextField.getText().trim().isEmpty() ? null : defaultBucketTextField.getText(), apiKeyField.getText(), String.valueOf(apiSecretField.getPassword()));
messageLabel.setText("Connection was successful");
TreeActionHandler.connectToCluster(project, sc, tree, null);
close(DialogWrapper.CANCEL_EXIT_CODE);
Expand Down Expand Up @@ -548,7 +548,7 @@ public Class<?> getColumnClass(int columnIndex) {


//TODO: Enable this when the new Capella Rest API is live
//tabbedPane.addTab("Management API Keys", managementAPIKeysPanel);
tabbedPane.addTab("Management API Keys", managementAPIKeysPanel);
// Add "Connection Troubleshooting" tab
thirdPanel = new JBPanel(new BorderLayout());
thirdPanel.setBorder(JBUI.Borders.empty(10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,28 @@ public void actionPerformed(@NotNull AnActionEvent e) {
actionGroup.add(clusterOverview);
actionGroup.addSeparator();

if (!ActiveCluster.getInstance().isReadOnlyMode() && !ActiveCluster.getInstance().isCapella()) {
// Add "Create New Bucket" option
AnAction createNewBucketItem = new AnAction("Create New Bucket") {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
try {
new NewBucketCreationDialog(project).show((cancelled, err) -> {
TreePath treePath = new TreePath(clickedNode.getPath());
tree.collapsePath(treePath);
tree.expandPath(treePath);
tree.invalidate();
});
} catch (Exception ex) {
Log.error("Bucket Creation failed ", ex);
if (!ActiveCluster.getInstance().isReadOnlyMode()) {
boolean isCapella = ActiveCluster.getInstance().isCapella();
boolean hasApiKey = ActiveCluster.getInstance().getApiKey() != null;
boolean hasApiSecret = ActiveCluster.getInstance().getApiSecret() != null;
if ((!isCapella || (hasApiKey && hasApiSecret))) {
AnAction createNewBucketItem = new AnAction("Create New Bucket") {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
try {
new NewBucketCreationDialog(project).show((cancelled, err) -> {
TreePath treePath = new TreePath(clickedNode.getPath());
tree.collapsePath(treePath);
tree.expandPath(treePath);
tree.invalidate();
});
} catch (Exception ex) {
Log.error("Bucket Creation failed ", ex);
}
}
}
};
actionGroup.add(createNewBucketItem);
};
actionGroup.add(createNewBucketItem);
}
}

AnAction refreshBuckets = new AnAction("Refresh Buckets") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static List<String> callAllEndpoints(String endpoint, String cbURL) thro

}

private static String callPostSingleEndpoint(String endpoint, String serverURL, String data) throws Exception {
public static String callPostSingleEndpoint(String endpoint, String serverURL, String data) throws Exception {
List<String> servers = NSLookup.getServerURL(serverURL);
return callEndpoint(false, endpoint, servers.get(0), data);
}
Expand Down