Skip to content

Commit

Permalink
Add ratelimit tier to database
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayangi committed Oct 15, 2024
1 parent 129a86a commit 86af7bc
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 12 deletions.
8 changes: 4 additions & 4 deletions common-controller/internal/database/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions common-controller/internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions common-controller/internal/database/db_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions common-controller/internal/database/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;"

Expand Down
1 change: 1 addition & 0 deletions common-controller/resources/db/postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" }}"
Expand Down
62 changes: 61 additions & 1 deletion helm-charts/templates/postgres/initdb-conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

0 comments on commit 86af7bc

Please sign in to comment.