Skip to content

Commit

Permalink
Create container repository command
Browse files Browse the repository at this point in the history
  • Loading branch information
petrovic-d committed Aug 13, 2024
1 parent 14d5cbc commit 653fddf
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.cloud.oracle.assets.OpenProjectsFinder;
import org.netbeans.modules.cloud.oracle.assets.Steps;
import org.netbeans.modules.cloud.oracle.compartment.CompartmentItem;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.netbeans.modules.cloud.oracle.requests.ContainerRepositoryRequest;
import org.netbeans.modules.cloud.oracle.requests.OCIItemCreationDetails;
import org.netbeans.modules.cloud.oracle.steps.CompartmentStep;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.NbBundle;

/**
*
* @author Dusan Petrovic
*/

@NbBundle.Messages({
"CreateContainerRepository=Create new container repository",
"EnterName=Enter OCI resource name"
})
public class CreateContainerRepository implements OCIItemCreator {

private static final String REPOSITORY_NAME_FIELD = "name";

@Override
public CompletableFuture<? extends OCIItem> create(Steps.Values values, Map<String, Object> params) {
String compartmentId = ((CompartmentItem)values.getValueForStep(CompartmentStep.class)).getKey().getValue();
String displayName = (String) params.get(REPOSITORY_NAME_FIELD);
OCIItemCreationDetails creationDetails = new ContainerRepositoryRequest(compartmentId, displayName);
return new org.netbeans.modules.cloud.oracle.actions.CreateContainerRepositoryCommand().create(creationDetails);
}

@Override
public CompletableFuture<Map<String, Object>> steps() {
Map<String, Object> result = new HashMap<>();
CompletableFuture future = new CompletableFuture();

String suggestedName = getSuggestedName(future);
NotifyDescriptor.InputLine ci = new NotifyDescriptor.InputLine(Bundle.EnterName(), Bundle.EnterName());

if (suggestedName != null) {
ci.setInputText(suggestedName);
}

DialogDisplayer.getDefault().notifyFuture(ci).handle((r, exception) -> {
result.put(REPOSITORY_NAME_FIELD, r.getInputText());

if (exception != null) {
future.completeExceptionally(exception);
} else {
future.complete(result);
}
return null;
});
return future;
}

private String getSuggestedName(CompletableFuture future) {
CompletableFuture<Project[]> projects = OpenProjectsFinder.getDefault().findTopLevelProjects();
ProjectInformation pi = null;
try {
Project[] p = projects.get();
if (p != null && p.length > 0) {
pi = ProjectUtils.getInformation(p[0]);
}
} catch (InterruptedException | ExecutionException ex) {
future.completeExceptionally(ex);
}
return pi == null ? null : pi.getDisplayName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.artifacts.ArtifactsClient;
import com.oracle.bmc.artifacts.model.ContainerRepository;
import com.oracle.bmc.artifacts.requests.CreateContainerRepositoryRequest;
import com.oracle.bmc.artifacts.responses.CreateContainerRepositoryResponse;
import org.netbeans.modules.cloud.oracle.developer.ContainerRepositoryItem;
import org.netbeans.modules.cloud.oracle.items.OCID;
import org.netbeans.modules.cloud.oracle.requests.OCIItemCreationDetails;

/**
*
* @author Dusan Petrovic
*/
public class CreateContainerRepositoryCommand extends CreateResourceCommand<ContainerRepositoryItem> {

@Override
ContainerRepositoryItem callCreate(OCIItemCreationDetails itemCreator) {
ArtifactsClient client = this.getProfile().newClient(ArtifactsClient.class);

CreateContainerRepositoryRequest request = (CreateContainerRepositoryRequest) itemCreator.getRequest();
CreateContainerRepositoryResponse response = client.createContainerRepository(request);
ContainerRepository res = response.getContainerRepository();

return new ContainerRepositoryItem(
OCID.of(res.getId(), "ContainerRepository"), //NOI18N
res.getCompartmentId(),
res.getDisplayName(),
this.getProfile().getRegion().getRegionCode(),
res.getNamespace(),
res.getIsPublic(),
res.getImageCount()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 java.util.concurrent.CompletableFuture;
import org.netbeans.modules.cloud.oracle.OCIManager;
import org.netbeans.modules.cloud.oracle.OCISessionInitiator;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.netbeans.modules.cloud.oracle.requests.OCIItemCreationDetails;

/**
*
* @author Dusan Petrovic
*/
public abstract class CreateResourceCommand<T extends OCIItem> {

private final OCISessionInitiator profile;

public CreateResourceCommand() {
this.profile = OCIManager.getDefault().getActiveProfile();
}

public OCISessionInitiator getProfile() {
return profile;
}

abstract T callCreate(OCIItemCreationDetails itemCreator);

public CompletableFuture<T> create(OCIItemCreationDetails creationDetails) {
CompletableFuture future = new CompletableFuture();

try {
T item = callCreate(creationDetails);
future.complete(item);
} catch (Exception ex) {
future.completeExceptionally(ex);
}
return future;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.netbeans.modules.cloud.oracle.assets.Steps;
import org.netbeans.modules.cloud.oracle.items.OCIItem;

/**
*
* @author Dusan Petrovic
*/
public interface OCIItemCreator {

CompletableFuture<Map<String, Object>> steps();

CompletableFuture<? extends OCIItem> create(Steps.Values values, Map<String, Object> params);

public static OCIItemCreator getCreator(String itemType) {
switch (itemType) {
case "ContainerRepository":
return new CreateContainerRepository();
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import java.util.concurrent.CompletableFuture;
import org.netbeans.api.project.Project;
import org.netbeans.modules.cloud.oracle.actions.AddADBAction;
import org.netbeans.modules.cloud.oracle.actions.OCIItemCreator;
import org.netbeans.modules.cloud.oracle.database.DatabaseItem;
import org.netbeans.modules.cloud.oracle.steps.ItemTypeStep;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.netbeans.modules.cloud.oracle.steps.DatabaseConnectionStep;
import org.netbeans.modules.cloud.oracle.steps.ItemCreationDecisionStep;
import org.netbeans.modules.cloud.oracle.steps.TenancyStep;
import org.netbeans.spi.lsp.CommandProvider;
import org.openide.util.lookup.Lookups;
Expand Down Expand Up @@ -73,24 +75,41 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
.stepForClass(ItemTypeStep.class, (s) -> {
if ("Databases".equals(s.getValue())) {
return new DatabaseConnectionStep();
}
}
return new TenancyStep();
}).stepForClass(TenancyStep.class, (s) -> new CompartmentStep())
.stepForClass(CompartmentStep.class, (s) -> new SuggestedStep(null))
.stepForClass(CompartmentStep.class, (s) -> new ItemCreationDecisionStep())
.stepForClass(ItemCreationDecisionStep.class, (s) -> {
if (ItemCreationDecisionStep.CREATE_NEW_OPTION.equals(s.getValue())) {
return null;
}
return new SuggestedStep(null);
})
.stepForClass(SuggestedStep.class, (s) -> new ProjectStep())
.build();

Steps.getDefault()
.executeMultistep(new ItemTypeStep(), Lookups.fixed(nsProvider))
.thenAccept(values -> {
Project project = values.getValueForStep(ProjectStep.class);
CompletableFuture<? extends OCIItem> item;
if ("Databases".equals(values.getValueForStep(ItemTypeStep.class))) {
CompletableFuture<? extends OCIItem> item = null;
String itemType = values.getValueForStep(ItemTypeStep.class);
if ("Databases".equals(itemType)) {
DatabaseItem i = values.getValueForStep(DatabaseConnectionStep.class);
if (i == null) {
item = new AddADBAction().addADB();
} else {
item = CompletableFuture.completedFuture(i);
}
} else if (ItemCreationDecisionStep.CREATE_NEW_OPTION.equals(values.getValueForStep(ItemCreationDecisionStep.class))) { //NOI18N
OCIItemCreator creator = OCIItemCreator.getCreator(itemType);
if (creator != null) {
CompletableFuture<Map<String, Object>> vals = creator.steps();
item = vals.thenCompose(params -> {
return creator.create(values, params);
});
}

} else {
OCIItem i = values.getValueForStep(SuggestedStep.class);
if (i == null) {
Expand All @@ -100,6 +119,7 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
item = CompletableFuture.completedFuture(i);
}
}

item.thenAccept(i -> {
CloudAssets.getDefault().addItem(i);
String[] art = DEP_MAP.get(i.getKey().getPath());
Expand All @@ -113,5 +133,5 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
});
return future;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
import org.netbeans.modules.cloud.oracle.steps.CompartmentStep;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
import org.netbeans.modules.cloud.oracle.actions.AddADBAction;
import org.netbeans.modules.cloud.oracle.actions.OCIItemCreator;
import org.netbeans.modules.cloud.oracle.steps.DatabaseConnectionStep;
import org.netbeans.modules.cloud.oracle.steps.TenancyStep;
import org.netbeans.modules.cloud.oracle.database.DatabaseItem;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.netbeans.modules.cloud.oracle.steps.ItemCreationDecisionStep;
import org.openide.awt.ActionID;
import org.openide.awt.ActionRegistration;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.lookup.Lookups;

/**
Expand Down Expand Up @@ -82,14 +85,31 @@ public void actionPerformed(ActionEvent e) {
}
Steps.NextStepProvider nsProvider = Steps.NextStepProvider.builder()
.stepForClass(TenancyStep.class, (s) -> new CompartmentStep())
.stepForClass(CompartmentStep.class, (s) -> new SuggestedStep(context.getPath()))
.stepForClass(CompartmentStep.class, (s) -> new ItemCreationDecisionStep(context.getPath()))
.stepForClass(ItemCreationDecisionStep.class, (s) -> {
if (ItemCreationDecisionStep.CREATE_NEW_OPTION.equals(s.getValue())) {
return null;
}
return new SuggestedStep(context.getPath());
})
.build();
Lookup lookup = Lookups.fixed(nsProvider);
Steps.getDefault().executeMultistep(new TenancyStep(), lookup)
.thenAccept(values -> {
OCIItem item = values.getValueForStep(SuggestedStep.class);
CloudAssets.getDefault().addItem(item);
if (ItemCreationDecisionStep.CREATE_NEW_OPTION.equals(values.getValueForStep(ItemCreationDecisionStep.class))) { //NOI18N
OCIItemCreator creator = OCIItemCreator.getCreator(context.getPath());
if (creator != null) {
CompletableFuture<Map<String, Object>> vals = creator.steps();
vals.thenCompose(params -> {
return creator.create(values, params);
}).thenAccept(i -> {
CloudAssets.getDefault().addItem(i);
});
}
} else {
OCIItem item = values.getValueForStep(SuggestedStep.class);
CloudAssets.getDefault().addItem(item);
}
});
}

}
Loading

0 comments on commit 653fddf

Please sign in to comment.