Compare commits

...

314 Commits
logs ... master

Author SHA1 Message Date
mr
754925c32c Merge branch 'master' of https://cloud.o-forge.io/core/oc-lib 2024-09-16 16:59:12 +02:00
mr
4956166f76 failed models 2024-09-16 16:58:38 +02:00
ycc
4f0ab6a376 Add GetConfig to entrypoint 2024-09-04 15:54:49 +02:00
ycc
235c8d5ccb doc fix 2024-09-04 14:41:27 +02:00
ycc
ad96b50464 conf singleton issue 2024-09-04 14:21:01 +02:00
ycc
a0cbf9ac6f conf loader bugfix 2024-09-04 14:11:08 +02:00
ycc
21ca50221f fix moving recover 2024-09-04 12:11:26 +02:00
ycc
26cfd2a38f fix accessor init location 2024-09-04 12:09:26 +02:00
ycc
bb36ac0fb4 Refactor and doc 2024-09-04 10:53:12 +02:00
ycc
fba1608edb getconfig fix 2024-09-02 15:21:16 +02:00
mr
af18dba556 adding READ ME main processes 2024-08-30 15:14:45 +02:00
mr
8180fe5e99 A question refers to the comment ! And if not Ooopsy 2024-08-30 14:50:48 +02:00
mr
db78c70dc3 add shared logic on peer 2024-08-30 09:14:03 +02:00
mr
2e8246fb2f correct workflow problem 2024-08-29 11:03:26 +02:00
mr
a53e3c987d correct workflow problem 2024-08-28 17:14:01 +02:00
mr
2f44ad0eff correct workflow problem 2024-08-28 16:43:53 +02:00
mr
14d6a5f11c correct workflow problem 2024-08-28 15:52:27 +02:00
ycc
cf26fd970c Merge branch 'master' of https://cloud.o-forge.io/core/oc-lib 2024-08-28 15:26:01 +02:00
ycc
ed381658b5 common config loader added to tools 2024-08-28 15:26:00 +02:00
pb
4a9465f729 typo 2024-08-28 15:12:42 +02:00
mr
4a3707efcf correct workflow problem 2024-08-28 14:44:56 +02:00
mr
ede91cde45 correct workflow problem 2024-08-28 13:41:59 +02:00
mr
88d2819394 correct workflow problem 2024-08-28 13:22:43 +02:00
mr
3c5c065b8d Bug on Format date 2024-08-27 14:17:39 +02:00
mr
4a932c8e23 Bug on Format date 2024-08-27 14:10:30 +02:00
mr
45d53fc9a9 Bug on Format date 2024-08-27 13:22:06 +02:00
mr
49cc7997f3 Bug on Format date 2024-08-27 13:12:09 +02:00
mr
2b23a65647 Bug on Format date 2024-08-27 13:11:34 +02:00
mr
772287225f Bug on Format date 2024-08-27 12:40:28 +02:00
mr
091cbb9e7f simplify call to peer 2024-08-27 10:07:13 +02:00
mr
0652215c18 simplify call to peer 2024-08-27 09:59:04 +02:00
mr
463144a062 simplify call to peer 2024-08-26 17:59:58 +02:00
mr
767c14044f simplify call to peer 2024-08-26 16:52:24 +02:00
mr
143fe1446e simplify call to peer 2024-08-26 16:39:16 +02:00
mr
00625eabd6 simplify call to peer 2024-08-26 16:10:22 +02:00
mr
63b02199f6 simplify call to peer 2024-08-26 15:28:51 +02:00
mr
050e6022cb simplify call to peer 2024-08-26 14:58:54 +02:00
mr
aff9304b1a simplify call to peer 2024-08-26 12:34:23 +02:00
mr
73dc43b329 simplify call to peer 2024-08-26 11:47:30 +02:00
mr
d43938236e simplify call to peer 2024-08-26 11:33:47 +02:00
mr
d0e1474f8f simplify call to peer 2024-08-26 10:59:16 +02:00
mr
7c44365f52 simplify call to peer 2024-08-26 10:26:33 +02:00
mr
144e7f8506 simplify call to peer 2024-08-26 09:42:07 +02:00
mr
8e80fa88be simplify call to peer 2024-08-23 23:00:57 +02:00
mr
762cc19c14 simplify call to peer 2024-08-23 22:17:03 +02:00
mr
dd3570fa37 simplify call to peer 2024-08-23 18:18:29 +02:00
mr
fd730741a4 simplify call to peer 2024-08-23 17:35:29 +02:00
mr
1986143dd0 simplify call to peer 2024-08-23 15:43:12 +02:00
mr
5b3779e4cb simplify call to peer 2024-08-23 11:13:01 +02:00
mr
98fa1f6ca8 simplify call to peer 2024-08-23 10:25:53 +02:00
mr
745fc7779e simplify call to peer 2024-08-23 10:01:37 +02:00
mr
c88b29a0ca simplify call to peer 2024-08-23 09:57:52 +02:00
mr
152bdd8061 simplify call to peer 2024-08-23 09:57:11 +02:00
mr
9f72f4b2f8 simplify call to peer 2024-08-23 09:55:30 +02:00
mr
e8acf8a127 simplify call to peer 2024-08-23 09:53:37 +02:00
mr
29c2ab0e4c send 2024-08-23 09:11:41 +02:00
mr
a2f1bef34a No discovery needed 2024-08-23 09:04:50 +02:00
mr
2404ee3fce No discovery needed 2024-08-23 09:03:51 +02:00
mr
e4ee33dcbe No discovery needed 2024-08-23 09:01:28 +02:00
mr
2ad4c0bb91 Discovery Nats automate 2024-08-23 08:23:24 +02:00
mr
37b7ed8d83 Discovery Nats automate 2024-08-22 17:09:31 +02:00
mr
fd2ad33f61 Discovery Nats automate 2024-08-22 16:54:13 +02:00
mr
65f3318f73 Discovery Nats automate 2024-08-22 16:53:31 +02:00
mr
767f10323d Discovery Nats automate 2024-08-22 16:47:15 +02:00
mr
3232cce1b4 Discovery Nats automate 2024-08-22 16:28:21 +02:00
mr
d9b26e3fce Discovery Nats automate 2024-08-22 16:13:07 +02:00
mr
d89826db44 Discovery Nats automate 2024-08-22 15:38:27 +02:00
mr
8c9a983216 Discovery Nats automate 2024-08-22 15:27:05 +02:00
mr
a42b6198cb Discovery Nats automate 2024-08-22 15:18:59 +02:00
mr
fb555be86c Discovery Nats automate 2024-08-22 15:17:14 +02:00
mr
6c28684859 Discovery Nats automate 2024-08-22 15:04:01 +02:00
mr
c8a1377851 Discovery Nats automate 2024-08-22 14:56:32 +02:00
mr
9c0107e87c Discovery Nats automate 2024-08-22 14:29:49 +02:00
mr
e69e13d8da Discovery Nats automate 2024-08-22 13:22:43 +02:00
mr
b77d003238 Discovery Nats automate 2024-08-22 13:21:37 +02:00
mr
c1371bc35c Discovery Nats automate 2024-08-22 13:19:08 +02:00
mr
0bfe87793b Discovery Nats automate 2024-08-22 13:11:21 +02:00
mr
ce34345914 Bug on Format date 2024-08-22 12:36:56 +02:00
mr
1d3620845d Bug on Format date 2024-08-22 12:29:38 +02:00
mr
d766e3a7c3 Bug on Format date 2024-08-22 11:59:23 +02:00
mr
bfa176a7c5 casual http debug 2024-08-22 10:39:03 +02:00
mr
4abf59a10d casual http debug 2024-08-22 10:19:14 +02:00
mr
fb80e05d6a Bug on Format date 2024-08-22 08:50:24 +02:00
mr
11c55d422f Bug on Format date 2024-08-22 08:45:26 +02:00
mr
b8b62dc2c8 Bug on Format date 2024-08-21 17:19:22 +02:00
mr
99bf1106f3 Bug on Format date 2024-08-21 17:06:44 +02:00
mr
498ec3e9d8 Bug on Format date 2024-08-21 16:38:29 +02:00
mr
cf6143887e Bug on Format date 2024-08-21 16:11:51 +02:00
mr
39030a0a80 Booking trace API 2024-08-21 16:07:57 +02:00
mr
950bbf1d7b Booking trace API 2024-08-21 16:03:56 +02:00
mr
6ef8cde225 Booking trace API 2024-08-21 15:46:16 +02:00
mr
9a6ab5c29c Booking trace API 2024-08-21 15:32:10 +02:00
mr
4db037afd0 Booking trace API 2024-08-21 15:22:13 +02:00
mr
c14e067449 Booking trace API 2024-08-21 14:12:28 +02:00
mr
ddb4f76af7 Booking trace API 2024-08-21 14:07:22 +02:00
mr
f64563c9ff Check Remote API 2024-08-21 11:30:44 +02:00
mr
01bbb9be2c Check Remote API 2024-08-21 11:28:13 +02:00
mr
4693b96130 State of an API with OClib :D 2024-08-21 11:23:07 +02:00
mr
c5f45e03f3 State of an API with OClib :D 2024-08-21 10:58:24 +02:00
mr
b7ecea25eb NATS is an OCLIB tool 2024-08-21 10:21:17 +02:00
mr
649a1098c3 NATS is an OCLIB tool 2024-08-21 10:04:09 +02:00
mr
e31815f576 default http behavior on is my self 2024-08-21 08:54:29 +02:00
mr
c6ea2195ed correct is DC link 2024-08-20 16:22:06 +02:00
mr
d13249d87d correct is DC link 2024-08-20 15:31:25 +02:00
mr
bfbcd58d1b correct is DC link 2024-08-20 15:27:12 +02:00
mr
b6d9b6910f correct is DC link 2024-08-20 14:59:06 +02:00
mr
f599131708 id on peer 2024-08-20 13:31:11 +02:00
mr
fbf4040f63 id on peer 2024-08-20 12:59:50 +02:00
mr
392cf1cf1a id on peer 2024-08-20 12:59:09 +02:00
mr
8751d36b34 id on peer 2024-08-20 12:20:30 +02:00
mr
9e795d795c id on peer 2024-08-20 11:10:28 +02:00
mr
d848585b7c id on peer 2024-08-20 09:28:02 +02:00
mr
bfa86de5ad simplify model 2024-08-14 08:20:44 +02:00
mr
2d9b4587ac peers logic 2024-08-13 14:33:26 +02:00
mr
4911e32ec2 mode adding to workflow 2024-08-13 10:42:12 +02:00
mr
60fee8abe7 mode adding to workflow 2024-08-13 10:36:39 +02:00
mr
3962b7c7aa mode adding to workflow 2024-08-13 10:29:55 +02:00
mr
e0eb8cd4ad mode adding to workflow 2024-08-13 09:56:09 +02:00
mr
e71bd3544f shared peer conn 2024-08-13 09:49:42 +02:00
mr
6fe862a9b5 implement remote call for remote action 2024-08-12 16:54:58 +02:00
mr
92516e20ab implement remote call for remote action 2024-08-12 16:44:01 +02:00
mr
e51377eeb3 implement remote call for remote action 2024-08-12 16:28:02 +02:00
mr
f4603e4171 implement remote call for remote action 2024-08-12 16:23:52 +02:00
mr
5986d73367 implement remote call for remote action 2024-08-12 16:21:38 +02:00
mr
bc8c508e04 implement remote call for remote action 2024-08-12 16:17:47 +02:00
mr
2ac24779cd implement remote call for remote action 2024-08-12 16:11:25 +02:00
mr
4575f9ad3f booking start intelligency 2024-08-12 14:31:33 +02:00
mr
93b10de61d booking start intelligency 2024-08-12 14:18:13 +02:00
mr
eea7f25379 add shared workspace logic 2024-08-12 12:29:07 +02:00
mr
62e245753b add shared workspace logic 2024-08-12 12:03:58 +02:00
mr
6e3069068c check nil on schedule 2024-08-12 09:55:55 +02:00
mr
3b1bd41638 check is true 2024-08-12 09:30:07 +02:00
mr
f8cf8156bd clarify error on execution gen 2024-08-09 11:56:41 +02:00
mr
2ae15c720a book in workflow 2024-08-08 17:00:44 +02:00
mr
8dc009564a book in workflow 2024-08-08 16:26:19 +02:00
mr
4688d9c681 book in workflow 2024-08-08 15:56:48 +02:00
mr
5ac4492369 fixing cron dependancies 2024-08-08 11:39:57 +02:00
mr
14f585c657 fixing cron dependancies 2024-08-08 11:33:32 +02:00
mr
1cfd684e89 fixing cron dependancies 2024-08-08 11:26:10 +02:00
mr
580b492fd3 fixing cron dependancies 2024-08-08 11:14:24 +02:00
mr
f45ad91687 check booking 2024-08-08 09:54:05 +02:00
mr
47cd936131 check booking 2024-08-08 09:47:00 +02:00
mr
e0eefd9c30 Merge branch 'master' of https://cloud.o-forge.io/core/oc-lib 2024-08-08 09:45:58 +02:00
mr
9c903907cd check booking 2024-08-08 09:45:33 +02:00
ycc
9040d141c8 model update - shared workspaces 2024-08-07 18:50:25 +02:00
mr
0ec80473cc argostatus to state 2024-08-07 15:41:03 +02:00
pb
6df71bde1d fix ToWorkflowExecution 2024-08-07 15:16:22 +02:00
mr
fe78927e73 execution delete only 2024-08-07 13:18:44 +02:00
pb
af9e7d8edd correct execution 2024-08-07 12:27:29 +02:00
pb
b7c4a78158 bug fix 2024-08-07 11:08:18 +02:00
pb
1f83148aca comments 2024-08-06 11:10:56 +02:00
mr
29fcd0e34a Merge branch 'master' of https://cloud.o-forge.io/core/oc-lib 2024-08-06 08:42:53 +02:00
mr
d2484e84f0 Add a Name to Workflow Execution 2024-08-06 08:40:05 +02:00
pb
17f62b6495 add labels to lokiwritter after initialisation 2024-08-05 15:47:53 +02:00
mr
dd5f8f5b2d kick out print 2024-08-05 10:10:58 +02:00
mr
1e8cb98098 kick out print 2024-08-05 08:50:20 +02:00
mr
e390dfa8a9 exec access 2024-08-05 08:28:37 +02:00
mr
9abc3a0d99 exec access 2024-08-05 08:25:05 +02:00
mr
1f09e48abf exec access 2024-08-05 08:15:43 +02:00
mr
0b5df45a02 exec access 2024-08-02 17:38:51 +02:00
mr
e320efd70e exec access 2024-08-02 17:00:29 +02:00
mr
caf0c1afcb mongo and or 2024-08-02 16:45:45 +02:00
mr
51541010e2 mongo and or 2024-08-02 16:39:10 +02:00
mr
89f14d99b8 mongo and or 2024-08-02 16:09:17 +02:00
mr
2525cc7783 mongo and or 2024-08-02 16:02:23 +02:00
mr
09b67b26b0 mongo and or 2024-08-02 15:45:57 +02:00
mr
24a277333d mongo and or 2024-08-02 15:36:52 +02:00
mr
efa73d8a45 mongo stability debug + zoom 2024-08-02 15:06:40 +02:00
mr
fa52c71e93 logs update 2024-08-02 14:07:43 +02:00
mr
e23756eba6 logs update 2024-08-02 13:13:04 +02:00
mr
e9dd17ae87 dbs and 2024-08-02 13:10:27 +02:00
mr
d0e2778246 nil except 2024-08-02 11:43:59 +02:00
mr
9da3a7c3e6 nil except 2024-08-02 11:17:04 +02:00
mr
ed4cb943a8 nil except 2024-08-02 11:04:18 +02:00
pb
b125c9be01 updated RAM attribute 2024-08-01 16:50:26 +02:00
pb
7d93c60e36 Added method item to resource 2024-08-01 16:50:16 +02:00
mr
c3e6f04dd8 nil except 2024-08-01 11:33:27 +02:00
mr
69d53f9d4d mongo procedure 2024-08-01 11:31:50 +02:00
mr
dbbda34117 pointer of pointer is kicked out 2024-08-01 11:19:14 +02:00
mr
b0f2cf46eb error 2024-08-01 11:11:41 +02:00
mr
0256266107 error 2024-08-01 11:09:39 +02:00
mr
b0f5c78e55 default bson 2024-08-01 10:03:28 +02:00
mr
27327019be logs 2024-08-01 10:02:40 +02:00
mr
1f8aefb7d8 logs 2024-08-01 09:55:59 +02:00
mr
745e175da4 COMPLEX SEARCH 2024-08-01 09:34:44 +02:00
mr
924a688a9d COMPLEX SEARCH 2024-08-01 09:13:10 +02:00
mr
ad455e0e3a nice shortcut on resource 2024-07-31 16:56:30 +02:00
mr
51f3a320b3 nullable itemresource 2024-07-31 16:21:19 +02:00
mr
c4448fd3af mongo unfatal 2024-07-31 16:06:47 +02:00
mr
78bef28a21 duplicate key 2024-07-31 14:30:52 +02:00
mr
63236362ca optionnal vars failed 2024-07-31 11:50:57 +02:00
mr
2795c924f7 optionnal vars failed 2024-07-31 11:47:47 +02:00
mr
1ff4cb08d1 activation workspace 2024-07-31 11:34:13 +02:00
mr
4f27aefc4b activation workspace 2024-07-31 11:29:21 +02:00
mr
e311553eb1 precise workspace flow 2024-07-31 11:23:59 +02:00
mr
274ce57f5b private accessor 2024-07-31 10:36:57 +02:00
mr
00fe19083b private accessor 2024-07-31 10:35:03 +02:00
mr
7ae1399a9a private accessor 2024-07-31 10:07:55 +02:00
mr
d6a2a416c1 debug automation workspace 2024-07-31 09:52:57 +02:00
mr
511e0c3f31 debug automation workspace 2024-07-31 08:57:20 +02:00
mr
19381d6438 debug automation workspace 2024-07-31 08:36:16 +02:00
mr
18d54cdb8f debug automation workspace 2024-07-30 16:30:44 +02:00
mr
b06193add2 rename arguments in args 2024-07-30 14:41:33 +02:00
mr
86c3e83eda missing collection resource model 2024-07-30 14:40:44 +02:00
mr
e784216584 Resource Model Apparel 2024-07-30 14:08:27 +02:00
mr
5405e91167 copy resource in workflow 2024-07-30 12:08:13 +02:00
mr
250fefd0d8 search is debug nested object included 2024-07-30 09:27:52 +02:00
mr
6ac855394b workin exucution scheduler 2024-07-30 09:20:33 +02:00
mr
4730d5b4d4 test on nested search 2024-07-30 08:59:31 +02:00
mr
5ab011844d repair accessor call 2024-07-30 08:52:14 +02:00
mr
0e96b5c01a logs on search 2024-07-30 08:39:28 +02:00
mr
50cecc40c4 dbs logs 2024-07-30 08:33:26 +02:00
mr
dc30412f2e debugging workflow_execution + search invest 2024-07-29 17:05:39 +02:00
mr
3e72510d53 search is not searchin 2024-07-29 16:23:52 +02:00
mr
beeac40a10 debug logs 2024-07-29 16:04:09 +02:00
mr
2f6fab2f7b missing context on search 2024-07-29 15:53:15 +02:00
mr
d77a403150 missing context on search 2024-07-29 15:22:14 +02:00
mr
8c3b92143e maximum log 2024-07-29 15:07:59 +02:00
mr
e2f722e17b set up print 2024-07-29 14:19:48 +02:00
mr
956af8f5f5 set up print 2024-07-29 13:36:25 +02:00
mr
c7f65914bb cast at root lib 2024-07-29 08:52:02 +02:00
mr
ff286583aa DATA misdirect 2024-07-26 16:47:11 +02:00
mr
fd64d423be models 2024-07-26 16:41:08 +02:00
mr
87b7cda0ce adjust model 2024-07-26 15:19:36 +02:00
mr
eb1417853a acces to string on enum 2024-07-26 14:15:55 +02:00
mr
8c97fca96c adding search func 2024-07-26 13:45:10 +02:00
mr
6c83e54d37 Get on Workspace auto load full data 2024-07-26 10:36:23 +02:00
mr
d5c5b454f4 not a proper type ref o nworkspace 2024-07-26 09:51:54 +02:00
mr
5ad4d523ab not a proper type ref o nworkspace 2024-07-26 08:58:59 +02:00
mr
1ca983db36 adding workspace to catalog 2024-07-26 08:27:09 +02:00
pb
2891dc8a68 adding push path to the url contacted by lokiwritter.Write 2024-07-25 12:35:14 +02:00
mr
6f6cb7c489 resource schedule init 2024-07-25 11:52:05 +02:00
mr
94b45387fb resource push 2024-07-25 11:46:33 +02:00
mr
094ae0a7f0 Adding Workspace Logic 2024-07-25 09:28:55 +02:00
mr
51e94e73e5 id in json 2024-07-24 17:08:30 +02:00
mr
357d79e68b Update Resource 2024-07-24 14:49:04 +02:00
mr
ac2111a365 Update loggon 2024-07-24 14:23:47 +02:00
mr
637b2c977d Serialize failures 2024-07-24 09:53:30 +02:00
mr
c5f4427f9f Update still not workin 2024-07-24 09:27:03 +02:00
mr
045f3471ec Execution missing to catalog 2024-07-24 08:58:40 +02:00
mr
bde70dec9b Debugging Put / GET ALL 2024-07-24 08:35:41 +02:00
mr
b775298bb0 LoadAll evolve 2024-07-23 18:06:54 +02:00
mr
aa7ce56174 rm env 2024-07-23 17:45:47 +02:00
mr
00f25b48c0 Execution 2024-07-23 16:14:46 +02:00
mr
806f5d0f20 datacenter 2024-07-23 14:57:16 +02:00
mr
8fb3e35142 no workflow 2024-07-23 14:38:47 +02:00
mr
0b410b8580 color value 2024-07-23 11:49:11 +02:00
mr
94cd62acfe Shallow Get All 2024-07-23 11:22:50 +02:00
mr
a687206a1b correction struct graph 2024-07-23 09:46:27 +02:00
mr
635b1d4ed4 color is an hex 2024-07-23 09:22:40 +02:00
mr
88c8feb7d8 missing bson model on graph 2024-07-23 09:15:42 +02:00
mr
febabe67e7 Not a proper WF model 2024-07-23 08:59:39 +02:00
mr
0c825b65b0 Update flatten object 2024-07-23 08:40:24 +02:00
mr
a6f2556050 log 2024-07-22 16:57:54 +02:00
mr
e44a6b65b7 omitempty 2024-07-22 16:39:26 +02:00
mr
ebdbd97ce6 update set 2024-07-22 16:12:38 +02:00
mr
4a90d3379c update fails 2024-07-22 15:18:59 +02:00
mr
7a49f6b957 t 2024-07-22 14:46:49 +02:00
mr
b1c3c056df another test 2024-07-22 14:06:53 +02:00
mr
9f17532646 insert uuid 2024-07-22 14:02:48 +02:00
mr
f7dfbe91dd another test flat in base 2024-07-22 13:26:59 +02:00
mr
b1293af229 test 2024-07-22 12:51:43 +02:00
mr
6696b0faac deserialize 2024-07-22 11:50:14 +02:00
mr
a5741d5831 omitempty is not null 2024-07-22 10:57:00 +02:00
mr
bbe4b3eee7 omitempty is not null 2024-07-22 10:31:05 +02:00
mr
7f294374e8 error adjust 2024-07-22 10:11:28 +02:00
mr
df556f7c7a apropriate db 2024-07-22 09:39:42 +02:00
mr
742bcd1acf config pb 2024-07-22 09:16:59 +02:00
mr
53811538c0 config pb 2024-07-22 09:03:57 +02:00
mr
070f2d063e deserialize test 2024-07-19 15:20:03 +02:00
mr
e89b5aae84 test copy 2024-07-19 13:41:10 +02:00
mr
3f9814e649 test * 2024-07-19 13:27:34 +02:00
mr
cdc077c59e test * 2024-07-19 13:15:51 +02:00
mr
5c44b46bcb test * 2024-07-19 12:57:25 +02:00
mr
d28662d70b add with http code 2024-07-19 11:27:58 +02:00
mr
650168f39c resource as resource named1 2024-07-19 11:03:56 +02:00
mr
20913f056b resource as resource named1 2024-07-19 11:00:15 +02:00
mr
2a9a784ec1 resource as resource named 2024-07-19 10:54:58 +02:00
mr
218714683b validator err 2024-07-19 09:43:18 +02:00
mr
694957a483 validator err test 2024-07-19 09:41:13 +02:00
mr
c494dba5ab validator not work 2024-07-19 09:36:46 +02:00
mr
e838474835 validator use test 2024-07-19 09:32:58 +02:00
mr
071a49cd81 validator2 2024-07-18 18:28:36 +02:00
mr
4f4e2949d6 validator 2024-07-18 18:14:12 +02:00
mr
cc4cc49617 main logger 2024-07-18 17:59:25 +02:00
mr
0a441cd3df main logger 2024-07-18 17:55:27 +02:00
mr
3e9eae007b apropriate db 2024-07-18 16:46:54 +02:00
mr
d273fdc57f config pb 2024-07-18 16:18:27 +02:00
mr
8eb88dcd82 config pb 2024-07-18 16:14:39 +02:00
mr
e3a7703bc5 config pb 2024-07-18 16:09:08 +02:00
mr
08100eb57f determine returnal 2024-07-18 15:35:30 +02:00
mr
6d104288b7 accessor 2024-07-18 15:15:01 +02:00
mr
33c284cd4c accessor 2024-07-18 15:04:12 +02:00
mr
e345204851 accessor 2024-07-18 15:02:39 +02:00
mr
4fab0cb153 access func 2024-07-18 14:39:54 +02:00
mr
7f1f85c00e accessor 2024-07-18 14:11:13 +02:00
mr
2aa9054148 missing conf 2024-07-18 13:58:49 +02:00
mr
c41eeb969a configuration 2024-07-18 13:55:38 +02:00
mr
9bc77b6699 config 2024-07-18 13:43:41 +02:00
mr
1999213f8b module name 2024-07-18 13:35:14 +02:00
7d5bdadbef rename 2024-07-18 13:16:53 +02:00
mr
1dfff4635e init 2024-07-18 12:05:32 +02:00
mr
85fc43368b lib conf 2024-07-18 11:56:54 +02:00
mr
29a75bced9 organize + graph 2024-07-18 11:51:12 +02:00
pb
2eb74da9d2 packaged every model 2024-07-17 18:02:30 +02:00
pb
2684703da5 organised in packages 2024-07-17 17:48:20 +02:00
pb
02b0a2a595 rewrote the mongo client handler 2024-07-17 17:20:25 +02:00
pb
90e9bcf378 improve testing 2024-07-17 17:19:42 +02:00
pb
4fd22dc131 tests for each model 2024-07-17 17:19:03 +02:00
pb
562720089f Accessor for each ressource to access DB 2024-07-17 17:18:27 +02:00
pb
3732187678 Improved models' structure 2024-07-17 17:17:37 +02:00
pb
f0eec45836 mongo 2024-07-16 11:05:11 +02:00
pb
034a81cedb First batch 2024-07-16 11:05:11 +02:00
pb
5ba33d3131 New libs 2024-07-16 11:05:11 +02:00
74 changed files with 5856 additions and 71 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
out/*

101
README.md
View File

@ -1,2 +1,103 @@
# oc-lib # oc-lib
oc-lib allows read/write/search operations into the main OpenCloud databases.
It also provides common initialization and configuration utilities for all OpenCloud components
## Usage example in a beego API
```go
const appname = "oc-mycomponent"
func main() {
// Init the oc-lib
oclib.Init(appname)
// Load the right config file
/* The configuration loader will load the configuration from the following sources:
* - the environment variables with the prefix OCAPPNAME_ - ex: OCMYCOMPONENT_MONGOURL
* - the file /etc/oc/appname.json - ex: /etc/oc/mycomponent.json
* - the file ./appname.json - ex: ./mycomponent.json
* The configuration loader will merge the configuration from the different sources
* The configuration loader will give priority to the environment variables
* The configuration loader will give priority to the local file over the default file
*/
o := oclib.GetConfLoader()
// init the local config object
models.GetConfig().Port = o.GetIntDefault("port", 8080)
models.GetConfig().LokiUrl = o.GetStringDefault("lokiurl", "")
models.GetConfig().LogLevel = o.GetStringDefault("loglevel", "info")
models.GetConfig().MongoUrl = o.GetStringDefault("mongourl", "mongodb://127.0.0.1:27017")
models.GetConfig().MongoDatabase = o.GetStringDefault("mongodatabase", "myDb")
models.GetConfig().NatsUrl = o.GetStringDefault("natsurl", "nats://localhost:4222")
models.GetConfig().mycomponentparam1 = o.GetStringDefault("mycomponentparam1", "mycomponentdefault1")
models.GetConfig().mycomponentparam2 = o.GetStringDefault("mycomponentparam2", "mycomponentdefault2")
// feed the library with the loaded config,
// this will also initialize a logger available via oclib.GetLogger()
oclib.SetConfig(
models.GetConfig().MongoUrl
models.GetConfig().MongoDatabase
models.GetConfig().NatsUrl
models.GetConfig().LokiUrl
models.GetConfig().LogLevel
)
// Beego init
beego.BConfig.AppName = appname
beego.BConfig.Listen.HTTPPort = models.GetConfig().Port
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
beego.Run()
}
```
## SPECIAL FLOWS IN OC-LIB RESUME :
### WORKFLOW AS ITS OWN WORKSPACE
A workflow on post, put, delete, manage a workspace with resources.
This workspace is deeply related to workflow by sharing its naming such as : "<workflow_name>_worspace"
### WORKFLOW GENERATE ITS OWN EXECUTION
A workflow on post, put, delete, with a schedule && schedule_active to "true", must manage execution by adding, deleting execution
depending on change (update -> involved delete into add)
If schedule_active is set to "false" execution will be deleted.
### WORKFLOW GENERATE ITS OWN BOOKING ON PEERS
A workflow on post, put, delete, with a schedule && schedule_active to "true", must manage booking by adding, deleting execution
with http requests on peers
depending on change (update -> involved delete into add)
If schedule_active is set to "false" booking will be deleted.
### SHARED WORKSPACE : WORSPACE & WORKFLOW
You can create a share workspace with workspace & workflow.
When a share workspace is post, put, delete it update workspace or workflow <shared> field.
Workspace can be shared on one share workspace
Workflow can be shared in multiple workspace
### SHARED WORKSPACE SHARE TO PEER
When writing a shared workspace, it set up to date on peers involved in shared workspace
It create or delete shared workspace in remote peers by http requests on oc-shared (update -> involved delete into add)
It create or delete workspace involved in shared workspace by http requests on oc-workspace (update -> involved delete into add)
It create or delete workflow involved in shared workspace by http requests on oc-workflow (update -> involved delete into add)
### WORKFLOW WRITE BUT SHARED
On delete & update & post, workflow will send to peer in <shared> field by http request on oc-workflow
### WORKSPACE WRITE BUT SHARED
On delete & update & post, workspace will send to peer in <shared> field by http request on oc-workspace

View File

@ -1 +0,0 @@
package oclib

19
config/app.go Normal file
View File

@ -0,0 +1,19 @@
package config
var appname string
// logs.CreateLogger
// Create a new logger
// Parameters:
// - appname: string : the name of the application using oclib
// - url: string : the url of a loki logger, console log only if ""
// Returns:
// - zerolog.Logger : the logger that will log for the library and the app
func SetAppName(name string) {
appname = name
}
func GetAppName() string {
return appname
}

55
config/conf.go Normal file
View File

@ -0,0 +1,55 @@
package config
import "sync"
// ===================================================
// This class has to be updated everytime
// a new configuration variable is defined
// in a componant that imports oc-lib
// ===================================================
type Config struct {
NATSUrl string
MongoUrl string
MongoDatabase string
Host string
Port string
LokiUrl string
LogLevel string
}
func (c Config) GetUrl() string {
return c.MongoUrl
}
func (c Config) GetDatabase() string {
return c.MongoDatabase
}
var instance *Config
var once sync.Once
func GetConfig() *Config {
once.Do(func() {
instance = &Config{}
})
return instance
}
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string) *Config {
/*once.Do(func() {
instance = &Config{
MongoUrl: mongoUrl,
MongoDatabase: database,
NATSUrl: natsUrl,
LokiUrl: lokiUrl,
LogLevel: logLevel,
}
})*/
GetConfig().MongoUrl = mongoUrl
GetConfig().MongoDatabase = database
GetConfig().NATSUrl = natsUrl
GetConfig().LokiUrl = lokiUrl
GetConfig().LogLevel = logLevel
return GetConfig()
}

56
config/conf_loader.go Normal file
View File

@ -0,0 +1,56 @@
package config
import (
"os"
"strings"
"github.com/goraz/onion"
"github.com/rs/zerolog"
)
/* GetConfLoader
* Get the configuration loader for the application
* Parameters:
* - AppName: string : the name of the application
* Returns:
* - *onion.Onion : the configuration loader
* The configuration loader will load the configuration from the following sources:
* - the environment variables with the prefix APPNAME_
* - the file /etc/oc/appname.json
* - the file ./appname.json
* The configuration loader will merge the configuration from the different sources
* The configuration loader will give priority to the environment variables
* The configuration loader will give priority to the local file over the default file
*/
func GetConfLoader() *onion.Onion {
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
AppName := GetAppName()
EnvPrefix := strings.ToUpper(AppName[0:2]+AppName[3:]) + "_"
defaultConfigFile := "/etc/oc/" + AppName[3:] + ".json"
localConfigFile := "./" + AppName[3:] + ".json"
var configFile string
var o *onion.Onion
l3 := onion.NewEnvLayerPrefix("_", EnvPrefix)
l2, err := onion.NewFileLayer(localConfigFile, nil)
if err == nil {
logger.Info().Msg("Local config file found " + localConfigFile + ", overriding default file")
configFile = localConfigFile
}
l1, err := onion.NewFileLayer(defaultConfigFile, nil)
if err == nil {
logger.Info().Msg("Config file found : " + defaultConfigFile)
configFile = defaultConfigFile
}
if configFile == "" {
logger.Info().Msg("No config file found, using env")
o = onion.New(l3)
} else if l1 != nil && l2 != nil {
o = onion.New(l1, l2, l3)
} else if l1 == nil {
o = onion.New(l2, l3)
} else if l2 == nil {
o = onion.New(l1, l3)
}
return o
}

View File

@ -1,5 +0,0 @@
package oclib
type Data struct {
Resource
}

View File

@ -1 +0,0 @@
package oclib

View File

@ -1 +0,0 @@
package oclib

190
dbs/dbs.go Normal file
View File

@ -0,0 +1,190 @@
package dbs
import (
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
)
type Operator int
const (
LIKE Operator = iota
EXISTS
IN
GTE
LTE
LT
GT
EQUAL
NOT
)
var str = [...]string{
"like",
"exists",
"in",
"gte",
"lte",
"lt",
"gt",
"equal",
"not",
}
func (m Operator) String() string {
return str[m]
}
func (m Operator) ToMongoEOperator(k string, value interface{}) bson.E {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered. Error:\n", r)
}
}()
defaultValue := bson.E{Key: k, Value: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
switch m {
case LIKE:
return bson.E{Key: k, Value: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
case EXISTS:
return bson.E{Key: k, Value: bson.M{"$exists": ToValueOperator(StringToOperator(m.String()), value)}}
case IN:
return bson.E{Key: k, Value: bson.M{"$in": ToValueOperator(StringToOperator(m.String()), value)}}
case GTE:
return bson.E{Key: k, Value: bson.M{"$gte": ToValueOperator(StringToOperator(m.String()), value)}}
case GT:
return bson.E{Key: k, Value: bson.M{"$gt": ToValueOperator(StringToOperator(m.String()), value)}}
case LTE:
return bson.E{Key: k, Value: bson.M{"$lte": ToValueOperator(StringToOperator(m.String()), value)}}
case LT:
return bson.E{Key: k, Value: bson.M{"$lt": ToValueOperator(StringToOperator(m.String()), value)}}
case EQUAL:
return bson.E{Key: k, Value: value}
case NOT:
v := value.(Filters)
orList := bson.A{}
andList := bson.A{}
f := bson.D{}
for k, filter := range v.Or {
for _, ff := range filter {
orList = append(orList, StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
for k, filter := range v.And {
for _, ff := range filter {
andList = append(andList, StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
if len(orList) > 0 && len(andList) == 0 {
f = bson.D{{"$or", orList}}
} else {
if len(orList) > 0 {
andList = append(andList, bson.M{"$or": orList})
}
f = bson.D{{"$and", andList}}
}
return bson.E{Key: "$not", Value: f}
default:
return defaultValue
}
}
func (m Operator) ToMongoOperator(k string, value interface{}) bson.M {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered. Error:\n", r)
}
}()
defaultValue := bson.M{k: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
switch m {
case LIKE:
return bson.M{k: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
case EXISTS:
return bson.M{k: bson.M{"$exists": ToValueOperator(StringToOperator(m.String()), value)}}
case IN:
return bson.M{k: bson.M{"$in": ToValueOperator(StringToOperator(m.String()), value)}}
case GTE:
return bson.M{k: bson.M{"$gte": ToValueOperator(StringToOperator(m.String()), value)}}
case GT:
return bson.M{k: bson.M{"$gt": ToValueOperator(StringToOperator(m.String()), value)}}
case LTE:
return bson.M{k: bson.M{"$lte": ToValueOperator(StringToOperator(m.String()), value)}}
case LT:
return bson.M{k: bson.M{"$lt": ToValueOperator(StringToOperator(m.String()), value)}}
case EQUAL:
return bson.M{k: value}
case NOT:
v := value.(Filters)
orList := bson.A{}
andList := bson.A{}
f := bson.D{}
for k, filter := range v.Or {
for _, ff := range filter {
orList = append(orList, StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
for k, filter := range v.And {
for _, ff := range filter {
andList = append(andList, StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
if len(orList) > 0 && len(andList) == 0 {
f = bson.D{{"$or", orList}}
} else {
if len(orList) > 0 {
andList = append(andList, bson.M{"$or": orList})
}
f = bson.D{{"$and", andList}}
}
return bson.M{"$not": f}
default:
return defaultValue
}
}
func StringToOperator(s string) Operator {
for i, v := range str {
if v == s {
return Operator(i)
}
}
return LIKE
}
func ToValueOperator(operator Operator, value interface{}) interface{} {
if strings.TrimSpace(fmt.Sprintf("%v", value)) == "*" {
value = ""
}
if operator == LIKE {
return "(?i).*" + strings.TrimSpace(fmt.Sprintf("%v", value)) + ".*"
}
return value
}
type Filters struct {
And map[string][]Filter `json:"and"`
Or map[string][]Filter `json:"or"`
}
type Filter struct {
Operator string `json:"operator,omitempty"`
Value interface{} `json:"value,omitempty"`
}
type Input = map[string]interface{}
func InputToBson(i Input, isUpdate bool) bson.D {
input := bson.D{}
for k, v := range i {
if k == "id" {
input = append(input, bson.E{Key: "_id", Value: v})
} else {
input = append(input, bson.E{Key: k, Value: v})
}
}
if isUpdate {
return bson.D{{Key: "$set", Value: input}}
}
return input
}

371
dbs/mongo/mongo.go Normal file
View File

@ -0,0 +1,371 @@
package mongo
import (
"context"
"errors"
"slices"
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/static"
"github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
mngoClient *mongo.Client
mngoDB *mongo.Database
MngoCtx context.Context
cancel context.CancelFunc
isConnected bool
existingCollections []string
mngoCollections []string
mngoConfig MongoConf
ResourceMap map[string]interface{}
)
var MONGOService = MongoDB{}
type MongoConf interface {
GetUrl() string
GetDatabase() string
}
type MongoDB struct {
Logger zerolog.Logger
}
func (m *MongoDB) Init(collections []string, config MongoConf) {
// var baseConfig string
isConnected = false
m.Logger = logs.GetLogger()
ResourceMap = make(map[string]interface{})
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
mngoCollections = collections
mngoConfig = config
if err := m.createClient(config.GetUrl(), false); err != nil {
m.Logger.Error().Msg(err.Error())
}
}
func (m *MongoDB) TestDB(config MongoConf) error {
err := m.createClient(config.GetUrl(), true)
if err != nil {
return err
}
return nil
}
func (m *MongoDB) TestCollections(config MongoConf, neededCols []string) error {
mngoDB = mngoClient.Database(config.GetDatabase())
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
existingCollections, err := mngoDB.ListCollectionNames(MngoCtx, bson.D{})
if err != nil {
return errors.New("Error contacting MongoDB\n" + err.Error())
}
for _, col := range neededCols {
if slices.Contains(existingCollections, col) {
continue
}
return errors.New("Collection " + col + " not found")
}
return nil
}
func (m *MongoDB) createClient(MongoURL string, test bool) error {
if mngoClient != nil {
return nil
}
var err error
// Allows us to use marshal and unmarshall with results of FindOne() and others
bsonOpts := &options.BSONOptions{
UseJSONStructTags: true,
NilSliceAsEmpty: true,
}
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
// Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil || mngoClient == nil {
mngoClient = nil
isConnected = false
return errors.New("Mongodb connect " + MongoURL + ":" + err.Error())
}
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
mngoClient = nil
isConnected = false
return errors.New("Mongodb ping " + MongoURL + ":" + err.Error())
}
if !isConnected && mngoClient != nil && !test {
m.Logger.Info().Msg("Connecting mongo client to db " + mngoConfig.GetDatabase())
m.prepareDB(mngoCollections, mngoConfig)
m.Logger.Info().Msg("Database is READY")
}
return nil
}
func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
var err error
mngoDB = mngoClient.Database(config.GetDatabase())
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
if err != nil {
m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
}
collectionMap := make(map[string]bool)
for _, name := range existingCollections {
collectionMap[name] = true
}
// Only do the collection definition process if it doesn't already exists
// we add the collection to the collection map from mongo/mongo_utils to provide faster access to the collection
for _, collection_name := range list_collection {
new_collection := mngoDB.Collection(collection_name)
if _, exists := collectionMap[collection_name]; !exists {
m.createCollection(collection_name, new_collection)
if collection_name == "peer" {
id, p := static.GetMyLocalBsonPeer()
m.StoreOne(p, id, collection_name)
}
} else {
CollectionMap[collection_name] = new_collection
}
}
isConnected = true
}
// Creates the collection with index specified in mongo/mongo_collections
// or use the basic collection creation function
func (m *MongoDB) createCollection(collection_name string, new_collection *mongo.Collection) {
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var err error
CollectionMap[collection_name] = new_collection
_, exists := IndexesMap[collection_name]
if exists {
if _, err = new_collection.Indexes().CreateMany(MngoCtx, IndexesMap[collection_name]); err != nil {
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
m.Logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
panic(err)
} else if !errors.As(err, &cmdErr) {
m.Logger.Fatal().Msg("Unexpected error: " + err.Error())
panic(err)
}
}
} else {
mngoDB.CreateCollection(MngoCtx, collection_name)
}
}
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return 0, 503, err
}
filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return 0, 404, err
}
return result.DeletedCount, 200, nil
}
func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return 0, 503, err
}
filter := bson.D{}
for k, v := range f {
filter = append(filter, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.DeleteMany(MngoCtx, filter, opts)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return 0, 404, err
}
return result.DeletedCount, 200, nil
}
func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{}, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return 0, 503, err
}
var doc map[string]interface{}
b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc)
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.UpdateMany(MngoCtx, f, dbs.InputToBson(doc, true))
if err != nil {
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return 0, 404, err
}
return res.UpsertedCount, 200, nil
}
func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string) (string, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return "", 503, err
}
var doc map[string]interface{}
b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc)
filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(doc, true))
if err != nil {
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return "", 404, err
}
return id, 200, nil
}
func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return "", 503, err
}
var doc map[string]interface{}
b, _ := bson.Marshal(obj)
bson.Unmarshal(b, &doc)
doc["_id"] = id
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := targetDBCollection.InsertOne(MngoCtx, doc)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", 409, err
}
return id, 200, nil
}
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return nil, 503, err
}
filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res := targetDBCollection.FindOne(MngoCtx, filter)
if res.Err() != nil {
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
err := res.Err()
return nil, 404, err
}
return res, 200, nil
}
func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return nil, 503, err
}
opts := options.Find()
opts.SetLimit(100)
targetDBCollection := CollectionMap[collection_name]
orList := bson.A{}
andList := bson.A{}
f := bson.D{}
if filters != nil {
for k, filter := range filters.Or {
for _, ff := range filter {
orList = append(orList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
for k, filter := range filters.And {
for _, ff := range filter {
andList = append(andList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
if len(orList) > 0 && len(andList) == 0 {
f = bson.D{{"$or", orList}}
} else {
if len(orList) > 0 {
andList = append(andList, bson.M{"$or": orList})
}
f = bson.D{{"$and", andList}}
}
}
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if cursor, err := targetDBCollection.Find(
MngoCtx,
f,
opts,
); err != nil {
return nil, 404, err
} else {
return cursor, 200, nil
}
}
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return nil, 503, err
}
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, f)
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl(), false); err != nil {
return nil, 503, err
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, bson.D{})
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}

55
dbs/mongo/mongo_utils.go Normal file
View File

@ -0,0 +1,55 @@
package mongo
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// Will store the created collection object for a faster access
var CollectionMap map[string]*mongo.Collection
var IndexesMap map[string][]mongo.IndexModel
func init() {
CollectionMap = make(map[string]*mongo.Collection)
IndexesMap = make(map[string][]mongo.IndexModel)
IndexesMap["data"] = append(IndexesMap["data"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"}},
})
IndexesMap["datacenter"] = append(IndexesMap["datacenter"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"}},
})
IndexesMap["storage"] = append(IndexesMap["storage"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"}},
})
IndexesMap["processing"] = append(IndexesMap["processing"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"},
},
})
IndexesMap["workflow"] = append(IndexesMap["workflow"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"},
},
})
}
func GetObjIDFromString(id string) interface{} {
objectID, err := primitive.ObjectIDFromHex(id)
if err == nil {
return objectID
}
return id
}

View File

@ -1,22 +1,22 @@
@startuml oclib @startuml oclib
abstract Ressource { abstract Resource{
+id: int +UUID: int
+name: string +name: string
+icon: string +icon: string
+description: string +description: string
+graphic: GraphicElement +graphic: GraphicElement
+element: Data/Processing/Storage/Workflow/Datacenter +element: DataResource/ProcessingResource/StorageResource/Workflow/DatacenterResource
} }
class Data { class DataResource {
+id: int +UUID: int
+name: string +name: string
} }
class Processing { class ProcessingResource {
+id: int +UUID: int
+name: string +name: string
+container: string +container: string
+command: int +command: int
@ -24,110 +24,154 @@ class Processing {
} }
class Storage { class StorageResource {
+id: int +UUID: int
+name: string +name: string
+url: string +url: string
+capacity: int +capacity: int
} }
class Datacenter { class DatacenterResource {
+id: int +UUID: int
+name: string +name: string
} }
class Workflow { class Workflow {
+id: int +UUID: int
+name: string +name: string
} }
class ResourceSet { class ResourceSet {
+id: int +UUID: int
+name: string +name: string
+ressources: Ressource[] +ressources: Ressource[]
} }
class WorkflowSchedule { class WorkflowSchedule {
+id: int +UUID: int
+start: date +start: date
+end: date +end: date
+cron : string +cron : string
} }
class Graph { class Graph {
+id: int +UUID: int
+ressources: map[GraphicElement.ID]Ressource +ressources: map[GraphicElement.UUID]Resource
+links: Link[] +links: Link[]
} }
class Link { class Link {
+id: int +UUID: int
+source: GraphicElement.ID +source: GraphicElement.UUID
+target: GraphicElement.ID +target: GraphicElement.UUID
+graphic: GraphicLink +graphic: GraphicLink
} }
class GraphicLink { class GraphicLink {
+id: int +UUID: int
+startXY: coord +startXY: coord
+endXY: coord +endXY: coord
+style: string +style: string
} }
class GraphicElement { class GraphicElement {
+id: int +UUID: int
+style: string +style: string
+xy: coord +xy: coord
} }
class Calendar { class Calendar {
+id: int +UUID: int
+name: string +name: string
+workflows: Workflow[] +workflows: Workflow[]
+owner: string +owner: string
} }
class UserWorkflows { class UserWorkflows {
+id: int +UUID: int
+user: string +user: string
+workflows: Workflow[] +workflows: Workflow[]
} }
class DatacenterWorkflows { class DatacenterWorkflows {
+id: int +UUID: int
+datacenter: Datacenter +datacenter: DatacenterResource
+workflows: Workflow[] +workflows: Workflow[]
} }
class Graph { class Graph {
+id: int +UUID: int
+graph: Graph +graph: Graph
+workflows: Workflow[] +workflows: Workflow[]
} }
class Workspace {
+UUID: int
+name: string
+resources: ResourceSet[]
+peers: Peer[]
}
class Peer {
+UUID: int
+name: string
+url: string
}
class SharedWorkspace {
+UUID: int
+name: string
+version: string
+description: string
+attributes: map[string]string
+workspaces: Workspace[]
+workflows: Workflow[]
+peers: Peer[]
}
class RuleSet {
+UUID: int
+name: string
+description: string
+rules: Rule[]
}
class Rule {
+UUID: int
+name: string
+description: string
+condition: string
+action: string
}
SharedWorkspace "1" o-- "0..*" Workspace
SharedWorkspace "1" o-- "0..*" Peer
Workspace "1" o-- "0..*" ResourceSet
Peer -- Resource
RuleSet "1" o-- "0..*" Rule
SharedWorkspace -- RuleSet
UserWorkflows "1" o-- "0..*" Workflow UserWorkflows "1" o-- "0..*" Workflow
DatacenterWorkflows "1" o-- "0..*" Workflow DatacenterWorkflows "1" o-- "0..*" Workflow
Ressource <|-- Data Resource<|-- DataResource
Ressource <|-- Processing Resource<|-- ProcessingResource
Ressource <|-- Storage Resource<|-- StorageResource
Ressource <|-- Datacenter Resource<|-- DatacenterResource
Ressource <|-- Workflow Resource<|-- Workflow
ResourceSet "1" o-- "0..*" Ressource ResourceSet "1" o-- "0..*" Ressource
Workflow "1" o-- "0..*" ResourceSet Workflow "1" o-- "0..*" ResourceSet
Workflow "1" o-- "0..*" WorkflowSchedule Workflow "1" o-- "0..1" WorkflowSchedule
Workflow "1" o-- "0..*" Graph Workflow "1" o-- "0..*" Graph
Graph "1" o-- "0..*" Resources Graph "1" o-- "0..*" Resources
Graph "1" o-- "0..*" Link Graph "1" o-- "0..*" Link
Ressource --o GraphicElement Resource--o GraphicElement
Link -- GraphicLink Link -- GraphicLink

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 102 KiB

466
entrypoint.go Normal file
View File

@ -0,0 +1,466 @@
package oclib
import (
"errors"
"fmt"
"runtime/debug"
"cloud.o-forge.io/core/oc-lib/config"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/utils"
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
"cloud.o-forge.io/core/oc-lib/models/workspace"
shared_workspace "cloud.o-forge.io/core/oc-lib/models/workspace/shared"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/goraz/onion"
"github.com/rs/zerolog"
)
type Filters = dbs.Filters
type LibDataEnum int
// init accessible constant to retrieve data from the database
const (
INVALID LibDataEnum = iota
DATA_RESOURCE = utils.DATA_RESOURCE
PROCESSING_RESOURCE = utils.PROCESSING_RESOURCE
STORAGE_RESOURCE = utils.STORAGE_RESOURCE
DATACENTER_RESOURCE = utils.DATACENTER_RESOURCE
WORKFLOW_RESOURCE = utils.WORKFLOW_RESOURCE
WORKFLOW = utils.WORKFLOW
WORKSPACE = utils.WORKSPACE
WORKFLOW_EXECUTION = utils.WORKFLOW_EXECUTION
PEER = utils.PEER
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
RULE = utils.RULE
BOOKING = utils.BOOKING
)
// will turn into standards api hostnames
func (d LibDataEnum) API() string {
return utils.DefaultAPI[d]
}
// will turn into standards name
func (d LibDataEnum) String() string {
return utils.Str[d]
}
// will turn into enum index
func (d LibDataEnum) EnumIndex() int {
return int(d)
}
// model to define the shallow data structure
type LibDataShallow struct {
Data []utils.ShallowDBObject `bson:"data" json:"data"`
Code int `bson:"code" json:"code"`
Err string `bson:"error" json:"error"`
}
// model to define the data structure
type LibData struct {
Data utils.DBObject `bson:"data" json:"data"`
Code int `bson:"code" json:"code"`
Err string `bson:"error" json:"error"`
}
// here is the singleton variable to store the paths that api will use
var paths map[LibDataEnum]string = map[LibDataEnum]string{}
// to get the paths
func GetPaths() map[LibDataEnum]string {
return paths
}
// to get the path
func GetPath(collection LibDataEnum) string {
return paths[collection]
}
// to add the path
func AddPath(collection LibDataEnum, path string) {
paths[collection] = path
}
func Init(appName string) {
config.SetAppName(appName) // set the app name to the logger to define the main log chan
// create a temporary console logger for init
logs.SetLogger(logs.CreateLogger("main"))
}
//
// Expose subpackages
//
/* GetLogger returns the main logger
* @return zerolog.Logger
*/
func GetLogger() zerolog.Logger {
return logs.GetLogger()
}
/* SetConfig will set the config and create a logger according to app configuration and initialize mongo accessor
* @param url string
* @param database string
* @param natsUrl string
* @param lokiUrl string
* @param logLevel string
* @return *Config
*/
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string) *config.Config {
cfg := config.SetConfig(mongoUrl, database, natsUrl, lokiUrl, logLevel)
defer func() {
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Init : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
fmt.Printf("Panic recovered in Init : %v - %v\n", r, string(debug.Stack()))
}
}()
logs.CreateLogger("main")
mongo.MONGOService.Init(models.GetModelsNames(), config.GetConfig()) // init the mongo service
/*
Here we will check if the resource model is already stored in the database
If not we will store it
Resource model is the model that will define the structure of the resources
*/
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
for _, model := range []string{utils.DATA_RESOURCE.String(), utils.PROCESSING_RESOURCE.String(), utils.STORAGE_RESOURCE.String(), utils.DATACENTER_RESOURCE.String(), utils.WORKFLOW_RESOURCE.String()} {
data, code, _ := accessor.Search(nil, model)
if code == 404 || len(data) == 0 {
m := map[string]resource_model.Model{}
// TODO Specify the model for each resource
// for now only processing is specified here (not an elegant way)
if model == utils.PROCESSING_RESOURCE.String() {
m["image"] = resource_model.Model{
Type: "string",
ReadOnly: false,
}
m["command"] = resource_model.Model{
Type: "string",
ReadOnly: false,
}
m["args"] = resource_model.Model{
Type: "string",
ReadOnly: false,
}
m["execute"] = resource_model.Model{
Type: "map[int]map[string]string",
ReadOnly: false,
}
}
accessor.StoreOne(&resource_model.ResourceModel{
ResourceType: model,
Model: m,
})
}
}
return cfg
}
/* GetConfig will get the config
* @return *Config
*/
func GetConfig() *config.Config {
return config.GetConfig()
}
/* GetConfLoader
* Get the configuration loader for the application
* Parameters:
* - AppName: string : the name of the application
* Returns:
* - *onion.Onion : the configuration loader
* The configuration loader will load the configuration from the following sources:
* - the environment variables with the prefix OCAPPNAME_
* - the file /etc/oc/appname.json
* - the file ./appname.json
* The configuration loader will merge the configuration from the different sources
* The configuration loader will give priority to the environment variables
* The configuration loader will give priority to the local file over the default file
*/
func GetConfLoader() *onion.Onion {
return config.GetConfLoader()
}
/*
* Search will search for the data in the database
* @param filters *dbs.Filters
* @param word string
* @param collection LibDataEnum
* @param c ...*tools.HTTPCaller
* @return data LibDataShallow
*/
func Search(filters *dbs.Filters, word string, collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Search : "+fmt.Sprintf("%v", r)))
data = LibDataShallow{Data: nil, Code: 500, Err: "Panic recovered in LoadAll : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).Search(filters, word)
if err != nil {
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
return
}
data = LibDataShallow{Data: d, Code: code}
return
}
/*
* LoadAll will load all the data from the database
* @param collection LibDataEnum
* @param c ...*tools.HTTPCaller
* @return data LibDataShallow
*/
func LoadAll(collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadAll : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibDataShallow{Data: nil, Code: 500, Err: "Panic recovered in LoadAll : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).LoadAll()
if err != nil {
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
return
}
data = LibDataShallow{Data: d, Code: code}
return
}
/*
* LoadOne will load one data from the database
* @param collection LibDataEnum
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func LoadOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in LoadOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).LoadOne(id)
if err != nil {
data = LibData{Data: d, Code: code, Err: err.Error()}
return
}
data = LibData{Data: d, Code: code}
return
}
/*
* UpdateOne will update one data from the database
* @param collection LibDataEnum
* @param set map[string]interface{}
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in UpdateOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in UpdateOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
model := models.Model(collection.EnumIndex())
d, code, err := model.GetAccessor(caller).UpdateOne(model.Deserialize(set), id)
if err != nil {
data = LibData{Data: d, Code: code, Err: err.Error()}
return
}
data = LibData{Data: d, Code: code}
return
}
/*
* DeleteOne will delete one data from the database
* @param collection LibDataEnum
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func DeleteOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in DeleteOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in DeleteOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).DeleteOne(id)
if err != nil {
data = LibData{Data: d, Code: code, Err: err.Error()}
return
}
data = LibData{Data: d, Code: code}
return
}
/*
* StoreOne will store one data from the database
* @param collection LibDataEnum
* @param object map[string]interface{}
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func StoreOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in StoreOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in StoreOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
model := models.Model(collection.EnumIndex())
d, code, err := model.GetAccessor(caller).StoreOne(model.Deserialize(object))
if err != nil {
data = LibData{Data: d, Code: code, Err: err.Error()}
return
}
data = LibData{Data: d, Code: code}
return
}
/*
* CopyOne will copy one data from the database
* @param collection LibDataEnum
* @param object map[string]interface{}
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
func CopyOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in CopyOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in UpdateOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
model := models.Model(collection.EnumIndex())
d, code, err := model.GetAccessor(caller).CopyOne(model.Deserialize(object))
if err != nil {
data = LibData{Data: d, Code: code, Err: err.Error()}
return
}
data = LibData{Data: d, Code: code}
return
}
// ================ CAST ========================= //
func (l *LibData) ToDataResource() *data.DataResource {
if l.Data.GetAccessor(nil).GetType() == utils.DATA_RESOURCE.String() {
return l.Data.(*data.DataResource)
}
return nil
}
func (l *LibData) ToDatacenterResource() *datacenter.DatacenterResource {
if l.Data != nil && l.Data.GetAccessor(nil).GetType() == utils.DATACENTER_RESOURCE.String() {
return l.Data.(*datacenter.DatacenterResource)
}
return nil
}
func (l *LibData) ToStorageResource() *storage.StorageResource {
if l.Data.GetAccessor(nil).GetType() == utils.STORAGE_RESOURCE.String() {
return l.Data.(*storage.StorageResource)
}
return nil
}
func (l *LibData) ToProcessingResource() *processing.ProcessingResource {
if l.Data.GetAccessor(nil).GetType() == utils.PROCESSING_RESOURCE.String() {
return l.Data.(*processing.ProcessingResource)
}
return nil
}
func (l *LibData) ToWorkflowResource() *w.WorkflowResource {
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW_RESOURCE.String() {
return l.Data.(*w.WorkflowResource)
}
return nil
}
func (l *LibData) ToPeer() *peer.Peer {
if l.Data.GetAccessor(nil).GetType() == utils.PEER.String() {
return l.Data.(*peer.Peer)
}
return nil
}
func (l *LibData) ToWorkflow() *w2.Workflow {
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW.String() {
return l.Data.(*w2.Workflow)
}
return nil
}
func (l *LibData) ToWorkspace() *workspace.Workspace {
if l.Data.GetAccessor(nil).GetType() == utils.WORKSPACE.String() {
return l.Data.(*workspace.Workspace)
}
return nil
}
func (l *LibData) ToSharedWorkspace() *shared_workspace.SharedWorkspace {
if l.Data.GetAccessor(nil).GetType() == utils.SHARED_WORKSPACE.String() {
return l.Data.(*shared_workspace.SharedWorkspace)
}
return nil
}
func (l *LibData) ToRule() *rule.Rule {
if l.Data.GetAccessor(nil).GetType() == utils.SHARED_WORKSPACE.String() {
return l.Data.(*rule.Rule)
}
return nil
}
func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW_EXECUTION.String() {
return l.Data.(*workflow_execution.WorkflowExecution)
}
return nil
}

45
go.mod
View File

@ -1,11 +1,48 @@
module oc-lib module cloud.o-forge.io/core/oc-lib
go 1.22.0 go 1.22.0
require github.com/rs/zerolog v1.33.0 require (
github.com/go-playground/validator/v10 v10.22.0
github.com/google/uuid v1.6.0
github.com/goraz/onion v0.1.3
github.com/nats-io/nats.go v1.37.0
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
)
require (
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
)
require ( require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.12.0 // indirect go.mongodb.org/mongo-driver v1.16.0
golang.org/x/sys v0.22.0 // indirect
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

132
go.sum
View File

@ -1,15 +1,143 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/coreos/etcd v3.3.17+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/etcd-io/etcd v3.3.17+incompatible/go.mod h1:cdZ77EstHBwVtD6iTgzgvogwcjo9m4iOqoijouPJ4bs=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/goraz/onion v0.1.3 h1:KhyvbDA2b70gcz/d5izfwTiOH8SmrvV43AsVzpng3n0=
github.com/goraz/onion v0.1.3/go.mod h1:XEmz1XoBz+wxTgWB8NwuvRm4RAu3vKxvrmYtzK+XCuQ=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/ogier/pflag v0.0.1/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/skarademir/naturalsort v0.0.0-20150715044055-69a5d87bef62/go.mod h1:oIdVclZaltY1Nf7OQUkg1/2jImBJ+ZfKZuDIRSwk3p0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 h1:tBiBTKHnIjovYoLX/TPkcf+OjqqKGQrPtGT3Foz+Pgo=
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76/go.mod h1:SQliXeA7Dhkt//vS29v3zpbEwoa+zb2Cn5xj5uO4K5U=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.16.0 h1:tpRsfBJMROVHKpdGyc1BBEzzjDUWjItxbVSZ8Ls4BQ4=
go.mongodb.org/mongo-driver v1.16.0/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1 +0,0 @@
package oclib

View File

@ -1,33 +1,43 @@
package oclib package logs
import ( import (
"oc-lib/logs"
"os" "os"
"runtime" "runtime"
"time" "time"
"cloud.o-forge.io/core/oc-lib/config"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
var logger zerolog.Logger var logger zerolog.Logger
// CreateLogger // logs.CreateLogger
// Create a new logger // Create a new logger
// Parameters: // Parameters:
// - appname: string : the name of the application using oclib // - appname: string : the name of the application using oclib
// - url: string : the url of a loki logger, console log only if "" // - url: string : the url of a loki logger, console log only if ""
// Returns: // Returns:
// - zerolog.Logger : the logger that will log for the library and the app // - zerolog.Logger : the logger that will log for the library and the app
func CreateLogger(appname string, url string) zerolog.Logger {
func GetLogger() zerolog.Logger {
return logger
}
func SetLogger(l zerolog.Logger) {
logger = l
}
func CreateLogger(funcName string) zerolog.Logger {
url := config.GetConfig().LokiUrl
if url != "" { if url != "" {
labels := map[string]string{ labels := map[string]string{
"app": "app", "app": config.GetAppName(),
"code": "go", "code": "go",
"platform": runtime.GOOS, "platform": runtime.GOOS,
"function": funcName,
} }
lokiWriter := logs.NewLokiWriter(url, labels) lokiWriter := NewLokiWriter(url, labels)
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}

View File

@ -55,6 +55,17 @@ func (w *LokiWriter) Write(p []byte) (n int, err error) {
} }
labels["level"] = level labels["level"] = level
// Add label that have been added to the event
// A bit unsafe since we don't know what could be stored in the event
// but we can't access this object once passed to the multilevel writter
for k,v := range(event){
if k != "level" && k != "time" && k != "message"{
labels[k] = v.(string)
}
}
// Format the timestamp in nanoseconds // Format the timestamp in nanoseconds
timestamp := fmt.Sprintf("%d000000", time.Now().UnixNano()/int64(time.Millisecond)) timestamp := fmt.Sprintf("%d000000", time.Now().UnixNano()/int64(time.Millisecond))
@ -76,7 +87,7 @@ func (w *LokiWriter) Write(p []byte) (n int, err error) {
//fmt.Printf("Sending payload to Loki: %s\n", string(payloadBytes)) //fmt.Printf("Sending payload to Loki: %s\n", string(payloadBytes))
req, err := http.NewRequest("POST", w.url, bytes.NewReader(payloadBytes)) req, err := http.NewRequest("POST", w.url + "/loki/api/v1/push", bytes.NewReader(payloadBytes))
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to create HTTP request: %w", err) return 0, fmt.Errorf("failed to create HTTP request: %w", err)
} }

89
models/booking/booking.go Normal file
View File

@ -0,0 +1,89 @@
package booking
import (
"encoding/json"
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson/primitive"
)
/*
* Booking is a struct that represents a booking
*/
type Booking struct {
workflow_execution.WorkflowExecution // WorkflowExecution contains the workflow execution data
DatacenterResourceID string `json:"datacenter_resource_id,omitempty" bson:"datacenter_resource_id,omitempty" validate:"required"` // DatacenterResourceID is the ID of the datacenter resource specified in the booking
}
// CheckBooking checks if a booking is possible on a specific datacenter resource
func (wfa *Booking) CheckBooking(id string, start time.Time, end *time.Time) (bool, error) {
// check if
if end == nil {
// if no end... then Book like a savage
return true, nil
}
e := *end
accessor := wfa.GetAccessor(nil)
res, code, err := accessor.Search(&dbs.Filters{
And: map[string][]dbs.Filter{ // check if there is a booking on the same datacenter resource by filtering on the datacenter_resource_id, the state and the execution date
"datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: id}},
"workflowexecution.state": {{Operator: dbs.EQUAL.String(), Value: workflow_execution.SCHEDULED.EnumIndex()}},
"workflowexecution.execution_date": {
{Operator: dbs.LTE.String(), Value: primitive.NewDateTimeFromTime(e)},
{Operator: dbs.GTE.String(), Value: primitive.NewDateTimeFromTime(start)},
},
},
}, "")
if code != 200 {
return false, err
}
return len(res) == 0, nil
}
// tool to convert the argo status to a state
func (wfa *Booking) ArgoStatusToState(status string) *Booking {
wfa.WorkflowExecution.ArgoStatusToState(status)
return wfa
}
func (ao *Booking) GetID() string {
return ao.UUID
}
func (r *Booking) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *Booking) GetName() string {
return d.UUID + "_" + d.ExecDate.String()
}
func (d *Booking) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.BOOKING, caller) // Initialize the accessor with the BOOKING model type
return data
}
func (dma *Booking) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Booking) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,88 @@
package booking
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type bookingMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the bookingMongoAccessor
func New() *bookingMongoAccessor {
return &bookingMongoAccessor{}
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (wfa *bookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *bookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &Booking{})
}
func (wfa *bookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *bookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *bookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Booking
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa bookingMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Booking
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractObject) // Warning only AbstractObject is returned
}
return objs, 200, nil
}
// Search is a function that searches for a booking in the database
func (wfa *bookingMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Booking
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

59
models/models.go Normal file
View File

@ -0,0 +1,59 @@
package models
import (
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/booking"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
d "cloud.o-forge.io/core/oc-lib/models/resources/data"
dc "cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
p "cloud.o-forge.io/core/oc-lib/models/resources/processing"
s "cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/utils"
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
w3 "cloud.o-forge.io/core/oc-lib/models/workspace"
shared_workspace "cloud.o-forge.io/core/oc-lib/models/workspace/shared"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
)
/*
This package contains the models used in the application
It's used to create the models dynamically
*/
var models = map[string]func() utils.DBObject{
utils.WORKFLOW_RESOURCE.String(): func() utils.DBObject { return &w.WorkflowResource{} },
utils.DATA_RESOURCE.String(): func() utils.DBObject { return &d.DataResource{} },
utils.DATACENTER_RESOURCE.String(): func() utils.DBObject { return &dc.DatacenterResource{} },
utils.STORAGE_RESOURCE.String(): func() utils.DBObject { return &s.StorageResource{} },
utils.PROCESSING_RESOURCE.String(): func() utils.DBObject { return &p.ProcessingResource{} },
utils.WORKFLOW.String(): func() utils.DBObject { return &w2.Workflow{} },
utils.WORKFLOW_EXECUTION.String(): func() utils.DBObject { return &workflow_execution.WorkflowExecution{} },
utils.WORKSPACE.String(): func() utils.DBObject { return &w3.Workspace{} },
utils.RESOURCE_MODEL.String(): func() utils.DBObject { return &resource_model.ResourceModel{} },
utils.PEER.String(): func() utils.DBObject { return &peer.Peer{} },
utils.SHARED_WORKSPACE.String(): func() utils.DBObject { return &shared_workspace.SharedWorkspace{} },
utils.RULE.String(): func() utils.DBObject { return &rule.Rule{} },
utils.BOOKING.String(): func() utils.DBObject { return &booking.Booking{} },
}
// Model returns the model object based on the model type
func Model(model int) utils.DBObject {
log := logs.GetLogger()
if _, ok := models[utils.FromInt(model)]; ok {
return models[utils.FromInt(model)]()
}
log.Error().Msg("Can't find model " + utils.FromInt(model) + ".")
return nil
}
// GetModelsNames returns the names of the models
func GetModelsNames() []string {
names := []string{}
for name := range models {
names = append(names, name)
}
return names
}

94
models/peer/peer.go Normal file
View File

@ -0,0 +1,94 @@
package peer
import (
"encoding/json"
"fmt"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/static"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
)
// Peer is a struct that represents a peer
type Peer struct {
utils.AbstractObject
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"` // Url is the URL of the peer (base64url)
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"` // PublicKey is the public key of the peer
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"` // Services is the services of the peer
FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"` // FailedExecution is the list of failed executions, to be retried
}
// AddExecution adds an execution to the list of failed executions
func (ao *Peer) AddExecution(exec PeerExecution) {
found := false
for _, v := range ao.FailedExecution { // Check if the execution is already in the list
if v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body) {
found = true
break
}
}
if !found {
ao.FailedExecution = append(ao.FailedExecution, exec)
}
}
// RemoveExecution removes an execution from the list of failed executions
func (ao *Peer) RemoveExecution(exec PeerExecution) {
new := []PeerExecution{}
for i, v := range ao.FailedExecution {
if !(v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body)) {
new = append(new, ao.FailedExecution[i])
}
}
ao.FailedExecution = new
}
// IsMySelf checks if the peer is the local peer
func (ao *Peer) IsMySelf() bool {
id, _ := static.GetMyLocalJsonPeer()
return ao.UUID == id
}
// LaunchPeerExecution launches an execution on a peer
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
p.UUID = peerID
return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache
}
func (ao *Peer) GetID() string {
return ao.UUID
}
func (r *Peer) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *Peer) GetName() string {
return d.Name
}
func (d *Peer) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.PEER, caller) // Initialize the accessor with the PEER model type
return data
}
func (dma *Peer) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Peer) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

