Skip to content

Commit

Permalink
Adding cloud assets
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorvath committed Jun 5, 2024
1 parent 136e384 commit b948eda
Show file tree
Hide file tree
Showing 44 changed files with 2,278 additions and 67 deletions.
8 changes: 8 additions & 0 deletions enterprise/cloud.oracle/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@
<specification-version>1.87</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.refactoring.api</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>1.72</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.server</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,7 @@ protected Node[] createNodesForKey(OCIItem key) {
NodeProvider nodeProvider = Lookups.forPath(
String.format("Cloud/Oracle/%s/Nodes", key.getKey().getPath()))
.lookup(NodeProvider.class);
if (nodeProvider instanceof NodeProvider.SessionAware) {
return new Node[]{((NodeProvider.SessionAware)nodeProvider).apply(key, session)};
} else {
return OCIManager.usingSession(session, () ->
new Node[]{nodeProvider.apply(key)}
);
}
return new Node[]{nodeProvider.apply(key, session)};
}

public void refreshKeys() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public interface NodeProvider<T extends OCIItem> {

public Node apply(T t);

public default Node apply(T t, OCISessionInitiator i) {
return OCIManager.usingSession(i, () -> apply(t));
}

public interface SessionAware<T extends OCIItem> extends NodeProvider<T> {
public default Node apply(T t) {
return apply(t, OCIManager.getDefault().getActiveSession());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ private void refresh() {
}

public void addConnectedProfile(OCIProfile profile) {
if (profile.getTenantId() == null) {
if (!profile.getTenancy().isPresent()) {
throw new IllegalArgumentException("Broken profiles are not supported.");
}
List<OCIProfile> current = getConnectedProfiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ public Region getRegion() {
return configProvider.getRegion();
}

@Override
public String getTenantId() {
return configProvider == null ? null : configProvider.getTenantId();
}

public Tenancy getTenancyData() {
if (configProvider == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.oracle.bmc.Region;
import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import java.util.Optional;
import org.netbeans.modules.cloud.oracle.items.TenancyItem;
import org.openide.util.Lookup;

/**
Expand All @@ -32,6 +34,6 @@
public interface OCISessionInitiator extends Lookup.Provider {
public BasicAuthenticationDetailsProvider getAuthenticationProvider();
public <T> T newClient(Class<T> clientClass);
public String getTenantId();
public Optional<TenancyItem> getTenancy();
public Region getRegion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ private void refresh() {
}
List<ProfileKey> currentKeys = new ArrayList<>();
for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) {
ProfileKey k = new ProfileKey(p.getConfigPath(), p.getId(), p.getTenantId());
String tenanctId = null;
if (p.getTenancy().isPresent()) {
tenanctId = p.getTenancy().get().getKey().getValue();
}
ProfileKey k = new ProfileKey(p.getConfigPath(), p.getId(), tenanctId);
ServerInstance prev = newInstances.get(k);
if (prev != null) {
OCIProfile prevProf = prev.getLookup().lookup(OCIProfile.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public TenancyInstance(OCIItem tenancy, OCIProfile profile) {
public String getDisplayName() {
if (tenancy != null) {
return Bundle.MSG_TenancyDesc(tenancy.getName(), profile.getId());
} else if (profile.getTenantId() != null) {
return Bundle.MSG_BrokenTenancy(profile.getTenantId(), profile.getId());
} else if (profile.getTenancy().isPresent()) {
return Bundle.MSG_BrokenTenancy(profile.getTenancy().get().getKey().getValue(), profile.getId());
} else {
return Bundle.MSG_BrokenProfile(profile.getId());
}
Expand All @@ -70,7 +70,8 @@ public Lookup getLookup() {

@Override
public String getServerDisplayName() {
return profile.getTenantId() != null ? profile.getTenantId() : Bundle.MSG_BrokenProfile(profile);
return profile.getTenancy().isPresent() ? profile.getTenancy().get().getKey().getValue()
: Bundle.MSG_BrokenProfile(profile);
}

public OCIProfile getProfile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public class AddADBAction implements ActionListener {
})
@Override
public void actionPerformed(ActionEvent e) {
addADB();
}

public DatabaseItem addADB() {
Map<String, Object> result = new HashMap<> ();

NotifyDescriptor.ComposedInput ci = new NotifyDescriptor.ComposedInput(Bundle.AddADB(), NUMBER_OF_INPUTS, new Callback() {
Expand Down Expand Up @@ -211,10 +215,12 @@ public NotifyDescriptor createInput(NotifyDescriptor.ComposedInput input, int nu
selectedDatabase.getKey().getValue(),
selectedDatabase.getCompartmentId());
action.addConnection(info);
return selectedDatabase;
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
return null;
}

private <T extends OCIItem> NotifyDescriptor.QuickPick createQuickPick(Map<String, T> ociItems, String title) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void addConnection(DownloadWalletDialog.WalletInfo p) {
}
Properties props = new Properties();
props.put("OCID", p.getOcid()); //NOI18N
props.put("CompartmentOCID", p.getOcid()); //NOI18N
props.put("CompartmentOCID", p.getComaprtmentId()); //NOI18N
String dbUrl = MessageFormat.format(URL_TEMPLATE, connectionName, BaseUtilities.escapeParameters(new String[] { walletPath.toString() }));
DatabaseConnection dbConn = DatabaseConnection.create(
drivers[0],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* 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.assets;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.cloud.oracle.assets.Steps.ItemTypeStep;
import org.netbeans.modules.cloud.oracle.assets.Steps.ProjectStep;
import org.netbeans.modules.cloud.oracle.assets.Steps.SuggestedContext;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.netbeans.modules.project.dependency.ArtifactSpec;
import org.netbeans.modules.project.dependency.Dependency;
import org.netbeans.modules.project.dependency.DependencyChange;
import org.netbeans.modules.project.dependency.DependencyChangeException;
import org.netbeans.modules.project.dependency.ProjectDependencies;
import org.netbeans.modules.project.dependency.ProjectOperationException;
import org.netbeans.modules.refactoring.spi.ModificationResult;
import org.netbeans.modules.project.dependency.Scope;
import org.netbeans.spi.lsp.CommandProvider;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.Pair;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ServiceProvider;

/**
*
* @author Jan Horvath
*/
@NbBundle.Messages({
"NoProjects=No Project Found",
"SelectProject=Select Project to Update Dependencies",
"SelectResourceType=Select Resource Type"})
@ServiceProvider(service = CommandProvider.class)
public class AddNewAssetCommand implements CommandProvider {

private static final String COMMAND_ADD_NEW_ASSET = "nbls.cloud.assets.add.new"; //NOI18N

private static final Set COMMANDS = new HashSet<>(Arrays.asList(
COMMAND_ADD_NEW_ASSET
));

private static final Map<String, String[]> DEP_MAP = new HashMap() {
{
put("Databases", new String[]{"io.micronaut.oraclecloud", "micronaut-oraclecloud-atp"}); //NOI18N
put("Bucket", new String[]{"io.micronaut.objectstorage", "micronaut-object-storage-oracle-cloud"}); //NOI18N
put("Vault", new String[]{"io.micronaut.oraclecloud", "micronaut-oraclecloud-vault"}); //NOI18N
}
};

@Override
public Set<String> getCommands() {
return Collections.unmodifiableSet(COMMANDS);
}

@Override
public CompletableFuture<Object> runCommand(String command, List<Object> arguments) {
CompletableFuture future = new CompletableFuture();
NewSuggestedContext context = new NewSuggestedContext(OpenProjectsFinder.getDefault().findTopLevelProjects());
Lookup lookup = Lookups.fixed(context);
Steps.getDefault()
.executeMultistep(new ItemTypeStep(), lookup)
.thenAccept(result -> {
Project project = ((Pair<Project, OCIItem>) result).first();
OCIItem item = ((Pair<Project, OCIItem>) result).second();
CloudAssets.getDefault().addItem(item);
Project projectToModify = null;
Set<Project> subProjects = ProjectUtils.getContainedProjects(project, false);
for (Project subProject : subProjects) {
if ("oci".equals(subProject.getProjectDirectory().getName())) { //NOI18N
projectToModify = subProject;
break;
}
}
if (projectToModify == null) {
projectToModify = project;
}
if (projectToModify != null) {
String[] art = DEP_MAP.get(context.getItemType());
ArtifactSpec spec = ArtifactSpec.make(art[0], art[1]);
Dependency dep = Dependency.make(spec, Scope.named("implementation")); //NOI18N
DependencyChange change = DependencyChange.add(Collections.singletonList(dep), DependencyChange.Options.skipConflicts);
try {
ModificationResult mod = ProjectDependencies.modifyDependencies(projectToModify, change);
mod.commit();
} catch (IOException ex) {
future.completeExceptionally(ex);
} catch (DependencyChangeException ex) {
future.completeExceptionally(ex);
} catch (ProjectOperationException ex) {
future.completeExceptionally(ex);
}
}
});
future.complete(null);
return future;
}

private final class NewSuggestedContext implements SuggestedContext {

private final CompletableFuture<Project[]> projects;
private String itemType = null;

public NewSuggestedContext(CompletableFuture<Project[]> projects) {
this.projects = projects;
}

@Override
public String getItemType() {
return itemType;
}

@Override
public Step getNextStep() {
return new ProjectStep(projects);
}

@Override
public void setItemType(String itemType) {
this.itemType = itemType;
}
}

}
Loading

0 comments on commit b948eda

Please sign in to comment.