diff --git a/common-controller/internal/database/dao.go b/common-controller/internal/database/dao.go index 44d4d0fde..e7a060c65 100644 --- a/common-controller/internal/database/dao.go +++ b/common-controller/internal/database/dao.go @@ -196,13 +196,13 @@ func DeleteAllAppAttributes(tx pgx.Tx) error { } // AddSubscription adds a subscription to the database -func AddSubscription(tx pgx.Tx, uuid, apiName, apiVersion, subStatus, organization string) error { - return ExecDBQuery(tx, insertSubscription, uuid, apiName, apiVersion, subStatus, organization) +func AddSubscription(tx pgx.Tx, uuid, apiName, apiVersion, subStatus, organization string, rateLimitTier string) error { + return ExecDBQuery(tx, insertSubscription, uuid, apiName, apiVersion, subStatus, organization, rateLimitTier) } // UpdateSubscription updates a subscription in the database -func UpdateSubscription(tx pgx.Tx, uuid, apiName, apiVersion, subStatus, organization string) error { - return ExecDBQuery(tx, updateSubscription, uuid, apiName, apiVersion, subStatus, organization) +func UpdateSubscription(tx pgx.Tx, uuid, apiName, apiVersion, subStatus, organization string, rateLimitTier string) error { + return ExecDBQuery(tx, updateSubscription, uuid, apiName, apiVersion, subStatus, organization, rateLimitTier) } // DeleteSubscription deletes a subscription from the database diff --git a/common-controller/internal/database/database.go b/common-controller/internal/database/database.go index 063168fc1..62604008b 100644 --- a/common-controller/internal/database/database.go +++ b/common-controller/internal/database/database.go @@ -95,6 +95,7 @@ func PrepareQueries(tx pgx.Tx, queries ...string) { // performTransaction performs a transaction func performTransaction(fn func(tx pgx.Tx) error) error { + ConnectToDB() con := context.Background() tx, err := dbPool.BeginTx(con, pgx.TxOptions{}) if err != nil { diff --git a/common-controller/internal/database/db_deployer.go b/common-controller/internal/database/db_deployer.go index a87ccb372..674207c2f 100644 --- a/common-controller/internal/database/db_deployer.go +++ b/common-controller/internal/database/db_deployer.go @@ -111,7 +111,7 @@ func (dbDeployer DBDeployer) DeploySubscription(subscription server.Subscription retryUntilTransaction(func(tx pgx.Tx) error { PrepareQueries(tx, insertSubscription) return AddSubscription(tx, subscription.UUID, subscription.SubscribedAPI.Name, subscription.SubscribedAPI.Version, - subscription.SubStatus, subscription.Organization) + subscription.SubStatus, subscription.Organization, subscription.RatelimitTier) }) server.AddSubscription(subscription) return nil @@ -122,7 +122,7 @@ func (dbDeployer DBDeployer) UpdateSubscription(subscription server.Subscription retryUntilTransaction(func(tx pgx.Tx) error { PrepareQueries(tx, updateSubscription) return UpdateSubscription(tx, subscription.UUID, subscription.SubscribedAPI.Name, subscription.SubscribedAPI.Version, - subscription.SubStatus, subscription.Organization) + subscription.SubStatus, subscription.Organization, subscription.RatelimitTier) }) server.DeleteSubscription(subscription.UUID) server.AddSubscription(subscription) @@ -344,7 +344,7 @@ func (dbDeployer DBDeployer) DeployAllSubscriptions(subscriptions server.Subscri server.DeleteAllSubscriptions() for _, subscription := range subscriptions.List { if err := AddSubscription(tx, subscription.UUID, subscription.SubscribedAPI.Name, subscription.SubscribedAPI.Version, - subscription.SubStatus, subscription.Organization); err != nil { + subscription.SubStatus, subscription.Organization, subscription.RatelimitTier); err != nil { loggers.LoggerAPI.Error("Error while adding subscription ", err) return err } diff --git a/common-controller/internal/database/queries.go b/common-controller/internal/database/queries.go index f7b505fed..1c35db5b1 100644 --- a/common-controller/internal/database/queries.go +++ b/common-controller/internal/database/queries.go @@ -29,9 +29,9 @@ const ( deleteApplicationAttributes = "DELETE FROM APPLICATION_ATTRIBUTES WHERE APPLICATION_UUID = $1;" deleteAllAppAttributes = "DELETE FROM APPLICATION_ATTRIBUTES;" - insertSubscription = "INSERT INTO SUBSCRIPTION (UUID, API_NAME, API_VERSION, SUB_STATUS, ORGANIZATION) VALUES ($1, $2, $3, $4, $5);" - getAllSubscriptions = "SELECT UUID, API_NAME, API_VERSION, SUB_STATUS, ORGANIZATION FROM SUBSCRIPTION;" - updateSubscription = "UPDATE SUBSCRIPTION SET API_NAME = $2, API_VERSION = $3, SUB_STATUS = $4, ORGANIZATION = $5 WHERE UUID = $1;" + insertSubscription = "INSERT INTO SUBSCRIPTION (UUID, API_NAME, API_VERSION, SUB_STATUS, ORGANIZATION, RATELIMIT_TIER) VALUES ($1, $2, $3, $4, $5, $6);" + getAllSubscriptions = "SELECT UUID, API_NAME, API_VERSION, SUB_STATUS, ORGANIZATION, RATELIMIT_TIER FROM SUBSCRIPTION;" + updateSubscription = "UPDATE SUBSCRIPTION SET API_NAME = $2, API_VERSION = $3, SUB_STATUS = $4, ORGANIZATION = $5, RATELIMIT_TIER = $6 WHERE UUID = $1;" deleteSubscription = "DELETE FROM SUBSCRIPTION WHERE UUID = $1;" deleteAllSubscriptions = "DELETE FROM SUBSCRIPTION;" diff --git a/common-controller/resources/db/postgresql.sql b/common-controller/resources/db/postgresql.sql index 40bf3bc15..0dc49ab2d 100644 --- a/common-controller/resources/db/postgresql.sql +++ b/common-controller/resources/db/postgresql.sql @@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS SUBSCRIPTION ( API_VERSION VARCHAR(30), SUB_STATUS VARCHAR(50), ORGANIZATION VARCHAR(100), + RATELIMIT_TIER VARCHAR(100), PRIMARY KEY (UUID) ); diff --git a/helm-charts/templates/data-plane/gateway-components/common-log-conf.yaml b/helm-charts/templates/data-plane/gateway-components/common-log-conf.yaml index 072da51bf..25ae51d4b 100644 --- a/helm-charts/templates/data-plane/gateway-components/common-log-conf.yaml +++ b/helm-charts/templates/data-plane/gateway-components/common-log-conf.yaml @@ -46,7 +46,7 @@ data: enabled = {{ .Values.wso2.apk.dp.commonController.deployment.database.enabled | default false }} name = "{{ .Values.wso2.apk.dp.commonController.deployment.database.name | default "DATAPLANE" }}" host = "{{ .Values.wso2.apk.dp.commonController.deployment.database.host | default "wso2apk-db-service.apk" }}" - port = "{{ .Values.wso2.apk.dp.commonController.deployment.database.port | default "5432" }}" + port = {{ .Values.wso2.apk.dp.commonController.deployment.database.port | default 5432 }} username = "{{ .Values.wso2.apk.dp.commonController.deployment.database.username | default "wso2carbon" }}" password = "{{ .Values.wso2.apk.dp.commonController.deployment.database.password | default "wso2carbon" }}" diff --git a/helm-charts/templates/postgres/initdb-conf.yaml b/helm-charts/templates/postgres/initdb-conf.yaml index 27fefd708..c084074ae 100644 --- a/helm-charts/templates/postgres/initdb-conf.yaml +++ b/helm-charts/templates/postgres/initdb-conf.yaml @@ -404,4 +404,64 @@ data: ); -- End of Non Prod IDP table -- commit; -{{ end }} + + {{- if .Values.wso2.apk.dp.commonController.deployment.database }} + CREATE DATABASE "{{ .Values.wso2.apk.dp.commonController.deployment.database.name | default "DATAPLANE" }}"; + GRANT ALL PRIVILEGES ON DATABASE "{{ .Values.wso2.apk.dp.commonController.deployment.database.name | default "DATAPLANE" }}" TO wso2carbon; + + \c "{{ .Values.wso2.apk.dp.commonController.deployment.database.name | default "DATAPLANE" }}" + BEGIN TRANSACTION; + CREATE TABLE IF NOT EXISTS SUBSCRIPTION ( + UUID VARCHAR(256), + API_NAME VARCHAR(256), + API_VERSION VARCHAR(30), + SUB_STATUS VARCHAR(50), + ORGANIZATION VARCHAR(100), + RATELIMIT_TIER VARCHAR(100), + PRIMARY KEY (UUID) + ); + + CREATE TABLE IF NOT EXISTS APPLICATION ( + UUID VARCHAR(256), + NAME VARCHAR(100), + OWNER VARCHAR(100), + ORGANIZATION VARCHAR(100), + PRIMARY KEY(UUID), + UNIQUE (UUID) + ); + + CREATE TABLE IF NOT EXISTS APPLICATION_SUBSCRIPTION_MAPPING ( + UUID VARCHAR(100), + APPLICATION_UUID VARCHAR(512), + SUBSCRIPTION_UUID VARCHAR(512), + ORGANIZATION VARCHAR(100), + FOREIGN KEY(APPLICATION_UUID) REFERENCES APPLICATION(UUID) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY(SUBSCRIPTION_UUID) REFERENCES SUBSCRIPTION(UUID) ON UPDATE CASCADE ON DELETE CASCADE, + PRIMARY KEY(APPLICATION_UUID, SUBSCRIPTION_UUID), + UNIQUE(UUID) + ); + + CREATE TABLE IF NOT EXISTS APPLICATION_KEY_MAPPING ( + APPLICATION_UUID VARCHAR(512), + APPLICATION_IDENTIFIER VARCHAR(512), + KEY_TYPE VARCHAR(512) NOT NULL, + ENVIRONMENT VARCHAR(512) NOT NULL, + SECURITY_SCHEME VARCHAR(512) NOT NULL, + ORGANIZATION VARCHAR(100), + FOREIGN KEY(APPLICATION_UUID) REFERENCES APPLICATION(UUID) ON UPDATE CASCADE ON DELETE CASCADE, + PRIMARY KEY(APPLICATION_UUID,SECURITY_SCHEME,KEY_TYPE,ENVIRONMENT) + ); + + CREATE TABLE IF NOT EXISTS APPLICATION_ATTRIBUTES ( + APPLICATION_UUID VARCHAR(256) NOT NULL, + NAME VARCHAR(255) NOT NULL, + APP_ATTRIBUTE VARCHAR(1024) NOT NULL, + FOREIGN KEY (APPLICATION_UUID) REFERENCES APPLICATION (UUID) ON DELETE CASCADE ON UPDATE CASCADE, + PRIMARY KEY (APPLICATION_UUID,NAME) + ); + + -- CREATE INDEX IF NOT EXISTS IDX_AAKM_CK on APPLICATION_KEY_MAPPING (APPLICATION_IDENTIFIER); + + commit; + {{ end }} + {{ end }}