135
models/peer/peer_cache.go Normal file
View File

@ -0,0 +1,135 @@
package peer
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
/*
* PeerExecution is a struct that represents an execution on a peer
* it defines the execution data
*/
type PeerExecution struct {
Method string `json:"method" bson:"method"`
Url string `json:"url" bson:"url"`
Body map[string]interface{} `json:"body" bson:"body"`
DataType int `json:"data_type" bson:"data_type"`
DataID string `json:"data_id" bson:"data_id"`
}
var cache = &PeerCache{} // Singleton instance of the peer cache
// PeerCache is a struct that represents a peer cache
type PeerCache struct {
Executions []*PeerExecution
}
// urlFormat formats the URL of the peer with the data type API function
func (p *PeerCache) urlFormat(url string, dt utils.DataType) string {
// localhost is replaced by the local peer URL
// because localhost must collide on a web request security protocol
if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") {
url = strings.ReplaceAll(url, "localhost", dt.API())
url = strings.ReplaceAll(url, "127.0.0.1", dt.API())
r := regexp.MustCompile("(:[0-9]+)")
t := r.FindString(url)
if t != "" {
url = strings.Replace(url, t, ":8080", -1)
}
r.ReplaceAllString(url, ":8080")
}
return url
}
// checkPeerStatus checks the status of a peer
func (p *PeerCache) checkPeerStatus(peerID string, appName string, caller *tools.HTTPCaller) (*Peer, bool) {
api := tools.API{}
access := (&Peer{}).GetAccessor(nil)
res, code, _ := access.LoadOne(peerID) // Load the peer from db
if code != 200 { // no peer no party
return nil, false
}
methods := caller.URLS[utils.PEER.String()] // Get the methods url of the peer
if methods == nil {
return res.(*Peer), false
}
meth := methods[tools.POST] // Get the POST method to check status
if meth == "" {
return res.(*Peer), false
}
url := p.urlFormat(res.(*Peer).Url+meth, utils.PEER) // Format the URL
state, services := api.CheckRemotePeer(url) // Check the status of the peer
res.(*Peer).Services = services // Update the services states of the peer
access.UpdateOne(res, peerID) // Update the peer in the db
return res.(*Peer), state != tools.DEAD && services[appName] == 0 // Return the peer and its status
}
// LaunchPeerExecution launches an execution on a peer
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
methods := caller.URLS[dt.String()] // Get the methods url of the data type
if _, ok := methods[method]; !ok {
return nil, errors.New("no path found")
}
meth := methods[method] // Get the method url to execute
if meth == "" {
return nil, errors.New("no path found")
} else {
meth = strings.ReplaceAll(meth, ":id", dataID) // Replace the id in the url in case of a DELETE / UPDATE method (it's a standard naming in OC)
}
url := ""
// Check the status of the peer
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok {
// If the peer is not reachable, add the execution to the failed executions list
pexec := &PeerExecution{
Method: method.String(),
Url: p.urlFormat((mypeer.Url)+meth, dt),
Body: body,
DataType: dt.EnumIndex(),
DataID: dataID,
}
mypeer.AddExecution(*pexec)
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID) // Update the peer in the db
return nil, errors.New("peer is not reachable")
} else {
// If the peer is reachable, launch the execution
url = p.urlFormat((mypeer.Url)+meth, dt) // Format the URL
tmp := mypeer.FailedExecution // Get the failed executions list
mypeer.FailedExecution = []PeerExecution{} // Reset the failed executions list
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID) // Update the peer in the db
for _, v := range tmp { // Retry the failed executions
go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
}
}
return nil, p.exec(url, method, body, caller) // Execute the method
}
// exec executes the method on the peer
func (p *PeerCache) exec(url string, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) error {
var b []byte
var err error
if method == tools.POST { // Execute the POST method if it's a POST method
b, err = caller.CallPost(url, "", body)
}
if method == tools.GET { // Execute the GET method if it's a GET method
b, err = caller.CallGet(url, "")
}
if method == tools.DELETE { // Execute the DELETE method if it's a DELETE method
b, err = caller.CallDelete(url, "")
}
var m map[string]interface{}
json.Unmarshal(b, &m)
if err != nil {
return err
}
if e, ok := m["error"]; !ok && e != "" { // Check if there is an error in the response
return errors.New(fmt.Sprintf("%v", m["error"]))
}
return err
}

