ucampostgresvro 0.1.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ DEFAULT_TABLES = {
2
+ "user": "vrauser",
3
+ "deploymentid": "deploymentid",
4
+ "proj": "projects",
5
+ "grant": "grants",
6
+ "costing": "costing",
7
+ }
@@ -0,0 +1,197 @@
1
+ import logging
2
+ from typing import Callable, Dict
3
+
4
+ from ucampostgresvro.DBA import DB
5
+ from ucampostgresvro.exceptions import DbException
6
+
7
+ LOG = logging.getLogger(__name__)
8
+
9
+
10
+ def create_table(tablename: str, db_params: Dict[str, str], design: str) -> bool:
11
+ """Creation of table with provided design
12
+
13
+ Args:
14
+ tablename (str): Name of the table to be created.
15
+ db_params (Dict[str, str]): provide parameters for DB connection
16
+ design (str): design to be created.
17
+
18
+ Raises:
19
+ DbException: Exception for the provided inputs.
20
+
21
+ Returns:
22
+ bool: True for the success and False for the failure.
23
+ """
24
+ db = DB(db_params)
25
+ conn = db.db_connection()
26
+ cursor = db.db_cursor()
27
+ with conn:
28
+ try:
29
+ cursor.execute(design)
30
+ LOG.info(f"Creation of the table '{tablename}' has been successful.")
31
+ return True
32
+ except Exception as e:
33
+ LOG.error(f"Error: Creation of table '{tablename}' failed: \n {e}")
34
+ raise DbException(f"Error: Creation of table '{tablename}' failed: \n {e}")
35
+
36
+
37
+ def drop_table(tablename: str, db_params: Dict[str, str]) -> bool:
38
+ """Drop the table.
39
+
40
+ Args:
41
+ tablename (str): Name of the table to be created.
42
+ db_params (Dict[str, str]): provide parameters for DB connection.
43
+
44
+ Raises:
45
+ DbException: Exception for the provided inputs.
46
+
47
+ Returns:
48
+ bool: True for the success and False for the failure.
49
+ """
50
+ db = DB(db_params)
51
+ conn = db.db_connection()
52
+ cursor = db.db_cursor()
53
+ with conn:
54
+ try:
55
+ cursor.execute(f'DROP TABLE "{tablename}";')
56
+ LOG.info(f"Drop of the table '{tablename}' has been successful.")
57
+ return True
58
+ except Exception as e:
59
+ LOG.error(f"Error: Drop of table {tablename} failed: \n {e}")
60
+ raise DbException(f"Error: Drop of table {tablename} failed: \n {e}")
61
+
62
+
63
+ def create_user_table(
64
+ tablename: str, db_params: Dict[str, str]
65
+ ) -> Callable[[str, Dict[str, str], str], bool]:
66
+ """create the user table.
67
+
68
+ Args:
69
+ tablename (str): Name of the table to be created.
70
+ db_params (Dict[str, str]): provide parameters for DB connection.
71
+
72
+ Returns:
73
+ Callable[[str, Dict[str, str], str], bool]: Invoke the create table function.
74
+ """
75
+ design = f"CREATE TABLE {tablename} (\
76
+ id SERIAL PRIMARY KEY, \
77
+ crsid VARCHAR(255) UNIQUE, \
78
+ name VARCHAR(255)\
79
+ );"
80
+ return create_table(tablename, db_params, design)
81
+
82
+
83
+ def create_deployment_table(tablename: str, db_params: Dict[str, str]) -> Callable:
84
+ """create the deployment table.
85
+
86
+ Args:
87
+ tablename (str): Name of the table to be created.
88
+ db_params (Dict[str, str]): provide parameters for DB connection.
89
+
90
+ Returns:
91
+ Callable[[str, Dict[str, str], str], bool]: Invoke the create table function.
92
+ """
93
+ design = f"CREATE TABLE {tablename} (id SERIAL PRIMARY KEY, deploymentId VARCHAR(50) UNIQUE);"
94
+ return create_table(tablename, db_params, design)
95
+
96
+
97
+ def create_project_table(
98
+ proj_tablename: str, user_tablename: str, db_params: Dict[str, str]
99
+ ) -> Callable[[str, Dict[str, str], str], bool]:
100
+ """create the project table.
101
+
102
+ Args:
103
+ proj_tablename (str): Name of the project table to be created.
104
+ user_tablename (str): Name of the user table to be referred.
105
+ db_params (Dict[str, str]): provide parameters for DB connection.
106
+
107
+ Returns:
108
+ Callable[[str, Dict[str, str], str], bool]: Invoke the create table function.
109
+ """
110
+ design = f"CREATE TABLE {proj_tablename} (\
111
+ id SERIAL PRIMARY KEY, \
112
+ project_number VARCHAR(255), \
113
+ paid_by INTEGER REFERENCES {user_tablename}(id), \
114
+ amount FLOAT NOT NULL \
115
+ );"
116
+ return create_table(proj_tablename, db_params, design)
117
+
118
+
119
+ def create_grant_table(
120
+ grant_tablename: str, user_tablename: str, db_params: Dict[str, str]
121
+ ) -> Callable[[str, Dict[str, str], str], bool]:
122
+ """create the grant table.
123
+
124
+ Args:
125
+ grant_tablename (str): Name of the grant table to be created.
126
+ user_tablename (str): Name of the user table to be referred.
127
+ db_params (Dict[str, str]): provide parameters for DB connection.
128
+
129
+ Returns:
130
+ Callable[[str, Dict[str, str], str], bool]: Invoke the create table function.
131
+ """
132
+ design = f"CREATE TABLE {grant_tablename} (\
133
+ id SERIAL PRIMARY KEY,\
134
+ grant_number VARCHAR(255),\
135
+ paid_by INTEGER REFERENCES {user_tablename}(id),\
136
+ amount FLOAT NOT NULL\
137
+ );"
138
+ return create_table(grant_tablename, db_params, design)
139
+
140
+
141
+ def create_costing_table(
142
+ costing_tablename: str,
143
+ deploy_tablename: str,
144
+ proj_tablename: str,
145
+ grant_tablename: str,
146
+ db_params: Dict[str, str],
147
+ ) -> Callable[[str, Dict[str, str], str], bool]:
148
+ """create the grant table.
149
+
150
+ Args:
151
+ costing_tablename (str): Name of the costing table to be created.
152
+ deploy_tablename (str): Name of the deploy table to be referred.
153
+ proj_tablename (str): Name of the project table to be referred.
154
+ grant_tablename (str): Name of the grant table to be referred.
155
+ db_params (Dict[str, str]): provide parameters for DB connection.
156
+
157
+ Returns:
158
+ Callable[[str, Dict[str, str], str], bool]: Invoke the create table function.
159
+ """
160
+ design = f"CREATE TABLE {costing_tablename} (\
161
+ id SERIAL PRIMARY KEY,\
162
+ deployment_id INTEGER REFERENCES {deploy_tablename}(id),\
163
+ type VARCHAR(100) CHECK (type IN ('Resource Expansion', 'Duration Expansion', 'Initial Resource')),\
164
+ project_id INTEGER REFERENCES {proj_tablename}(id),\
165
+ grant_id INTEGER REFERENCES {grant_tablename}(id)\
166
+ );"
167
+ return create_table(costing_tablename, db_params, design)
168
+
169
+
170
+ def check_table_exists(table_name: str, db_params: Dict[str, str]) -> bool:
171
+ """Check the status of the table.
172
+
173
+ Args:
174
+ table_name (str): Name of the table to be checked.
175
+ db_params (Dict[str, str]): provide parameters for DB connection.
176
+
177
+ Raises:
178
+ DbException: Exception for the provided inputs.
179
+
180
+ Returns:
181
+ bool: True for the success of table search and False for the failing in table search.
182
+ """
183
+ db = DB(db_params)
184
+ conn = db.db_connection()
185
+ cursor = db.db_cursor()
186
+ with conn:
187
+ try:
188
+ cursor.execute(
189
+ f"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '{table_name}' \
190
+ AND table_schema = 'public')"
191
+ )
192
+ exists = cursor.fetchone()[0]
193
+ LOG.info(f"'{table_name}' status : {exists}")
194
+ return exists
195
+ except Exception as e:
196
+ LOG.error(f"Error: checking of table {table_name} failed: \n {e}")
197
+ raise DbException(f"Error: checking of table '{table_name}' failed: \n {e}")
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Information Services / Infrastructure
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,232 @@
1
+ Metadata-Version: 2.3
2
+ Name: ucampostgresvro
3
+ Version: 0.1.0
4
+ Summary:
5
+ Author: Ishan Mahajan
6
+ Author-email: mahanishanmahajan@gmail.com
7
+ Requires-Python: >=3.10
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Requires-Dist: psycopg2-binary (>=2.9.10,<3.0.0)
14
+ Description-Content-Type: text/markdown
15
+
16
+ # Script for VRA database for Costing
17
+
18
+ # Developing
19
+ 1. [Install docker-compose](https://docs.docker.com/compose/install/).
20
+ 2. Docker will run the postgres on port 5455 so, ensure the system has the port available
21
+
22
+ ```
23
+ # run pytest testing
24
+ ./developer.sh pytest start
25
+
26
+ # run flake8 testing
27
+ ./developer.sh flake8 start
28
+
29
+ # delete the testing environment
30
+ ./developer.sh pytest stop
31
+
32
+ # delete the flake8 environment
33
+ ./developer.sh flake8 stop
34
+ ```
35
+
36
+
37
+ ## Package usage
38
+
39
+ - To setup database
40
+ ```
41
+ from ucampostgresvro import pre_setupconfig
42
+ from ucampostgresvro.DB import DB
43
+ db_params = {
44
+ "dbname": "vrapricing",
45
+ "user": "postgres",
46
+ "password": <1Password: vrapricingpostgres>,
47
+ "host": "infra-db.srv.uis.cam.ac.uk",
48
+ "port": "5432",
49
+ "sslmode": "require",
50
+ "sslrootcert": "./ca.crt", # path to your client certificate
51
+ }
52
+ db = DB(db_params)
53
+ pre_setupconfig(db_params)
54
+ ```
55
+ - To perform CRUD operation
56
+ ```
57
+ from ucampostgresvro.DB import DB
58
+ db_params = {
59
+ "dbname": "vrapricing",
60
+ "user": "postgres",
61
+ "password": <1Password: vrapricingpostgres>,
62
+ "host": "infra-db.srv.uis.cam.ac.uk",
63
+ "port": "5432",
64
+ "sslmode": "require",
65
+ "sslrootcert": "./ca.crt", # path to your client certificate
66
+ }
67
+ db = DB(db_params)
68
+
69
+ # CRUD on user DB./
70
+
71
+ # create user
72
+ db.insert_vrauser("ll220", "Ling-Yan Lau")
73
+
74
+ # read user
75
+ print(db.get_vrauser())
76
+
77
+ # update user
78
+ db.update_vrauser("ll220", "bda20", 'Ben Argyle')
79
+
80
+ # delete user
81
+ db.remove_vrauser('bda20')
82
+
83
+ # create vra deploymentid
84
+ db.insert_deployment_id("1231ee112ad11212")
85
+
86
+ # read vra deployment id
87
+ print(db.get_deployment_id("1231a"))
88
+
89
+ # update vra deployment id
90
+ db.update_deployment_id("1231ee112ad1", "1231a")
91
+
92
+ # delete vra deployment id
93
+ db.remove_deployment_id('1231a')
94
+
95
+ # create project
96
+ db.insert_project("0001",1,100.0)
97
+
98
+ # read project
99
+ print(db.get_project())
100
+
101
+ # update project
102
+ db.update_project("0001", "0002", 4, 200)
103
+
104
+ # delete project
105
+ # db.remove_project("0002")
106
+
107
+ # create grant
108
+ db.insert_grant("0001",1,100.0)
109
+
110
+ # read grant
111
+ print(db.get_grant())
112
+
113
+ # update grant
114
+ db.update_grant("0001", "0002", 4, 200)
115
+
116
+ # delete grant
117
+ db.remove_grant("0002")
118
+
119
+ # create costing
120
+ db.insert_costing(2, "Initial Resource", project_id=4, grant_id=None)
121
+
122
+ # read costing
123
+ print(db.get_costing())
124
+
125
+ # update costing
126
+ db.update_costing(2, "Duration Expansion", old_grant_id=4, old_project_id=None, grant_id=4, project_id=None)
127
+
128
+ # delete costing
129
+ db.remove_costing(2, "Duration Expansion", 4, None)
130
+
131
+ # to close db connection
132
+ db.closedb()
133
+ ```
134
+
135
+ ---
136
+ ### Design
137
+
138
+ ![DB Design](./db.jpg "DB design")
139
+
140
+ ## - VRAUSER table
141
+ ```
142
+ vrapricing=# \d vrauser;
143
+ Table "public.vrauser"
144
+ Column | Type | Collation | Nullable | Default
145
+ --------+------------------------+-----------+----------+-------------------------------------
146
+ id | integer | | not null | nextval('vrauser_id_seq'::regclass)
147
+ crsid | character varying(255) | | |
148
+ name | character varying(255) | | |
149
+ Indexes:
150
+ "vrauser_pkey" PRIMARY KEY, btree (id)
151
+ "vrauser_crsid_key" UNIQUE CONSTRAINT, btree (crsid)
152
+ Referenced by:
153
+ TABLE "purchaseorder" CONSTRAINT "purchaseorder_paid_by_fkey" FOREIGN KEY (paid_by) REFERENCES vrauser(id)
154
+ TABLE "grant" CONSTRAINT "grant_paid_by_fkey" FOREIGN KEY (paid_by) REFERENCES vrauser(id)
155
+
156
+ ```
157
+
158
+ ## - VRA Deployment ID tabel
159
+ ```
160
+ vrapricing=# \d deploymentid;
161
+ Table "public.deploymentid"
162
+ Column | Type | Collation | Nullable | Default
163
+ --------------+-----------------------+-----------+----------+------------------------------------------
164
+ id | integer | | not null | nextval('deploymentid_id_seq'::regclass)
165
+ deploymentid | character varying(50) | | |
166
+ Indexes:
167
+ "deploymentid_pkey" PRIMARY KEY, btree (id)
168
+ "deploymentid_deploymentid_key" UNIQUE CONSTRAINT, btree (deploymentid)
169
+ Referenced by:
170
+ TABLE "costing" CONSTRAINT "costing_deployment_id_fkey" FOREIGN KEY (deployment_id) REFERENCES deploymentid(id)
171
+
172
+ ```
173
+
174
+ ## - project table
175
+ ```
176
+ vrapricing=# \d projects;
177
+ Table "public.projects"
178
+ Column | Type | Collation | Nullable | Default
179
+ ----------------+------------------------+-----------+----------+--------------------------------------
180
+ id | integer | | not null | nextval('projects_id_seq'::regclass)
181
+ project_number | character varying(255) | | |
182
+ paid_by | integer | | |
183
+ amount | double precision | | not null |
184
+ Indexes:
185
+ "projects_pkey" PRIMARY KEY, btree (id)
186
+ Foreign-key constraints:
187
+ "projects_paid_by_fkey" FOREIGN KEY (paid_by) REFERENCES vrauser(id)
188
+ Referenced by:
189
+ TABLE "costing" CONSTRAINT "costing_project_id_fkey" FOREIGN KEY (project_id) REFERENCES projects(id)
190
+
191
+ ```
192
+
193
+ ## - grants table
194
+ ```
195
+ vrapricing=# \d grant;
196
+ Table "public.grant"
197
+ Column | Type | Collation | Nullable | Default
198
+ ----------------+------------------------+-----------+----------+--------------------------------------
199
+ id | integer | | not null | nextval('grant_id_seq'::regclass)
200
+ voucher_number | character varying(255) | | |
201
+ paid_by | integer | | |
202
+ amount | double precision | | not null |
203
+ Indexes:
204
+ "grant_pkey" PRIMARY KEY, btree (id)
205
+ Foreign-key constraints:
206
+ "grant_paid_by_fkey" FOREIGN KEY (paid_by) REFERENCES vrauser(id)
207
+ Referenced by:
208
+ TABLE "costing" CONSTRAINT "costing_voucher_id_fkey" FOREIGN KEY (voucher_id) REFERENCES grant(id)
209
+ ```
210
+
211
+ ## - Costing table
212
+ ```
213
+ vrapricing=# \d costing;
214
+ Table "public.costing"
215
+ Column | Type | Collation | Nullable | Default
216
+ ---------------+------------------------+-----------+----------+-------------------------------------
217
+ id | integer | | not null | nextval('costing_id_seq'::regclass)
218
+ deployment_id | integer | | |
219
+ type | character varying(100) | | |
220
+ po_number_id | integer | | |
221
+ voucher_id | integer | | |
222
+ Indexes:
223
+ "costing_pkey" PRIMARY KEY, btree (id)
224
+ Check constraints:
225
+ "costing_type_check" CHECK (type::text = ANY (ARRAY['Resource Expansion'::character varying, 'Duration Expansion'::character varying, 'Initial Resource'::character varying]::text[]))
226
+ Foreign-key constraints:
227
+ "costing_deployment_id_fkey" FOREIGN KEY (deployment_id) REFERENCES deploymentid(id)
228
+ "costing_po_number_id_fkey" FOREIGN KEY (po_number_id) REFERENCES purchaseorder(id)
229
+ "costing_voucher_id_fkey" FOREIGN KEY (voucher_id) REFERENCES grant(id)
230
+
231
+ ```
232
+
@@ -0,0 +1,16 @@
1
+ ucampostgresvro/DBA.py,sha256=LGj2JpP2B-6z7CFV-zloLNmW8dPcIyuItVRi5x0KQT0,30876
2
+ ucampostgresvro/__init__.py,sha256=CpXi3jGlx23RvRyU7iytOMZrnspdWw4yofS8lpP1AJU,18
3
+ ucampostgresvro/__main__.py,sha256=bTpOR1L6o5nZhM391sdZA6G3861jdwHQRdgAuXvL_dQ,4395
4
+ ucampostgresvro/ca.crt,sha256=pVDr3AsvwCqJ36JTifsAzIiS2ve_fWC1CBfVI0cnVmY,1135
5
+ ucampostgresvro/exceptions.py,sha256=XVi8Sk_gg3VE4ryTtiYOnGbmCPIsfFt2AE5sOoqQDuI,39
6
+ ucampostgresvro/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ ucampostgresvro/tests/conftest.py,sha256=t89U_SOcWf2q0C_m4Vn89LSLwxnQC-_rUAvt6yNeOWA,1803
8
+ ucampostgresvro/tests/dbconnect.py,sha256=T2rVoUmG6mHwS-i4h8R4O4GuEhuucCd3zyrFUMMhkE4,240
9
+ ucampostgresvro/tests/test_DB.py,sha256=SLi5C0NO0dIeIQOfywkAPWKznfANopVEnHOxCD-EZpU,12134
10
+ ucampostgresvro/tests/utils.py,sha256=lpcvf6HPOOtxAwSwvoAUhil1zQ6Qfr0ZILa0qDVdES0,3722
11
+ ucampostgresvro/tools.py,sha256=Y8MUK1lIG10A-724yrhve8LfjQUmFwpqi19UIO5DeuI,153
12
+ ucampostgresvro/utils.py,sha256=d49ecdYfFXHK0H4Huc7x2C9sEc_R_3MG3lyNgBvWSyg,6907
13
+ ucampostgresvro-0.1.0.dist-info/LICENSE,sha256=CVTj8C-BHEJjzYlYtHnfykxKkfmk-ImXOs5rcMgXebY,1094
14
+ ucampostgresvro-0.1.0.dist-info/METADATA,sha256=XHjKJPZjynMcdLtamiHaOvc_qPtZFNgUTlhBtUpKnJI,7592
15
+ ucampostgresvro-0.1.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
16
+ ucampostgresvro-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.0.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any