Skip to content

Commit

Permalink
Adding new action which registers ADB connection (#5291)
Browse files Browse the repository at this point in the history
* Adding new action which registers ADB connection

* minor fixes in AddABAction
  • Loading branch information
jhorvath committed Jan 17, 2023
1 parent b0d67bc commit 8fbcf30
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.cloud.oracle.actions;

import com.oracle.bmc.model.BmcException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.netbeans.modules.cloud.oracle.OCIManager;
import org.netbeans.modules.cloud.oracle.OCIProfile;
import org.netbeans.modules.cloud.oracle.compartment.CompartmentItem;
import org.netbeans.modules.cloud.oracle.compartment.CompartmentNode;
import org.netbeans.modules.cloud.oracle.database.DatabaseItem;
import org.netbeans.modules.cloud.oracle.database.DatabaseNode;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.netbeans.modules.cloud.oracle.items.TenancyItem;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.NotifyDescriptor.QuickPick.Item;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle;

/**
*
* @author Jan Horvath
*/
@ActionID(
category = "Tools",
id = "org.netbeans.modules.cloud.oracle.actions.AddADBAction"
)
@ActionRegistration(
displayName = "#AddADB",
asynchronous = true
)

@ActionReferences(value = {
@ActionReference(path = "Cloud/Oracle/Common/Actions", position = 260)
})
@NbBundle.Messages({
"AddADB=Add Oracle Autonomous DB",
"SelectTenancy=Select Tenancy",
"SelectCompartment=Select Compartment",
"SelectDatabase=Select Database"
})
public class AddADBAction implements ActionListener {
private static final Logger LOGGER = Logger.getLogger(AddADBAction.class.getName());


@Override
public void actionPerformed(ActionEvent e) {
List<TenancyItem> tenancies = new ArrayList<>();
for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) {
p.getTenancy().ifPresent(tenancies::add);
}
Optional<TenancyItem> selectedTenancy = chooseOneItem(tenancies, Bundle.SelectTenancy());

Optional<CompartmentItem> selectedCompartment = Optional.empty();

if (!selectedTenancy.isPresent()) {
return;
}

List<CompartmentItem> compartments = CompartmentNode.getCompartments().apply(selectedTenancy.get());
selectedCompartment = chooseOneItem(compartments, Bundle.SelectCompartment());
DatabaseItem selectedDatabase = null;

if (selectedCompartment.isPresent()) {
while(selectedDatabase == null) {
OCIItem item = chooseCopartmentOrDb(selectedCompartment.get());
if (item == null) {
return;
}
if (item instanceof DatabaseItem) {
selectedDatabase = (DatabaseItem) item;
}
if (item instanceof CompartmentItem) {
selectedCompartment = Optional.of((CompartmentItem) item);
}
}
}
if (selectedDatabase != null) {
DownloadWalletAction action = new DownloadWalletAction(selectedDatabase);
action.actionPerformed(null);
}
}

private <T extends OCIItem> Optional<T> chooseOneItem(List<T> ociItems, String title) {
Optional<T> result = Optional.empty();
if (ociItems.size() == 1) {
result = Optional.of(ociItems.get(0));
} else if (ociItems.size() > 0) {
List<Item> items = ociItems.stream()
.map(tenancy -> new Item(tenancy.getName(), tenancy.getDescription()))
.collect(Collectors.toList());
NotifyDescriptor.QuickPick qp = new NotifyDescriptor.QuickPick(title, title, items, false);
if (DialogDescriptor.OK_OPTION == DialogDisplayer.getDefault().notify(qp)) {
Optional<String> selected = qp.getItems().stream().filter(item -> item.isSelected()).map(item -> item.getLabel()).findFirst();
if (selected.isPresent()) {
result = ociItems.stream().filter(t -> t.getName().equals(selected.get())).findFirst();
}

}
}
return result;
}


private OCIItem chooseCopartmentOrDb(CompartmentItem compartment) {
List<OCIItem> items = new ArrayList<> ();
try {
items.addAll(DatabaseNode.getDatabases().apply(compartment));
} catch (BmcException e) {
LOGGER.log(Level.SEVERE, "Unable to load compartment list", e); // NOI18N
}
items.addAll(CompartmentNode.getCompartments().apply(compartment));
return chooseOneItem(items, Bundle.SelectDatabase()).orElseGet(() -> null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<file name="cloud-resources-commands.instance">
<attr name="instanceCreate" methodvalue="org.netbeans.modules.java.lsp.server.explorer.NodeActionsProvider.forFile"/>
<attr name="action:org.netbeans.modules.cloud.oracle.actions.DownloadWalletAction" stringvalue="Tools"/>
<attr name="action:org.netbeans.modules.cloud.oracle.actions.AddADBAction" stringvalue="Tools"/>
<attr name="action:org.netbeans.modules.cloud.oracle.actions.CreateAutonomousDBAction" stringvalue="Tools"/>
<attr name="action:org.netbeans.modules.cloud.oracle.actions.OpenServiceConsoleAction" stringvalue="Tools"/>
<attr name="action:org.netbeans.modules.cloud.oracle.actions.CloudRefresh" stringvalue="Tools"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.netbeans.modules.java.lsp.server.db;

import com.google.gson.Gson;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import java.net.URL;
import java.sql.DatabaseMetaData;
Expand Down Expand Up @@ -104,7 +105,7 @@ public CompletableFuture<Object> processCommand(NbCodeLanguageClient client, Str
}

if (arguments != null && !arguments.isEmpty()) {
final Map m = gson.fromJson((JsonObject) arguments.get(0), Map.class);
final Map m = arguments.get(0) instanceof JsonNull ? Collections.emptyMap() : gson.fromJson((JsonObject) arguments.get(0), Map.class);
String userId = m != null ? (String) m.get(USER_ID) : null;
String password = m != null ? (String) m.get(PASSWORD) : null;
String dbUrl = m != null ? (String) m.get(DB_URL) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.openide.filesystems.FileUtil;
import org.openide.nodes.Node;
import org.openide.util.ContextAwareAction;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
Expand Down Expand Up @@ -100,15 +99,17 @@ public CompletableFuture<Object> processCommand(NbCodeLanguageClient client, Str
if (!command.startsWith(NBLS_ACTION_PREFIX)) {
return CompletableFuture.completedFuture(false);
}
JsonObject item = gson.fromJson(gson.toJson(arguments.get(0)), JsonObject.class);
JsonElement el = item.get("data"); // NOI18N
JsonElement el = null;
if (arguments.size() > 0) {
JsonObject item = gson.fromJson(gson.toJson(arguments.get(0)), JsonObject.class);
el = item.get("data"); // NOI18N
}
int id = -1;

if (el != null) {
JsonElement nodeId = el.getAsJsonObject().get("id");
JsonElement nodeId = el.getAsJsonObject().get("id"); // NOI18N
if (nodeId != null && nodeId.isJsonPrimitive()) {
id = nodeId.getAsJsonPrimitive().getAsInt();

}
}

Expand Down Expand Up @@ -171,6 +172,11 @@ CompletableFuture<Object> invokeAction(NbCodeLanguageClient client, String categ
FileObject config = FileUtil.getConfigFile(path);
String contextType = (String) config.getAttribute("type"); //NOI18N
try {
if (contextType == null) {
Action a = Actions.forID(category, aid);
a.actionPerformed(new ActionEvent(client, 0, aid));
return CompletableFuture.completedFuture(false);
}
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(contextType);
Object context = gson.fromJson(gson.toJson(arguments.get(0)), clazz);
if (context != null) {
Expand Down
6 changes: 5 additions & 1 deletion java/java.lsp.server/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"viewsWelcome": [
{
"view": "database.connections",
"contents": "No Database Connections found.\n[Add a new connection](command:db.add.connection)"
"contents": "No Database Connections found.\n[Add a new connection](command:db.add.connection)\n[Add Oracle Autonomous DB](command:nbls:Tools:org.netbeans.modules.cloud.oracle.actions.AddADBAction)"
}
],
"configuration": {
Expand Down Expand Up @@ -584,6 +584,10 @@
"command": "nbls:Tools:org.netbeans.modules.cloud.oracle.actions.DownloadWalletAction",
"title": "Add DB Connection"
},
{
"command": "nbls:Tools:org.netbeans.modules.cloud.oracle.actions.AddADBAction",
"title": "Add Oracle Autonomous Database"
},
{
"command": "nbls:Tools:org.netbeans.modules.cloud.oracle.actions.CreateAutonomousDBAction",
"title": "Create Autonomous Database"
Expand Down

0 comments on commit 8fbcf30

Please sign in to comment.