View File

@ -0,0 +1,89 @@
package peer
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type peerMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the peerMongoAccessor
func New() *peerMongoAccessor {
return &peerMongoAccessor{}
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (wfa *peerMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *peerMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set.(*Peer), id, wfa, &Peer{})
}
func (wfa *peerMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data.(*Peer), wfa)
}
func (wfa *peerMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *peerMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var peer Peer
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&peer)
return &peer, 200, nil
}
func (wfa peerMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Peer
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}
func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Peer
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -0,0 +1,131 @@
package resource_model
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
)
/*
* AbstractResource is a struct that represents a resource
* it defines the resource data
*/
type AbstractResource struct {
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"` // ShortDescription is the short description of the resource
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"` // Logo is the logo of the resource
Owner string `json:"owner,omitempty" bson:"owner,omitempty" validate:"required"` // Owner is the owner of the resource
OwnerLogo string `json:"owner_logo,omitempty" bson:"owner_logo,omitempty"` // OwnerLogo is the owner logo of the resource
SourceUrl string `json:"source_url,omitempty" bson:"source_url,omitempty" validate:"required"` // SourceUrl is the source URL of the resource
PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"` // PeerID is the ID of the peer getting this resource
Price string `json:"price,omitempty" bson:"price,omitempty"` // Price is the price of access to the resource
License string `json:"license,omitempty" bson:"license,omitempty"` // License is the license of the resource
ResourceModel *ResourceModel `json:"resource_model,omitempty" bson:"resource_model,omitempty"` // ResourceModel is the model of the resource
}
/*
* GetModelValue returns the value of the model key
*/
func (abs *AbstractResource) GetModelValue(key string) interface{} {
if abs.ResourceModel == nil || abs.ResourceModel.Model == nil {
return nil
}
if _, ok := abs.ResourceModel.Model[key]; !ok {
return nil
}
return abs.ResourceModel.Model[key].Value
}
/*
* GetModelType returns the type of the model key
*/
func (abs *AbstractResource) GetModelType(key string) interface{} {
if abs.ResourceModel == nil || abs.ResourceModel.Model == nil {
return nil
}
if _, ok := abs.ResourceModel.Model[key]; !ok {
return nil
}
return abs.ResourceModel.Model[key].Type
}
/*
* GetModelKeys returns the keys of the model
*/
func (abs *AbstractResource) GetModelKeys() []string {
keys := make([]string, 0)
for k := range abs.ResourceModel.Model {
keys = append(keys, k)
}
return keys
}
/*
* GetModelReadOnly returns the readonly of the model key
*/
func (abs *AbstractResource) GetModelReadOnly(key string) interface{} {
if abs.ResourceModel == nil || abs.ResourceModel.Model == nil {
return nil
}
if _, ok := abs.ResourceModel.Model[key]; !ok {
return nil
}
return abs.ResourceModel.Model[key].ReadOnly
}
type Model struct {
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the model
Value interface{} `json:"value,omitempty" bson:"value,omitempty"` // Value is the value of the model
ReadOnly bool `json:"readonly,omitempty" bson:"readonly,omitempty"` // ReadOnly is the readonly of the model
}
/*
* ResourceModel is a struct that represents a resource model
* it defines the resource metadata and specificity
* Warning: This struct is not user available, it is only used by the system
*/
type ResourceModel struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
ResourceType string `json:"resource_type,omitempty" bson:"resource_type,omitempty" validate:"required"`
Model map[string]Model `json:"model,omitempty" bson:"model,omitempty"`
}
func (ao *ResourceModel) GetID() string {
return ao.UUID
}
func (r *ResourceModel) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *ResourceModel) GetName() string {
return d.UUID
}
func (d *ResourceModel) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := &ResourceModelMongoAccessor{}
data.Init(utils.RESOURCE_MODEL, caller)
return data
}
func (dma *ResourceModel) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *ResourceModel) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,83 @@
package resource_model
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type ResourceModelMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (wfa *ResourceModelMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *ResourceModelMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &ResourceModel{})
}
func (wfa *ResourceModelMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *ResourceModelMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *ResourceModelMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow ResourceModel
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa ResourceModelMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []ResourceModel
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}
func (wfa *ResourceModelMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"resource_type": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []ResourceModel
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -0,0 +1,45 @@
package data
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
/*
* DataResource is a struct that represents a data resource
* it defines the resource data
*/
type DataResource struct {
resource_model.AbstractResource // AbstractResource contains the basic fields of an object (id, name)
Protocols []string `json:"protocol,omitempty" bson:"protocol,omitempty"` //TODO Enum type
DataType string `json:"datatype,omitempty" bson:"datatype,omitempty"` // DataType is the type of the data
Example string `json:"example,omitempty" bson:"example,omitempty" description:"base64 encoded data"` // Example is an example of the data
}
func (dma *DataResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *DataResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *DataResource) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.DATA_RESOURCE, caller) // Initialize the accessor with the DATA_RESOURCE model type
return data
}

