Skip to content
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
11 changes: 8 additions & 3 deletions requests/Switcher API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -5764,16 +5764,21 @@
"method": "GET",
"header": [],
"url": {
"raw": "{{url}}/gitops/v1/account/{{gitops_domain_id}}/default",
"raw": "{{url}}/gitops/v1/account/{{gitops_domain_id}}?environment=default",
"host": [
"{{url}}"
],
"path": [
"gitops",
"v1",
"account",
"{{gitops_domain_id}}",
"default"
"{{gitops_domain_id}}"
],
"query": [
{
"key": "environment",
"value": "default"
}
]
}
},
Expand Down
83 changes: 50 additions & 33 deletions src/external/gitops.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,69 @@ export async function createAccount(account) {
});

if (response.status !== 201) {
throw new GitOpsError(`Failed to create account [${response.status}] ${JSON.stringify(response.data)}`);
throw new GitOpsError(`Failed to create account [${response.status}] ${JSON.stringify(response.data)}`,
response.status);
}

return response.data;
}

export async function updateAccount(account) {
const url = `${process.env.SWITCHER_GITOPS_URL}/account`;
const response = await axios.put(url, account, {
httpsAgent: agent(url),
headers: headers(account.domain.id)
});

if (response.status !== 200) {
throw new GitOpsError(`Failed to update account [${response.status}] ${JSON.stringify(response.data)}`);
try {
const url = `${process.env.SWITCHER_GITOPS_URL}/account`;
const response = await axios.put(url, account, {
httpsAgent: agent(url),
headers: headers(account.domain.id)
});

if (response.status !== 200) {
throw new GitOpsError(`Failed to update account [${response.status}] ${JSON.stringify(response.data)}`,
response.status);
}

return response.data;
} catch (e) {
throw new GitOpsError(`Failed to update account: ${e.message}`, e.status);
}

return response.data;
}

export async function deleteAccount(domainId, environment) {
const url = `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}`;
const response = await axios.delete(url, {
httpsAgent: agent(url),
headers: headers(domainId)
});

if (response.status !== 204) {
throw new GitOpsError(`Failed to delete account [${response.status}] ${JSON.stringify(response.data)}`);
try {
const url = `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}`;
const response = await axios.delete(url, {
httpsAgent: agent(url),
headers: headers(domainId)
});

if (response.status !== 204) {
throw new GitOpsError(`Failed to delete account [${response.status}] ${JSON.stringify(response.data)}`,
response.status);
}
} catch (e) {
throw new GitOpsError(`Failed to delete account: ${e.message}`, e.status);
}
}

export async function fetchAccounts(domainId, environment) {
const url = environment ?
`${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}` :
`${process.env.SWITCHER_GITOPS_URL}/account/${domainId}`;

const response = await axios.get(url, {
httpsAgent: agent(url),
headers: headers(domainId)
});

if (response.status !== 200) {
throw new GitOpsError(`Failed to fetch accounts [${response.status}] ${JSON.stringify(response.data)}`);
try {
const url = environment ?
`${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}` :
`${process.env.SWITCHER_GITOPS_URL}/account/${domainId}`;

const response = await axios.get(url, {
httpsAgent: agent(url),
headers: headers(domainId)
});

if (response.status !== 200) {
throw new GitOpsError(`Failed to fetch accounts [${response.status}] ${JSON.stringify(response.data)}`,
response.status);
}

return response.data;
} catch (e) {
throw new GitOpsError(`Failed to fetch accounts: ${e.message}`, e.status);
}

return response.data;
}

function generateToken(subject) {
Expand All @@ -81,9 +97,10 @@ function generateToken(subject) {
}

export class GitOpsError extends Error {
constructor(message) {
constructor(message, code = 500) {
super(message);
this.name = this.constructor.name;
this.code = code;
Error.captureStackTrace(this, this.constructor);
}
}
2 changes: 1 addition & 1 deletion tests/gitops-account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('GitOps Account - Subscribe', () => {
.post('/gitops/v1/account/subscribe')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send(VALID_SUBSCRIPTION_REQUEST)
.expect(500);
.expect(401);

// assert
expect(req.body.error).toBe('Account subscription failed');
Expand Down
Loading