forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cost quota and price (labring#5014)
* feat: init template cost quota and price * feat: usage * feat: price box * fix: checkQuotaAllow * fix: get quota * fix: nodeport pricea * fix: no used code * fix: no used code * fix: CheckQuotaType * fix: cannot slice number
- Loading branch information
1 parent
6fe30a7
commit d7a7179
Showing
16 changed files
with
739 additions
and
11 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
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
3 changes: 3 additions & 0 deletions
3
frontend/providers/template/src/components/Icon/icons/help.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
23 changes: 23 additions & 0 deletions
23
frontend/providers/template/src/pages/api/platform/getQuota.ts
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,23 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { getK8s } from '@/services/backend/kubernetes'; | ||
import { jsonRes } from '@/services/backend/response'; | ||
import { authSession } from '@/services/backend/auth'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
// source price | ||
const { getUserQuota } = await getK8s({ | ||
kubeconfig: await authSession(req.headers) | ||
}); | ||
|
||
const quota = await getUserQuota(); | ||
|
||
jsonRes(res, { | ||
data: { | ||
quota | ||
} | ||
}); | ||
} catch (error) { | ||
jsonRes(res, { code: 500, message: 'get price error' }); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
frontend/providers/template/src/pages/api/platform/resourcePrice.ts
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,75 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { POST } from '@/services/request'; | ||
import { getK8s } from '@/services/backend/kubernetes'; | ||
import { jsonRes } from '@/services/backend/response'; | ||
import { authSession } from '@/services/backend/auth'; | ||
import type { userPriceType } from '@/types/user'; | ||
|
||
type properties = { | ||
properties: property[]; | ||
}; | ||
|
||
type property = { | ||
name: string; | ||
unit_price: number; | ||
unit: string; | ||
}; | ||
|
||
export function transformProperties(data: properties): userPriceType { | ||
const userPrice: userPriceType = { | ||
cpu: 0, | ||
memory: 0, | ||
storage: 0, | ||
nodeports: 0 | ||
}; | ||
|
||
data.properties.forEach((property: property) => { | ||
switch (property.name) { | ||
case 'cpu': | ||
userPrice.cpu = property.unit_price; | ||
break; | ||
case 'memory': | ||
userPrice.memory = property.unit_price; | ||
break; | ||
case 'storage': | ||
userPrice.storage = property.unit_price; | ||
break; | ||
case 'services.nodeports': | ||
userPrice.nodeports = property.unit_price; | ||
break; | ||
} | ||
}); | ||
|
||
return userPrice; | ||
} | ||
|
||
const getResourcePrice = async () => { | ||
const res = await fetch( | ||
`https://account-api.${process.env.SEALOS_CLOUD_DOMAIN}/account/v1alpha1/properties`, | ||
{ | ||
method: 'POST' | ||
} | ||
); | ||
const data = await res.json(); | ||
return transformProperties(data.data as properties); | ||
}; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
// source price | ||
const { applyYamlList, k8sCustomObjects, k8sCore, namespace } = await getK8s({ | ||
kubeconfig: await authSession(req.headers) | ||
}); | ||
|
||
const data = await getResourcePrice(); | ||
|
||
jsonRes<userPriceType>(res, { | ||
code: 200, | ||
data: data | ||
}); | ||
} catch (error) { | ||
console.log('get resoure price error: ', error); | ||
|
||
jsonRes(res, { code: 500, message: 'get price error' }); | ||
} | ||
} |
Oops, something went wrong.