View File

@ -0,0 +1,110 @@
package data
import (
"cloud.o-forge.io/core/oc-lib/dbs"
mongo "cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type dataMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the dataMongoAccessor
func New() *dataMongoAccessor {
return &dataMongoAccessor{}
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (dma *dataMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return dma.GenericDeleteOne(id, dma)
}
func (dma *dataMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*DataResource).ResourceModel = nil
return dma.GenericUpdateOne(set, id, dma, &DataResource{})
}
func (dma *dataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*DataResource).ResourceModel = nil
return dma.GenericStoreOne(data, dma)
}
func (dma *dataMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return dma.GenericStoreOne(data, dma)
}
func (dma *dataMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var data DataResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, dma.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&data)
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, dma.GetType())
if err == nil && len(resources) > 0 {
data.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &data, 200, nil
}
func (wfa dataMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []DataResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}
func (wfa *dataMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []DataResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}

View File

@ -0,0 +1,45 @@
package data
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneData(t *testing.T) {
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dma := New()
id, _, _ := dma.StoreOne(&d)
assert.NotEmpty(t, id)
}
func TestLoadOneDate(t *testing.T) {
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dma := New()
new_d, _, _ := dma.StoreOne(&d)
assert.Equal(t, d, new_d)
}

View File

