Skip to content
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

Command for uploading OCI policies #7761

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
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,26 +91,12 @@
"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())
.stepForClass(CompartmentStep.class, (s) -> new DevopsStep())
Expand Down Expand Up @@ -172,7 +151,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 +170,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 +207,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 @@ -44,13 +44,15 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
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 +67,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 +76,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 +140,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, true)) {
items.add(newItem);
update();
storeAssets();
Expand All @@ -160,6 +167,42 @@ void update() {
setSuggestions(analyzer.findSuggestions(projects));
});
}

public synchronized String getTenancyId() {
Optional<OCIItem> ociItem = items.stream().findFirst();
return ociItem == null || ociItem.isEmpty() ? null : ociItem.get().getTenancyId();
}

public synchronized boolean isTenancyCompatible(OCIItem toCheck) {
return isTenancyCompatible(toCheck, false);
}

public synchronized boolean isTenancyCompatible(OCIItem toCheck, boolean showWarning) {
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())) {
if (showWarning) {
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.items;

/**
*
* @author Dusan Petrovic
*/
public class IncompatibleTenancyItem extends TenancyItem {

}
Loading
Loading