Skip to content

Commit

Permalink
Command for uploading OCI policies
Browse files Browse the repository at this point in the history
  • Loading branch information
petrovic-d committed Sep 19, 2024
1 parent 0b56bd5 commit 8ef6f52
Show file tree
Hide file tree
Showing 13 changed files with 535 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public static void showErrorMessage(String message) {
public static void showMessage(String message) {
DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message(message));
}

public static void showWarningMessage(String message) {
DialogDisplayer.getDefault()
.notifyLater(new NotifyDescriptor.Message(message, NotifyDescriptor.WARNING_MESSAGE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
Expand All @@ -70,23 +67,19 @@
import org.netbeans.modules.cloud.oracle.assets.Steps;
import org.netbeans.modules.cloud.oracle.database.DatabaseItem;
import org.netbeans.modules.cloud.oracle.devops.DevopsProjectItem;
import org.netbeans.modules.cloud.oracle.steps.DatasourceNameStep;
import org.netbeans.modules.cloud.oracle.steps.KeyStep;
import org.netbeans.modules.cloud.oracle.steps.OverwriteStep;
import org.netbeans.modules.cloud.oracle.steps.PasswordStep;
import org.netbeans.modules.cloud.oracle.steps.ProjectStep;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.netbeans.modules.cloud.oracle.steps.CompartmentStep;
import org.netbeans.modules.cloud.oracle.steps.TenancyStep;
import org.netbeans.modules.cloud.oracle.vault.KeyItem;
import org.netbeans.modules.cloud.oracle.vault.VaultItem;
import org.netbeans.spi.lsp.CommandProvider;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ServiceProvider;

/**
* Command that updates a ConfigMap with the current properties generated from the contents of {@link CloudAssets}.
Expand All @@ -98,28 +91,19 @@
"UpdatingConfigMap=Updating Config Map",
"CMUpdated=ConfigMap in \"{0}\" project was updated"
})
@ServiceProvider(service = CommandProvider.class)
public class ConfigMapCommand implements CommandProvider {
public class ConfigMapUploader {

private static final Logger LOG = Logger.getLogger(ConfigMapCommand.class.getName());
private static final Logger LOG = Logger.getLogger(ConfigMapUploader.class.getName());

private static final String COMMAND_UPLOAD_TO_CONFIGMAP = "nbls.cloud.assets.configmap.upload"; //NOI18N

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

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

@Override
public CompletableFuture<Object> runCommand(String command, List<Object> arguments) {
public static void uploadConfigMap(CompletableFuture<Object> future) {
Steps.NextStepProvider.Builder nsProviderBuilder = Steps.NextStepProvider.builder();
CompletableFuture future = new CompletableFuture();

nsProviderBuilder.stepForClass(TenancyStep.class, (s) -> new CompartmentStep())
nsProviderBuilder.stepForClass(TenancyStep.class, (s) -> {
if (s.getValue() == null) {
return s;
}
return new CompartmentStep();
})
.stepForClass(CompartmentStep.class, (s) -> new DevopsStep())
.stepForClass(DevopsStep.class, (s) -> new ProjectStep());

Expand Down Expand Up @@ -172,7 +156,7 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
if (vaultRef.get() != null) {
updateVault(h, key, vaultRef.get(), propGen, project);
}
if (updateConfigMap(h, devopsProject, propGen, command)) {
if (updateConfigMap(h, devopsProject, propGen)) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(Bundle.CMUpdated(devopsProject.getName()), NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notifyLater(msg);
} else {
Expand All @@ -191,10 +175,9 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
h.finish();
}
});
return future;
}

private boolean updateConfigMap(ProgressHandle h, DevopsProjectItem devopsProject, PropertiesGenerator propGen, String datasourceName) {
private static boolean updateConfigMap(ProgressHandle h, DevopsProjectItem devopsProject, PropertiesGenerator propGen) {
// Add Vault to the ConfigMap artifact
DevopsClient devopsClient = DevopsClient.builder().build(OCIManager.getDefault().getActiveProfile().getConfigProvider());
ListDeployArtifactsRequest request = ListDeployArtifactsRequest.builder()
Expand Down Expand Up @@ -229,7 +212,7 @@ private boolean updateConfigMap(ProgressHandle h, DevopsProjectItem devopsProjec
return found;
}

private void updateVault(ProgressHandle h, KeyItem key, VaultItem vault, PropertiesGenerator propGen, Project project) {
private static void updateVault(ProgressHandle h, KeyItem key, VaultItem vault, PropertiesGenerator propGen, Project project) {
h.progress(Bundle.UpdatingVault(vault.getName()));
VaultsClient client = VaultsClient.builder().build(getDefault().getActiveProfile().getConfigProvider());
ListSecretsRequest listSecretsRequest = ListSecretsRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
return new DatabaseConnectionStep();
}
return new TenancyStep();
}).stepForClass(TenancyStep.class, (s) -> new CompartmentStep())
}).stepForClass(TenancyStep.class, (s) -> {
if (s.getValue() == null) {
return s;
}
return new CompartmentStep();
})
.stepForClass(CompartmentStep.class, (s) -> new SuggestedStep(null))
.stepForClass(SuggestedStep.class, (s) -> new ProjectStep())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ public void actionPerformed(ActionEvent e) {
return;
}
Steps.NextStepProvider nsProvider = Steps.NextStepProvider.builder()
.stepForClass(TenancyStep.class, (s) -> new CompartmentStep())
.stepForClass(TenancyStep.class, (s) -> {
if (s.getValue() == null) {
return s;
}
return new CompartmentStep();
})
.stepForClass(CompartmentStep.class, (s) -> new SuggestedStep(context.getPath()))
.build();
Lookup lookup = Lookups.fixed(nsProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import javax.swing.event.ChangeListener;
import org.netbeans.api.db.explorer.ConnectionManager;
import org.netbeans.api.db.explorer.DatabaseConnection;
import static org.netbeans.modules.cloud.oracle.NotificationUtils.showWarningMessage;
import org.netbeans.modules.cloud.oracle.bucket.BucketItem;
import org.netbeans.modules.cloud.oracle.compute.ClusterItem;
import org.netbeans.modules.cloud.oracle.compute.ComputeInstanceItem;
Expand All @@ -65,6 +66,7 @@
import org.openide.filesystems.FileUtil;
import org.openide.util.ChangeSupport;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.util.Parameters;

/**
Expand All @@ -73,6 +75,10 @@
*
* @author Jan Horvath
*/
@NbBundle.Messages({
"MSG_TenancyNotCompatible=Tenancy must be the same accross all selected Cloud Assets",
"MSG_AddItemsAgain=Missing information. Please remove than add following items again: {0}",
})
public final class CloudAssets {

private static final Logger LOG = Logger.getLogger(CloudAssets.class.getName());
Expand Down Expand Up @@ -133,7 +139,7 @@ public synchronized void addItem(OCIItem newItem) {
long presentCount = items.stream()
.filter(i -> i.getKey().getPath().equals(newItem.getKey().getPath()))
.count();
if (newItem.maxInProject() > presentCount) {
if (newItem.maxInProject() > presentCount && isTenancyCompatible(newItem)) {
items.add(newItem);
update();
storeAssets();
Expand All @@ -160,6 +166,31 @@ void update() {
setSuggestions(analyzer.findSuggestions(projects));
});
}

public synchronized boolean isTenancyCompatible(OCIItem toCheck) {
List<OCIItem> itemsMissingInfo = new ArrayList();
for(OCIItem item: items) {
if (item != null && item.getTenancyId() == null) {
itemsMissingInfo.add(item);
} else if (itemsMissingInfo.isEmpty() && item != null && !toCheck.getTenancyId().equals(item.getTenancyId())) {
showWarningMessage(Bundle.MSG_TenancyNotCompatible());
return false;
}
}
if (!itemsMissingInfo.isEmpty()) {
suggestToAddItemsAgain(itemsMissingInfo);
return false;
}

return true;
}

private void suggestToAddItemsAgain(List<OCIItem> itemsForRemoval) {
String itemNames = itemsForRemoval.stream()
.map(OCIItem::getName)
.collect(Collectors.joining(", "));
showWarningMessage(Bundle.MSG_AddItemsAgain(itemNames));
}

private synchronized void setSuggestions(Set<SuggestedItem> newSuggested) {
suggested.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.netbeans.spi.lsp.CommandProvider;
import org.openide.util.lookup.ServiceProvider;

/**
*
* @author Dusan Petrovic
*/
@ServiceProvider(service = CommandProvider.class)
public class CloudAssetsRefreshCommand implements CommandProvider {

private static final String COMMAND_CLOUD_ASSETS_REFRESH = "nbls.cloud.assets.refresh"; //NOI18N

private static final Set COMMANDS = Collections.singleton(COMMAND_CLOUD_ASSETS_REFRESH);

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

@Override
public CompletableFuture<Object> runCommand(String command, List<Object> arguments) {
CloudAssets.getDefault().update();
return CompletableFuture.completedFuture(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.netbeans.modules.cloud.oracle.actions.ConfigMapUploader;
import org.netbeans.spi.lsp.CommandProvider;
import org.openide.util.lookup.ServiceProvider;

/**
*
* @author Dusan Petrovic
*/
@ServiceProvider(service = CommandProvider.class)
public class CreateConfigCommand implements CommandProvider {

private static final String COMMAND_CREATE_CONFIG = "nbls.cloud.assets.config.create.local"; //NOI18N
private static final String COMMAND_UPLOAD_TO_CONFIGMAP = "nbls.cloud.assets.configmap.upload"; //NOI18N

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

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

@Override
public CompletableFuture<Object> runCommand(String command, List<Object> arguments) {
CompletableFuture future = new CompletableFuture();
if (COMMAND_CREATE_CONFIG.equals(command)) {
PropertiesGenerator propGen = new PropertiesGenerator(false);
ApplicationPropertiesGenerator appPropGen = new ApplicationPropertiesGenerator(propGen);
String toWrite = appPropGen.getApplicationPropertiesString();
future.complete(toWrite);
} else if (COMMAND_UPLOAD_TO_CONFIGMAP.equals(command)) {
ConfigMapUploader.uploadConfigMap(future);
}
return future;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.netbeans.modules.cloud.oracle.policy.PolicyGenerator;
import org.netbeans.modules.cloud.oracle.policy.PolicyUploader;
import org.netbeans.spi.lsp.CommandProvider;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
Expand All @@ -38,15 +39,13 @@
@ServiceProvider(service = CommandProvider.class)
public class CreatePoliciesCommand implements CommandProvider {
private static final String COMMAND_CREATE_POLICIES = "nbls.cloud.assets.policy.create.local"; //NOI18N
private static final String COMMAND_CREATE_CONFIG = "nbls.cloud.assets.config.create.local"; //NOI18N
private static final String COMMAND_CLOUD_ASSETS_REFRESH = "nbls.cloud.assets.refresh"; //NOI18N

private static final String COMMAND_UPLOAD_POLICIES = "nbls.cloud.assets.policy.upload"; //NOI18N

private static final RequestProcessor RP = new RequestProcessor("PoliciesCommand"); //NOI18N

private static final Set COMMANDS = new HashSet<>(Arrays.asList(
COMMAND_CREATE_POLICIES,
COMMAND_CREATE_CONFIG,
COMMAND_CLOUD_ASSETS_REFRESH
COMMAND_UPLOAD_POLICIES
));

@Override
Expand All @@ -69,14 +68,19 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
}
});
return future;
} else if (COMMAND_CREATE_CONFIG.equals(command)) {
PropertiesGenerator propGen = new PropertiesGenerator(false);
ApplicationPropertiesGenerator appPropGen = new ApplicationPropertiesGenerator(propGen);
String toWrite = appPropGen.getApplicationPropertiesString();
future.complete(toWrite);
} else if (COMMAND_CLOUD_ASSETS_REFRESH.equals(command)) {
CloudAssets.getDefault().update();
}
} else if (COMMAND_UPLOAD_POLICIES.equals(command)) {
RP.post(() -> {
try {
List<String> statements = PolicyGenerator.getPolicyStatementsFor(CloudAssets.getDefault().getItems());
PolicyUploader uploader = new PolicyUploader();
uploader.uploadPolicies(statements);
} catch (IllegalStateException e) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(e.getMessage());
DialogDisplayer.getDefault().notifyLater(msg);
future.completeExceptionally(e);
}
});
}
return future;
}

Expand Down
Loading

0 comments on commit 8ef6f52

Please sign in to comment.