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

feat(imperative): Enhanced ProfileInfo.updateProperty #1983

Merged
merged 3 commits into from
Dec 8, 2023
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
4 changes: 4 additions & 0 deletions packages/imperative/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ that would be used if a command were executed.
- BugFix: Fixed incorrect description for untyped profiles in team config files. [zowe/zowe-cli#1303](https://github.com/zowe/zowe-cli/issues/1303)
- **Next Breaking**: Schema files created or updated with the above changes are not backward compatible with older versions of Imperative.

## Recent Changes

- Enhancement: Added the ability to `forceUpdate` a property using the `ProfileInfo.updateProperty` method. [zowe-explorer#2493](https://github.com/zowe/vscode-extension-for-zowe/issues/2493)

## `5.0.0-next.202203222132`

- BugFix: Reverted unintentional breaking change that prevented `DefaultCredentialManager` from finding Keytar outside of calling CLI's node_modules folder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { AbstractSession, SessConstants } from "../../rest";
import { ConfigAutoStore } from "../src/ConfigAutoStore";
import { ImperativeConfig } from "../../utilities/src/ImperativeConfig";
import { ImperativeError } from "../../error";
import { IProfInfoUpdatePropOpts } from "../src/doc/IProfInfoUpdatePropOpts";

const testAppNm = "ProfInfoApp";
const testEnvPrefix = testAppNm.toUpperCase();
Expand Down Expand Up @@ -1009,6 +1010,36 @@ describe("TeamConfig ProfileInfo tests", () => {
expect(updateKnownPropertySpy).toHaveBeenCalledWith({ ...profileOptions, mergedArgs: {}, osLocInfo: undefined });
});

it("should succeed forceUpdating a property even if the jsonLoc points somewhere else", async () => {
const profInfo = createNewProfInfo(teamProjDir);
await profInfo.readProfilesFromDisk();
const mergedArgs = {
knownArgs: [{
argName: "rejectUnauthorized",
argLoc: { jsonLoc: "profiles.base_glob.properties.rejectUnauthorized" }
}]
};
jest.spyOn(profInfo as any, "mergeArgsForProfile").mockReturnValue(mergedArgs);
const updateKnownPropertySpy = jest.spyOn(profInfo as any, "updateKnownProperty").mockResolvedValue(true);
const profileOptions: IProfInfoUpdatePropOpts = {
profileName: "LPAR4",
profileType: "dummy",
property: "rejectUnauthorized",
value: true,
forceUpdate: true
};
let caughtError;
try {
await profInfo.updateProperty(profileOptions);
} catch (error) {
caughtError = error;
}
expect(caughtError).toBeUndefined();
expect(mergedArgs.knownArgs[0].argLoc.jsonLoc).toEqual("profiles.LPAR4.properties.rejectUnauthorized");
const osLocInfo = { global: false, user: false, name: "LPAR4", path: path.join(teamProjDir, `${testAppNm}.config.json`) };
expect(updateKnownPropertySpy).toHaveBeenCalledWith({ ...profileOptions, mergedArgs, osLocInfo });
});

it("should attempt to store session config properties without adding profile types to the loadedConfig", async () => {
const profInfo = createNewProfInfo(teamProjDir);
await profInfo.readProfilesFromDisk();
Expand Down Expand Up @@ -1153,11 +1184,11 @@ describe("TeamConfig ProfileInfo tests", () => {
await profInfo.readProfilesFromDisk();

const prof = profInfo.mergeArgsForProfile(profInfo.getAllProfiles("dummy")[0]);
await profInfo.updateProperty({profileName: 'LPAR4', property: "someProperty", value: "example.com", profileType: "dummy"});
await profInfo.updateProperty({ profileName: 'LPAR4', property: "someProperty", value: "example.com", profileType: "dummy" });
const afterUpdate = profInfo.mergeArgsForProfile(profInfo.getAllProfiles("dummy")[0]);
expect(afterUpdate.knownArgs.find(v => v.argName === 'someProperty')?.argValue).toBe('example.com');

await profInfo.removeKnownProperty({mergedArgs: afterUpdate, property: 'someProperty'});
await profInfo.removeKnownProperty({ mergedArgs: afterUpdate, property: 'someProperty' });
const afterRemove = profInfo.mergeArgsForProfile(profInfo.getAllProfiles("dummy")[0]);

expect(afterRemove.knownArgs.find(v => v.argName === 'someProperty')).toBeUndefined();
Expand Down
7 changes: 7 additions & 0 deletions packages/imperative/src/config/src/ProfileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ export class ProfileInfo {
}

const mergedArgs = this.mergeArgsForProfile(desiredProfile, { getSecureVals: false });
if (options.forceUpdate && this.usingTeamConfig) {
const knownProperty = mergedArgs.knownArgs.find((v => v.argName === options.property));
const profPath = this.getTeamConfig().api.profiles.getProfilePathFromName(options.profileName);
if (!knownProperty?.argLoc.jsonLoc.startsWith(profPath)) {
knownProperty.argLoc.jsonLoc = `${profPath}.properties.${options.property}`;
}
}
if (!(await this.updateKnownProperty({ ...options, mergedArgs, osLocInfo: this.getOsLocInfo(desiredProfile)?.[0] }))) {
if (this.usingTeamConfig) {
// Check to see if loadedConfig already contains the schema for the specified profile type
Expand Down
10 changes: 10 additions & 0 deletions packages/imperative/src/config/src/doc/IProfInfoUpdatePropOpts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export interface IProfInfoUpdatePropOpts extends IProfInfoUpdatePropCommonOpts {
* Name of the active profile
*/
profileName: string;

/**
* Force the update to the profile specified even if the property comes from somehwere else
zFernand0 marked this conversation as resolved.
Show resolved Hide resolved
* @example Token Value could be in the base profile (not in the service profile specified)
* and the programmer has the intention of storing the token in the service profile
* @default false When the property is not specified, the updateProperty method follows current
* procedure of updating the property in the known jsonLoc (e.g. base profile). Otherwise,
* the updateProperty method updates the specified profile name-type combination.
*/
forceUpdate?: boolean
}

/**
Expand Down