-
Notifications
You must be signed in to change notification settings - Fork 22
Create Product Consumption Tracking Module #689
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
828c37c
1532794
e6390da
7c6fd38
838f126
5725a50
a7a4213
63dc846
4cf2f26
29f12dc
78d3a59
ca7254c
4e7184c
da5ef75
1e669ce
7b1438d
759bab8
44b2701
e34c4e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import ballerina/http; | ||
|
|
||
| configurable string productConsumptionTrackingBaseUrl = ?; | ||
| configurable ClientCredentialsOauth2Config clientCredentialsOauth2Config = ?; | ||
|
|
||
| @display { | ||
| label: "Product Consumption Tracking", | ||
| id: "product-consumption-tracking" | ||
| } | ||
| final http:Client productConsumptionTrackingClient = check new (productConsumptionTrackingBaseUrl, { | ||
| auth: {...clientCredentialsOauth2Config}, | ||
| httpVersion: http:HTTP_1_1, | ||
| http1Settings: {keepAlive: http:KEEPALIVE_NEVER}, | ||
| timeout: 300.0, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting this into a constant or configurable value
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
| retryConfig: { | ||
| count: 3, | ||
| interval: 2.0, | ||
|
agzaiyenth marked this conversation as resolved.
Outdated
|
||
| statusCodes: [ | ||
| http:STATUS_INTERNAL_SERVER_ERROR, | ||
| http:STATUS_REQUEST_TIMEOUT, | ||
| http:STATUS_BAD_GATEWAY, | ||
| http:STATUS_SERVICE_UNAVAILABLE, | ||
| http:STATUS_GATEWAY_TIMEOUT | ||
| ] | ||
| } | ||
|
agzaiyenth marked this conversation as resolved.
|
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| const CONTENT_TYPE_APPLICATION_ZIP = "application/zip"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| # Import deployment usage data from a zip file. | ||
| # | ||
| # + email - Email address of the importing user | ||
| # + zipFile - Zip file content as bytes | ||
| # + return - Deployment usage import response or error | ||
| public isolated function importDeploymentUsage(string email, byte[] zipFile) | ||
| returns DeploymentUsageImportResponse|error { | ||
|
agzaiyenth marked this conversation as resolved.
Outdated
|
||
|
|
||
| http:Request req = new; | ||
|
agzaiyenth marked this conversation as resolved.
Outdated
|
||
| req.setBinaryPayload(zipFile); | ||
| req.setContentType(CONTENT_TYPE_APPLICATION_ZIP); | ||
|
agzaiyenth marked this conversation as resolved.
Outdated
|
||
|
|
||
| return productConsumptionTrackingClient->/deployment\-usage\-import.post(req, email); | ||
|
|
||
|
agzaiyenth marked this conversation as resolved.
Outdated
agzaiyenth marked this conversation as resolved.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| # [Configurable] Client credentials grant type oauth2 configuration. | ||
| public type ClientCredentialsOauth2Config record {| | ||
| # Token URL | ||
| string tokenUrl; | ||
| # Client ID | ||
| string clientId; | ||
| # Client Secret | ||
| string clientSecret; | ||
|
agzaiyenth marked this conversation as resolved.
|
||
| |}; | ||
|
|
||
| # Deployment usage import response. | ||
| public type DeploymentUsageImportResponse record {| | ||
| # Response message | ||
| string message?; | ||
| # Response result | ||
| json result?; | ||
|
agzaiyenth marked this conversation as resolved.
|
||
| json...; | ||
| |}; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import customer_portal.ai_chat_agent; | |
| import customer_portal.authorization; | ||
| import customer_portal.entity; | ||
| import customer_portal.product_consumption_subscription; | ||
| import customer_portal.product_consumption_tracking; | ||
| import customer_portal.registry; | ||
| import customer_portal.scim; | ||
| import customer_portal.types; | ||
|
|
@@ -4001,7 +4002,7 @@ service http:InterceptableService / on new http:Listener(9090, listenerConf) { | |
| resource function post projects/[entity:IdString id]/cases/time\-cards/search(http:RequestContext ctx, | ||
| types:TimeCardSearchPayload payload) | ||
| returns http:Ok|http:BadRequest|http:Unauthorized|http:Forbidden|http:InternalServerError { | ||
|
agzaiyenth marked this conversation as resolved.
|
||
|
|
||
|
agzaiyenth marked this conversation as resolved.
Outdated
|
||
| authorization:UserInfoPayload|error userInfo = ctx.getWithType(authorization:HEADER_USER_INFO); | ||
| if userInfo is error { | ||
| return <http:InternalServerError>{ | ||
|
|
@@ -5108,6 +5109,65 @@ service http:InterceptableService / on new http:Listener(9090, listenerConf) { | |
| return licenseResponse; | ||
| } | ||
|
|
||
| # Import product consumption usage from a zip file. | ||
| # | ||
| # + req - Request containing zip file binary body | ||
| # + return - Deployment usage import response or error | ||
| isolated resource function post deployment\-usage\-import(http:RequestContext ctx, http:Request req) | ||
|
agzaiyenth marked this conversation as resolved.
Outdated
|
||
| returns product_consumption_tracking:DeploymentUsageImportResponse|http:BadRequest|http:InternalServerError { | ||
|
|
||
| authorization:UserInfoPayload|error userInfo = ctx.getWithType(authorization:HEADER_USER_INFO); | ||
| if userInfo is error { | ||
| return <http:InternalServerError>{ | ||
| body: { | ||
| message: ERR_MSG_USER_INFO_HEADER_NOT_FOUND | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| string? validationError = validateDeploymentUsageImportRequest(req); | ||
| if validationError is string { | ||
| log:printWarn(validationError); | ||
| return <http:BadRequest>{ | ||
| body: { | ||
| message: validationError | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we return a custom error here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
| } | ||
| }; | ||
| } | ||
|
|
||
| byte[]|http:ClientError zipFile = req.getBinaryPayload(); | ||
|
agzaiyenth marked this conversation as resolved.
|
||
| if zipFile is http:ClientError { | ||
|
agzaiyenth marked this conversation as resolved.
|
||
| string customError = "Failed to read deployment usage import zip file."; | ||
| log:printError(customError, zipFile); | ||
| return <http:BadRequest>{ | ||
| body: { | ||
| message: customError | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| product_consumption_tracking:DeploymentUsageImportResponse|error response = | ||
| product_consumption_tracking:importDeploymentUsage(userInfo.email, zipFile); | ||
| if response is error { | ||
| if getStatusCode(response) == http:STATUS_BAD_REQUEST { | ||
| return <http:BadRequest>{ | ||
| body: { | ||
| message: "Invalid deployment usage import request." | ||
|
agzaiyenth marked this conversation as resolved.
|
||
| } | ||
| }; | ||
| } | ||
|
|
||
| string customError = "Failed to import deployment usage data."; | ||
| log:printError(customError, response); | ||
| return <http:InternalServerError>{ | ||
| body: { | ||
| message: customError | ||
| } | ||
| }; | ||
| } | ||
| return response; | ||
| } | ||
|
|
||
| # Get usage stats for a specific project. | ||
| # | ||
| # + id - ID of the project | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.