@ -0,0 +1,66 @@
package datacenter
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
/*
* DatacenterResource is a struct that represents a datacenter resource
* it defines the resource datacenter
*/
type DatacenterResource struct {
resource_model.AbstractResource
CPUs []*CPU `bson:"cpus,omitempty" json:"cpus,omitempty"` // CPUs is the list of CPUs
RAM *RAM `bson:"ram,omitempty" json:"ram,omitempty"` // RAM is the RAM
GPUs []*GPU `bson:"gpus,omitempty" json:"gpus,omitempty"` // GPUs is the list of GPUs
}
func (dma *DatacenterResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *DatacenterResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *DatacenterResource) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New()
data.Init(utils.DATACENTER_RESOURCE, caller)
return data
}
// CPU is a struct that represents a CPU
type CPU struct {
Cores uint `bson:"cores,omitempty" json:"cores,omitempty"` //TODO: validate
Architecture string `bson:"architecture,omitempty" json:"architecture,omitempty"` //TOOD: enum
Shared bool `bson:"shared,omitempty" json:"shared,omitempty"`
MinimumMemory uint `bson:"minimum_memory,omitempty" json:"minimum_memory,omitempty"`
Platform string `bson:"platform,omitempty" json:"platform,omitempty"`
}
type RAM struct {
Size uint `bson:"size,omitempty" json:"size,omitempty" description:"Units in MB"`
Ecc bool `bson:"ecc,omitempty" json:"ecc,omitempty"`
}
type GPU struct {
CudaCores uint `bson:"cuda_cores,omitempty" json:"cuda_cores,omitempty"`
Model string `bson:"model,omitempty" json:"model,omitempty"`
Memory uint `bson:"memory,omitempty" json:"memory,omitempty" description:"Units in MB"`
TensorCores uint `bson:"tensor_cores,omitempty" json:"tensor_cores,omitempty"`
}

View File

@ -0,0 +1,112 @@
package datacenter
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type datacenterMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the datacenterMongoAccessor
func New() *datacenterMongoAccessor {
return &datacenterMongoAccessor{}
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (dca *datacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return dca.GenericDeleteOne(id, dca)
}
func (dca *datacenterMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*DatacenterResource).ResourceModel = nil
return dca.GenericUpdateOne(set, id, dca, &DatacenterResource{})
}
func (dca *datacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*DatacenterResource).ResourceModel = nil
return dca.GenericStoreOne(data, dca)
}
func (dca *datacenterMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return dca.GenericStoreOne(data, dca)
}
func (dca *datacenterMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var datacenter DatacenterResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, dca.GetType())
if err != nil {
dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&datacenter)
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, dca.GetType())
if err == nil && len(resources) > 0 {
datacenter.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &datacenter, 200, nil
}
func (wfa datacenterMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []DatacenterResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}
func (wfa *datacenterMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []DatacenterResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}

View File

@ -0,0 +1,46 @@
package datacenter
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneDatacenter(t *testing.T) {
dc := DatacenterResource{
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dcma := New()
id, _, _ := dcma.StoreOne(&dc)
assert.NotEmpty(t, id)
}
func TestLoadOneDatacenter(t *testing.T) {
dc := DatacenterResource{
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dcma := New()
new_dc, _, _ := dcma.StoreOne(&dc)
assert.Equal(t, dc, new_dc)
}

View File

@ -0,0 +1,50 @@
package processing
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
/*
* ProcessingResource is a struct that represents a processing resource
* it defines the resource processing
*/
type ProcessingResource struct {
resource_model.AbstractResource
CPUs []*datacenter.CPU `bson:"cpus,omitempty" json:"cp_us,omitempty"` // CPUs is the list of CPUs
GPUs []*datacenter.GPU `bson:"gpus,omitempty" json:"gp_us,omitempty"` // GPUs is the list of GPUs
RAM *datacenter.RAM `bson:"ram,omitempty" json:"ram,omitempty"` // RAM is the RAM
Storage uint `bson:"storage,omitempty" json:"storage,omitempty"` // Storage is the storage
Parallel bool `bson:"parallel,omitempty" json:"parallel,omitempty"` // Parallel is a flag that indicates if the processing is parallel
ScalingModel uint `bson:"scaling_model,omitempty" json:"scaling_model,omitempty"` // ScalingModel is the scaling model
DiskIO string `bson:"disk_io,omitempty" json:"disk_io,omitempty"` // DiskIO is the disk IO
}
func (dma *ProcessingResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *ProcessingResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *ProcessingResource) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.PROCESSING_RESOURCE, caller) // Initialize the accessor with the PROCESSING_RESOURCE model type
return data
}

View File

@ -0,0 +1,114 @@
package processing
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type processingMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the processingMongoAccessor
func New() *processingMongoAccessor {
return &processingMongoAccessor{}
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (pma *processingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return pma.GenericDeleteOne(id, pma)
}
func (pma *processingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*ProcessingResource).ResourceModel = nil
return pma.GenericUpdateOne(set, id, pma, &ProcessingResource{})
}
func (pma *processingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*ProcessingResource).ResourceModel = nil
return pma.GenericStoreOne(data, pma)
}
func (pma *processingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return pma.GenericStoreOne(data, pma)
}
func (pma *processingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var processing ProcessingResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, pma.GetType())
if err != nil {
pma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&processing)
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, pma.GetType())
if err == nil && len(resources) > 0 {
processing.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &processing, 200, nil
}
func (wfa processingMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []ProcessingResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}
// Search searches for processing resources in the database, given some filters OR a search string
func (wfa *processingMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []ProcessingResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}

View File

@ -0,0 +1,38 @@
package processing
/*
func TestStoreOneProcessing(t *testing.T) {
p := ProcessingResource{Container: "totoCont",
AbstractResource: resources.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := ProcessingMongoAccessor{}
id, _, _ := sma.StoreOne(&p)
assert.NotEmpty(t, id)
}
func TestLoadOneProcessing(t *testing.T) {
p := ProcessingResource{Container: "totoCont",
AbstractResource: resources.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := ProcessingMongoAccessor{}
new_s, _, _ := sma.StoreOne(&p)
assert.Equal(t, p, new_s)
}
*/

View File

@ -0,0 +1,58 @@
package resources
import (
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
)
// AbstractResource is the struct containing all of the attributes commons to all ressources
// Resource is the interface to be implemented by all classes inheriting from Resource to have the same behavior
// http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
type ResourceSet struct {
Datas []string `bson:"datas,omitempty" json:"datas,omitempty"`
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
Processings []string `bson:"processings,omitempty" json:"processings,omitempty"`
Datacenters []string `bson:"datacenters,omitempty" json:"datacenters,omitempty"`
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
DataResources []*data.DataResource `bson:"-" json:"data_resources,omitempty"`
StorageResources []*storage.StorageResource `bson:"-" json:"storage_resources,omitempty"`
ProcessingResources []*processing.ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
DatacenterResources []*datacenter.DatacenterResource `bson:"-" json:"datacenter_resources,omitempty"`
WorkflowResources []*w.WorkflowResource `bson:"-" json:"workflow_resources,omitempty"`
}
type ItemResource struct {
Data *data.DataResource `bson:"data,omitempty" json:"data,omitempty"`
Processing *processing.ProcessingResource `bson:"processing,omitempty" json:"processing,omitempty"`
Storage *storage.StorageResource `bson:"storage,omitempty" json:"storage,omitempty"`
Datacenter *datacenter.DatacenterResource `bson:"datacenter,omitempty" json:"datacenter,omitempty"`
Workflow *w.WorkflowResource `bson:"workflow,omitempty" json:"workflow,omitempty"`
}
func (i *ItemResource) GetAbstractRessource() *resource_model.AbstractResource {
if(i.Data != nil){
return &i.Data.AbstractResource
}
if(i.Processing != nil){
return &i.Processing.AbstractResource
}
if(i.Storage != nil){
return &i.Storage.AbstractResource
}
if(i.Datacenter != nil){
return &i.Datacenter.AbstractResource
}
if(i.Workflow != nil){
return &i.Workflow.AbstractResource
}
return nil
}

View File

@ -0,0 +1,55 @@
package storage
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
type URL struct {
Protocol string `bson:"protocol,omitempty" json:"protocol,omitempty"` // Protocol is the protocol of the URL
Path string `bson:"path,omitempty" json:"path,omitempty"` // Path is the path of the URL
}
/*
* StorageResource is a struct that represents a storage resource
* it defines the resource storage
*/
type StorageResource struct {
resource_model.AbstractResource // AbstractResource contains the basic fields of an object (id, name)
Acronym string `bson:"acronym,omitempty" json:"acronym,omitempty"` // Acronym is the acronym of the storage
Type string `bson:"type,omitempty" json:"type,omitempty"` // Type is the type of the storage
Size uint `bson:"size,omitempty" json:"size,omitempty"` // Size is the size of the storage
Url *URL `bson:"url,omitempty" json:"url,omitempty"` // Will allow to select between several protocols
Encryption bool `bson:"encryption,omitempty" json:"encryption,omitempty"` // Encryption is a flag that indicates if the storage is encrypted
Redundancy string `bson:"redundancy,omitempty" json:"redundancy,omitempty"` // Redundancy is the redundancy of the storage
Throughput string `bson:"throughput,omitempty" json:"throughput,omitempty"` // Throughput is the throughput of the storage
}
func (dma *StorageResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *StorageResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *StorageResource) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.STORAGE_RESOURCE, caller) // Initialize the accessor with the STORAGE_RESOURCE model type
return data
}

View File

@ -0,0 +1,114 @@
package storage
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type storageMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the storageMongoAccessor
func New() *storageMongoAccessor {
return &storageMongoAccessor{}
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (sma *storageMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return sma.GenericDeleteOne(id, sma)
}
func (sma *storageMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*StorageResource).ResourceModel = nil
return sma.GenericUpdateOne(set, id, sma, &StorageResource{})
}
func (sma *storageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*StorageResource).ResourceModel = nil
return sma.GenericStoreOne(data, sma)
}
func (sma *storageMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return sma.GenericStoreOne(data, sma)
}
func (sma *storageMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var storage StorageResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, sma.GetType())
if err != nil {
sma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&storage)
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, sma.GetType())
if err == nil && len(resources) > 0 {
storage.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &storage, 200, nil
}
func (wfa storageMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []StorageResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}
// Search searches for storage resources in the database, given some filters OR a search string
func (wfa *storageMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []StorageResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource) // only get the abstract resource !
}
return objs, 200, nil
}

View File

@ -0,0 +1,46 @@
package storage
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneStorage(t *testing.T) {
s := StorageResource{Size: 123, Url: &URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := New()
id, _, _ := sma.StoreOne(&s)
assert.NotEmpty(t, id)
}
func TestLoadOneStorage(t *testing.T) {
s := StorageResource{Size: 123, Url: &URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := New()
new_s, _, _ := sma.StoreOne(&s)
assert.Equal(t, s, new_s)
}

View File

@ -0,0 +1,49 @@
package graph
import "cloud.o-forge.io/core/oc-lib/models/resources"
// Graph is a struct that represents a graph
type Graph struct {
Zoom float64 `bson:"zoom" json:"zoom" default:"1"` // Zoom is the graphical zoom of the graph
Items map[string]GraphItem `bson:"items" json:"items" default:"{}" validate:"required"` // Items is the list of elements in the graph
Links []GraphLink `bson:"links" json:"links" default:"{}" validate:"required"` // Links is the list of links between elements in the graph
}
// GraphItem is a struct that represents an item in a graph
type GraphItem struct {
ID string `bson:"id" json:"id" validate:"required"` // ID is the unique identifier of the item
Width float64 `bson:"width" json:"width" validate:"required"` // Width is the graphical width of the item
Height float64 `bson:"height" json:"height" validate:"required"` // Height is the graphical height of the item
Position Position `bson:"position" json:"position" validate:"required"` // Position is the graphical position of the item
*resources.ItemResource // ItemResource is the resource of the item affected to the item
}
// GraphLink is a struct that represents a link between two items in a graph
type GraphLink struct {
Source Position `bson:"source" json:"source" validate:"required"` // Source is the source graphical position of the link
Destination Position `bson:"destination" json:"destination" validate:"required"` // Destination is the destination graphical position of the link
Style *GraphLinkStyle `bson:"style,omitempty" json:"style,omitempty"` // Style is the graphical style of the link
}
// GraphLinkStyle is a struct that represents the style of a link in a graph
type GraphLinkStyle struct {
Color int64 `bson:"color" json:"color"` // Color is the graphical color of the link (int description of a color, can be transpose as hex)
Stroke float64 `bson:"stroke" json:"stroke"` // Stroke is the graphical stroke of the link
Tension float64 `bson:"tension" json:"tension"` // Tension is the graphical tension of the link
HeadRadius float64 `bson:"head_radius" json:"head_radius"` // graphical pin radius
DashWidth float64 `bson:"dash_width" json:"dash_width"` // DashWidth is the graphical dash width of the link
DashSpace float64 `bson:"dash_space" json:"dash_space"` // DashSpace is the graphical dash space of the link
EndArrow Position `bson:"end_arrow" json:"end_arrow"` // EndArrow is the graphical end arrow of the link
StartArrow Position `bson:"start_arrow" json:"start_arrow"` // StartArrow is the graphical start arrow of the link
ArrowStyle int64 `bson:"arrow_style" json:"arrow_style"` // ArrowStyle is the graphical arrow style of the link (enum foundable in UI)
ArrowDirection int64 `bson:"arrow_direction" json:"arrow_direction"` // ArrowDirection is the graphical arrow direction of the link (enum foundable in UI)
StartArrowWidth float64 `bson:"start_arrow_width" json:"start_arrow_width"` // StartArrowWidth is the graphical start arrow width of the link
EndArrowWidth float64 `bson:"end_arrow_width" json:"end_arrow_width"` // EndArrowWidth is the graphical end arrow width of the link
}
// Position is a struct that represents a graphical position
type Position struct {
ID string `json:"id" bson:"id"` // ID reprents ItemID (optionnal), TODO: rename to ItemID
X float64 `json:"x" bson:"x" validate:"required"` // X is the graphical x position
Y float64 `json:"y" bson:"y" validate:"required"` // Y is the graphical y position
}

View File

@ -0,0 +1,41 @@
package oclib
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
// WorkflowResource is a struct that represents a workflow resource
// it defines the resource workflow
type WorkflowResource struct {
resource_model.AbstractResource
WorkflowID string `bson:"workflow_id,omitempty" json:"workflow_id,omitempty"` // WorkflowID is the ID of the native workflow
}
func (d *WorkflowResource) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.WORKFLOW_RESOURCE, caller) // Initialize the accessor with the WORKFLOW_RESOURCE model type
return data
}
func (dma *WorkflowResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *WorkflowResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,113 @@
package oclib
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type workflowResourceMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
func New() *workflowResourceMongoAccessor {
return &workflowResourceMongoAccessor{}
}
func (wfa *workflowResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *workflowResourceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*WorkflowResource).ResourceModel = nil
return wfa.GenericUpdateOne(set, id, wfa, &WorkflowResource{})
}
func (wfa *workflowResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*WorkflowResource).ResourceModel = nil
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workflowResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
res, _, _ := wfa.LoadOne(data.GetID())
data.(*WorkflowResource).WorkflowID = data.GetID()
if res == nil {
return wfa.GenericStoreOne(data, wfa)
} else {
data.(*WorkflowResource).UUID = res.GetID()
return wfa.GenericUpdateOne(data, res.GetID(), wfa, &WorkflowResource{})
}
}
func (wfa *workflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow WorkflowResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
if err == nil && len(resources) > 0 {
workflow.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &workflow, 200, nil
}
func (wfa workflowResourceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}
// Search searches for workflow resources in the database, given some filters OR a search string
func (wfa *workflowResourceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@ -0,0 +1,43 @@
package oclib
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneWorkflow(t *testing.T) {
w := WorkflowResource{AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
wma := New()
id, _, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
}
func TestLoadOneWorkflow(t *testing.T) {
w := WorkflowResource{AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
wma := New()
new_w, _, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}

154
models/utils/abstracts.go Normal file
View File

@ -0,0 +1,154 @@
package utils
import (
"encoding/json"
"errors"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
// single instance of the validator used in every model Struct to validate the fields
var validate = validator.New(validator.WithRequiredStructEnabled())
/*
* AbstractObject is a struct that represents the basic fields of an object
* it defines the object id and name
* every data in base root model should inherit from this struct (only exception is the ResourceModel)
*/
type AbstractObject struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
}
// GetID returns the id of the object (abstract)
func (ao *AbstractObject) GetID() string {
return ao.UUID
}
// GetName returns the name of the object (abstract)
func (ao *AbstractObject) GetName() string {
return ao.Name
}
// GetAccessor returns the accessor of the object (abstract)
func (dma *AbstractObject) GetAccessor(caller *tools.HTTPCaller) Accessor {
return nil
}
func (dma *AbstractObject) Deserialize(j map[string]interface{}) DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *AbstractObject) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (r *AbstractObject) GenerateID() {
if r.UUID == "" {
r.UUID = uuid.New().String()
}
}
type AbstractAccessor struct {
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
Type string // Type is the data type of the accessor
Caller *tools.HTTPCaller // Caller is the http caller of the accessor (optionnal) only need in a peer connection
}
func (dma *AbstractAccessor) GetType() string {
return dma.Type
}
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
return dma.Caller
}
// Init initializes the accessor with the data type and the http caller
func (dma *AbstractAccessor) Init(t DataType, caller *tools.HTTPCaller) {
dma.Logger = logs.CreateLogger(t.String()) // Create a logger with the data type
dma.Caller = caller // Set the caller
dma.Type = t.String() // Set the data type
}
// GenericLoadOne loads one object from the database (generic)
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
data.GenerateID()
f := dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{
Operator: dbs.LIKE.String(),
Value: data.GetName(),
}},
"abstractobject.name": {{
Operator: dbs.LIKE.String(),
Value: data.GetName(),
}},
},
}
if cursor, _, _ := accessor.Search(&f, ""); len(cursor) > 0 {
return nil, 409, errors.New(accessor.GetType() + " with name " + data.GetName() + " already exists")
}
err := validate.Struct(data)
if err != nil {
return nil, 422, err
}
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, code, err
}
return accessor.LoadOne(id)
}
// GenericLoadOne loads one object from the database (generic)
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
res, code, err := accessor.LoadOne(id)
if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
return nil, code, err
}
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
return nil, code, err
}
return res, 200, nil
}
// GenericLoadOne loads one object from the database (generic)
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor Accessor, new DBObject) (DBObject, int, error) {
r, c, err := accessor.LoadOne(id)
if err != nil {
return nil, c, err
}
change := set.Serialize() // get the changes
loaded := r.Serialize() // get the loaded object
for k, v := range change { // apply the changes, with a flatten method
loaded[k] = v
}
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded), id, accessor.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
return nil, code, err
}
return accessor.LoadOne(id)
}

