-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
743 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Logger } from '../logger.js'; | ||
import * as ng from '../models/ng.js'; | ||
import * as ngApi from '@clevercloud/client/cjs/api/v4/network-group.js'; | ||
import { sendToApi } from '../models/send-to-api.js'; | ||
|
||
export async function listMembers (params) { | ||
const [networkGroupIdOrLabel] = params.args; | ||
const { org: orgaIdOrName, 'natural-name': naturalName, format } = params.options; | ||
|
||
const ownerId = await ng.getOwnerId(orgaIdOrName); | ||
const networkGroupId = await ng.getId(ownerId, networkGroupIdOrLabel); | ||
|
||
Logger.info(`Listing members from Network Group '${networkGroupId}'`); | ||
Logger.info(naturalName); | ||
|
||
const result = await ngApi.listNetworkGroupMembers({ ownerId, networkGroupId }).then(sendToApi); | ||
Logger.debug(`Received from API: ${JSON.stringify(result, null, 2)}`); | ||
|
||
switch (format) { | ||
case 'json': { | ||
Logger.println(JSON.stringify(result, null, 2)); | ||
break; | ||
} | ||
case 'human': | ||
default: { | ||
if (result.length === 0) { | ||
Logger.println('No member found. You can add one with \'clever ng members add\'.'); | ||
} | ||
else { | ||
const domainNames = result.map((item) => ({ domainName: item.domainName })); | ||
console.table(domainNames); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export async function getMember (params) { | ||
const [networkGroupIdOrLabel, memberId] = params.args; | ||
const { org: orgaIdOrName, 'natural-name': naturalName, format } = params.options; | ||
|
||
const ownerId = await ng.getOwnerId(orgaIdOrName); | ||
const networkGroupId = await ng.getId(ownerId, networkGroupIdOrLabel); | ||
|
||
Logger.info(`Getting details for member ${memberId} in Network Group ${networkGroupId}`); | ||
Logger.info(`Natural name: ${naturalName}`); | ||
const result = await ngApi.getNetworkGroupMember({ ownerId, networkGroupId, memberId: memberId }).then(sendToApi); | ||
Logger.debug(`Received from API: ${JSON.stringify(result, null, 2)}`); | ||
|
||
switch (format) { | ||
case 'json': { | ||
Logger.println(JSON.stringify(result, null, 2)); | ||
break; | ||
} | ||
case 'human': | ||
default: { | ||
const domainName = [result].map((item) => ({ domainName: item.domainName })); | ||
console.table(domainName); | ||
} | ||
} | ||
} | ||
|
||
export async function addMember (params) { | ||
const [networkGroupIdOrLabel, memberId] = params.args; | ||
const { org: orgaIdOrName, label } = params.options; | ||
|
||
const ownerId = await ng.getOwnerId(orgaIdOrName); | ||
const networkGroupId = await ng.getId(ownerId, networkGroupIdOrLabel); | ||
const domainName = `${memberId}.m.${networkGroupId}.ng.clever-cloud.com`; | ||
|
||
let type = null; | ||
if (memberId.startsWith('app_')) { | ||
type = 'application'; | ||
} | ||
else if (memberId.startsWith('addon_')) { | ||
type = 'addon'; | ||
} | ||
else if (memberId.startsWith('external_')) { | ||
type = 'external'; | ||
} | ||
else { | ||
// throw new Error(`Member ID ${Formatter.formatString(memberId)} is not a valid format. It should start with 'app_', 'addon_' or 'external_'`); | ||
type = 'addon'; | ||
} | ||
|
||
const body = { id: memberId, label, domainName: domainName, type }; | ||
Logger.debug('Sending body: ' + JSON.stringify(body, null, 2)); | ||
await ngApi.createNetworkGroupMember({ ownerId, networkGroupId }, body).then(sendToApi); | ||
|
||
Logger.println(`Successfully added member ${memberId} to Network Group ${networkGroupId}.`); | ||
} | ||
|
||
export async function removeMember (params) { | ||
const [networkGroupIdOrLabel, memberId] = params.args; | ||
const { org: orgaIdOrName } = params.options; | ||
|
||
const ownerId = await ng.getOwnerId(orgaIdOrName); | ||
const networkGroupId = await ng.getId(ownerId, networkGroupIdOrLabel); | ||
|
||
await ngApi.deleteNetworkGroupMember({ ownerId, networkGroupId, memberId }).then(sendToApi); | ||
|
||
Logger.println(`Successfully removed member ${memberId} from Network Group ${networkGroupId}.`); | ||
} |
Oops, something went wrong.