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

[artifact-manager) (#588) Policies import and export for multi tenant environments #600

Merged
merged 24 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
abe72ba
refactor(approval-policies)!: Import and export reworked
Michaelpalacce Jan 21, 2025
a364c3c
fix(VraNgApprovalPolicy): Removed unused function
Michaelpalacce Jan 21, 2025
449ff0e
docs: Added one todo for projectId
Michaelpalacce Jan 22, 2025
1883bfc
refactor(vrang-policy-stores): Restructured methods to be grouped based
Michaelpalacce Jan 22, 2025
6596587
refactor(policy): Better organization and some small error handling
Michaelpalacce Jan 22, 2025
ff28df1
refactor(cs-policy-store): Export done, working on import
Michaelpalacce Jan 23, 2025
e49b65b
refactor(content sharing policy): Import/export working
Michaelpalacce Jan 23, 2025
e75d839
refactor(day-two-actions-policy): Import and export
Michaelpalacce Jan 23, 2025
a65d9f3
refactor(policies): Finished all the other policies, need to test
Michaelpalacce Jan 23, 2025
e1fbc60
test: Fixed unit tests to match current functionality
Michaelpalacce Jan 24, 2025
7e42c47
style(lint): Remove unused import
Michaelpalacce Jan 28, 2025
5166281
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
118cd68
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
39dd7c7
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
82e1bc7
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
c7f0b3c
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
21ead90
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
42e663c
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
6c92eec
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
2a44b28
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
3981bdb
Update common/artifact-manager/src/main/java/com/vmware/pscoe/iac/art…
Michaelpalacce Jan 29, 2025
21ceb5a
refactor(error-handling): Improved error handling message
Michaelpalacce Jan 29, 2025
7276804
Merge branch 'main' of https://github.com/vmware/build-tools-for-vmwa…
Michaelpalacce Jan 29, 2025
feb5977
docs: Release
Michaelpalacce Jan 29, 2025
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
5 changes: 2 additions & 3 deletions .github/linters/sun_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,15 @@ https://www.oracle.com/java/technologies/javase/codeconventions-contents.html
<module name="EqualsHashCode" />
<module name="IllegalInstantiation" />
<module name="InnerAssignment" />
<!-- <module name="MagicNumber" /> -->
<module name="MagicNumber" />
<module name="MissingSwitchDefault" />
<module name="MultipleVariableDeclarations" />
<module name="SimplifyBooleanExpression" />
<module name="SimplifyBooleanReturn" />

<!-- Checks for class design -->
<!-- See https://checkstyle.org/config_design.html -->
<!-- Uncomment later -->
<!-- <module name="DesignForExtension" /> -->
Michaelpalacce marked this conversation as resolved.
Show resolved Hide resolved
<module name="DesignForExtension" />
<module name="FinalClass" />
<module name="HideUtilityClassConstructor" />
<module name="InterfaceIsType" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,8 @@ public VraNgContentSourceBase getContentSource(final String id) {
try {
return this.getContentSourcePrimitive(id);
} catch (Exception e) {
throw new RuntimeException(String.format("Could not get content source by id '%s'.", id, e));
throw new RuntimeException(
String.format("Could not get content source by id '%s', error was: '%s'.", id, e.getMessage()), e);
}
}

Expand All @@ -671,7 +672,7 @@ public VraNgWorkflowContentSource getVraWorkflowContentSource(final String id) {
return this.getVraWorkflowContentSourcePrimitive(id);
} catch (Exception e) {
logger.error("Could not find VRA workflow content source {}", id);
throw new RuntimeException(e.getMessage());
throw new RuntimeException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
import com.vmware.pscoe.iac.artifact.aria.automation.models.VraNgSecret;
import com.vmware.pscoe.iac.artifact.aria.automation.models.VraNgSubscription;
import com.vmware.pscoe.iac.artifact.aria.automation.models.VraNgWorkflowContentSource;

import com.vmware.pscoe.iac.artifact.aria.automation.rest.models.VraNgPolicyTypes;
import com.vmware.pscoe.iac.artifact.aria.automation.utils.VraNgOrganizationUtil;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -96,6 +96,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.Strictness;
import com.jayway.jsonpath.JsonPath;
import com.vmware.pscoe.iac.artifact.configuration.Configuration;
import com.vmware.pscoe.iac.artifact.aria.automation.configuration.ConfigurationVraNg;
Expand Down Expand Up @@ -133,26 +134,6 @@ public class RestClientVraNgPrimitive extends RestClient {
* SERVICE_SUBSCRIPTION.
*/
private static final String SERVICE_SUBSCRIPTION = "/event-broker/api/subscriptions";
/**
* SERVICE_FLAVORS.
*/
private static final String SERVICE_FLAVORS = "/iaas/api/flavors";
/**
* SERVICE_FLAVOR_PROFILE.
*/
private static final String SERVICE_FLAVOR_PROFILE = "/iaas/api/flavor-profiles";
/**
* SERVICE_IMAGES.
*/
private static final String SERVICE_IMAGES = "/iaas/api/images";
/**
* SERVICE_IMAGE_PROFILE.
*/
private static final String SERVICE_IMAGE_PROFILE = "/iaas/api/image-profiles";
/**
* SERVICE_STORAGE_PROFILE.
*/
private static final String SERVICE_STORAGE_PROFILE = "/iaas/api/storage-profiles";
/**
* SERVICE_CLOUD_ACCOUNT.
*/
Expand Down Expand Up @@ -270,31 +251,6 @@ public class RestClientVraNgPrimitive extends RestClient {
* CUSTOM_FORM_DEFAULT_FORMAT.
*/
private static final String CUSTOM_FORM_DEFAULT_FORMAT = "JSON";
/**
* CONTENT_SHARING_POLICY_TYPE.
*/
private static final String CONTENT_SHARING_POLICY_TYPE = "com.vmware.policy.catalog.entitlement";
/**
* APPROVAL_POLICY_TYPE.
*/
private static final String APPROVAL_POLICY_TYPE = "com.vmware.policy.approval";
/**
* DAY2_ACTION_POLICY_TYPE.
*/
private static final String DAY2_ACTION_POLICY_TYPE = "com.vmware.policy.deployment.action";
/**
* LEASE_POLICY_TYPE.
*/
private static final String LEASE_POLICY_TYPE = "com.vmware.policy.deployment.lease";
/**
* DEPLOYMENT_LIMIT_POLICY_TYPE.
*/
private static final String DEPLOYMENT_LIMIT_POLICY_TYPE = "com.vmware.policy.deployment.limit";
/**
* RESOURCE_QUOTA_POLICY_TYPE.
*/
private static final String RESOURCE_QUOTA_POLICY_TYPE = "com.vmware.policy.resource.quota";

/**
* MAX_BLUEPRINTS_RETRIEVAL_COUNT.
*/
Expand Down Expand Up @@ -829,7 +785,7 @@ public boolean isBlueprintReleasedPrimitive(final String blueprintId) {
*/
public String createOrUpdateContentSourcePrimitive(final VraNgContentSourceBase contentSource) {
URI url = getURI(getURIBuilder().setPath(SERVICE_CONTENT_SOURCE));
Gson gson = new GsonBuilder().setLenient().serializeNulls().create();
Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeNulls().create();
ResponseEntity<String> response = this.postJsonPrimitive(url, HttpMethod.POST, gson.toJson(contentSource));
return new Gson().fromJson(response.getBody(), contentSource.getType().getTypeClass()).getId();
}
Expand Down Expand Up @@ -1357,7 +1313,7 @@ private VraNgCatalogEntitlementDto[] getCatalogEntitlementsPerProject(final Stri
URI url = getURI(getURIBuilder().setPath(SERVICE_CATALOG_ENTITLEMENTS).addParameter("projectId", project));
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, getDefaultHttpEntity(),
String.class);
Gson gson = new GsonBuilder().setLenient().setPrettyPrinting().serializeNulls().create();
Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).setPrettyPrinting().serializeNulls().create();
VraNgCatalogEntitlementDto[] entitlements = gson.fromJson(response.getBody(),
VraNgCatalogEntitlementDto[].class);
return entitlements;
Expand Down Expand Up @@ -1739,7 +1695,8 @@ public VraNgOrganization getOrganizationByName(final String organizationName) {
url = uriBuilder.build();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, getDefaultHttpEntity(),
String.class);
Gson gson = new GsonBuilder().setLenient().setPrettyPrinting().serializeNulls().create();
Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).setPrettyPrinting().serializeNulls()
.create();
VraNgOrganizations organizations = gson.fromJson(response.getBody(), VraNgOrganizations.class);
result = organizations.getItems().stream().filter(vraNgOrganization -> {
return vraNgOrganization.getName().equalsIgnoreCase(organizationName);
Expand Down Expand Up @@ -1778,7 +1735,8 @@ public VraNgOrganization getOrganizationById(final String organizationId) {
}
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, getDefaultHttpEntity(),
String.class);
Gson gson = new GsonBuilder().setLenient().setPrettyPrinting().serializeNulls().create();
Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).setPrettyPrinting().serializeNulls()
.create();
org = gson.fromJson(response.getBody(), VraNgOrganization.class);

LOGGER.debug("Found organization: {}", gson.toJson(org));
Expand Down Expand Up @@ -2202,22 +2160,6 @@ private boolean isJsonElementPresent(final JsonElement jsonElement) {
return jsonElement != null && !jsonElement.isJsonNull();
}

/**
* Extract region id from a link fragment part of REST response body.
*
* @param ob fragment
* @return region id
*/
private String getLinkRegionId(final JsonObject ob) {

LOGGER.debug("Extracting data: {}", ob);

String regionHref = ob.get("_links").getAsJsonObject().get("region").getAsJsonObject().get("href")
.getAsString();

return regionHref.substring(regionHref.lastIndexOf('/') + 1);
}

/**
* Extract cloud account id from a link fragment part of REST response body.
*
Expand Down Expand Up @@ -2288,7 +2230,7 @@ protected Map<String, Object> createAbxActionMap(final AbxAction action) throws
}

private String getJsonString(final Map<String, Object> entity) {
Gson gson = new GsonBuilder().setLenient().serializeNulls().create();
Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeNulls().create();

return gson.toJson(entity);
}
Expand Down Expand Up @@ -2402,11 +2344,11 @@ protected List<VraNgContentSharingPolicy> getAllContentSharingPoliciesPrimitive(
// Add additional filter to reduce the data received from server for newer vRA
// versions (8.16 and above)
if (isVraAbove810) {
params.put("typeId", CONTENT_SHARING_POLICY_TYPE);
params.put("typeId", VraNgPolicyTypes.CONTENT_SHARING_POLICY_TYPE);
}
List<VraNgContentSharingPolicy> results = this.getPagedContent(SERVICE_POLICIES, params).stream()
.map(jsonOb -> new Gson().fromJson(jsonOb.toString(), VraNgContentSharingPolicy.class))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(CONTENT_SHARING_POLICY_TYPE))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(VraNgPolicyTypes.CONTENT_SHARING_POLICY_TYPE))
.collect(Collectors.toList());
LOGGER.debug("Policy Ids found on server - {}, for projectId: {}", results.size(), this.getProjectId());

Expand All @@ -2426,7 +2368,7 @@ public String getContentSharingPolicyIdByName(final String name) {

VraNgContentSharingPolicy policy = this.getPagedContent(SERVICE_POLICIES, params).stream()
.map(jsonOb -> new Gson().fromJson(jsonOb.toString(), VraNgContentSharingPolicy.class))
.filter(p -> p.getTypeId().equalsIgnoreCase(CONTENT_SHARING_POLICY_TYPE))
.filter(p -> p.getTypeId().equalsIgnoreCase(VraNgPolicyTypes.CONTENT_SHARING_POLICY_TYPE))
.filter(p -> p.getName().equals(name) && p.getProjectId().equals(this.getProjectId())).findFirst()
.orElse(null);
if (policy == null) {
Expand Down Expand Up @@ -2501,11 +2443,11 @@ protected List<VraNgResourceQuotaPolicy> getAllResourceQuotaPoliciesPrimitive()
Map<String, String> params = new HashMap<>();
params.put("expandDefinition", "true");
params.put("computeStats", "true");
params.put("typeId", RESOURCE_QUOTA_POLICY_TYPE);
params.put("typeId", VraNgPolicyTypes.RESOURCE_QUOTA_POLICY_TYPE);

List<VraNgResourceQuotaPolicy> results = this.getPagedContent(SERVICE_POLICIES, params).stream()
.map(jsonOb -> new Gson().fromJson(jsonOb.toString(), VraNgResourceQuotaPolicy.class))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(RESOURCE_QUOTA_POLICY_TYPE))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(VraNgPolicyTypes.RESOURCE_QUOTA_POLICY_TYPE))
.collect(Collectors.toList());

LOGGER.debug("Policy Ids found on server - {}, for projectId: {}", results.size(), this.getProjectId());
Expand Down Expand Up @@ -2568,11 +2510,11 @@ protected List<VraNgDay2ActionsPolicy> getAllDay2ActionsPoliciesPrimitive() {
params.put("expandDefinition", "true");
params.put("computeStats", "true");
// filtering by typeId works on 8.16 but not on earlier versions.
params.put("typeId", DAY2_ACTION_POLICY_TYPE);
params.put("typeId", VraNgPolicyTypes.DAY2_ACTION_POLICY_TYPE);

List<VraNgDay2ActionsPolicy> results = this.getPagedContent(SERVICE_POLICIES, params).stream()
.map(jsonOb -> new Gson().fromJson(jsonOb.toString(), VraNgDay2ActionsPolicy.class))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(DAY2_ACTION_POLICY_TYPE))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(VraNgPolicyTypes.DAY2_ACTION_POLICY_TYPE))
.collect(Collectors.toList());

LOGGER.debug("Policy Ids found on server - {}, for projectId: {}", results.size(), this.getProjectId());
Expand Down Expand Up @@ -2630,11 +2572,11 @@ protected List<VraNgLeasePolicy> getAllLeasePoliciesPrimitive() {
Map<String, String> params = new HashMap<>();
params.put("expandDefinition", "true");
params.put("computeStats", "true");
params.put("typeId", LEASE_POLICY_TYPE);
params.put("typeId", VraNgPolicyTypes.LEASE_POLICY_TYPE);

List<VraNgLeasePolicy> results = this.getPagedContent(SERVICE_POLICIES, params).stream()
.map(jsonOb -> new Gson().fromJson(jsonOb.toString(), VraNgLeasePolicy.class))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(LEASE_POLICY_TYPE))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(VraNgPolicyTypes.LEASE_POLICY_TYPE))
.collect(Collectors.toList());

LOGGER.debug("Lease Policies found on server - {}, for projectId: {}", results.size(), this.getProjectId());
Expand Down Expand Up @@ -2729,11 +2671,12 @@ protected List<VraNgDeploymentLimitPolicy> getAllDeploymentLimitPoliciesPrimitiv
params.put("expandDefinition", "true");
params.put("computeStats", "true");
// filtering by typeId works on 8.16 but not on earlier versions.
params.put("typeId", DEPLOYMENT_LIMIT_POLICY_TYPE);
params.put("typeId", VraNgPolicyTypes.DEPLOYMENT_LIMIT_POLICY_TYPE);

List<VraNgDeploymentLimitPolicy> results = this.getPagedContent(SERVICE_POLICIES, params).stream()
.map(jsonOb -> new Gson().fromJson(jsonOb.toString(), VraNgDeploymentLimitPolicy.class))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(DEPLOYMENT_LIMIT_POLICY_TYPE))
.filter(policy -> policy.getTypeId()
.equalsIgnoreCase(VraNgPolicyTypes.DEPLOYMENT_LIMIT_POLICY_TYPE))
.collect(Collectors.toList());

LOGGER.debug("Policy Ids found on server - {}, for projectId: {}", results.size(), this.getProjectId());
Expand Down Expand Up @@ -2792,12 +2735,12 @@ protected List<VraNgApprovalPolicy> getAllApprovalPoliciesPrimitive() {
params.put("computeStats", "true");
// filtering by typeId works on 8.16 but not on earlier versions.
// filter here to reduce traffic for newer vRA versions.
params.put("typeId", APPROVAL_POLICY_TYPE);
params.put("typeId", VraNgPolicyTypes.APPROVAL_POLICY_TYPE);

// filter here for older vRA versions.
List<VraNgApprovalPolicy> results = this.getPagedContent(SERVICE_POLICIES, params).stream()
.map(jsonOb -> new Gson().fromJson(jsonOb.toString(), VraNgApprovalPolicy.class))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(APPROVAL_POLICY_TYPE))
.filter(policy -> policy.getTypeId().equalsIgnoreCase(VraNgPolicyTypes.APPROVAL_POLICY_TYPE))
.collect(Collectors.toList());

LOGGER.debug("Policy Ids found on server - {}, for projectId: {}", results.size(), this.getProjectId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.vmware.pscoe.iac.artifact.aria.automation.rest.models;

/*-
* #%L
* artifact-manager
* %%
* Copyright (C) 2023 - 2025 VMware
* %%
* Build Tools for VMware Aria
* Copyright 2023 VMware, Inc.
*
* This product is licensed to you under the BSD-2 license (the "License"). You may not use this product except in compliance with the BSD-2 License.
*
* This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
* #L%
*/

/**
* VraNgPolicyTypes is used to hold different policy types
*/
public final class VraNgPolicyTypes {
/**
* CONTENT_SHARING_POLICY_TYPE.
*/
public static final String CONTENT_SHARING_POLICY_TYPE = "com.vmware.policy.catalog.entitlement";
/**
* APPROVAL_POLICY_TYPE.
*/
public static final String APPROVAL_POLICY_TYPE = "com.vmware.policy.approval";
/**
* DAY2_ACTION_POLICY_TYPE.
*/
public static final String DAY2_ACTION_POLICY_TYPE = "com.vmware.policy.deployment.action";
/**
* LEASE_POLICY_TYPE.
*/
public static final String LEASE_POLICY_TYPE = "com.vmware.policy.deployment.lease";
/**
* DEPLOYMENT_LIMIT_POLICY_TYPE.
*/
public static final String DEPLOYMENT_LIMIT_POLICY_TYPE = "com.vmware.policy.deployment.limit";
/**
* RESOURCE_QUOTA_POLICY_TYPE.
*/
public static final String RESOURCE_QUOTA_POLICY_TYPE = "com.vmware.policy.resource.quota";

private VraNgPolicyTypes() {
}
}
Loading
Loading