8
models/utils/common.go Normal file
View File

@ -0,0 +1,8 @@
package utils
/*
type Price struct {
Price float64 `json:"price,omitempty" bson:"price,omitempty"`
Currency string `json:"currency,omitempty" bson:"currency,omitempty"`
}
*/

74
models/utils/enums.go Normal file
View File

@ -0,0 +1,74 @@
package utils
type DataType int
// DataType - Enum for the different types of resources in db accessible from the outside
const (
INVALID DataType = iota
DATA_RESOURCE
PROCESSING_RESOURCE
STORAGE_RESOURCE
DATACENTER_RESOURCE
WORKFLOW_RESOURCE
WORKFLOW
WORKFLOW_EXECUTION
WORKSPACE
RESOURCE_MODEL
PEER
SHARED_WORKSPACE
RULE
BOOKING
)
// Bind the standard API name to the data type
var DefaultAPI = [...]string{
"",
"oc-catalog",
"oc-catalog",
"oc-catalog",
"oc-catalog",
"oc-catalog",
"oc-workflow",
"",
"oc-workspace",
"",
"oc-peers",
"oc-shared",
"oc-shared",
"oc-datacenter",
}
// Bind the standard data name to the data type
var Str = [...]string{
"invalid",
"data_resource",
"processing_resource",
"storage_resource",
"datacenter_resource",
"workflow_resource",
"workflow",
"workflow_execution",
"workspace",
"resource_model",
"peer",
"shared_workspace",
"rule",
"booking",
}
func FromInt(i int) string {
return Str[i]
}
func (d DataType) API() string { // API - Returns the API name of the data type
return DefaultAPI[d]
}
func (d DataType) String() string { // String - Returns the string name of the data type
return Str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d DataType) EnumIndex() int {
return int(d)
}

View File

@ -0,0 +1,39 @@
package utils
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/tools"
)
// ShallowDBObject is an interface that defines the basic methods shallowed version of a DBObject
type ShallowDBObject interface {
GenerateID()
GetID() string
GetName() string
Deserialize(j map[string]interface{}) DBObject
Serialize() map[string]interface{}
}
// DBObject is an interface that defines the basic methods for a DBObject
type DBObject interface {
GenerateID()
GetID() string
GetName() string
Deserialize(j map[string]interface{}) DBObject
Serialize() map[string]interface{}
GetAccessor(caller *tools.HTTPCaller) Accessor
}
// Accessor is an interface that defines the basic methods for an Accessor
type Accessor interface {
Init(t DataType, caller *tools.HTTPCaller)
GetType() string
GetCaller() *tools.HTTPCaller
Search(filters *dbs.Filters, search string) ([]ShallowDBObject, int, error)
LoadAll() ([]ShallowDBObject, int, error)
LoadOne(id string) (DBObject, int, error)
DeleteOne(id string) (DBObject, int, error)
CopyOne(data DBObject) (DBObject, int, error)
StoreOne(data DBObject) (DBObject, int, error)
UpdateOne(set DBObject, id string) (DBObject, int, error)
}

108
models/workflow/workflow.go Normal file
View File

