Skip to content

Commit

Permalink
Merge pull request #7622 from petrovic-d/add-tenency-id-and-region-to…
Browse files Browse the repository at this point in the history
…-cloud-assets

Save tenancyId and region when Cloud Asset is added
  • Loading branch information
jhorvath authored Sep 16, 2024
2 parents 9fce56d + e18df72 commit 0b56bd5
Show file tree
Hide file tree
Showing 51 changed files with 321 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface ChildrenProvider<T extends OCIItem, U extends OCIItem> {
*/
public interface SessionAware<T extends OCIItem, U extends OCIItem> extends ChildrenProvider<T, U> {
public default List<U> apply(T t) {
return apply(t, OCIManager.getDefault().getActiveSession());
return apply(t, OCIManager.getDefault().getActiveProfile(t));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,22 @@ public List<OCIProfile> getConnectedProfiles() {

return new ArrayList<>(toReturn);
}


/**
* Returns the active profile based on OCI Item.
* @return
*/
public OCIProfile getActiveProfile(OCIItem ociItem) {
List<OCIProfile> profiles = getConnectedProfiles();
return profiles.stream().filter(p -> isMatchingProfile(ociItem, p)).findFirst().orElse(null);
}

private static boolean isMatchingProfile(OCIItem ociItem, OCIProfile profile) {
if (profile.getTenancy().isEmpty()) return profile.getRegion().getRegionCode().equals(ociItem.getRegionCode());
return profile.getRegion().getRegionCode().equals(ociItem.getRegionCode()) &&
profile.getTenancy().get().getKey().getValue().equals(ociItem.getTenancyId());
}

/**
* Returns the active profile.
* @return
Expand All @@ -488,7 +503,7 @@ public OCIProfile getActiveProfile() {
Preferences prefs = NbPreferences.forModule(OCIManager.class);
String confPath = prefs.get("activeProfilePath", null);
String id = null;

Path path = null;
if (confPath != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public <T> T newClient(Class<T> clientClass) {
}

private TenancyItem createTenancyItem(Tenancy tenancy) {
TenancyItem item = new TenancyItem(OCID.of(tenancy.getId(), "Tenancy"), tenancy.getName());
String regionCode = configProvider.getRegion().getRegionCode();
TenancyItem item = new TenancyItem(OCID.of(tenancy.getId(), "Tenancy"), tenancy.getName(), regionCode);
item.setDescription(Bundle.LBL_HomeRegion(tenancy.getHomeRegionKey()));
return item;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ private Map<String, OCIItem> getFlatCompartment(TenancyItem tenancy) {
Identity identityClient = session.newClient(IdentityClient.class);
String nextPageToken = null;

String tenancyId = session.getTenancy().isPresent() ? session.getTenancy().get().getKey().getValue() : null;
String regionCode = session.getRegion().getRegionCode();

do {
ListCompartmentsResponse response
= identityClient.listCompartments(
Expand All @@ -254,7 +257,7 @@ private Map<String, OCIItem> getFlatCompartment(TenancyItem tenancy) {
.page(nextPageToken)
.build());
for (Compartment comp : response.getItems()) {
FlatCompartmentItem ci = new FlatCompartmentItem(comp) {
FlatCompartmentItem ci = new FlatCompartmentItem(comp, tenancyId, regionCode) {
FlatCompartmentItem getItem(OCID compId) {
return compartments.get(compId);
}
Expand All @@ -281,8 +284,8 @@ private abstract class FlatCompartmentItem extends CompartmentItem {
private final OCID parentId;
private String flatName;

private FlatCompartmentItem(Compartment ociComp) {
super(OCID.of(ociComp.getId(), "Compartment"), ociComp.getCompartmentId(), ociComp.getName()); // NOI18N
private FlatCompartmentItem(Compartment ociComp, String tenancyId, String regionCode) {
super(OCID.of(ociComp.getId(), "Compartment"), ociComp.getCompartmentId(), ociComp.getName(), tenancyId, regionCode); // NOI18N
setDescription(ociComp.getDescription());
parentId = OCID.of(ociComp.getCompartmentId(), "Compartment"); // NOI18N
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ ContainerRepositoryItem callCreate(OCIItemCreationDetails itemCreator) {
CreateContainerRepositoryResponse response = client.createContainerRepository(request);
ContainerRepository res = response.getContainerRepository();

String tenancyId = this.getProfile().getTenancy().isPresent() ? this.getProfile().getTenancy().get().getKey().getValue() : null;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class KnowledgeBaseItem extends OCIItem implements URLProvider{

protected final Date timeUpdated;

public KnowledgeBaseItem(OCID id, String compartmentId, String displayName, Date timeUpdated) {
super(id, compartmentId, displayName);
public KnowledgeBaseItem(OCID id, String compartmentId, String displayName, Date timeUpdated, String tenancyId, String regionCode) {
super(id, compartmentId, displayName, tenancyId, regionCode);
this.timeUpdated = timeUpdated;
registerItem();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,34 @@ public static NodeProvider<KnowledgeBaseItem> createNode() {
// }
//
// }

// @ChildrenProvider.Registration(parentPath = "Oracle/Compartment")
public static ChildrenProvider.SessionAware<CompartmentItem, KnowledgeBaseItem> listKnowledgeBases() {
return (compartment, session) -> {
try ( ApplicationDependencyManagementClient client
try ( ApplicationDependencyManagementClient client
= session.newClient(ApplicationDependencyManagementClient.class)) {

ListKnowledgeBasesRequest request = ListKnowledgeBasesRequest.builder()
.compartmentId(compartment.getKey().getValue())
.lifecycleState(KnowledgeBase.LifecycleState.Active)
.build();
ListKnowledgeBasesResponse response = client.listKnowledgeBases(request);
List<KnowledgeBaseSummary> projects = response.getKnowledgeBaseCollection().getItems();

String tenancyId = session.getTenancy().isPresent() ?
session.getTenancy().get().getKey().getValue() : null;
String regionCode = session.getRegion().getRegionCode();

return projects.stream()
.map(p -> new KnowledgeBaseItem(
OCID.of(p.getId(), "KnowledgeBase"), // NOI18N
p.getCompartmentId(),
p.getDisplayName(), p.getTimeUpdated())).collect(Collectors.toList());
OCID.of(p.getId(), "KnowledgeBase"), // NOI18N
p.getCompartmentId(),
p.getDisplayName(),
p.getTimeUpdated(),
tenancyId,
regionCode)).collect(Collectors.toList());
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.cloud.oracle.OCIManager;
import org.netbeans.modules.cloud.oracle.OCIProfile;
import org.netbeans.modules.cloud.oracle.OCISessionInitiator;
import org.netbeans.modules.cloud.oracle.items.OCID;
import org.netbeans.spi.project.AuxiliaryProperties;
import org.netbeans.spi.project.LookupProvider.Registration.ProjectType;
Expand Down Expand Up @@ -105,10 +106,16 @@ public KnowledgeBaseItem getProjectKnowledgeBase() {
GetKnowledgeBaseResponse resp = admClient.getKnowledgeBase(req);
KnowledgeBase p = resp.getKnowledgeBase();

String tenancyId = active.getTenancy().isPresent() ?
active.getTenancy().get().getKey().getValue() : null;
String regionCode = active.getRegion().getRegionCode();

item = new KnowledgeBaseItem(
OCID.of(p.getId(), "KnowledgeBase"), // NOI18N
p.getCompartmentId(),
p.getDisplayName(), p.getTimeUpdated()
p.getDisplayName(), p.getTimeUpdated(),
tenancyId,
regionCode
);
} catch (IllegalArgumentException | BmcException ex) {
item = knowledgeBaseItem;
Expand Down Expand Up @@ -162,12 +169,19 @@ public CompletableFuture<KnowledgeBaseItem> findKnowledgeBase(String knowledgeBa
.knowledgeBaseId(knowledgeBaseId).
build();

OCISessionInitiator session = OCIManager.getDefault().getActiveProfile();
String tenancyId = session.getTenancy().isPresent() ?
session.getTenancy().get().getKey().getValue() : null;
String regionCode = session.getRegion().getRegionCode();

GetKnowledgeBaseResponse response = client.getKnowledgeBase(request);
KnowledgeBase p = response.getKnowledgeBase();
result.complete(new KnowledgeBaseItemProxy(
OCID.of(p.getId(), "KnowledgeBase"), // NOI18N
p.getCompartmentId(),
p.getDisplayName(), p.getTimeUpdated())
p.getDisplayName(), p.getTimeUpdated(),
tenancyId,
regionCode)
);
} catch (ThreadDeath x) {
throw x;
Expand All @@ -186,8 +200,8 @@ public CompletableFuture<KnowledgeBaseItem> findKnowledgeBase(String knowledgeBa
* + presented in the IDE UI.
*/
static class KnowledgeBaseItemProxy extends KnowledgeBaseItem {
public KnowledgeBaseItemProxy(OCID id, String compartmenId, String displayName, Date timeUpdated) {
super(id, compartmenId, displayName, timeUpdated);
public KnowledgeBaseItemProxy(OCID id, String compartmenId, String displayName, Date timeUpdated, String tenancyId, String regionCode) {
super(id, compartmenId, displayName, timeUpdated, tenancyId, regionCode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CreateNewResourceItem extends OCIItem {
private static final String DISPLAY_NAME = "<Create new>";

public CreateNewResourceItem() {
super(null, null, DISPLAY_NAME);
super(null, null, DISPLAY_NAME, null, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class SuggestedItem extends OCIItem {
private final Set<String> exclusivePaths;

public SuggestedItem(String path, String name, Set<String> exclusivePaths) {
super(OCID.of("", "Suggested"), null, name); //NOI18N
super(OCID.of("", "Suggested"), null, name, null, null); //NOI18N
this.path = path;
this.exclusivePaths = exclusivePaths;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public final class BucketItem extends OCIItem {

private String namespace;

public BucketItem(OCID id, String compartmentId, String name, String namespace) {
super(id, compartmentId, name);
public BucketItem(OCID id, String compartmentId, String name, String namespace, String tenancyId, String regionCode) {
super(id, compartmentId, name, tenancyId, regionCode);
this.namespace = namespace;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ public static ChildrenProvider.SessionAware<CompartmentItem, BucketItem> getBuck
.limit(88)
.build();

String tenancyId = session.getTenancy().isPresent() ? session.getTenancy().get().getKey().getValue() : null;
String regionCode = session.getRegion().getRegionCode();

List<BucketItem> x = client.listBuckets(listBucketsRequest)
.getItems()
.stream()
.map(d -> new BucketItem(
OCID.of(d.getName(), "Bucket"), //NOI18N
compartmentId.getKey().getValue(),
d.getName(),
d.getNamespace())
d.getNamespace(),
tenancyId,
regionCode)
)
.collect(Collectors.toList());
return x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
*/
public class CompartmentItem extends OCIItem {

public CompartmentItem(OCID id, String compartmentId, String name) {
super(id, compartmentId, name);
public CompartmentItem(OCID id, String compartmentId, String name, String tenancyId, String regionCode) {
super(id, compartmentId, name, tenancyId, regionCode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static ChildrenProvider.SessionAware<OCIItem, CompartmentItem> getCompart

List<CompartmentItem> compartments = new ArrayList<>();

String tenancyId = session.getTenancy().isPresent() ? session.getTenancy().get().getKey().getValue() : null;
String regionCode = session.getRegion().getRegionCode();

String nextPageToken = null;
do {
ListCompartmentsResponse response
Expand All @@ -79,7 +82,12 @@ public static ChildrenProvider.SessionAware<OCIItem, CompartmentItem> getCompart
.page(nextPageToken)
.build());
response.getItems().stream()
.map(c -> new CompartmentItem(OCID.of(c.getId(), "Compartment"), parent.getKey().getValue(), c.getName())) // NOI18N
.map(c -> new CompartmentItem(
OCID.of(c.getId(), "Compartment"),
parent.getKey().getValue(),
c.getName(),
tenancyId,
regionCode)) // NOI18N
.collect(Collectors.toCollection(() -> compartments));
nextPageToken = response.getOpcNextPage();
} while (nextPageToken != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
*/
public final class ClusterItem extends OCIItem {

public ClusterItem(OCID id, String compartmentId, String name) {
super(id, compartmentId, name);
public ClusterItem(OCID id, String compartmentId, String name, String tenancyId, String regionCode) {
super(id, compartmentId, name, tenancyId, regionCode);
}

public ClusterItem() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,20 @@ public static ChildrenProvider.SessionAware<CompartmentItem, ClusterItem> getClu
.limit(88)
.build();

String tenancyId = session.getTenancy().isPresent() ?
session.getTenancy().get().getKey().getValue() : null;
String regionCode = session.getRegion().getRegionCode();

return client.listClusters(listClustersRequest)
.getItems()
.stream()
.filter(c -> !c.getLifecycleState().equals(Instance.LifecycleState.Terminated))
.map(d -> new ClusterItem(
OCID.of(d.getId(), "Cluster"), //NOI18N
OCID.of(d.getId(), "Cluster"), //NOI18N
compartmentId.getKey().getValue(),
d.getName()
d.getName(),
tenancyId,
regionCode
))
.collect(Collectors.toList());
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public final class ComputeInstanceItem extends OCIItem {
private String username;
private String imageId;

public ComputeInstanceItem(OCID id, String compartmentId, String name, String processorDescription, String imageId, String publicIp, String username) {
super(id, compartmentId, name);
public ComputeInstanceItem(OCID id, String compartmentId, String name, String processorDescription, String imageId, String publicIp, String username, String tenancyId, String regionCode) {
super(id, compartmentId, name, tenancyId, regionCode);
this.processorDescription = processorDescription;
this.publicIp = publicIp;
this.username = username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public static ChildrenProvider.SessionAware<CompartmentItem, ComputeInstanceItem
.limit(88)
.build();

String tenancyId = session.getTenancy().isPresent() ? session.getTenancy().get().getKey().getValue() : null;
String regionCode = session.getRegion().getRegionCode();

return client.listInstances(listInstancesRequest)
.getItems()
.stream()
Expand All @@ -146,7 +149,9 @@ public static ChildrenProvider.SessionAware<CompartmentItem, ComputeInstanceItem
d.getShapeConfig().getProcessorDescription(),
d.getImageId(),
null,
null
null,
tenancyId,
regionCode
))
.collect(Collectors.toList());
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class DatabaseItem extends OCIItem {
private final String serviceUrl;
private final String connectionName;

public DatabaseItem(OCID id, String compartmentId, String name, String serviceUrl, String connectionName) {
super(id, compartmentId, name);
public DatabaseItem(OCID id, String compartmentId, String name, String serviceUrl, String connectionName, String tenancyId, String regionCode) {
super(id, compartmentId, name, tenancyId, regionCode);
this.serviceUrl = serviceUrl;
this.connectionName = connectionName;
}
Expand Down
Loading

0 comments on commit 0b56bd5

Please sign in to comment.