Skip to content

Commit

Permalink
Merge pull request #7929 from jhorvath/arch-warning
Browse files Browse the repository at this point in the history
Validation before adding items into CloudAssets
  • Loading branch information
jhorvath authored Nov 10, 2024
2 parents f640500 + 4fe26a7 commit 4503a37
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,28 @@ public static synchronized CloudAssets getDefault() {
return instance;
}

public synchronized void addItem(OCIItem newItem) {
public synchronized boolean addItem(OCIItem newItem) {
Parameters.notNull("newItem cannot be null", newItem);
long presentCount = items.stream()
.filter(i -> i.getKey().getPath().equals(newItem.getKey().getPath()))
.count();
if (newItem.maxInProject() > presentCount && isTenancyCompatible(newItem, true)) {
if (newItem instanceof Validator) {
Validator.Result result = ((Validator) newItem).validate();
if (result.status == Validator.ValidationStatus.WARNING) {
showWarningMessage(result.message);
}
if (result.status == Validator.ValidationStatus.ERROR) {
showWarningMessage(result.message);
return false;
}
}
items.add(newItem);
newItem.addChangeListener(itemsListener);
update();
storeAssets();
}
return true;
}

synchronized void removeItem(OCIItem item) {
Expand Down Expand Up @@ -184,7 +195,7 @@ public synchronized boolean isTenancyCompatible(OCIItem toCheck) {
}

public synchronized boolean isTenancyCompatible(OCIItem toCheck, boolean showWarning) {
List<OCIItem> itemsMissingInfo = new ArrayList();
List<OCIItem> itemsMissingInfo = new ArrayList<> ();
for(OCIItem item: items) {
if (item != null && item.getTenancyId() == null) {
itemsMissingInfo.add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public PropertiesGenerator(boolean local) {
}
if (vault != null) {
bootstrap.put("oci.vault.config.enabled", "true"); // NOI18N
bootstrap.put("micronaut.config-client.enabled", "true"); // NOI18N
bootstrap.put("oci.vault.vaults[0].ocid", vault.getKey().getValue()); //NOI18N
bootstrap.put("oci.vault.vaults[0].compartment-ocid", vault.getCompartmentId()); //NOI18N
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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;

/**
* Validates if the implementing class is suitable for adding to {@link CloudAssets}.
* The validation result is represented by {@link Result}, which includes a {@link ValidationStatus}
* indicating the outcome of the validation.
*
* <p>The possible validation states are:
* <ul>
* <li>{@link ValidationStatus#OK} - Indicates that the item is suitable for adding to {@link CloudAssets}
* without any warnings or restrictions.</li>
* <li>{@link ValidationStatus#WARNING} - Indicates that the item may be added to {@link CloudAssets},
* but with a warning message to notify the user of potential issues.</li>
* <li>{@link ValidationStatus#ERROR} - Indicates that the item cannot be added to {@link CloudAssets}.
* A warning message will be shown, and the addition process will be blocked.</li>
* </ul>
*
* @author Jan Horvath
*/
public interface Validator {

enum ValidationStatus {
OK, WARNING, ERROR
};

public class Result {

public final ValidationStatus status;
public final String message;

public Result(ValidationStatus status, String message) {
this.status = status;
this.message = message;
}

}

public Result validate();

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@
import org.netbeans.modules.cloud.oracle.items.OCID;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.netbeans.modules.cloud.oracle.assets.Validator;

/**
*
* @author Jan Horvath
*/
public final class ComputeInstanceItem extends OCIItem implements URLProvider {
@NbBundle.Messages({
"SuggestAmpere=The Compute Instance has a different architecture than the local machine. Container images built on the local machine may not be compatible with this Compute Instance. Please consider creating a Compute Instance with an Ampere® shape.",
"SuggestIntel=The Compute Instance has a different architecture than the local machine. Container images built on the local machine may not be compatible with this Compute Instance. Please consider creating a Compute Instance with an AMD or Intel® shape."
})
public final class ComputeInstanceItem extends OCIItem implements URLProvider, Validator {
private final String AARCH = "aarch64";
private String publicIp = null;
private String processorDescription;
private String username;
Expand Down Expand Up @@ -88,4 +95,19 @@ public URL getURL() {
}
return null;
}

@Override
public Result validate() {
String osArch = System.getProperty("os.arch"); //NOI18N
String shapeDesc = getProcessorDescription();
boolean os_arm = AARCH.equals(osArch);
boolean shape_arm = shapeDesc != null && shapeDesc.contains("Ampere"); //NOI18N
if (os_arm == shape_arm) {
return new Result(ValidationStatus.OK, null);
} else if (os_arm && !shape_arm) {
return new Result(ValidationStatus.WARNING, Bundle.SuggestAmpere());
}
return new Result(ValidationStatus.WARNING, Bundle.SuggestIntel());
}

}

0 comments on commit 4503a37

Please sign in to comment.