@ -0,0 +1,108 @@
package workflow
import (
"encoding/json"
"errors"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
/*
* AbstractWorkflow is a struct that represents a workflow for resource or native workflow
* Warning: there is 2 types of workflows, the resource workflow and the native workflow
* native workflow is the one that you create to schedule an execution
* resource workflow is the one that is created to set our native workflow in catalog
*/
type AbstractWorkflow struct {
resources.ResourceSet
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"` // Graph UI & logic representation of the workflow
ScheduleActive bool `json:"schedule_active" bson:"schedule_active"` // ScheduleActive is a flag that indicates if the schedule is active, if not the workflow is not scheduled and no execution or booking will be set
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"` // Schedule is the schedule of the workflow
Shared []string `json:"shared,omitempty" bson:"shared,omitempty"` // Shared is the ID of the shared workflow
}
// tool function to check if a link is a link between a datacenter and a resource
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) (bool, string) {
if w.Graph == nil || w.Graph.Items == nil {
return false, ""
}
if d, ok := w.Graph.Items[link.Source.ID]; ok && d.Datacenter != nil {
return true, d.Datacenter.UUID
}
if d, ok := w.Graph.Items[link.Destination.ID]; ok && d.Datacenter != nil {
return true, d.Datacenter.UUID
}
return false, ""
}
/*
* Workflow is a struct that represents a workflow
* it defines the native workflow
*/
type Workflow struct {
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
AbstractWorkflow // AbstractWorkflow contains the basic fields of a workflow
}
/*
* CheckBooking is a function that checks the booking of the workflow on peers (even ourselves)
*/
func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
// check if
if wfa.Graph == nil { // no graph no booking
return false, nil
}
accessor := (&datacenter.DatacenterResource{}).GetAccessor(nil)
for _, link := range wfa.Graph.Links {
if ok, dc_id := wfa.isDCLink(link); ok { // check if the link is a link between a datacenter and a resource
dc, code, _ := accessor.LoadOne(dc_id)
if code != 200 {
continue
}
// CHECK BOOKING ON PEER, datacenter could be a remote one
peerID := dc.(*datacenter.DatacenterResource).PeerID
if peerID == "" {
return false, errors.New("no peer id")
} // no peer id no booking, we need to know where to book
_, err := (&peer.Peer{}).LaunchPeerExecution(peerID, dc_id, utils.BOOKING, tools.GET, nil, caller)
if err != nil {
return false, err
}
}
}
return true, nil
}
func (d *Workflow) GetName() string {
return d.Name
}
func (d *Workflow) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.WORKFLOW, caller) // Initialize the accessor with the WORKFLOW model type
return data
}
func (dma *Workflow) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Workflow) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,379 @@
package workflow
import (
"errors"
"fmt"
"strings"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
"cloud.o-forge.io/core/oc-lib/models/workspace"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/shallow_shared_workspace"
"cloud.o-forge.io/core/oc-lib/tools"
cron "github.com/robfig/cron/v3"
)
type workflowMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the workflowMongoAccessor
func New() *workflowMongoAccessor {
return &workflowMongoAccessor{}
}
/*
* THERE IS A LOT IN THIS FILE SHOULD BE AWARE OF THE COMMENTS
*/
/*
* getExecutions is a function that returns the executions of a workflow
* it returns an array of workflow_execution.WorkflowExecution
*/
func (wfa *workflowMongoAccessor) getExecutions(id string, data *Workflow) ([]*workflow_execution.WorkflowExecution, error) {
workflows_execution := []*workflow_execution.WorkflowExecution{}
if data.Schedule != nil { // only set execution on a scheduled workflow
if data.Schedule.Start == nil { // if no start date, return an error
return workflows_execution, errors.New("should get a start date on the scheduler.")
}
if data.Schedule.End != nil && data.Schedule.End.IsZero() { // if end date is zero, set it to nil
data.Schedule.End = nil
}
if len(data.Schedule.Cron) > 0 { // if cron is set then end date should be set
if data.Schedule.End == nil {
return workflows_execution, errors.New("a cron task should have an end date.")
}
cronStr := strings.Split(data.Schedule.Cron, " ") // split the cron string to treat it
if len(cronStr) < 6 { // if the cron string is less than 6 fields, return an error because format is : ss mm hh dd MM dw (6 fields)
return nil, errors.New("Bad cron message: " + data.Schedule.Cron + ". Should be at least ss mm hh dd MM dw")
}
subCron := strings.Join(cronStr[:6], " ")
// cron should be parsed as ss mm hh dd MM dw t (min 6 fields)
specParser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow) // create a new cron parser
sched, err := specParser.Parse(subCron) // parse the cron string
if err != nil {
return workflows_execution, errors.New("Bad cron message: " + err.Error())
}
// loop through the cron schedule to set the executions
for s := sched.Next(*data.Schedule.Start); !s.IsZero() && s.Before(*data.Schedule.End); s = sched.Next(s) {
obj := &workflow_execution.WorkflowExecution{
AbstractObject: utils.AbstractObject{
Name: data.Schedule.Name, // set the name of the execution
},
ExecDate: &s, // set the execution date
EndDate: data.Schedule.End, // set the end date
State: 1, // set the state to 1 (scheduled)
WorkflowID: id, // set the workflow id dependancy of the execution
}
workflows_execution = append(workflows_execution, obj) // append the execution to the array
}
} else { // if no cron, set the execution to the start date
obj := &workflow_execution.WorkflowExecution{ // create a new execution
AbstractObject: utils.AbstractObject{
Name: data.Schedule.Name,
},
ExecDate: data.Schedule.Start,
EndDate: data.Schedule.End,
State: 1,
WorkflowID: id,
}
workflows_execution = append(workflows_execution, obj) // append the execution to the array
}
}
return workflows_execution, nil
}
// DeleteOne deletes a workflow from the database, delete depending executions and bookings
func (wfa *workflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
wfa.execution(id, &Workflow{
AbstractWorkflow: AbstractWorkflow{ScheduleActive: false},
}, true) // delete the executions
res, code, err := wfa.GenericDeleteOne(id, wfa)
if res != nil && code == 200 {
wfa.execute(res.(*Workflow), false) // up to date the workspace for the workflow
}
wfa.share(res.(*Workflow), true, wfa.Caller) // send the deletion to the peers where workflow is shared
return res, code, err
}
/*
* book is a function that books a workflow on the peers
* it takes the workflow id, the real data and the executions
* it returns an error if the booking fails
*/
func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*workflow_execution.WorkflowExecution) error {
if wfa.Caller == nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[utils.BOOKING.String()] == nil {
return errors.New("no caller defined")
}
methods := wfa.Caller.URLS[utils.BOOKING.String()]
if _, ok := methods[tools.POST]; !ok {
return errors.New("no path found")
}
res, code, _ := wfa.LoadOne(id)
if code != 200 {
return errors.New("could not load workflow")
}
r := res.(*Workflow)
g := r.Graph
if realData.Graph != nil { // if the graph is set, set it to the real data
g = realData.Graph
}
if g != nil && g.Links != nil && len(g.Links) > 0 { // if the graph is set and has links then book the workflow (even on ourselves)
accessor := (&datacenter.DatacenterResource{}).GetAccessor(nil)
for _, link := range g.Links {
if ok, dc_id := realData.isDCLink(link); ok { // check if the link is a link between a datacenter and a resource booking is only on datacenter
dc, code, _ := accessor.LoadOne(dc_id)
if code != 200 {
continue
}
// CHECK BOOKING
peerID := dc.(*datacenter.DatacenterResource).PeerID
if peerID == "" { // no peer id no booking
continue
}
// BOOKING ON PEER
_, err := (&peer.Peer{}).LaunchPeerExecution(peerID, "", utils.BOOKING, tools.POST,
(&workflow_execution.WorkflowExecutions{ // it's the standard model for booking see OC-PEER
WorkflowID: id, // set the workflow id "WHO"
ResourceID: dc_id, // set the datacenter id "WHERE"
Executions: execs, // set the executions to book "WHAT"
}).Serialize(), wfa.Caller)
if err != nil {
return err
}
}
}
}
return nil
}
/*
* share is a function that shares a workflow to the peers if the workflow is shared
*/
func (wfa *workflowMongoAccessor) share(realData *Workflow, delete bool, caller *tools.HTTPCaller) {
if realData.Shared == nil || len(realData.Shared) == 0 { // no shared no sharing
return
}
for _, sharedID := range realData.Shared { // loop through the shared ids
access := (&shallow_shared_workspace.ShallowSharedWorkspace{}).GetAccessor(nil)
res, code, _ := access.LoadOne(sharedID)
if code != 200 {
continue
}
var err error
paccess := &peer.Peer{}
for _, p := range res.(*shallow_shared_workspace.ShallowSharedWorkspace).Peers {
paccess.UUID = p
if paccess.IsMySelf() { // if the peer is the current peer, never share because it will create a loop
continue
}
if delete { // if the workflow is deleted, share the deletion
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKFLOW, tools.DELETE, map[string]interface{}{}, caller)
} else { // if the workflow is updated, share the update
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKFLOW, tools.PUT, res.Serialize(), caller)
}
}
if err != nil {
wfa.Logger.Error().Msg(err.Error())
}
}
}
/*
* execution is a create or delete function for the workflow executions depending on the schedule of the workflow
*/
func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delete bool) (int, error) {
var err error
nats := tools.NewNATSCaller() // create a new nats caller because executions are sent to the nats for daemons
if !realData.ScheduleActive { // if the schedule is not active, delete the executions
mongo.MONGOService.DeleteMultiple(map[string]interface{}{
"state": 1, // only delete the scheduled executions only scheduled if executions are in progress or ended, they should not be deleted for registration
"workflow_id": id,
}, utils.WORKFLOW_EXECUTION.String())
err := wfa.book(id, realData, []*workflow_execution.WorkflowExecution{}) // delete the booking of the workflow on the peers
nats.SetNATSPub(utils.WORKFLOW.String(), tools.REMOVE, realData) // send the deletion to the nats
if err != nil {
return 409, err
}
return 200, nil
}
accessor := (&workflow_execution.WorkflowExecution{}).GetAccessor(nil)
execs, err := wfa.getExecutions(id, realData) // get the executions of the workflow
if err != nil {
return 422, err
}
err = wfa.book(id, realData, execs) // book the workflow on the peers
if err != nil {
return 409, err // if the booking fails, return an error for integrity between peers
}
if delete { // if delete is set to true, delete the executions
mongo.MONGOService.DeleteMultiple(map[string]interface{}{
"workflow_id": id,
"state": 1,
}, utils.WORKFLOW_EXECUTION.String())
wfa.book(id, realData, []*workflow_execution.WorkflowExecution{})
nats.SetNATSPub(utils.WORKFLOW.String(), tools.REMOVE, realData)
return 200, nil
}
if len(execs) > 0 { // if the executions are set, store them
for _, obj := range execs {
_, code, err := accessor.StoreOne(obj)
if code != 200 {
return code, err
}
}
nats.SetNATSPub(utils.WORKFLOW.String(), tools.CREATE, realData) // send the creation to the nats
} else {
return 422, err
}
return 200, nil
}
// UpdateOne updates a workflow in the database
func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
res, code, err := wfa.LoadOne(id)
if code != 200 {
return nil, 409, err
}
// avoid the update if the schedule is the same
avoid := set.(*Workflow).Schedule == nil || (res.(*Workflow).Schedule != nil && res.(*Workflow).ScheduleActive == set.(*Workflow).ScheduleActive && res.(*Workflow).Schedule.Start == set.(*Workflow).Schedule.Start && res.(*Workflow).Schedule.End == set.(*Workflow).Schedule.End && res.(*Workflow).Schedule.Cron == set.(*Workflow).Schedule.Cron)
res, code, err = wfa.GenericUpdateOne(set, id, wfa, &Workflow{})
fmt.Println(code, err)
if code != 200 {
return nil, code, err
}
if !avoid { // if the schedule is not avoided, update the executions
if code, err := wfa.execution(id, res.(*Workflow), true); code != 200 {
fmt.Println(code, err)
return nil, code, err
}
}
wfa.execute(res.(*Workflow), false) // update the workspace for the workflow
wfa.share(res.(*Workflow), false, wfa.Caller) // share the update to the peers
return res, code, err
}
// StoreOne stores a workflow in the database
func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
res, code, err := wfa.GenericStoreOne(data, wfa)
if err != nil {
return nil, code, err
}
wfa.share(res.(*Workflow), false, wfa.Caller) // share the creation to the peers
//store the executions
if code, err := wfa.execution(res.GetID(), res.(*Workflow), false); err != nil {
return nil, code, err
}
wfa.execute(res.(*Workflow), false) // store the workspace for the workflow
return res, code, err
}
// CopyOne copies a workflow in the database
func (wfa *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
// execute is a function that executes a workflow
// it stores the workflow resources in a specific workspace to never have a conflict in UI and logic
func (wfa *workflowMongoAccessor) execute(workflow *Workflow, delete bool) {
accessor := (&workspace.Workspace{}).GetAccessor(nil)
filters := &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by standard workspace name attached to a workflow
"abstractobject.name": {{dbs.LIKE.String(), workflow.Name + "_workspace"}},
},
}
resource, _, err := accessor.Search(filters, "")
if delete { // if delete is set to true, delete the workspace
for _, r := range resource {
accessor.DeleteOne(r.GetID())
}
return
}
if err == nil && len(resource) > 0 { // if the workspace already exists, update it
accessor.UpdateOne(&workspace.Workspace{
Active: true,
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
Processings: workflow.Processings,
Storages: workflow.Storages,
Workflows: workflow.Workflows,
Datacenters: workflow.Datacenters,
},
}, resource[0].GetID())
} else { // if the workspace does not exist, create it
accessor.StoreOne(&workspace.Workspace{
Active: true,
AbstractObject: utils.AbstractObject{Name: workflow.Name + "_workspace"},
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
Processings: workflow.Processings,
Storages: workflow.Storages,
Workflows: workflow.Workflows,
Datacenters: workflow.Datacenters,
},
})
}
}
// LoadOne loads a workflow from the database
func (wfa *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Workflow
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
wfa.execute(&workflow, false) // if no workspace is attached to the workflow, create it
return &workflow, 200, nil
}
// LoadAll loads all the workflows from the database
func (wfa workflowMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Workflow
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractObject) // only AbstractObject fields !
}
return objs, 200, nil
}
func (wfa *workflowMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Workflow
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -0,0 +1,23 @@
package workflow
import "time"
// WorkflowSchedule is a struct that contains the scheduling information of a workflow
type ScheduleMode int
const (
TASK ScheduleMode = iota
SERVICE
)
/*
* WorkflowSchedule is a struct that contains the scheduling information of a workflow
* It contains the mode of the schedule (Task or Service), the name of the schedule, the start and end time of the schedule and the cron expression
*/
type WorkflowSchedule struct {
Mode int64 `json:"mode" bson:"mode" validate:"required"` // Mode is the mode of the schedule (Task or Service)
Name string `json:"name" bson:"name" validate:"required"` // Name is the name of the schedule
Start *time.Time `json:"start" bson:"start" validate:"required,ltfield=End"` // Start is the start time of the schedule, is required and must be less than the End time
End *time.Time `json:"end,omitempty" bson:"end,omitempty"` // End is the end time of the schedule
Cron string `json:"cron,omitempty" bson:"cron,omitempty"` // here the cron format : ss mm hh dd MM dw task
}

View File

@ -0,0 +1,29 @@
package workflow
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneWorkflow(t *testing.T) {
w := Workflow{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
}
wma := New()
id, _, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
}
func TestLoadOneWorkflow(t *testing.T) {
w := Workflow{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
}
wma := New()
new_w, _, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}

View File

@ -0,0 +1,141 @@
package workflow_execution
import (
"encoding/json"
"strings"
"time"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
)
// ScheduledType - Enum for the different states of a workflow execution
type ScheduledType int
const (
SCHEDULED ScheduledType = iota + 1
STARTED
FAILURE
SUCCESS
)
var str = [...]string{
"scheduled",
"started",
"failure",
"success",
}
func FromInt(i int) string {
return str[i]
}
func (d ScheduledType) String() string {
return str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d ScheduledType) EnumIndex() int {
return int(d)
}
/*
* WorkflowExecutions is a struct that represents a list of workflow executions
* Warning: No user can write (del, post, put) a workflow execution, it is only used by the system
* workflows generate their own executions
*/
type WorkflowExecutions struct {
WorkflowID string `json:"workflow_id" bson:"workflow_id"`
ResourceID string `json:"resource_id" bson:"resource_id"`
Executions []*WorkflowExecution `json:"executions" bson:"executions"`
}
// New - Creates a new instance of the WorkflowExecutions from a map
func (dma *WorkflowExecutions) Deserialize(j map[string]interface{}) *WorkflowExecutions {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
// Serialize - Returns the WorkflowExecutions as a map
func (dma *WorkflowExecutions) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
/*
* WorkflowExecution is a struct that represents a workflow execution
* Warning: No user can write (del, post, put) a workflow execution, it is only used by the system
* workflows generate their own executions
*/
type WorkflowExecution struct {
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
ExecDate *time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"` // ExecDate is the execution date of the workflow, is required
EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"` // EndDate is the end date of the workflow
State int64 `json:"state,omitempty" bson:"state,omitempty"` // State is the state of the workflow
WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
}
// tool to transform the argo status to a state
func (wfa *WorkflowExecution) ArgoStatusToState(status string) *WorkflowExecution {
status = strings.ToLower(status)
switch status {
case "succeeded": // Succeeded
wfa.State = int64(SUCCESS.EnumIndex())
case "pending": // Pending
wfa.State = int64(SCHEDULED.EnumIndex())
case "running": // Running
wfa.State = int64(STARTED.EnumIndex())
default: // Failed
wfa.State = int64(FAILURE.EnumIndex())
}
return wfa
}
func (ao *WorkflowExecution) GetID() string {
return ao.UUID
}
func (r *WorkflowExecution) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *WorkflowExecution) GetName() string {
return d.UUID + "_" + d.ExecDate.String()
}
func (d *WorkflowExecution) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.WORKFLOW_EXECUTION, caller) // Initialize the accessor with the WORKFLOW_EXECUTION model type
return data
}
// New creates a new instance of the WorkflowExecution from a map
func (dma *WorkflowExecution) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
// Serialize returns the WorkflowExecution as a map
func (dma *WorkflowExecution) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,84 @@
package workflow_execution
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type workflowExecutionMongoAccessor struct {
utils.AbstractAccessor
}
func New() *workflowExecutionMongoAccessor {
return &workflowExecutionMongoAccessor{}
}
func (wfa *workflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *workflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &WorkflowExecution{})
}
func (wfa *workflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow WorkflowExecution
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa workflowExecutionMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowExecution
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractObject)
}
return objs, 200, nil
}
// Search searches for workflow executions in the database, given some filters OR a search string
func (wfa *workflowExecutionMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowExecution
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -0,0 +1,56 @@
package rule
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
)
/*
* Rule is a struct that represents a rule of a shared workspace
*/
type Rule struct {
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the rule
Condition string `json:"condition,omitempty" bson:"condition,omitempty"` // NOT DEFINITIVE TO SPECIFICATION
Actions []string `json:"actions,omitempty" bson:"actions,omitempty"` // NOT DEFINITIVE TO SPECIFICATION
}
func (ao *Rule) GetID() string {
return ao.UUID
}
func (r *Rule) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *Rule) GetName() string {
return d.Name
}
func (d *Rule) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New()
data.Init(utils.RULE, caller)
return data
}
func (dma *Rule) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Rule) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,90 @@
package rule
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type ruleMongoAccessor struct {
utils.AbstractAccessor
}
// New creates a new instance of the ruleMongoAccessor
func New() *ruleMongoAccessor {
return &ruleMongoAccessor{}
}
// GetType returns the type of the rule
func (wfa *ruleMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
// UpdateOne updates a rule in the database
func (wfa *ruleMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set.(*Rule), id, wfa, &Rule{})
}
// StoreOne stores a rule in the database
func (wfa *ruleMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data.(*Rule), wfa)
}
func (wfa *ruleMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
// LoadOne loads a rule from the database
func (wfa *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var rule Rule
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&rule)
return &rule, 200, nil
}
// LoadAll loads all rules from the database
func (wfa ruleMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Rule
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}
// Search searches for rules in the database, given some filters OR a search string
func (wfa *ruleMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Rule
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -0,0 +1,61 @@
package shallow_shared_workspace
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
)
type ShallowSharedWorkspace struct {
utils.AbstractObject
IsSent bool `json:"is_sent" bson:"-"`
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"`
Version string `json:"version,omitempty" bson:"version,omitempty"`
Description string `json:"description,omitempty" bson:"description,omitempty" validate:"required"`
Attributes map[string]interface{} `json:"attributes,omitempty" bson:"attributes,omitempty"`
Workspaces []string `json:"workspaces,omitempty" bson:"workspaces,omitempty"`
Workflows []string `json:"workflows,omitempty" bson:"workflows,omitempty"`
Peers []string `json:"peers,omitempty" bson:"peers,omitempty"`
Rules []string `json:"rules,omitempty" bson:"rules,omitempty"`
}
func (ao *ShallowSharedWorkspace) GetID() string {
return ao.UUID
}
func (r *ShallowSharedWorkspace) GenerateID() {
if r.UUID == "" {
r.UUID = uuid.New().String()
}
}
func (d *ShallowSharedWorkspace) GetName() string {
return d.Name
}
func (d *ShallowSharedWorkspace) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New()
data.Init(utils.SHARED_WORKSPACE, caller)
return data
}
func (dma *ShallowSharedWorkspace) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *ShallowSharedWorkspace) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,83 @@
package shallow_shared_workspace
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type shallowSharedWorkspaceMongoAccessor struct {
utils.AbstractAccessor
}
func New() *shallowSharedWorkspaceMongoAccessor {
return &shallowSharedWorkspaceMongoAccessor{}
}
func (wfa *shallowSharedWorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *shallowSharedWorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set.(*ShallowSharedWorkspace), id, wfa, &ShallowSharedWorkspace{})
}
func (wfa *shallowSharedWorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data.(*ShallowSharedWorkspace), wfa)
}
func (wfa *shallowSharedWorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.StoreOne(data)
}
func (wfa *shallowSharedWorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var sharedWorkspace ShallowSharedWorkspace
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&sharedWorkspace)
return &sharedWorkspace, 200, nil
}
func (wfa shallowSharedWorkspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []ShallowSharedWorkspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}
func (wfa *shallowSharedWorkspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []ShallowSharedWorkspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -0,0 +1,74 @@
package shared_workspace
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/utils"
w "cloud.o-forge.io/core/oc-lib/models/workflow"
"cloud.o-forge.io/core/oc-lib/models/workspace"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
)
// SharedWorkspace is a struct that represents a shared workspace
// WARNING : it got a shallow object version, this one is the full version
// full version is the one used by API
// other one is a shallow version used by the Lib for import cycle problem purposes
type SharedWorkspace struct {
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
IsSent bool `json:"is_sent" bson:"-"` // IsSent is a flag that indicates if the workspace is sent
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"` // CreatorID is the ID of the creator
Version string `json:"version,omitempty" bson:"version,omitempty"` // Version is the version of the workspace
Description string `json:"description,omitempty" bson:"description,omitempty" validate:"required"` // Description is the description of the workspace
Attributes map[string]interface{} `json:"attributes,omitempty" bson:"attributes,omitempty"` // Attributes is the attributes of the workspace (TODO)
Workspaces []string `json:"workspaces,omitempty" bson:"workspaces,omitempty"` // Workspaces is the workspaces of the workspace
Workflows []string `json:"workflows,omitempty" bson:"workflows,omitempty"` // Workflows is the workflows of the workspace
Peers []string `json:"peers,omitempty" bson:"peers,omitempty"` // Peers is the peers of the workspace
Rules []string `json:"rules,omitempty" bson:"rules,omitempty"` // Rules is the rules of the workspace
SharedRules []*rule.Rule `json:"shared_rules,omitempty" bson:"-"` // SharedRules is the shared rules of the workspace
SharedWorkspaces []*workspace.Workspace `json:"shared_workspaces,omitempty" bson:"-"` // SharedWorkspaces is the shared workspaces of the workspace
SharedWorkflows []*w.Workflow `json:"shared_workflows,omitempty" bson:"-"` // SharedWorkflows is the shared workflows of the workspace
SharedPeers []*peer.Peer `json:"shared_peers,omitempty" bson:"-"` // SharedPeers is the shared peers of the workspace
}
func (ao *SharedWorkspace) GetID() string {
return ao.UUID
}
func (r *SharedWorkspace) GenerateID() {
if r.UUID == "" {
r.UUID = uuid.New().String()
}
}
func (d *SharedWorkspace) GetName() string {
return d.Name
}
func (d *SharedWorkspace) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.SHARED_WORKSPACE, caller) // Initialize the accessor with the SHARED_WORKSPACE model type
return data
}
func (dma *SharedWorkspace) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *SharedWorkspace) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,330 @@
package shared_workspace
import (
"fmt"
"slices"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/utils"
w "cloud.o-forge.io/core/oc-lib/models/workflow"
"cloud.o-forge.io/core/oc-lib/models/workspace"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
"cloud.o-forge.io/core/oc-lib/static"
"cloud.o-forge.io/core/oc-lib/tools"
)
// SharedWorkspace is a struct that represents a shared workspace
type sharedWorkspaceMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the sharedWorkspaceMongoAccessor
func New() *sharedWorkspaceMongoAccessor {
return &sharedWorkspaceMongoAccessor{}
}
// DeleteOne deletes a shared workspace from the database, given its ID, it automatically share to peers if the workspace is shared
func (wfa *sharedWorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
set, code, _ := wfa.LoadOne(id)
if code == 200 { // always delete on peers than recreate
wfa.deleteToPeer(set.(*SharedWorkspace))
}
wfa.sharedWorkflow(&SharedWorkspace{}, id) // create all shared workflows
wfa.sharedWorkspace(&SharedWorkspace{}, id) // create all shared workspaces
return wfa.GenericDeleteOne(id, wfa) // then add on yours
}
/*
sharedWorkspace is a function that shares the shared workspace to the peers
*/
func (wfa *sharedWorkspaceMongoAccessor) sharedWorkspace(shared *SharedWorkspace, id string) {
eldest, code, _ := wfa.LoadOne(id) // get the eldest
accessor := (&workspace.Workspace{}).GetAccessor(nil)
if code == 200 {
eld := eldest.(*SharedWorkspace)
if eld.Workspaces != nil { // update all your workspaces in the eldest by replacing shared ref by an empty string
for _, v := range eld.Workspaces {
accessor.UpdateOne(&workspace.Workspace{Shared: ""}, v)
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[utils.WORKSPACE.String()] == nil {
continue
}
paccess := (&peer.Peer{}) // send to all peers
for _, p := range shared.Peers { // delete the shared workspace on the peer
b, err := paccess.LaunchPeerExecution(p, v, utils.WORKSPACE, tools.DELETE, nil, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
}
}
}
}
}
if shared.Workspaces != nil {
for _, v := range shared.Workspaces { // update all the shared workspaces
workspace, code, _ := accessor.UpdateOne(&workspace.Workspace{Shared: shared.UUID}, v) // add the shared ref to workspace
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[utils.WORKSPACE.String()] == nil {
continue
}
for _, p := range shared.Peers {
if code != 200 {
continue
}
paccess := (&peer.Peer{}) // send to all peers, add the shared workspace on the peer
s := workspace.Serialize()
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + p
b, err := paccess.LaunchPeerExecution(p, v, utils.WORKSPACE, tools.POST, s, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
}
}
}
}
// deleting on peers before adding, to avoid conflicts on peers side
// because you have no reference to the remote shared workspace
}
// sharedWorkflow is a function that shares the shared workflow to the peers
func (wfa *sharedWorkspaceMongoAccessor) sharedWorkflow(shared *SharedWorkspace, id string) {
accessor := (&w.Workflow{}).GetAccessor(nil)
eldest, code, _ := wfa.LoadOne(id) // get the eldest
if code == 200 {
eld := eldest.(*SharedWorkspace)
if eld.Workflows != nil {
for _, v := range eld.Workflows {
data, code, _ := accessor.LoadOne(v)
if code == 200 {
s := data.(*w.Workflow)
new := []string{}
for _, id2 := range s.Shared {
if id2 != id {
new = append(new, id2)
}
} // kick the shared reference in your old shared workflow
n := &w.Workflow{}
n.Shared = new
accessor.UpdateOne(n, v)
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[utils.WORKFLOW.String()] == nil {
continue
}
paccess := (&peer.Peer{}) // send to all peers
for _, p := range shared.Peers { // delete the shared workflow on the peer
b, err := paccess.LaunchPeerExecution(p, v, utils.WORKFLOW, tools.DELETE, nil, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
}
}
}
}
}
}
if shared.Workflows != nil { // update all the shared workflows
for _, v := range shared.Workflows {
data, code, _ := accessor.LoadOne(v)
if code == 200 {
s := data.(*w.Workflow)
if !slices.Contains(s.Shared, id) {
s.Shared = append(s.Shared, id)
workflow, code, _ := accessor.UpdateOne(s, v)
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[utils.WORKFLOW.String()] == nil {
continue
}
paccess := (&peer.Peer{})
for _, p := range shared.Peers { // send to all peers
if code == 200 {
s := workflow.Serialize() // add the shared workflow on the peer
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + p
b, err := paccess.LaunchPeerExecution(p, shared.UUID, utils.WORKFLOW, tools.POST, s, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
}
}
}
}
}
}
}
// deleting on peers before adding, to avoid conflicts on peers side
// because you have no reference to the remote shared workflow
}
// sharedWorkspace is a function that shares the shared workspace to the peers
func (wfa *sharedWorkspaceMongoAccessor) deleteToPeer(shared *SharedWorkspace) {
if wfa.Caller == nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[utils.SHARED_WORKSPACE.String()] == nil {
return
}
paccess := (&peer.Peer{})
for _, v := range shared.Peers {
if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() {
continue
}
b, err := paccess.LaunchPeerExecution(v, shared.UUID, utils.SHARED_WORKSPACE, tools.DELETE, nil, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + v + ". Error: " + err.Error())
}
}
}
// sharedWorkspace is a function that shares the shared workspace to the peers
func (wfa *sharedWorkspaceMongoAccessor) sendToPeer(shared *SharedWorkspace) {
if wfa.Caller == nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[utils.SHARED_WORKSPACE.String()] == nil {
return
}
paccess := (&peer.Peer{})
for _, v := range shared.Peers {
if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() || shared.IsSent {
continue
}
shared.IsSent = true
b, err := paccess.LaunchPeerExecution(v, v, utils.SHARED_WORKSPACE, tools.POST, shared.Serialize(), wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + v + ". Error: " + err.Error())
}
}
}
// UpdateOne updates a shared workspace in the database, given its ID and the new data, it automatically share to peers if the workspace is shared
func (wfa *sharedWorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
res, code, err := wfa.GenericUpdateOne(set.(*SharedWorkspace), id, wfa, &SharedWorkspace{})
wfa.deleteToPeer(res.(*SharedWorkspace)) // delete the shared workspace on the peer
wfa.sharedWorkflow(res.(*SharedWorkspace), id) // replace all shared workflows
wfa.sharedWorkspace(res.(*SharedWorkspace), id) // replace all shared workspaces (not shared worspace obj but workspace one)
wfa.sendToPeer(res.(*SharedWorkspace)) // send the shared workspace (shared workspace object) to the peers
return res, code, err
}
// StoreOne stores a shared workspace in the database, it automatically share to peers if the workspace is shared
func (wfa *sharedWorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
id, _ := static.GetMyLocalJsonPeer() // get the local peer
data.(*SharedWorkspace).CreatorID = id // set the creator id
data.(*SharedWorkspace).Peers = append(data.(*SharedWorkspace).Peers, id) // add the creator id to the peers
// then reset the shared fields
if data.(*SharedWorkspace).Workspaces == nil {
data.(*SharedWorkspace).Workspaces = []string{}
}
if data.(*SharedWorkspace).Workflows == nil {
data.(*SharedWorkspace).Workflows = []string{}
}
if data.(*SharedWorkspace).Rules == nil {
data.(*SharedWorkspace).Rules = []string{}
}
d, code, err := wfa.GenericStoreOne(data.(*SharedWorkspace), wfa)
if code == 200 {
wfa.sharedWorkflow(d.(*SharedWorkspace), d.GetID()) // create all shared workflows
wfa.sharedWorkspace(d.(*SharedWorkspace), d.GetID()) // create all shared workspaces
wfa.sendToPeer(d.(*SharedWorkspace)) // send the shared workspace (shared workspace object) to the peers
}
return data, code, err
}
// CopyOne copies a shared workspace in the database
func (wfa *sharedWorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.StoreOne(data)
}
// enrich is a function that enriches the shared workspace with the shared objects
func (wfa *sharedWorkspaceMongoAccessor) enrich(sharedWorkspace *SharedWorkspace) *SharedWorkspace {
access := (&workspace.Workspace{}).GetAccessor(nil)
res, code, _ := access.Search(&dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Workspaces}},
},
}, "")
if code == 200 {
for _, r := range res {
sharedWorkspace.SharedWorkspaces = append(sharedWorkspace.SharedWorkspaces, r.(*workspace.Workspace))
}
}
access = (&w.Workflow{}).GetAccessor(nil)
res, code, _ = access.Search(&dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Workflows}},
},
}, "")
if code == 200 {
for _, r := range res {
sharedWorkspace.SharedWorkflows = append(sharedWorkspace.SharedWorkflows, r.(*w.Workflow))
}
}
access = (&peer.Peer{}).GetAccessor(nil)
res, code, _ = access.Search(&dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Peers}},
},
}, "")
if code == 200 {
for _, r := range res {
sharedWorkspace.SharedPeers = append(sharedWorkspace.SharedPeers, r.(*peer.Peer))
}
}
access = (&rule.Rule{}).GetAccessor(nil)
res, code, _ = access.Search(&dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Rules}},
},
}, "")
if code == 200 {
for _, r := range res {
sharedWorkspace.SharedRules = append(sharedWorkspace.SharedRules, r.(*rule.Rule))
}
}
return sharedWorkspace
}
// LoadOne loads a shared workspace from the database, given its ID and enrich it
func (wfa *sharedWorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var sharedWorkspace SharedWorkspace
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&sharedWorkspace)
return wfa.enrich(&sharedWorkspace), 200, nil // enrich the shared workspace
}
// LoadAll loads all the shared workspaces from the database and enrich them
func (wfa sharedWorkspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []SharedWorkspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, wfa.enrich(&r)) // enrich the shared workspace
}
return objs, 200, nil
}
// Search searches for shared workspaces in the database, given some filters OR a search string
func (wfa *sharedWorkspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // search by name only by default can be override
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []SharedWorkspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, wfa.enrich(&r)) // enrich the shared workspace
}
return objs, 200, nil
}

View File

@ -0,0 +1,60 @@
package workspace
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid"
)
// Workspace is a struct that represents a workspace
type Workspace struct {
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
resources.ResourceSet // ResourceSet contains the resources of the workspace (data, datacenter, processing, storage, workflow)
IsContextual bool `json:"is_contextual" bson:"is_contextual" default:"false"` // IsContextual is a flag that indicates if the workspace is contextual
Active bool `json:"active" bson:"active" default:"false"` // Active is a flag that indicates if the workspace is active
Shared string `json:"shared,omitempty" bson:"shared,omitempty"` // Shared is the ID of the shared workspace
}
func (ao *Workspace) GetID() string {
return ao.UUID
}
func (r *Workspace) GenerateID() {
if r.UUID == "" {
r.UUID = uuid.New().String()
}
}
func (d *Workspace) GetName() string {
return d.Name
}
func (d *Workspace) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(utils.WORKSPACE, caller) // Initialize the accessor with the WORKSPACE model type
return data
}
// New creates a new instance of the workspaceMongoAccessor from a map
func (dma *Workspace) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
// Serialize returns the workspaceMongoAccessor as a map
func (dma *Workspace) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,228 @@
package workspace
import (
"errors"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/shallow_shared_workspace"
"cloud.o-forge.io/core/oc-lib/tools"
)
// Workspace is a struct that represents a workspace
type workspaceMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}
// New creates a new instance of the workspaceMongoAccessor
func New() *workspaceMongoAccessor {
return &workspaceMongoAccessor{}
}
// DeleteOne deletes a workspace from the database, given its ID, it automatically share to peers if the workspace is shared
// it checks if a workspace with the same name already exists
func (wfa *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
res, code, err := wfa.GenericDeleteOne(id, wfa)
wfa.share(res.(*Workspace), true, wfa.Caller) // Share the deletion to the peers
return res, code, err
}
// UpdateOne updates a workspace in the database, given its ID, it automatically share to peers if the workspace is shared
func (wfa *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
d := set.(*Workspace) // Get the workspace from the set
d.DataResources = nil // Reset the resources
d.DatacenterResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
if d.Active { // If the workspace is active, deactivate all the other workspaces
res, _, err := wfa.LoadAll()
if err == nil {
for _, r := range res {
if r.GetID() != id {
r.(*Workspace).Active = false
wfa.UpdateOne(r.(*Workspace), r.GetID())
}
}
}
}
res, code, err := wfa.GenericUpdateOne(set, id, wfa, &Workspace{})
wfa.share(res.(*Workspace), false, wfa.Caller)
return res, code, err
}
// StoreOne stores a workspace in the database, it checks if a workspace with the same name already exists
func (wfa *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
filters := &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{dbs.LIKE.String(), data.GetName() + "_workspace"}},
},
}
res, _, err := wfa.Search(filters, "") // Search for the workspace
if err == nil && len(res) > 0 { // If the workspace already exists, return an error
return nil, 409, errors.New("A workspace with the same name already exists")
}
// reset the resources
d := data.(*Workspace)
d.DataResources = nil
d.DatacenterResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
return wfa.GenericStoreOne(d, wfa)
}
// CopyOne copies a workspace in the database
func (wfa *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
/*
This function is used to fill the workspace with the resources
*/
func (wfa *workspaceMongoAccessor) fill(workflow *Workspace) *Workspace {
// Fill the workspace with the resources
if workflow.Datas != nil && len(workflow.Datas) > 0 {
dataAccessor := (&data.DataResource{}).GetAccessor(nil)
for _, id := range workflow.Datas {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.DataResources = append(workflow.DataResources, d.(*data.DataResource))
}
}
}
// Fill the workspace with the datacenters
if workflow.Datacenters != nil && len(workflow.Datacenters) > 0 {
dataAccessor := (&datacenter.DatacenterResource{}).GetAccessor(nil)
for _, id := range workflow.Datacenters {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.DatacenterResources = append(workflow.DatacenterResources, d.(*datacenter.DatacenterResource))
}
}
}
// Fill the workspace with the storages
if workflow.Storages != nil && len(workflow.Storages) > 0 {
dataAccessor := (&storage.StorageResource{}).GetAccessor(nil)
for _, id := range workflow.Storages {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.StorageResources = append(workflow.StorageResources, d.(*storage.StorageResource))
}
}
}
// Fill the workspace with the processings
if workflow.Processings != nil && len(workflow.Processings) > 0 {
dataAccessor := (&processing.ProcessingResource{}).GetAccessor(nil)
for _, id := range workflow.Processings {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.ProcessingResources = append(workflow.ProcessingResources, d.(*processing.ProcessingResource))
}
}
}
// Fill the workspace with the workflows
if workflow.Workflows != nil && len(workflow.Workflows) > 0 {
dataAccessor := (&w.WorkflowResource{}).GetAccessor(nil)
for _, id := range workflow.Workflows {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.WorkflowResources = append(workflow.WorkflowResources, d.(*w.WorkflowResource))
}
}
}
return workflow
}
// LoadOne loads a workspace from the database, given its ID
func (wfa *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Workspace
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
return wfa.fill(&workflow), 200, nil
}
// LoadAll loads all the workspaces from the database
func (wfa workspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Workspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, wfa.fill(&r))
}
return objs, 200, nil
}
// Search searches for workspaces in the database, given some filters OR a search string
func (wfa *workspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Workspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, wfa.fill(&r))
}
return objs, 200, nil
}
/*
This function is used to share the workspace with the peers
*/
func (wfa *workspaceMongoAccessor) share(realData *Workspace, delete bool, caller *tools.HTTPCaller) {
if realData.Shared == "" {
return
}
access := (&shallow_shared_workspace.ShallowSharedWorkspace{}).GetAccessor(nil)
res, code, _ := access.LoadOne(realData.Shared)
if code != 200 {
return
}
var err error
paccess := &peer.Peer{}
for _, p := range res.(*shallow_shared_workspace.ShallowSharedWorkspace).Peers {
paccess.UUID = p
if paccess.IsMySelf() { // If the peer is the current peer, never share because it will create a loop
continue
}
if delete { // If the workspace is deleted, share the deletion
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKSPACE, tools.DELETE, map[string]interface{}{}, caller)
} else { // If the workspace is updated, share the update
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKSPACE, tools.PUT, res.Serialize(), caller)
}
}
if err != nil {
wfa.Logger.Error().Msg(err.Error())
}
}

View File

@ -1,12 +0,0 @@
package oclib
type Resource struct {
Id string
Name string
ShortDescription string
Description string
Logo string
Owner string
OwnerLogo string
SourceUrl string
}

View File

@ -1 +0,0 @@
package oclib

30
static/peer_static.go Normal file
View File

@ -0,0 +1,30 @@
package static
/*
This package contains static data for the peer model
It's used to test the peer model
Temporary version, will be removed in the future and replaced with a more dynamic solution
to generate the data
*/
// GetMyLocalBsonPeer returns a tuple with the peer ID and the peer data in BSON format
func GetMyLocalBsonPeer() (string, map[string]interface{}) {
return "6fd0134c-fefc-427e-94c2-e01365fc5fb0", map[string]interface{}{
"abstractobject": map[string]interface{}{
"id": "6fd0134c-fefc-427e-94c2-e01365fc5fb0",
"name": "local_peer",
},
"url": "http://localhost",
"public_key": "public_key_lulz",
}
}
// GetMyLocalJsonPeer returns a tuple with the peer ID and the peer data in JSON format
func GetMyLocalJsonPeer() (string, map[string]interface{}) {
return "6fd0134c-fefc-427e-94c2-e01365fc5fb0", map[string]interface{}{
"id": "6fd0134c-fefc-427e-94c2-e01365fc5fb0",
"name": "local_peer",
"url": "http://localhost",
"public_key": "public_key_lulz",
}
}

View File

@ -1 +0,0 @@
package oclib

1
tests/config.go Normal file
View File

@ -0,0 +1 @@
package tests

6
tests/oclib_conf.json Normal file
View File

@ -0,0 +1,6 @@
{
"DB_URL_LOCAL" : "mongodb://127.0.0.1:27017",
"DB_URL_DOCKER": "mongodb://mongo:27017/",
"DBPOINT" : "oclib_tests",
"DCNAME" : "testDC"
}

145
tools/api.go Normal file
View File

@ -0,0 +1,145 @@
package tools
import (
"encoding/json"
"errors"
"cloud.o-forge.io/core/oc-lib/config"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
)
/*
* API is the Health Check API
* it defines the health check methods
*/
var UncatchedError = []error{} // Singleton instance of the api 500 error cache
type State int
// State is an enum that defines the state of the API
const (
ALIVE State = iota
REDUCED_SERVICE // occurs when some services are down
UNPROCESSABLE_ENTITY // occurs when the database is up but the collections are not
DB_FALLOUT // occurs when the database is down
TEAPOT // well some things boils in here, i'm probably a teapot, occurs when uncatched errors are present (it's fun)
DEAD // occurs when the peer is dead
)
// EnumIndex returns the index of the enum
func (s State) EnumIndex() int {
return int(s)
}
// ToState returns the state from a string
func ToState(str string) State {
for _, s := range []State{ALIVE, REDUCED_SERVICE, UNPROCESSABLE_ENTITY, DB_FALLOUT, TEAPOT, DEAD} {
if s.String() == str {
return s
}
}
return DEAD
}
// String returns the string of the enum
func (s State) String() string {
return [...]string{"alive", "reduced service", "unprocessable entity", "database fallout",
"some things boils in here, i'm probably a teapot", "dead"}[s]
}
type API struct{}
// GetState returns the state of the API
func (a *API) GetState() (State, int, error) {
// Check if the database is up
err := mongo.MONGOService.TestDB(config.GetConfig())
if err != nil {
return DB_FALLOUT, 200, err // If the database is not up, return database fallout
}
err = mongo.MONGOService.TestCollections(config.GetConfig(), []string{}) // Check if the collections are up
if err != nil {
return UNPROCESSABLE_ENTITY, 200, err // If the collections are not up, return unprocessable entity
}
if len(UncatchedError) > 0 { // If there are uncatched errors, return teapot
errStr := ""
for _, e := range UncatchedError {
errStr += e.Error() + "\n"
}
return TEAPOT, 200, errors.New(errStr)
}
return ALIVE, 200, nil // If everything is up, return alive
}
// CheckRemotePeer checks the state of a remote peer
func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
// Check if the database is up
caller := NewHTTPCaller(map[string]map[METHOD]string{}) // Create a new http caller
var resp APIStatusResponse
b, err := caller.CallPost(url, "/status", map[string]interface{}{}) // Call the status endpoint of the peer
if err != nil {
return DEAD, map[string]int{} // If the peer is not reachable, return dead
}
json.Unmarshal(b, &resp)
if resp.Data == nil { // If the response is empty, return dead
return DEAD, map[string]int{}
}
new := map[string]int{}
for k, v := range resp.Data.Services { // Return the services states of the peer
new[k] = ToState(v).EnumIndex()
}
return ToState(resp.Data.State), new // Return the state of the peer & its services states
}
// CheckRemoteAPIs checks the state of remote APIs from your proper OC
func (a *API) CheckRemoteAPIs(urls map[string]string) (State, map[string]string, error) {
// Check if the database is up
new := map[string]string{}
caller := NewHTTPCaller(map[string]map[METHOD]string{}) // Create a new http caller
code := 0
e := ""
state := ALIVE
reachable := false
for appName, url := range urls { // Check the state of each remote API in the list
var resp APIStatusResponse
b, err := caller.CallGet(url, "/version/status") // Call the status endpoint of the remote API (standard OC status endpoint)
if err != nil {
state = REDUCED_SERVICE // If a remote API is not reachable, return reduced service
continue
}
json.Unmarshal(b, &resp)
if resp.Data == nil { //
state = REDUCED_SERVICE // If the response is empty, return reduced service
continue
}
new[appName] = resp.Data.State
if resp.Data.Code > code {
code = resp.Data.Code
e += resp.Error
}
reachable = true // If the remote API is reachable, set reachable to true cause we are not dead
}
if !reachable {
state = DEAD // If no remote API is reachable, return dead, nobody is alive
}
if code > 0 {
state = REDUCED_SERVICE
}
return state, new, nil
}
/* APIStatusResponse is the response of the API status */
type APIStatusResponse struct {
Data *APIStatus `json:"data"`
Error string `json:"error"`
}
/*
* APIStatus is the status of the API
* it defines the state of the API
* Code is the status code, where 0 is ALIVE, 1 is REDUCED_SERVICE, 2 is UNPROCESSABLE_ENTITY, 3 is DB_FALLOUT, 4 is TEAPOT, 5 is DEAD
*/
type APIStatus struct {
Code int `json:"code"` // Code is the status code, where 0 is ALIVE, 1 is REDUCED_SERVICE, 2 is UNPROCESSABLE_ENTITY, 3 is DB_FALLOUT, 4 is TEAPOT, 5 is DEAD
State string `json:"state"` // State is the state of the API (status shows as a string) (alive, reduced service, unprocessable entity, database fallout, some things boils in here, i'm probably a teapot, dead)
Services map[string]string `json:"services"` // Services is the state of the services of the API (status shows as a string) (alive, reduced service, unprocessable entity, database fallout, some things boils in here, i'm probably a teapot, dead)
}

65
tools/nats_caller.go Normal file
View File

@ -0,0 +1,65 @@
package tools
import (
"encoding/json"
"strings"
"cloud.o-forge.io/core/oc-lib/config"
"github.com/nats-io/nats.go"
)
// NATS Method Enum defines the different methods that can be used to interact with the NATS server
type NATSMethod int
const (
REMOVE NATSMethod = iota
CREATE
)
// NameToMethod returns the NATSMethod enum value from a string
func NameToMethod(name string) NATSMethod {
for _, v := range [...]NATSMethod{REMOVE, CREATE} {
if strings.Contains(strings.ToLower(v.String()), strings.ToLower(name)) {
return v
}
}
return -1
}
// GenerateKey generates a key for the NATSMethod usefull for standard key based on data name & method
func (d NATSMethod) GenerateKey(name string) string {
return name + "_" + d.String()
}
// String returns the string of the enum
func (d NATSMethod) String() string {
return [...]string{"remove", "create", "discovery"}[d]
}
type natsCaller struct{}
// NewNATSCaller creates a new instance of the NATS Caller
func NewNATSCaller() *natsCaller {
return &natsCaller{}
}
// SetNATSPub sets a message to the NATS server
func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interface{}) string {
if config.GetConfig().NATSUrl == "" {
return " -> NATS_SERVER is not set"
}
nc, err := nats.Connect(config.GetConfig().NATSUrl)
if err != nil {
return " -> Could not reach NATS server : " + err.Error()
}
defer nc.Close()
js, err := json.Marshal(data)
if err != nil {
return " -> " + err.Error()
}
err = nc.Publish(method.GenerateKey(dataName), js) // Publish the message on the NATS server with a channel name based on the data name (or whatever start) and the method
if err != nil {
return " -> " + err.Error() // Return an error if the message could not be published
}
return ""
}

85
tools/remote_caller.go Normal file
View File

@ -0,0 +1,85 @@
package tools
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
// HTTP Method Enum defines the different methods that can be used to interact with the HTTP server
type METHOD int
const (
GET METHOD = iota
PUT
POST
DELETE
)
// String returns the string of the enum
func (m METHOD) String() string {
return [...]string{"GET", "PUT", "POST", "DELETE"}[m]
}
// EnumIndex returns the index of the enum
func (m METHOD) EnumIndex() int {
return int(m)
}
// ToMethod returns the method from a string
func ToMethod(str string) METHOD {
for _, s := range []METHOD{GET, PUT, POST, DELETE} {
if s.String() == str {
return s
}
}
return GET
}
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
type HTTPCaller struct {
URLS map[string]map[METHOD]string // Map of the different methods and their urls
}
// NewHTTPCaller creates a new instance of the HTTP Caller
func NewHTTPCaller(urls map[string]map[METHOD]string) *HTTPCaller {
return &HTTPCaller{
URLS: urls, // Set the urls defined in the config & based on the data name type & method
}
}
// CallGet calls the GET method on the HTTP server
func (caller *HTTPCaller) CallGet(url string, subpath string) ([]byte, error) {
resp, err := http.Get(url + subpath)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// CallPut calls the DELETE method on the HTTP server
func (caller *HTTPCaller) CallDelete(url string, subpath string) ([]byte, error) {
resp, err := http.NewRequest("DELETE", url+subpath, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// CallPost calls the POST method on the HTTP server
func (caller *HTTPCaller) CallPost(url string, subpath string, body map[string]interface{}) ([]byte, error) {
postBody, _ := json.Marshal(body)
responseBody := bytes.NewBuffer(postBody)
resp, err := http.Post(url+subpath, "application/json", responseBody)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// NO PUT IN HERE TO DANGEROUS TO USE ON A REMOTE SERVER, MAYBE NEEDED IN THE FUTURE

View File

@ -1 +0,0 @@
package oclib

View File

@ -1 +0,0 @@
package oclib