Compare commits
179 Commits
chart
...
918006302b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
918006302b | ||
|
|
f30076e0f5 | ||
|
|
78157b80d2 | ||
|
|
4309309bc9 | ||
|
|
0feab329c1 | ||
|
|
1c32cd2d12 | ||
|
|
a0f436b3e1 | ||
|
|
6e5c873796 | ||
|
|
3cdb43074b | ||
|
|
11905339bb | ||
|
|
301ef8dc05 | ||
|
|
0e84db61de | ||
|
|
21a7ff9010 | ||
|
|
be3b09b683 | ||
|
|
7696f065f8 | ||
|
|
02d1e93c78 | ||
|
|
fbbce7817b | ||
|
|
1fcbc7c08a | ||
|
|
fd01f535a1 | ||
|
|
6681c455d8 | ||
|
|
4b88da8ff6 | ||
|
|
ea55c94c73 | ||
|
|
471e0c9d9b | ||
|
|
2924ccd23b | ||
|
|
6042d47700 | ||
|
|
599a614480 | ||
|
|
e2ddd7e4e6 | ||
|
|
9a2ed2351d | ||
|
|
2ec6899a18 | ||
|
|
f17f48921d | ||
|
|
cb21db672b | ||
|
|
b591533ba4 | ||
|
|
8d701e67d8 | ||
|
|
8a11805fe8 | ||
|
|
68bb4a51f1 | ||
|
|
30be88c8ce | ||
|
|
e524c865ea | ||
|
|
9ea342c4c4 | ||
|
|
b388e43f30 | ||
|
|
e560f34215 | ||
|
|
11a3767255 | ||
|
|
0349ad9a29 | ||
|
|
195f5b0794 | ||
|
|
2816e3ea35 | ||
|
|
15ca06aba8 | ||
|
|
e9b3a65a0e | ||
|
|
d8fac883d2 | ||
|
|
ac49d3324d | ||
|
|
3b77c0da83 | ||
|
|
57f18b2244 | ||
|
|
b49685aa82 | ||
|
|
08e9ee67fe | ||
|
|
94803f820a | ||
|
|
898700d127 | ||
|
|
cfcad2343d | ||
|
|
aab0d54dfa | ||
|
|
603641a589 | ||
|
|
35d90f9c4e | ||
|
|
3b8079ca87 | ||
|
|
902ad0ea71 | ||
|
|
9a8625f8b4 | ||
|
|
c24f2f26c4 | ||
|
|
14fe694fd3 | ||
|
|
06c3af5d4d | ||
|
|
8a21a7c803 | ||
|
|
6497e7dbdd | ||
|
|
b5dba2458a | ||
|
|
b2a6ac19cb | ||
|
|
d6dba8e1f1 | ||
|
|
bc79d54284 | ||
|
|
7fd44a55cb | ||
|
|
f3df1e42b9 | ||
|
|
4c0c75be91 | ||
|
|
e6ca520a88 | ||
|
|
2d249f38ff | ||
|
|
4a17d0c07d | ||
|
|
1d6b9db5f9 | ||
|
|
d249bcdf94 | ||
|
|
66413e15bb | ||
|
|
828efb4c98 | ||
|
|
06e048165e | ||
|
|
c47ba64d49 | ||
|
|
e4860ff67e | ||
|
|
5f05b73366 | ||
|
|
20ce1f5ef3 | ||
|
|
4de43a301c | ||
|
|
1a55212378 | ||
|
|
c34e5579fc | ||
|
|
cc8fc2df21 | ||
|
|
cd902c6688 | ||
|
|
84deb17e37 | ||
|
|
158d3aacc8 | ||
|
|
55a25aba83 | ||
|
|
719fa239e0 | ||
|
|
72d5c64c2d | ||
|
|
e45fefe883 | ||
|
|
8e82b87fb3 | ||
|
|
59a1b52242 | ||
|
|
3c54f3d39e | ||
|
|
56f81fb30e | ||
|
|
73fce1d8fb | ||
|
|
3e7f8513eb | ||
|
|
1561d0c81e | ||
|
|
847ef2e95c | ||
|
|
dbcd9cf004 | ||
|
|
9654d59fc0 | ||
|
|
b432266486 | ||
|
|
73602b6c3d | ||
|
|
9f5e6d6018 | ||
|
|
62705cc9b9 | ||
|
|
69fe3f8d76 | ||
|
|
556d711ab6 | ||
|
|
17749c6c0b | ||
|
|
84024a143e | ||
|
|
ae49c40ea5 | ||
|
|
b90ffbc4f0 | ||
|
|
ba6ac86bff | ||
|
|
5d9c922b26 | ||
|
|
208f69ae7e | ||
|
|
3c14c64f4a | ||
|
|
3bbde50f96 | ||
|
|
65668f5bee | ||
|
|
03e066c297 | ||
|
|
e69d5b3351 | ||
|
|
a39f539426 | ||
|
|
6724ff38fe | ||
|
|
c09994fd84 | ||
|
|
1d0b06ac4a | ||
|
|
7b2da3ba94 | ||
|
|
c5bdc96542 | ||
|
|
e28b87ce7b | ||
|
|
0efb852839 | ||
|
|
6dfd6058ef | ||
|
|
c083ce748c | ||
|
|
3388fcc6f3 | ||
|
|
91128313fc | ||
|
|
34212e86a5 | ||
|
|
2de218782f | ||
|
|
1a061f2d1f | ||
|
|
de0b910e09 | ||
|
|
d865ee2766 | ||
|
|
9d246ad180 | ||
|
|
2797d97537 | ||
|
|
a09a04e1a7 | ||
|
|
3f610619b5 | ||
|
|
c309d97623 | ||
|
|
93903b4938 | ||
|
|
3c1a84011e | ||
|
|
f9bfafa004 | ||
|
|
f26249ab8b | ||
|
|
4ec32bafa7 | ||
|
|
e621fb616a | ||
|
|
aaef5334ce | ||
|
|
be976bd8ed | ||
|
|
5f735b6e9d | ||
|
|
f490dc3555 | ||
|
|
cdf513c2c4 | ||
|
|
eba6fd4f97 | ||
|
|
1582654a9c | ||
|
|
0ac55a0ec1 | ||
|
|
3d58416d9b | ||
|
|
279090889f | ||
|
|
8953e2cd42 | ||
|
|
a48bcf25e4 | ||
|
|
2df71c6571 | ||
|
|
89a1ab3f6e | ||
|
|
021b461b0a | ||
|
|
5c9adcf597 | ||
|
|
92bb274d03 | ||
|
|
95a4acff36 | ||
|
|
50ea0969e6 | ||
|
|
6acbcb6704 | ||
|
|
6c9b9ea30d | ||
|
|
a4374602e1 | ||
|
|
610eb0cfda | ||
|
|
4f798263b1 | ||
|
|
a0dd8e1c42 | ||
|
|
754925c32c | ||
|
|
4956166f76 |
660
LICENSE.md
Normal file
660
LICENSE.md
Normal file
@@ -0,0 +1,660 @@
|
|||||||
|
# GNU AFFERO GENERAL PUBLIC LICENSE
|
||||||
|
|
||||||
|
Version 3, 19 November 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc.
|
||||||
|
<https://fsf.org/>
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies of this
|
||||||
|
license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
## Preamble
|
||||||
|
|
||||||
|
The GNU Affero General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works, specifically designed to ensure
|
||||||
|
cooperation with the community in the case of network server software.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
our General Public Licenses are intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains
|
||||||
|
free software for all its users.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
Developers that use our General Public Licenses protect your rights
|
||||||
|
with two steps: (1) assert copyright on the software, and (2) offer
|
||||||
|
you this License which gives you legal permission to copy, distribute
|
||||||
|
and/or modify the software.
|
||||||
|
|
||||||
|
A secondary benefit of defending all users' freedom is that
|
||||||
|
improvements made in alternate versions of the program, if they
|
||||||
|
receive widespread use, become available for other developers to
|
||||||
|
incorporate. Many developers of free software are heartened and
|
||||||
|
encouraged by the resulting cooperation. However, in the case of
|
||||||
|
software used on network servers, this result may fail to come about.
|
||||||
|
The GNU General Public License permits making a modified version and
|
||||||
|
letting the public access it on a server without ever releasing its
|
||||||
|
source code to the public.
|
||||||
|
|
||||||
|
The GNU Affero General Public License is designed specifically to
|
||||||
|
ensure that, in such cases, the modified source code becomes available
|
||||||
|
to the community. It requires the operator of a network server to
|
||||||
|
provide the source code of the modified version running there to the
|
||||||
|
users of that server. Therefore, public use of a modified version, on
|
||||||
|
a publicly accessible server, gives the public access to the source
|
||||||
|
code of the modified version.
|
||||||
|
|
||||||
|
An older license, called the Affero General Public License and
|
||||||
|
published by Affero, was designed to accomplish similar goals. This is
|
||||||
|
a different license, not a version of the Affero GPL, but Affero has
|
||||||
|
released a new version of the Affero GPL which permits relicensing
|
||||||
|
under this license.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
## TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
### 0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU Affero General Public
|
||||||
|
License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds
|
||||||
|
of works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of
|
||||||
|
an exact copy. The resulting work is called a "modified version" of
|
||||||
|
the earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user
|
||||||
|
through a computer network, with no transfer of a copy, is not
|
||||||
|
conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices" to
|
||||||
|
the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
### 1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work for
|
||||||
|
making modifications to it. "Object code" means any non-source form of
|
||||||
|
a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users can
|
||||||
|
regenerate automatically from other parts of the Corresponding Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that same
|
||||||
|
work.
|
||||||
|
|
||||||
|
### 2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not convey,
|
||||||
|
without conditions so long as your license otherwise remains in force.
|
||||||
|
You may convey covered works to others for the sole purpose of having
|
||||||
|
them make modifications exclusively for you, or provide you with
|
||||||
|
facilities for running those works, provided that you comply with the
|
||||||
|
terms of this License in conveying all material for which you do not
|
||||||
|
control copyright. Those thus making or running the covered works for
|
||||||
|
you must do so exclusively on your behalf, under your direction and
|
||||||
|
control, on terms that prohibit them from making any copies of your
|
||||||
|
copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under the
|
||||||
|
conditions stated below. Sublicensing is not allowed; section 10 makes
|
||||||
|
it unnecessary.
|
||||||
|
|
||||||
|
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such
|
||||||
|
circumvention is effected by exercising rights under this License with
|
||||||
|
respect to the covered work, and you disclaim any intention to limit
|
||||||
|
operation or modification of the work as a means of enforcing, against
|
||||||
|
the work's users, your or third parties' legal rights to forbid
|
||||||
|
circumvention of technological measures.
|
||||||
|
|
||||||
|
### 4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
### 5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
- a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
- b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under
|
||||||
|
section 7. This requirement modifies the requirement in section 4
|
||||||
|
to "keep intact all notices".
|
||||||
|
- c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
- d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
### 6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms of
|
||||||
|
sections 4 and 5, provided that you also convey the machine-readable
|
||||||
|
Corresponding Source under the terms of this License, in one of these
|
||||||
|
ways:
|
||||||
|
|
||||||
|
- a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
- b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the Corresponding
|
||||||
|
Source from a network server at no charge.
|
||||||
|
- c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
- d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
- e) Convey the object code using peer-to-peer transmission,
|
||||||
|
provided you inform other peers where the object code and
|
||||||
|
Corresponding Source of the work are being offered to the general
|
||||||
|
public at no charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal,
|
||||||
|
family, or household purposes, or (2) anything designed or sold for
|
||||||
|
incorporation into a dwelling. In determining whether a product is a
|
||||||
|
consumer product, doubtful cases shall be resolved in favor of
|
||||||
|
coverage. For a particular product received by a particular user,
|
||||||
|
"normally used" refers to a typical or common use of that class of
|
||||||
|
product, regardless of the status of the particular user or of the way
|
||||||
|
in which the particular user actually uses, or expects or is expected
|
||||||
|
to use, the product. A product is a consumer product regardless of
|
||||||
|
whether the product has substantial commercial, industrial or
|
||||||
|
non-consumer uses, unless such uses represent the only significant
|
||||||
|
mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to
|
||||||
|
install and execute modified versions of a covered work in that User
|
||||||
|
Product from a modified version of its Corresponding Source. The
|
||||||
|
information must suffice to ensure that the continued functioning of
|
||||||
|
the modified object code is in no case prevented or interfered with
|
||||||
|
solely because modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or
|
||||||
|
updates for a work that has been modified or installed by the
|
||||||
|
recipient, or for the User Product in which it has been modified or
|
||||||
|
installed. Access to a network may be denied when the modification
|
||||||
|
itself materially and adversely affects the operation of the network
|
||||||
|
or violates the rules and protocols for communication across the
|
||||||
|
network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
### 7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders
|
||||||
|
of that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
- a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
- b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
- c) Prohibiting misrepresentation of the origin of that material,
|
||||||
|
or requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
- d) Limiting the use for publicity purposes of names of licensors
|
||||||
|
or authors of the material; or
|
||||||
|
- e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
- f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions
|
||||||
|
of it) with contractual assumptions of liability to the recipient,
|
||||||
|
for any liability that these contractual assumptions directly
|
||||||
|
impose on those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions; the
|
||||||
|
above requirements apply either way.
|
||||||
|
|
||||||
|
### 8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your license
|
||||||
|
from a particular copyright holder is reinstated (a) provisionally,
|
||||||
|
unless and until the copyright holder explicitly and finally
|
||||||
|
terminates your license, and (b) permanently, if the copyright holder
|
||||||
|
fails to notify you of the violation by some reasonable means prior to
|
||||||
|
60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
### 9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or run
|
||||||
|
a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
### 10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
### 11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims owned
|
||||||
|
or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within the
|
||||||
|
scope of its coverage, prohibits the exercise of, or is conditioned on
|
||||||
|
the non-exercise of one or more of the rights that are specifically
|
||||||
|
granted under this License. You may not convey a covered work if you
|
||||||
|
are a party to an arrangement with a third party that is in the
|
||||||
|
business of distributing software, under which you make payment to the
|
||||||
|
third party based on the extent of your activity of conveying the
|
||||||
|
work, and under which the third party grants, to any of the parties
|
||||||
|
who would receive the covered work from you, a discriminatory patent
|
||||||
|
license (a) in connection with copies of the covered work conveyed by
|
||||||
|
you (or copies made from those copies), or (b) primarily for and in
|
||||||
|
connection with specific products or compilations that contain the
|
||||||
|
covered work, unless you entered into that arrangement, or that patent
|
||||||
|
license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
### 12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under
|
||||||
|
this License and any other pertinent obligations, then as a
|
||||||
|
consequence you may not convey it at all. For example, if you agree to
|
||||||
|
terms that obligate you to collect a royalty for further conveying
|
||||||
|
from those to whom you convey the Program, the only way you could
|
||||||
|
satisfy both those terms and this License would be to refrain entirely
|
||||||
|
from conveying the Program.
|
||||||
|
|
||||||
|
### 13. Remote Network Interaction; Use with the GNU General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, if you modify the
|
||||||
|
Program, your modified version must prominently offer all users
|
||||||
|
interacting with it remotely through a computer network (if your
|
||||||
|
version supports such interaction) an opportunity to receive the
|
||||||
|
Corresponding Source of your version by providing access to the
|
||||||
|
Corresponding Source from a network server at no charge, through some
|
||||||
|
standard or customary means of facilitating copying of software. This
|
||||||
|
Corresponding Source shall include the Corresponding Source for any
|
||||||
|
work covered by version 3 of the GNU General Public License that is
|
||||||
|
incorporated pursuant to the following paragraph.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the work with which it is combined will remain governed by version
|
||||||
|
3 of the GNU General Public License.
|
||||||
|
|
||||||
|
### 14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions
|
||||||
|
of the GNU Affero General Public License from time to time. Such new
|
||||||
|
versions will be similar in spirit to the present version, but may
|
||||||
|
differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Program
|
||||||
|
specifies that a certain numbered version of the GNU Affero General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU Affero General Public License, you may choose any version ever
|
||||||
|
published by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future versions
|
||||||
|
of the GNU Affero General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
### 15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
|
||||||
|
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
|
||||||
|
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
|
||||||
|
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
|
||||||
|
CORRECTION.
|
||||||
|
|
||||||
|
### 16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
|
||||||
|
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||||
|
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
||||||
|
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
|
||||||
|
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
|
||||||
|
LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
|
||||||
|
TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
|
||||||
|
PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
### 17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
## How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these
|
||||||
|
terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest to
|
||||||
|
attach them to the start of each source file to most effectively state
|
||||||
|
the exclusion of warranty; and each file should have at least the
|
||||||
|
"copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as
|
||||||
|
published by the Free Software Foundation, either version 3 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper
|
||||||
|
mail.
|
||||||
|
|
||||||
|
If your software can interact with users remotely through a computer
|
||||||
|
network, you should also make sure that it provides a way for users to
|
||||||
|
get its source. For example, if your program is a web application, its
|
||||||
|
interface could display a "Source" link that leads users to an archive
|
||||||
|
of the code. There are many ways you could offer source, and different
|
||||||
|
solutions will be better for different programs; see section 13 for
|
||||||
|
the specific requirements.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or
|
||||||
|
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||||
|
necessary. For more information on this, and how to apply and follow
|
||||||
|
the GNU AGPL, see <https://www.gnu.org/licenses/>.
|
||||||
@@ -16,6 +16,7 @@ type Config struct {
|
|||||||
Port string
|
Port string
|
||||||
LokiUrl string
|
LokiUrl string
|
||||||
LogLevel string
|
LogLevel string
|
||||||
|
Whitelist bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) GetUrl() string {
|
func (c Config) GetUrl() string {
|
||||||
@@ -37,19 +38,11 @@ func GetConfig() *Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string) *Config {
|
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().MongoUrl = mongoUrl
|
||||||
GetConfig().MongoDatabase = database
|
GetConfig().MongoDatabase = database
|
||||||
GetConfig().NATSUrl = natsUrl
|
GetConfig().NATSUrl = natsUrl
|
||||||
GetConfig().LokiUrl = lokiUrl
|
GetConfig().LokiUrl = lokiUrl
|
||||||
GetConfig().LogLevel = logLevel
|
GetConfig().LogLevel = logLevel
|
||||||
|
GetConfig().Whitelist = true
|
||||||
return GetConfig()
|
return GetConfig()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ package mongo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/static"
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
@@ -132,10 +132,6 @@ func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
|
|||||||
new_collection := mngoDB.Collection(collection_name)
|
new_collection := mngoDB.Collection(collection_name)
|
||||||
if _, exists := collectionMap[collection_name]; !exists {
|
if _, exists := collectionMap[collection_name]; !exists {
|
||||||
m.createCollection(collection_name, new_collection)
|
m.createCollection(collection_name, new_collection)
|
||||||
if collection_name == "peer" {
|
|
||||||
id, p := static.GetMyLocalBsonPeer()
|
|
||||||
m.StoreOne(p, id, collection_name)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
CollectionMap[collection_name] = new_collection
|
CollectionMap[collection_name] = new_collection
|
||||||
}
|
}
|
||||||
@@ -219,7 +215,7 @@ func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{},
|
|||||||
f = append(f, bson.E{Key: k, Value: v})
|
f = append(f, bson.E{Key: k, Value: v})
|
||||||
}
|
}
|
||||||
targetDBCollection := CollectionMap[collection_name]
|
targetDBCollection := CollectionMap[collection_name]
|
||||||
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 50*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
res, err := targetDBCollection.UpdateMany(MngoCtx, f, dbs.InputToBson(doc, true))
|
res, err := targetDBCollection.UpdateMany(MngoCtx, f, dbs.InputToBson(doc, true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -238,7 +234,7 @@ func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string)
|
|||||||
bson.Unmarshal(b, &doc)
|
bson.Unmarshal(b, &doc)
|
||||||
filter := bson.M{"_id": id}
|
filter := bson.M{"_id": id}
|
||||||
targetDBCollection := CollectionMap[collection_name]
|
targetDBCollection := CollectionMap[collection_name]
|
||||||
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 50*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(doc, true))
|
_, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(doc, true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -275,7 +271,6 @@ func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResul
|
|||||||
}
|
}
|
||||||
filter := bson.M{"_id": id}
|
filter := bson.M{"_id": id}
|
||||||
targetDBCollection := CollectionMap[collection_name]
|
targetDBCollection := CollectionMap[collection_name]
|
||||||
|
|
||||||
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@@ -294,6 +289,7 @@ func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.C
|
|||||||
}
|
}
|
||||||
opts := options.Find()
|
opts := options.Find()
|
||||||
opts.SetLimit(100)
|
opts.SetLimit(100)
|
||||||
|
fmt.Println("Filters: ", CollectionMap, collection_name)
|
||||||
targetDBCollection := CollectionMap[collection_name]
|
targetDBCollection := CollectionMap[collection_name]
|
||||||
orList := bson.A{}
|
orList := bson.A{}
|
||||||
andList := bson.A{}
|
andList := bson.A{}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func init() {
|
|||||||
{Key: "example", Value: "text"}},
|
{Key: "example", Value: "text"}},
|
||||||
})
|
})
|
||||||
|
|
||||||
IndexesMap["datacenter"] = append(IndexesMap["datacenter"], mongo.IndexModel{Keys: bson.D{
|
IndexesMap["compute"] = append(IndexesMap["compute"], mongo.IndexModel{Keys: bson.D{
|
||||||
{Key: "description", Value: "text"},
|
{Key: "description", Value: "text"},
|
||||||
{Key: "example", Value: "text"},
|
{Key: "example", Value: "text"},
|
||||||
{Key: "owner", Value: "text"}},
|
{Key: "owner", Value: "text"}},
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ abstract Resource{
|
|||||||
+icon: string
|
+icon: string
|
||||||
+description: string
|
+description: string
|
||||||
+graphic: GraphicElement
|
+graphic: GraphicElement
|
||||||
+element: DataResource/ProcessingResource/StorageResource/Workflow/DatacenterResource
|
+element: DataResource/ProcessingResource/StorageResource/Workflow/ComputeResource
|
||||||
}
|
}
|
||||||
|
|
||||||
class DataResource {
|
class DataResource {
|
||||||
@@ -31,7 +31,7 @@ class StorageResource {
|
|||||||
+capacity: int
|
+capacity: int
|
||||||
}
|
}
|
||||||
|
|
||||||
class DatacenterResource {
|
class ComputeResource {
|
||||||
+UUID: int
|
+UUID: int
|
||||||
+name: string
|
+name: string
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ class UserWorkflows {
|
|||||||
|
|
||||||
class DatacenterWorkflows {
|
class DatacenterWorkflows {
|
||||||
+UUID: int
|
+UUID: int
|
||||||
+datacenter: DatacenterResource
|
+compute: ComputeResource
|
||||||
+workflows: Workflow[]
|
+workflows: Workflow[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ DatacenterWorkflows "1" o-- "0..*" Workflow
|
|||||||
Resource<|-- DataResource
|
Resource<|-- DataResource
|
||||||
Resource<|-- ProcessingResource
|
Resource<|-- ProcessingResource
|
||||||
Resource<|-- StorageResource
|
Resource<|-- StorageResource
|
||||||
Resource<|-- DatacenterResource
|
Resource<|-- ComputeResource
|
||||||
Resource<|-- Workflow
|
Resource<|-- Workflow
|
||||||
|
|
||||||
ResourceSet "1" o-- "0..*" Ressource
|
ResourceSet "1" o-- "0..*" Ressource
|
||||||
|
|||||||
325
doc/order_model.puml
Normal file
325
doc/order_model.puml
Normal file
@@ -0,0 +1,325 @@
|
|||||||
|
@startuml
|
||||||
|
|
||||||
|
class AbstractObject {
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
IsDraft bool // is consider as a draft
|
||||||
|
UpdateDate date
|
||||||
|
LastPeerWriter string
|
||||||
|
CreatorID string
|
||||||
|
AccessMode int // public or private
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractObject ^-- AbstractResource
|
||||||
|
AbstractObject ^-- Order
|
||||||
|
AbstractObject ^-- Booking
|
||||||
|
AbstractObject ^-- BuyingStatus
|
||||||
|
AbstractObject ^-- WorkflowExecution
|
||||||
|
AbstractObject ^-- Workflow
|
||||||
|
|
||||||
|
class AbstractResource {
|
||||||
|
Logo string
|
||||||
|
Description string
|
||||||
|
ShortDescription string
|
||||||
|
Owners []string
|
||||||
|
UsageRestrictions string
|
||||||
|
|
||||||
|
VerifyAuth(request) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractResource "1 " --* "many " ResourceInstanceITF
|
||||||
|
AbstractCustomizedResource "1 " --* "1 " ResourceInstanceITF
|
||||||
|
|
||||||
|
AbstractResource ^-- ComputeResource
|
||||||
|
AbstractResource ^-- DataResource
|
||||||
|
AbstractResource ^-- ProcessingResource
|
||||||
|
AbstractResource ^-- StorageResource
|
||||||
|
AbstractResource ^-- WorkflowResource
|
||||||
|
class ComputeResource {
|
||||||
|
Architecture string
|
||||||
|
Infrastructure string
|
||||||
|
}
|
||||||
|
class DataResource {
|
||||||
|
Type string
|
||||||
|
Quality string
|
||||||
|
OpenData bool
|
||||||
|
Static bool
|
||||||
|
UpdatePeriod date
|
||||||
|
PersonalData bool
|
||||||
|
AnonymizedPersonalData bool
|
||||||
|
SizeGB float64
|
||||||
|
Licence string
|
||||||
|
Example string
|
||||||
|
}
|
||||||
|
ProcessingResource "1 " *-- "1 " ProcessingUsage
|
||||||
|
class ProcessingUsage {
|
||||||
|
CPUs map[string]CPU
|
||||||
|
GPUs map[string]GPU
|
||||||
|
RAM RAM
|
||||||
|
StorageGB float64
|
||||||
|
Hypothesis string
|
||||||
|
ScalingModel string
|
||||||
|
}
|
||||||
|
|
||||||
|
class ProcessingResource {
|
||||||
|
Infrastructure string
|
||||||
|
Service bool
|
||||||
|
Usage ProcessingUsage
|
||||||
|
OpenSource bool
|
||||||
|
License string
|
||||||
|
Maturity string
|
||||||
|
}
|
||||||
|
class StorageResource {
|
||||||
|
Type string
|
||||||
|
Accronym string
|
||||||
|
}
|
||||||
|
WorkflowResource "1 " --* "many " ComputeResource
|
||||||
|
WorkflowResource "1 " --* "many " DataResource
|
||||||
|
WorkflowResource "1 " --* "many " ProcessingResource
|
||||||
|
WorkflowResource "1 " --* "many " StorageResource
|
||||||
|
class WorkflowResource {
|
||||||
|
WorkflowID string
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExploitResourceSet {}
|
||||||
|
|
||||||
|
AbstractCustomizedResource --^ AbstractResource
|
||||||
|
AbstractCustomizedResource --* ExploitResourceSet
|
||||||
|
ExploitResourceSet ^-- CustomizedComputeResource
|
||||||
|
ExploitResourceSet ^-- CustomizedDataResource
|
||||||
|
ExploitResourceSet ^-- CustomizedProcessingResource
|
||||||
|
ExploitResourceSet ^-- CustomizedStorageResource
|
||||||
|
ExploitResourceSet ^-- CustomizedWorkflowResource
|
||||||
|
class AbstractCustomizedResource {
|
||||||
|
// A customized resource is an
|
||||||
|
// extended abstract resource not use in catalog
|
||||||
|
ExplicitBookingDurationS float64
|
||||||
|
UsageStart date
|
||||||
|
UsageEnd date
|
||||||
|
SelectedPricing string
|
||||||
|
}
|
||||||
|
class CustomizedComputeResource {
|
||||||
|
CPUsLocated map[string]float64
|
||||||
|
GPUsLocated map[string]float64
|
||||||
|
RAMLocated float64
|
||||||
|
}
|
||||||
|
class CustomizedDataResource {
|
||||||
|
StorageGB float64
|
||||||
|
}
|
||||||
|
class CustomizedProcessingResource {
|
||||||
|
Container Container
|
||||||
|
}
|
||||||
|
class CustomizedStorageResource {
|
||||||
|
StorageGB bool
|
||||||
|
}
|
||||||
|
class CustomizedWorkflowResource {}
|
||||||
|
|
||||||
|
interface ResourceInstanceITF {
|
||||||
|
GetID() string
|
||||||
|
VerifyPartnership() bool // eval if there is one partnership per peer groups in every instance
|
||||||
|
GetPeerGroups() []ResourcePartnerITF, []map[string][]string
|
||||||
|
ClearPeerGroups()
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourceInstanceITF -- ResourceInstance
|
||||||
|
ResourceInstance ^-- ComputeResourceInstance
|
||||||
|
ResourceInstance ^-- StorageResourceInstance
|
||||||
|
ResourceInstance "many " --* "1 " ResourcePartnerITF
|
||||||
|
class ResourceInstance {
|
||||||
|
ID string
|
||||||
|
Location Geopoint
|
||||||
|
Country CountryCode
|
||||||
|
AccessProtocol string
|
||||||
|
}
|
||||||
|
class ComputeResourceInstance {
|
||||||
|
SecurityLevel string
|
||||||
|
PowerSource string
|
||||||
|
CPUs map[string]CPU
|
||||||
|
GPUs map[string]GPU
|
||||||
|
RAM RAM
|
||||||
|
}
|
||||||
|
class StorageResourceInstance {
|
||||||
|
Local bool
|
||||||
|
SecurityLevel string
|
||||||
|
SizeType string
|
||||||
|
SizeGB int
|
||||||
|
Encryption bool
|
||||||
|
Redundancy string
|
||||||
|
Throughput string
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourcePartnerITF -- ResourcePartnership
|
||||||
|
ResourcePartnership ^-- ComputeResourcePartnership
|
||||||
|
ResourcePartnership ^-- DataResourcePartnership
|
||||||
|
ResourcePartnership ^-- StorageResourcePartnership
|
||||||
|
|
||||||
|
interface ResourcePartnerITF {
|
||||||
|
GetPricing(id string) PricingProfileITF
|
||||||
|
GetPeerGroups() []ResourcePartnerITF, []map[string][]string
|
||||||
|
ClearPeerGroups()
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourcePartnership "many " --* "1 " PricingProfileITF
|
||||||
|
class ResourcePartnership{
|
||||||
|
Namespace string
|
||||||
|
PeerGroups map[string][]string
|
||||||
|
}
|
||||||
|
class ComputeResourcePartnership {
|
||||||
|
MaxAllowedCPUsCores map[string]int
|
||||||
|
MaxAllowedGPUsMemoryGB map[string]float64
|
||||||
|
RAMSizeGB float64
|
||||||
|
}
|
||||||
|
class DataResourcePartnership {
|
||||||
|
MaxDownloadableGBAllowed float64
|
||||||
|
PersonalDataAllowed bool
|
||||||
|
AnonymizedPersonalDataAllowed bool
|
||||||
|
}
|
||||||
|
class StorageResourcePartnership {
|
||||||
|
MaxSizeGBAllowed float64
|
||||||
|
OnlyEncryptedAllowed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
RefundType -- AccessPricingProfile
|
||||||
|
enum RefundType {
|
||||||
|
REFUND_DEAD_END
|
||||||
|
REFUND_ON_ERROR
|
||||||
|
REFUND_ON_EARLY_END
|
||||||
|
}
|
||||||
|
PricingProfileITF -- AccessPricingProfile
|
||||||
|
PricingProfileITF -- ExploitPricingProfile
|
||||||
|
PricingProfileITF -- WorkflowResourcePricingProfile
|
||||||
|
AccessPricingProfile ^-- DataResourcePricingProfile
|
||||||
|
AccessPricingProfile ^-- ProcessingResourcePricingProfile
|
||||||
|
ExploitPricingProfile ^-- ComputeResourcePricingProfile
|
||||||
|
ExploitPricingProfile ^-- StorageResourcePricingProfile
|
||||||
|
interface PricingProfileITF {
|
||||||
|
GetPrice(quantity float64, val float64, start date, end date, request) float64
|
||||||
|
IsPurchased() bool
|
||||||
|
}
|
||||||
|
class AccessPricingProfile {
|
||||||
|
ID string
|
||||||
|
Pricing PricingStrategy
|
||||||
|
DefaultRefundType RefundType
|
||||||
|
RefundRatio int // percentage of refund on price
|
||||||
|
}
|
||||||
|
class DataResourcePricingProfile {}
|
||||||
|
class ProcessingResourcePricingProfile {}
|
||||||
|
|
||||||
|
ExploitPrivilegeStrategy -- ExploitPricingProfile
|
||||||
|
enum ExploitPrivilegeStrategy {
|
||||||
|
BASIC
|
||||||
|
GARANTED_ON_DELAY
|
||||||
|
GARANTED
|
||||||
|
}
|
||||||
|
|
||||||
|
AccessPricingProfile --* PricingStrategy
|
||||||
|
AccessPricingProfile ^-- ExploitPricingProfile
|
||||||
|
class ExploitPricingProfile {
|
||||||
|
AdditionnalRefundTypes RefundTypeint
|
||||||
|
PrivilegeStrategy ExploitPrivilegeStrategy
|
||||||
|
GarantedDelaySecond int
|
||||||
|
Exceeding bool
|
||||||
|
ExceedingRatio int // percentage of Exceeding based on price
|
||||||
|
}
|
||||||
|
class ComputeResourcePricingProfile {
|
||||||
|
OverrideCPUsPrices map[string]float64
|
||||||
|
OverrideGPUsPrices map[string]float64
|
||||||
|
OverrideRAMPrice float64
|
||||||
|
}
|
||||||
|
class StorageResourcePricingProfile {}
|
||||||
|
WorkflowResourcePricingProfile "1 " --* "many " ExploitResourceSet
|
||||||
|
|
||||||
|
class WorkflowResourcePricingProfile {
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
BuyingStrategy -- PricingStrategy
|
||||||
|
enum BuyingStrategy {
|
||||||
|
UNLIMITED
|
||||||
|
SUBSCRIPTION
|
||||||
|
PAY_PER_USE
|
||||||
|
}
|
||||||
|
Strategy -- TimePricingStrategy
|
||||||
|
Strategy "0-1 " *-- " " PricingStrategy
|
||||||
|
interface Strategy {
|
||||||
|
GetStrategy () string
|
||||||
|
GetStrategyValue() int
|
||||||
|
}
|
||||||
|
enum TimePricingStrategy {
|
||||||
|
ONCE
|
||||||
|
PER_SECOND
|
||||||
|
PER_MINUTE
|
||||||
|
PER_HOUR
|
||||||
|
PER_DAY
|
||||||
|
PER_WEEK
|
||||||
|
PER_MONTH
|
||||||
|
}
|
||||||
|
|
||||||
|
class PricingStrategy {
|
||||||
|
Price float64
|
||||||
|
BuyingStrategy
|
||||||
|
TimePricingStrategy TimePricingStrategy
|
||||||
|
OverrideStrategy Strategy
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PeerOrder "many " *-- "1 " Order
|
||||||
|
PeerItemOrder "many " *-- "1 " PeerOrder
|
||||||
|
PricedItemITF "many " *-- "1 " PeerItemOrder
|
||||||
|
|
||||||
|
PricedItemITF -- AbstractCustomizedResource
|
||||||
|
|
||||||
|
class Order {
|
||||||
|
OrderBy string
|
||||||
|
WorkflowExecutionIDs []string
|
||||||
|
Status string
|
||||||
|
Total float64
|
||||||
|
}
|
||||||
|
class PeerOrder {
|
||||||
|
PeerID string
|
||||||
|
Error string
|
||||||
|
Status string
|
||||||
|
BillingAddress string
|
||||||
|
Total float64
|
||||||
|
}
|
||||||
|
class PeerItemOrder {
|
||||||
|
Quantity int
|
||||||
|
BuyingStatus string
|
||||||
|
}
|
||||||
|
|
||||||
|
class BuyingStatus {}
|
||||||
|
|
||||||
|
WorkflowExecution "many " --* "1 " Workflow
|
||||||
|
Workflow "1 " --* "many " WorkflowScheduler
|
||||||
|
WorkflowScheduler "1 " --* "many " WorkflowExecution
|
||||||
|
|
||||||
|
class WorkflowExecution {
|
||||||
|
ExecDate date
|
||||||
|
EndDate date
|
||||||
|
State string
|
||||||
|
WorkflowID string
|
||||||
|
|
||||||
|
ToBookings() []Booking
|
||||||
|
}
|
||||||
|
class WorkflowScheduler* {
|
||||||
|
Message string
|
||||||
|
Warning string
|
||||||
|
Start date
|
||||||
|
End date
|
||||||
|
DurationS float64
|
||||||
|
Cron string
|
||||||
|
|
||||||
|
Schedules(workflowID string, request) []WorkflowExecution
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Workflow "1 " --* "many " ExploitResourceSet
|
||||||
|
|
||||||
|
class Workflow {}
|
||||||
|
|
||||||
|
interface PricedItemITF {
|
||||||
|
getPrice(request) float64, error
|
||||||
|
}
|
||||||
|
|
||||||
|
@enduml
|
||||||
29
doc/paymentflowV1.puml
Normal file
29
doc/paymentflowV1.puml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
@startuml
|
||||||
|
user -> client : schedule
|
||||||
|
client -> OrderAPIP1 : check book
|
||||||
|
OrderAPIP1 -> datacenterAPIP2 : check book
|
||||||
|
datacenterAPIP2 -> OrderAPIP1 : send ok
|
||||||
|
OrderAPIP1 -> datacenterAPIP2 : generate draft book
|
||||||
|
OrderAPIP1 -> client : send ok
|
||||||
|
client -> OrderAPIP1 : send scheduler
|
||||||
|
OrderAPIP1 -> OrderAPIP1 : draft executions
|
||||||
|
OrderAPIP1 -> OrderAPIP1 : draft order
|
||||||
|
OrderAPIP1 -> client : send drafted order
|
||||||
|
client -> user :
|
||||||
|
user -> client : select pricing profile
|
||||||
|
client -> OrderAPIP1 : update order
|
||||||
|
OrderAPIP1 -> datacenterAPIP2 : check book
|
||||||
|
datacenterAPIP2 -> OrderAPIP1 : send ok
|
||||||
|
OrderAPIP1 -> datacenterAPIP2 : generate draft book
|
||||||
|
OrderAPIP1 -> client : send order
|
||||||
|
user -> client : order
|
||||||
|
client -> OrderAPIP1 : order
|
||||||
|
OrderAPIP1 -> PaymentAPIBCP1 : send payment
|
||||||
|
PaymentAPIBCP1 -> OrderAPIP1 : send ok
|
||||||
|
OrderAPIP1 -> datacenterAPIP2 : undraft booking
|
||||||
|
OrderAPIP1 -> OrderAPIP1 : undraft execution
|
||||||
|
OrderAPIP1 -> OrderAPIP1 : undraft order
|
||||||
|
OrderAPIP1 -> client : send ok
|
||||||
|
client -> client : redirect
|
||||||
|
@enduml
|
||||||
|
|
||||||
398
entrypoint.go
398
entrypoint.go
@@ -1,8 +1,12 @@
|
|||||||
package oclib
|
package oclib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
@@ -11,20 +15,19 @@ 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/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models"
|
"cloud.o-forge.io/core/oc-lib/models"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/order"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"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"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resources/data"
|
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
|
||||||
"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/utils"
|
||||||
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
|
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/workflow_execution"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/workspace"
|
"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"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
beego "github.com/beego/beego/v2/server/web"
|
||||||
|
"github.com/beego/beego/v2/server/web/context"
|
||||||
"github.com/goraz/onion"
|
"github.com/goraz/onion"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
@@ -36,28 +39,29 @@ type LibDataEnum int
|
|||||||
// init accessible constant to retrieve data from the database
|
// init accessible constant to retrieve data from the database
|
||||||
const (
|
const (
|
||||||
INVALID LibDataEnum = iota
|
INVALID LibDataEnum = iota
|
||||||
DATA_RESOURCE = utils.DATA_RESOURCE
|
DATA_RESOURCE = tools.DATA_RESOURCE
|
||||||
PROCESSING_RESOURCE = utils.PROCESSING_RESOURCE
|
PROCESSING_RESOURCE = tools.PROCESSING_RESOURCE
|
||||||
STORAGE_RESOURCE = utils.STORAGE_RESOURCE
|
STORAGE_RESOURCE = tools.STORAGE_RESOURCE
|
||||||
DATACENTER_RESOURCE = utils.DATACENTER_RESOURCE
|
COMPUTE_RESOURCE = tools.COMPUTE_RESOURCE
|
||||||
WORKFLOW_RESOURCE = utils.WORKFLOW_RESOURCE
|
WORKFLOW_RESOURCE = tools.WORKFLOW_RESOURCE
|
||||||
WORKFLOW = utils.WORKFLOW
|
WORKFLOW = tools.WORKFLOW
|
||||||
WORKSPACE = utils.WORKSPACE
|
WORKSPACE = tools.WORKSPACE
|
||||||
WORKFLOW_EXECUTION = utils.WORKFLOW_EXECUTION
|
WORKFLOW_EXECUTION = tools.WORKFLOW_EXECUTION
|
||||||
PEER = utils.PEER
|
PEER = tools.PEER
|
||||||
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
|
COLLABORATIVE_AREA = tools.COLLABORATIVE_AREA
|
||||||
RULE = utils.RULE
|
RULE = tools.RULE
|
||||||
BOOKING = utils.BOOKING
|
BOOKING = tools.BOOKING
|
||||||
|
ORDER = tools.ORDER
|
||||||
)
|
)
|
||||||
|
|
||||||
// will turn into standards api hostnames
|
// will turn into standards api hostnames
|
||||||
func (d LibDataEnum) API() string {
|
func (d LibDataEnum) API() string {
|
||||||
return utils.DefaultAPI[d]
|
return tools.DefaultAPI[d]
|
||||||
}
|
}
|
||||||
|
|
||||||
// will turn into standards name
|
// will turn into standards name
|
||||||
func (d LibDataEnum) String() string {
|
func (d LibDataEnum) String() string {
|
||||||
return utils.Str[d]
|
return tools.Str[d]
|
||||||
}
|
}
|
||||||
|
|
||||||
// will turn into enum index
|
// will turn into enum index
|
||||||
@@ -65,6 +69,20 @@ func (d LibDataEnum) EnumIndex() int {
|
|||||||
return int(d)
|
return int(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsQueryParamsEquals(input *context.BeegoInput, name string, val interface{}) bool {
|
||||||
|
path := strings.Split(input.URI(), "?")
|
||||||
|
if len(path) >= 2 {
|
||||||
|
uri := strings.Split(path[1], "&")
|
||||||
|
for _, val := range uri {
|
||||||
|
kv := strings.Split(val, "=")
|
||||||
|
if kv[0] == name && fmt.Sprintf("%v", val) == kv[1] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// model to define the shallow data structure
|
// model to define the shallow data structure
|
||||||
type LibDataShallow struct {
|
type LibDataShallow struct {
|
||||||
Data []utils.ShallowDBObject `bson:"data" json:"data"`
|
Data []utils.ShallowDBObject `bson:"data" json:"data"`
|
||||||
@@ -79,29 +97,75 @@ type LibData struct {
|
|||||||
Err string `bson:"error" json:"error"`
|
Err string `bson:"error" json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// here is the singleton variable to store the paths that api will use
|
func InitDaemon(appName string) {
|
||||||
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
|
config.SetAppName(appName) // set the app name to the logger to define the main log chan
|
||||||
// create a temporary console logger for init
|
// create a temporary console logger for init
|
||||||
logs.SetLogger(logs.CreateLogger("main"))
|
logs.SetLogger(logs.CreateLogger("main"))
|
||||||
|
// Load the right config file
|
||||||
|
o := GetConfLoader()
|
||||||
|
|
||||||
|
// feed the library with the loaded config
|
||||||
|
SetConfig(
|
||||||
|
o.GetStringDefault("MONGO_URL", "mongodb://127.0.0.1:27017"),
|
||||||
|
o.GetStringDefault("MONGO_DATABASE", "DC_myDC"),
|
||||||
|
o.GetStringDefault("NATS_URL", "nats://localhost:4222"),
|
||||||
|
o.GetStringDefault("LOKI_URL", ""),
|
||||||
|
o.GetStringDefault("LOG_LEVEL", "info"),
|
||||||
|
)
|
||||||
|
// Beego init
|
||||||
|
beego.BConfig.AppName = appName
|
||||||
|
beego.BConfig.Listen.HTTPPort = o.GetIntDefault("port", 8080)
|
||||||
|
beego.BConfig.WebConfig.DirectoryIndex = true
|
||||||
|
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
||||||
|
}
|
||||||
|
|
||||||
|
type IDTokenClaims struct {
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
PeerID string `json:"peer_id"`
|
||||||
|
Groups []string `json:"groups"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionClaims struct
|
||||||
|
type SessionClaims struct {
|
||||||
|
AccessToken map[string]interface{} `json:"access_token"`
|
||||||
|
IDToken IDTokenClaims `json:"id_token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Claims struct
|
||||||
|
type Claims struct {
|
||||||
|
Session SessionClaims `json:"session"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExtractTokenInfo(request http.Request) (string, string, []string) {
|
||||||
|
reqToken := request.Header.Get("Authorization")
|
||||||
|
splitToken := strings.Split(reqToken, "Bearer ")
|
||||||
|
if len(splitToken) < 2 {
|
||||||
|
reqToken = ""
|
||||||
|
} else {
|
||||||
|
reqToken = splitToken[1]
|
||||||
|
}
|
||||||
|
if reqToken != "" {
|
||||||
|
token := strings.Split(reqToken, ".")
|
||||||
|
if len(token) > 2 {
|
||||||
|
bytes, err := base64.StdEncoding.DecodeString(token[2])
|
||||||
|
if err != nil {
|
||||||
|
return "", "", []string{}
|
||||||
|
}
|
||||||
|
var c Claims
|
||||||
|
err = json.Unmarshal(bytes, &c)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", []string{}
|
||||||
|
}
|
||||||
|
return c.Session.IDToken.UserID, c.Session.IDToken.PeerID, c.Session.IDToken.Groups
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", "", []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init(appName string) {
|
||||||
|
InitDaemon(appName)
|
||||||
|
api := &tools.API{}
|
||||||
|
api.Discovered(beego.BeeApp.Handlers.GetAllControllerInfo())
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -139,17 +203,16 @@ func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string,
|
|||||||
Resource model is the model that will define the structure of the resources
|
Resource model is the model that will define the structure of the resources
|
||||||
*/
|
*/
|
||||||
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
|
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()} {
|
for _, model := range []string{tools.DATA_RESOURCE.String(), tools.PROCESSING_RESOURCE.String(), tools.STORAGE_RESOURCE.String(), tools.COMPUTE_RESOURCE.String(), tools.WORKFLOW_RESOURCE.String()} {
|
||||||
data, code, _ := accessor.Search(nil, model)
|
data, code, _ := accessor.Search(nil, model, true)
|
||||||
if code == 404 || len(data) == 0 {
|
if code == 404 || len(data) == 0 {
|
||||||
|
refs := map[string]string{}
|
||||||
m := map[string]resource_model.Model{}
|
m := map[string]resource_model.Model{}
|
||||||
// TODO Specify the model for each resource
|
|
||||||
// for now only processing is specified here (not an elegant way)
|
// for now only processing is specified here (not an elegant way)
|
||||||
if model == utils.PROCESSING_RESOURCE.String() {
|
if model == tools.DATA_RESOURCE.String() || model == tools.STORAGE_RESOURCE.String() {
|
||||||
m["image"] = resource_model.Model{
|
refs["path"] = "string"
|
||||||
Type: "string",
|
}
|
||||||
ReadOnly: false,
|
if model == tools.PROCESSING_RESOURCE.String() {
|
||||||
}
|
|
||||||
m["command"] = resource_model.Model{
|
m["command"] = resource_model.Model{
|
||||||
Type: "string",
|
Type: "string",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -158,14 +221,21 @@ func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string,
|
|||||||
Type: "string",
|
Type: "string",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
}
|
}
|
||||||
m["execute"] = resource_model.Model{
|
m["env"] = resource_model.Model{
|
||||||
Type: "map[int]map[string]string",
|
Type: "string",
|
||||||
|
ReadOnly: false,
|
||||||
|
}
|
||||||
|
m["volumes"] = resource_model.Model{
|
||||||
|
Type: "map[string]string",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
accessor.StoreOne(&resource_model.ResourceModel{
|
accessor.StoreOne(&resource_model.ResourceModel{
|
||||||
ResourceType: model,
|
ResourceType: model,
|
||||||
Model: m,
|
VarRefs: refs,
|
||||||
|
Model: map[string]map[string]resource_model.Model{
|
||||||
|
"container": m,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -198,6 +268,76 @@ func GetConfLoader() *onion.Onion {
|
|||||||
return config.GetConfLoader()
|
return config.GetConfLoader()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Request struct {
|
||||||
|
collection LibDataEnum
|
||||||
|
user string
|
||||||
|
peerID string
|
||||||
|
groups []string
|
||||||
|
caller *tools.HTTPCaller
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRequest(collection LibDataEnum, user string, peerID string, groups []string, caller *tools.HTTPCaller) *Request {
|
||||||
|
return &Request{collection: collection, user: user, peerID: peerID, groups: groups, caller: caller}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToScheduler(m interface{}) (n *workflow_execution.WorkflowSchedule) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return m.(*workflow_execution.WorkflowSchedule)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) Schedule(wfID string, start string, end string, durationInS float64, cron string) (*workflow_execution.WorkflowSchedule, error) {
|
||||||
|
scheduler := workflow_execution.NewScheduler(start, end, durationInS, cron)
|
||||||
|
if _, _, err := scheduler.Schedules(wfID, &tools.APIRequest{
|
||||||
|
Caller: r.caller,
|
||||||
|
Username: r.user,
|
||||||
|
PeerID: r.peerID,
|
||||||
|
Groups: r.groups,
|
||||||
|
}); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return scheduler, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) CheckBooking(wfID string, start string, end string, durationInS float64, cron string) bool {
|
||||||
|
ok, _, _, err := workflow_execution.NewScheduler(start, end, durationInS, cron).CheckBooking(wfID, &tools.APIRequest{
|
||||||
|
Caller: r.caller,
|
||||||
|
Username: r.user,
|
||||||
|
PeerID: r.peerID,
|
||||||
|
Groups: r.groups,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) DraftOrder(scheduler *workflow_execution.WorkflowSchedule) (*order.Order, error) {
|
||||||
|
o := &order.Order{}
|
||||||
|
if err := o.DraftOrder(scheduler, &tools.APIRequest{
|
||||||
|
Caller: r.caller,
|
||||||
|
Username: r.user,
|
||||||
|
PeerID: r.peerID,
|
||||||
|
Groups: r.groups,
|
||||||
|
}); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return o, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) PaymentTunnel(o *order.Order, scheduler *workflow_execution.WorkflowSchedule) error {
|
||||||
|
return o.Pay(scheduler, &tools.APIRequest{
|
||||||
|
Caller: r.caller,
|
||||||
|
Username: r.user,
|
||||||
|
PeerID: r.peerID,
|
||||||
|
Groups: r.groups,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Search will search for the data in the database
|
* Search will search for the data in the database
|
||||||
* @param filters *dbs.Filters
|
* @param filters *dbs.Filters
|
||||||
@@ -206,18 +346,19 @@ func GetConfLoader() *onion.Onion {
|
|||||||
* @param c ...*tools.HTTPCaller
|
* @param c ...*tools.HTTPCaller
|
||||||
* @return data LibDataShallow
|
* @return data LibDataShallow
|
||||||
*/
|
*/
|
||||||
func Search(filters *dbs.Filters, word string, collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
|
func (r *Request) Search(filters *dbs.Filters, word string, isDraft bool) (data LibDataShallow) {
|
||||||
defer func() { // recover the panic
|
defer func() { // recover the panic
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Search : "+fmt.Sprintf("%v", r)))
|
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())}
|
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
|
d, code, err := models.Model(r.collection.EnumIndex()).GetAccessor(&tools.APIRequest{
|
||||||
if len(c) > 0 {
|
Caller: r.caller,
|
||||||
caller = c[0]
|
Username: r.user,
|
||||||
}
|
PeerID: r.peerID,
|
||||||
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).Search(filters, word)
|
Groups: r.groups,
|
||||||
|
}).Search(filters, word, isDraft)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
|
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
|
||||||
return
|
return
|
||||||
@@ -232,18 +373,19 @@ func Search(filters *dbs.Filters, word string, collection LibDataEnum, c ...*too
|
|||||||
* @param c ...*tools.HTTPCaller
|
* @param c ...*tools.HTTPCaller
|
||||||
* @return data LibDataShallow
|
* @return data LibDataShallow
|
||||||
*/
|
*/
|
||||||
func LoadAll(collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
|
func (r *Request) LoadAll(isDraft bool) (data LibDataShallow) {
|
||||||
defer func() { // recover the panic
|
defer func() { // recover the panic
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadAll : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
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())}
|
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
|
d, code, err := models.Model(r.collection.EnumIndex()).GetAccessor(&tools.APIRequest{
|
||||||
if len(c) > 0 {
|
Caller: r.caller,
|
||||||
caller = c[0]
|
Username: r.user,
|
||||||
}
|
PeerID: r.peerID,
|
||||||
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).LoadAll()
|
Groups: r.groups,
|
||||||
|
}).LoadAll(isDraft)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
|
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
|
||||||
return
|
return
|
||||||
@@ -259,18 +401,19 @@ func LoadAll(collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallo
|
|||||||
* @param c ...*tools.HTTPCaller
|
* @param c ...*tools.HTTPCaller
|
||||||
* @return data LibData
|
* @return data LibData
|
||||||
*/
|
*/
|
||||||
func LoadOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
|
func (r *Request) LoadOne(id string) (data LibData) {
|
||||||
defer func() { // recover the panic
|
defer func() { // recover the panic
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
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())}
|
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
|
d, code, err := models.Model(r.collection.EnumIndex()).GetAccessor(&tools.APIRequest{
|
||||||
if len(c) > 0 {
|
Caller: r.caller,
|
||||||
caller = c[0]
|
Username: r.user,
|
||||||
}
|
PeerID: r.peerID,
|
||||||
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).LoadOne(id)
|
Groups: r.groups,
|
||||||
|
}).LoadOne(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data = LibData{Data: d, Code: code, Err: err.Error()}
|
data = LibData{Data: d, Code: code, Err: err.Error()}
|
||||||
return
|
return
|
||||||
@@ -287,19 +430,20 @@ func LoadOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data Li
|
|||||||
* @param c ...*tools.HTTPCaller
|
* @param c ...*tools.HTTPCaller
|
||||||
* @return data LibData
|
* @return data LibData
|
||||||
*/
|
*/
|
||||||
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string, c ...*tools.HTTPCaller) (data LibData) {
|
func (r *Request) UpdateOne(set map[string]interface{}, id string) (data LibData) {
|
||||||
defer func() { // recover the panic
|
defer func() { // recover the panic
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in UpdateOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
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())}
|
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
|
model := models.Model(r.collection.EnumIndex())
|
||||||
if len(c) > 0 {
|
d, code, err := model.GetAccessor(&tools.APIRequest{
|
||||||
caller = c[0]
|
Caller: r.caller,
|
||||||
}
|
Username: r.user,
|
||||||
model := models.Model(collection.EnumIndex())
|
PeerID: r.peerID,
|
||||||
d, code, err := model.GetAccessor(caller).UpdateOne(model.Deserialize(set), id)
|
Groups: r.groups,
|
||||||
|
}).UpdateOne(model.Deserialize(set, model), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data = LibData{Data: d, Code: code, Err: err.Error()}
|
data = LibData{Data: d, Code: code, Err: err.Error()}
|
||||||
return
|
return
|
||||||
@@ -315,18 +459,19 @@ func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string, c
|
|||||||
* @param c ...*tools.HTTPCaller
|
* @param c ...*tools.HTTPCaller
|
||||||
* @return data LibData
|
* @return data LibData
|
||||||
*/
|
*/
|
||||||
func DeleteOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
|
func (r *Request) DeleteOne(id string) (data LibData) {
|
||||||
defer func() { // recover the panic
|
defer func() { // recover the panic
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in DeleteOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
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())}
|
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
|
d, code, err := models.Model(r.collection.EnumIndex()).GetAccessor(&tools.APIRequest{
|
||||||
if len(c) > 0 {
|
Caller: r.caller,
|
||||||
caller = c[0]
|
Username: r.user,
|
||||||
}
|
PeerID: r.peerID,
|
||||||
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).DeleteOne(id)
|
Groups: r.groups,
|
||||||
|
}).DeleteOne(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data = LibData{Data: d, Code: code, Err: err.Error()}
|
data = LibData{Data: d, Code: code, Err: err.Error()}
|
||||||
return
|
return
|
||||||
@@ -342,19 +487,20 @@ func DeleteOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data
|
|||||||
* @param c ...*tools.HTTPCaller
|
* @param c ...*tools.HTTPCaller
|
||||||
* @return data LibData
|
* @return data LibData
|
||||||
*/
|
*/
|
||||||
func StoreOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
|
func (r *Request) StoreOne(object map[string]interface{}) (data LibData) {
|
||||||
defer func() { // recover the panic
|
defer func() { // recover the panic
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in StoreOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
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())}
|
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
|
model := models.Model(r.collection.EnumIndex())
|
||||||
if len(c) > 0 {
|
d, code, err := model.GetAccessor(&tools.APIRequest{
|
||||||
caller = c[0]
|
Caller: r.caller,
|
||||||
}
|
Username: r.user,
|
||||||
model := models.Model(collection.EnumIndex())
|
PeerID: r.peerID,
|
||||||
d, code, err := model.GetAccessor(caller).StoreOne(model.Deserialize(object))
|
Groups: r.groups,
|
||||||
|
}).StoreOne(model.Deserialize(object, model))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data = LibData{Data: d, Code: code, Err: err.Error()}
|
data = LibData{Data: d, Code: code, Err: err.Error()}
|
||||||
return
|
return
|
||||||
@@ -370,19 +516,20 @@ func StoreOne(collection LibDataEnum, object map[string]interface{}, c ...*tools
|
|||||||
* @param c ...*tools.HTTPCaller
|
* @param c ...*tools.HTTPCaller
|
||||||
* @return data LibData
|
* @return data LibData
|
||||||
*/
|
*/
|
||||||
func CopyOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
|
func (r *Request) CopyOne(object map[string]interface{}) (data LibData) {
|
||||||
defer func() { // recover the panic
|
defer func() { // recover the panic
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in CopyOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
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())}
|
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
|
model := models.Model(r.collection.EnumIndex())
|
||||||
if len(c) > 0 {
|
d, code, err := model.GetAccessor(&tools.APIRequest{
|
||||||
caller = c[0]
|
Caller: r.caller,
|
||||||
}
|
Username: r.user,
|
||||||
model := models.Model(collection.EnumIndex())
|
PeerID: r.peerID,
|
||||||
d, code, err := model.GetAccessor(caller).CopyOne(model.Deserialize(object))
|
Groups: r.groups,
|
||||||
|
}).CopyOne(model.Deserialize(object, model))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data = LibData{Data: d, Code: code, Err: err.Error()}
|
data = LibData{Data: d, Code: code, Err: err.Error()}
|
||||||
return
|
return
|
||||||
@@ -393,74 +540,81 @@ func CopyOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.
|
|||||||
|
|
||||||
// ================ CAST ========================= //
|
// ================ CAST ========================= //
|
||||||
|
|
||||||
func (l *LibData) ToDataResource() *data.DataResource {
|
func (l *LibData) ToDataResource() *resources.DataResource {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.DATA_RESOURCE.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.DATA_RESOURCE {
|
||||||
return l.Data.(*data.DataResource)
|
return l.Data.(*resources.DataResource)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LibData) ToDatacenterResource() *datacenter.DatacenterResource {
|
func (l *LibData) ToComputeResource() *resources.ComputeResource {
|
||||||
if l.Data != nil && l.Data.GetAccessor(nil).GetType() == utils.DATACENTER_RESOURCE.String() {
|
if l.Data != nil && l.Data.GetAccessor(nil).GetType() == tools.COMPUTE_RESOURCE {
|
||||||
return l.Data.(*datacenter.DatacenterResource)
|
return l.Data.(*resources.ComputeResource)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (l *LibData) ToStorageResource() *storage.StorageResource {
|
func (l *LibData) ToStorageResource() *resources.StorageResource {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.STORAGE_RESOURCE.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.STORAGE_RESOURCE {
|
||||||
return l.Data.(*storage.StorageResource)
|
return l.Data.(*resources.StorageResource)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (l *LibData) ToProcessingResource() *processing.ProcessingResource {
|
func (l *LibData) ToProcessingResource() *resources.ProcessingResource {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.PROCESSING_RESOURCE.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.PROCESSING_RESOURCE {
|
||||||
return l.Data.(*processing.ProcessingResource)
|
return l.Data.(*resources.ProcessingResource)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (l *LibData) ToWorkflowResource() *w.WorkflowResource {
|
func (l *LibData) ToWorkflowResource() *resources.WorkflowResource {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW_RESOURCE.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.WORKFLOW_RESOURCE {
|
||||||
return l.Data.(*w.WorkflowResource)
|
return l.Data.(*resources.WorkflowResource)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (l *LibData) ToPeer() *peer.Peer {
|
func (l *LibData) ToPeer() *peer.Peer {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.PEER.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.PEER {
|
||||||
return l.Data.(*peer.Peer)
|
return l.Data.(*peer.Peer)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LibData) ToWorkflow() *w2.Workflow {
|
func (l *LibData) ToWorkflow() *w2.Workflow {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.WORKFLOW {
|
||||||
return l.Data.(*w2.Workflow)
|
return l.Data.(*w2.Workflow)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (l *LibData) ToWorkspace() *workspace.Workspace {
|
func (l *LibData) ToWorkspace() *workspace.Workspace {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.WORKSPACE.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.WORKSPACE {
|
||||||
return l.Data.(*workspace.Workspace)
|
return l.Data.(*workspace.Workspace)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LibData) ToSharedWorkspace() *shared_workspace.SharedWorkspace {
|
func (l *LibData) ToCollaborativeArea() *collaborative_area.CollaborativeArea {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.SHARED_WORKSPACE.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.COLLABORATIVE_AREA {
|
||||||
return l.Data.(*shared_workspace.SharedWorkspace)
|
return l.Data.(*collaborative_area.CollaborativeArea)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LibData) ToRule() *rule.Rule {
|
func (l *LibData) ToRule() *rule.Rule {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.SHARED_WORKSPACE.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.COLLABORATIVE_AREA {
|
||||||
return l.Data.(*rule.Rule)
|
return l.Data.(*rule.Rule)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
|
func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecutions {
|
||||||
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW_EXECUTION.String() {
|
if l.Data.GetAccessor(nil).GetType() == tools.WORKFLOW_EXECUTION {
|
||||||
return l.Data.(*workflow_execution.WorkflowExecution)
|
return l.Data.(*workflow_execution.WorkflowExecutions)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LibData) ToOrder() *order.Order {
|
||||||
|
if l.Data.GetAccessor(nil).GetType() == tools.ORDER {
|
||||||
|
return l.Data.(*order.Order)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
16
go.mod
16
go.mod
@@ -3,6 +3,7 @@ module cloud.o-forge.io/core/oc-lib
|
|||||||
go 1.22.0
|
go 1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/beego/beego/v2 v2.3.1
|
||||||
github.com/go-playground/validator/v10 v10.22.0
|
github.com/go-playground/validator/v10 v10.22.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/goraz/onion v0.1.3
|
github.com/goraz/onion v0.1.3
|
||||||
@@ -25,16 +26,27 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
|
github.com/biter777/countries v1.7.5
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
|
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||||
github.com/klauspost/compress v1.17.9 // indirect
|
github.com/klauspost/compress v1.17.9 // indirect
|
||||||
|
github.com/kr/text v0.1.0 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/montanaflynn/stats v0.7.1 // 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/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/prometheus/client_golang v1.19.0 // indirect
|
||||||
|
github.com/prometheus/client_model v0.5.0 // indirect
|
||||||
|
github.com/prometheus/common v0.48.0 // indirect
|
||||||
|
github.com/prometheus/procfs v0.12.0 // indirect
|
||||||
|
github.com/robfig/cron v1.2.0
|
||||||
|
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
|
||||||
github.com/xdg-go/pbkdf2 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/scram v1.1.2 // indirect
|
||||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||||
@@ -43,6 +55,6 @@ require (
|
|||||||
golang.org/x/net v0.27.0 // indirect
|
golang.org/x/net v0.27.0 // indirect
|
||||||
golang.org/x/sync v0.7.0 // indirect
|
golang.org/x/sync v0.7.0 // indirect
|
||||||
golang.org/x/text v0.16.0 // indirect
|
golang.org/x/text v0.16.0 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
google.golang.org/protobuf v1.34.2 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
38
go.sum
38
go.sum
@@ -1,10 +1,20 @@
|
|||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
github.com/beego/beego/v2 v2.3.1 h1:7MUKMpJYzOXtCUsTEoXOxsDV/UcHw6CPbaWMlthVNsc=
|
||||||
|
github.com/beego/beego/v2 v2.3.1/go.mod h1:5cqHsOHJIxkq44tBpRvtDe59GuVRVv/9/tyVDxd5ce4=
|
||||||
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
|
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||||
|
github.com/biter777/countries v1.7.5 h1:MJ+n3+rSxWQdqVJU8eBy9RqcdH6ePPn4PJHocVWUa+Q=
|
||||||
|
github.com/biter777/countries v1.7.5/go.mod h1:1HSpZ526mYqKJcpT5Ti1kcGQ0L0SrXWIaptUWjFfv2E=
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/coreos/etcd v3.3.17+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
|
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-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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw=
|
||||||
|
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
|
||||||
github.com/etcd-io/etcd v3.3.17+incompatible/go.mod h1:cdZ77EstHBwVtD6iTgzgvogwcjo9m4iOqoijouPJ4bs=
|
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/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 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
|
||||||
@@ -29,12 +39,16 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa
|
|||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
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 h1:KhyvbDA2b70gcz/d5izfwTiOH8SmrvV43AsVzpng3n0=
|
||||||
github.com/goraz/onion v0.1.3/go.mod h1:XEmz1XoBz+wxTgWB8NwuvRm4RAu3vKxvrmYtzK+XCuQ=
|
github.com/goraz/onion v0.1.3/go.mod h1:XEmz1XoBz+wxTgWB8NwuvRm4RAu3vKxvrmYtzK+XCuQ=
|
||||||
|
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
|
||||||
|
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
|
||||||
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
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/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 h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
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 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
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 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
@@ -48,6 +62,8 @@ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
|
|||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
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/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/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||||
|
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
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/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 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
@@ -60,18 +76,30 @@ 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/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 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
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/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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
|
||||||
|
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
|
||||||
|
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
|
||||||
|
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
|
||||||
|
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
|
||||||
|
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
|
||||||
|
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
|
||||||
|
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
|
||||||
|
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
|
||||||
|
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
|
||||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
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/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||||
|
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||||
|
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||||
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/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 h1:DAYUYH5869yV94zvCES9F51oYtN5oGlwjxJJz7ZCnik=
|
||||||
|
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18/go.mod h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
|
||||||
github.com/skarademir/naturalsort v0.0.0-20150715044055-69a5d87bef62/go.mod h1:oIdVclZaltY1Nf7OQUkg1/2jImBJ+ZfKZuDIRSwk3p0=
|
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 h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
|
||||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||||
@@ -134,9 +162,11 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
|
|||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
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/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=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||||
|
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
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.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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
package booking
|
package booking
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"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"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/google/uuid"
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,74 +14,100 @@ import (
|
|||||||
* Booking is a struct that represents a booking
|
* Booking is a struct that represents a booking
|
||||||
*/
|
*/
|
||||||
type Booking struct {
|
type Booking struct {
|
||||||
workflow_execution.WorkflowExecution // WorkflowExecution contains the workflow execution data
|
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||||
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
|
DestPeerID string `json:"dest_peer_id,omitempty"` // DestPeerID is the ID of the destination peer
|
||||||
|
WorkflowID string `json:"workflow_id,omitempty" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
|
||||||
|
ExecutionID string `json:"execution_id,omitempty" bson:"execution_id,omitempty" validate:"required"`
|
||||||
|
State common.ScheduledType `json:"state,omitempty" bson:"state,omitempty" validate:"required"` // State is the state of the booking
|
||||||
|
ExpectedStartDate time.Time `json:"expected_start_date,omitempty" bson:"expected_start_date,omitempty" validate:"required"` // ExpectedStartDate is the expected start date of the booking
|
||||||
|
ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking
|
||||||
|
|
||||||
|
RealStartDate *time.Time `json:"real_start_date,omitempty" bson:"real_start_date,omitempty"` // RealStartDate is the real start date of the booking
|
||||||
|
RealEndDate *time.Time `json:"real_end_date,omitempty" bson:"real_end_date,omitempty"` // RealEndDate is the real end date of the booking
|
||||||
|
|
||||||
|
ResourceType tools.DataType `json:"resource_type,omitempty" bson:"resource_type,omitempty" validate:"required"` // ResourceType is the type of the resource
|
||||||
|
ResourceID string `json:"resource_id,omitempty" bson:"resource_id,omitempty" validate:"required"` // could be a Compute or a Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckBooking checks if a booking is possible on a specific datacenter resource
|
// CheckBooking checks if a booking is possible on a specific compute resource
|
||||||
func (wfa *Booking) CheckBooking(id string, start time.Time, end *time.Time) (bool, error) {
|
func (wfa *Booking) Check(id string, start time.Time, end *time.Time, parrallelAllowed int) (bool, error) {
|
||||||
// check if
|
// check if
|
||||||
if end == nil {
|
if end == nil {
|
||||||
// if no end... then Book like a savage
|
// if no end... then Book like a savage
|
||||||
return true, nil
|
e := start.Add(time.Hour)
|
||||||
|
end = &e
|
||||||
}
|
}
|
||||||
e := *end
|
accessor := NewAccessor(nil)
|
||||||
accessor := wfa.GetAccessor(nil)
|
|
||||||
res, code, err := accessor.Search(&dbs.Filters{
|
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
|
And: map[string][]dbs.Filter{ // check if there is a booking on the same compute resource by filtering on the compute_resource_id, the state and the execution date
|
||||||
"datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: id}},
|
"resource_id": {{Operator: dbs.EQUAL.String(), Value: id}},
|
||||||
"workflowexecution.state": {{Operator: dbs.EQUAL.String(), Value: workflow_execution.SCHEDULED.EnumIndex()}},
|
"state": {{Operator: dbs.EQUAL.String(), Value: common.DRAFT.EnumIndex()}},
|
||||||
"workflowexecution.execution_date": {
|
"expected_start_date": {
|
||||||
{Operator: dbs.LTE.String(), Value: primitive.NewDateTimeFromTime(e)},
|
{Operator: dbs.LTE.String(), Value: primitive.NewDateTimeFromTime(*end)},
|
||||||
{Operator: dbs.GTE.String(), Value: primitive.NewDateTimeFromTime(start)},
|
{Operator: dbs.GTE.String(), Value: primitive.NewDateTimeFromTime(start)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, "")
|
}, "", wfa.IsDraft)
|
||||||
if code != 200 {
|
if code != 200 {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return len(res) == 0, nil
|
return len(res) <= parrallelAllowed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// tool to convert the argo status to a state
|
func (d *Booking) GetDelayForLaunch() time.Duration {
|
||||||
func (wfa *Booking) ArgoStatusToState(status string) *Booking {
|
return d.RealStartDate.Sub(d.ExpectedStartDate)
|
||||||
wfa.WorkflowExecution.ArgoStatusToState(status)
|
|
||||||
return wfa
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *Booking) GetID() string {
|
func (d *Booking) GetDelayForFinishing() time.Duration {
|
||||||
return ao.UUID
|
if d.ExpectedEndDate == nil {
|
||||||
|
return time.Duration(0)
|
||||||
|
}
|
||||||
|
return d.RealEndDate.Sub(d.ExpectedStartDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Booking) GenerateID() {
|
func (d *Booking) GetUsualDuration() time.Duration {
|
||||||
r.UUID = uuid.New().String()
|
return d.ExpectedEndDate.Sub(d.ExpectedStartDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Booking) GetRealDuration() time.Duration {
|
||||||
|
if d.RealEndDate == nil || d.RealStartDate == nil {
|
||||||
|
return time.Duration(0)
|
||||||
|
}
|
||||||
|
return d.RealEndDate.Sub(*d.RealStartDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Booking) GetDelayOnDuration() time.Duration {
|
||||||
|
return d.GetRealDuration() - d.GetUsualDuration()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Booking) GetName() string {
|
func (d *Booking) GetName() string {
|
||||||
return d.UUID + "_" + d.ExecDate.String()
|
return d.GetID() + "_" + d.ExpectedStartDate.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Booking) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
func (d *Booking) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
data := New() // Create a new instance of the accessor
|
return NewAccessor(request) // 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 {
|
func (d *Booking) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
b, err := json.Marshal(j)
|
return true
|
||||||
if err != nil {
|
}
|
||||||
return nil
|
|
||||||
|
func (r *Booking) StoreDraftDefault() {
|
||||||
|
r.IsDraft = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Booking) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||||
|
if !r.IsDraft && r.State != set.(*Booking).State || r.RealStartDate != set.(*Booking).RealStartDate || r.RealEndDate != set.(*Booking).RealEndDate {
|
||||||
|
return true, &Booking{
|
||||||
|
State: set.(*Booking).State,
|
||||||
|
RealStartDate: set.(*Booking).RealStartDate,
|
||||||
|
RealEndDate: set.(*Booking).RealEndDate,
|
||||||
|
} // only state can be updated
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, dma)
|
// TODO : HERE WE CAN HANDLE THE CASE WHERE THE BOOKING IS DELAYED OR EXCEEDING OR ending sooner
|
||||||
return dma
|
return r.IsDraft, set
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *Booking) Serialize() map[string]interface{} {
|
func (r *Booking) CanDelete() bool {
|
||||||
var m map[string]interface{}
|
return r.IsDraft // only draft bookings can be deleted
|
||||||
b, err := json.Marshal(dma)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
json.Unmarshal(b, &m)
|
|
||||||
return m
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package booking
|
package booking
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"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/common"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type bookingMongoAccessor struct {
|
type bookingMongoAccessor struct {
|
||||||
@@ -11,78 +15,62 @@ type bookingMongoAccessor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the bookingMongoAccessor
|
// New creates a new instance of the bookingMongoAccessor
|
||||||
func New() *bookingMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *bookingMongoAccessor {
|
||||||
return &bookingMongoAccessor{}
|
return &bookingMongoAccessor{
|
||||||
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.BOOKING.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: tools.BOOKING,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Nothing special here, just the basic CRUD operations
|
* Nothing special here, just the basic CRUD operations
|
||||||
*/
|
*/
|
||||||
func (wfa *bookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
func (a *bookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericDeleteOne(id, wfa)
|
return utils.GenericDeleteOne(id, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *bookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (a *bookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericUpdateOne(set, id, wfa, &Booking{})
|
return utils.GenericUpdateOne(set, id, a, &Booking{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *bookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (a *bookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return utils.GenericStoreOne(data, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *bookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (a *bookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return utils.GenericStoreOne(data, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *bookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *bookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
var workflow Booking
|
return utils.GenericLoadOne[*Booking](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
if (d.(*Booking).ExpectedEndDate) == nil {
|
||||||
if err != nil {
|
d.(*Booking).State = common.FORGOTTEN
|
||||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
utils.GenericRawUpdateOne(d, id, a)
|
||||||
return nil, code, err
|
} else if d.(*Booking).State == common.SCHEDULED && time.Now().UTC().After(*&d.(*Booking).ExpectedStartDate) {
|
||||||
}
|
d.(*Booking).State = common.DELAYED
|
||||||
res_mongo.Decode(&workflow)
|
utils.GenericRawUpdateOne(d, id, a)
|
||||||
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}},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
return d, 200, nil
|
||||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
|
}, a)
|
||||||
if err != nil {
|
}
|
||||||
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
|
||||||
return nil, code, err
|
func (a *bookingMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
}
|
return utils.GenericLoadAll[*Booking](a.getExec(), isDraft, a)
|
||||||
var results []Booking
|
}
|
||||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
|
||||||
return nil, 404, err
|
func (a *bookingMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
}
|
return utils.GenericSearch[*Booking](filters, search, (&Booking{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||||
for _, r := range results {
|
}
|
||||||
objs = append(objs, &r)
|
|
||||||
}
|
func (a *bookingMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||||
return objs, 200, nil
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
if d.(*Booking).State == common.SCHEDULED && time.Now().UTC().After(*&d.(*Booking).ExpectedStartDate) {
|
||||||
|
d.(*Booking).State = common.DELAYED
|
||||||
|
utils.GenericRawUpdateOne(d, d.GetID(), a)
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
98
models/collaborative_area/collaborative_area.go
Normal file
98
models/collaborative_area/collaborative_area.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
package collaborative_area
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/config"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||||
|
"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/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CollaborativeAreaRule struct {
|
||||||
|
ShareMode string `json:"share_mode,omitempty" bson:"share_mode,omitempty"` // Share is the share of the rule
|
||||||
|
CreatedAt time.Time `json:"created_at,omitempty" bson:"created_at,omitempty"` // CreatedAt is the time the rule was created
|
||||||
|
Creator string `json:"creator,omitempty" bson:"creator,omitempty"` // Creator is the creator of the rule
|
||||||
|
ExploitedBy string `json:"exploited_by,omitempty" bson:"exploited_by,omitempty"` // ExploitedBy is the exploited by of the rule
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 CollaborativeArea 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
|
||||||
|
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
|
||||||
|
CollaborativeAreaRule *CollaborativeAreaRule `json:"collaborative_area,omitempty" bson:"collaborative_area,omitempty"` // CollaborativeArea is the collaborative area 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" bson:"workspaces"` // Workspaces is the workspaces of the workspace
|
||||||
|
Workflows []string `json:"workflows" bson:"workflows"` // Workflows is the workflows of the workspace
|
||||||
|
AllowedPeersGroup map[string][]string `json:"allowed_peers_group,omitempty" bson:"allowed_peers_group,omitempty"` // AllowedPeersGroup is the group of allowed peers
|
||||||
|
Rules []string `json:"rules" 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 *CollaborativeArea) Clear(peerID string) {
|
||||||
|
ao.CreatorID = peerID
|
||||||
|
if config.GetConfig().Whitelist {
|
||||||
|
ao.AllowedPeersGroup[peerID] = []string{"*"}
|
||||||
|
} else {
|
||||||
|
ao.AllowedPeersGroup[peerID] = []string{}
|
||||||
|
}
|
||||||
|
// then reset the shared fields
|
||||||
|
if ao.Workspaces == nil {
|
||||||
|
ao.Workspaces = []string{}
|
||||||
|
}
|
||||||
|
if ao.Workflows == nil {
|
||||||
|
ao.Workflows = []string{}
|
||||||
|
}
|
||||||
|
if ao.Rules == nil {
|
||||||
|
ao.Rules = []string{}
|
||||||
|
}
|
||||||
|
if ao.CollaborativeAreaRule == nil {
|
||||||
|
ao.CollaborativeAreaRule = &CollaborativeAreaRule{
|
||||||
|
ShareMode: "private",
|
||||||
|
ExploitedBy: "collaborators only",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ao.CollaborativeAreaRule.CreatedAt = time.Now().UTC()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao *CollaborativeArea) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
|
if (ao.AllowedPeersGroup != nil || config.GetConfig().Whitelist) && request != nil {
|
||||||
|
if grps, ok := ao.AllowedPeersGroup[request.PeerID]; ok || config.GetConfig().Whitelist {
|
||||||
|
fmt.Println("grps", grps, "ok", ok, "config.GetConfig().Whitelist", config.GetConfig().Whitelist)
|
||||||
|
if slices.Contains(grps, "*") || (!ok && config.GetConfig().Whitelist) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, grp := range grps {
|
||||||
|
if slices.Contains(request.Groups, grp) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ao.AbstractObject.VerifyAuth(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *CollaborativeArea) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor(request) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *CollaborativeArea) Trim() *CollaborativeArea {
|
||||||
|
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: d.CreatorID}}).IsMySelf(); !ok {
|
||||||
|
d.AllowedPeersGroup = map[string][]string{}
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
276
models/collaborative_area/collaborative_area_mongo_accessor.go
Normal file
276
models/collaborative_area/collaborative_area_mongo_accessor.go
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
package collaborative_area
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||||
|
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/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SharedWorkspace is a struct that represents a collaborative area
|
||||||
|
type collaborativeAreaMongoAccessor struct {
|
||||||
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
|
||||||
|
workspaceAccessor utils.Accessor
|
||||||
|
workflowAccessor utils.Accessor
|
||||||
|
peerAccessor utils.Accessor
|
||||||
|
ruleAccessor utils.Accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAccessor(request *tools.APIRequest) *collaborativeAreaMongoAccessor {
|
||||||
|
return &collaborativeAreaMongoAccessor{
|
||||||
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.COLLABORATIVE_AREA.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: tools.COLLABORATIVE_AREA,
|
||||||
|
},
|
||||||
|
workspaceAccessor: (&workspace.Workspace{}).GetAccessor(nil),
|
||||||
|
workflowAccessor: (&w.Workflow{}).GetAccessor(nil),
|
||||||
|
peerAccessor: (&peer.Peer{}).GetAccessor(nil),
|
||||||
|
ruleAccessor: (&rule.Rule{}).GetAccessor(nil),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOne deletes a collaborative area from the database, given its ID, it automatically share to peers if the workspace is shared
|
||||||
|
func (a *collaborativeAreaMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
|
set, code, err := a.LoadOne(id)
|
||||||
|
if code != 200 {
|
||||||
|
return nil, code, err
|
||||||
|
}
|
||||||
|
a.deleteToPeer(set.(*CollaborativeArea))
|
||||||
|
a.sharedWorkflow(&CollaborativeArea{}, id) // create all shared workflows
|
||||||
|
a.sharedWorkspace(&CollaborativeArea{}, id) // create all collaborative areas
|
||||||
|
return utils.GenericDeleteOne(id, a) // then add on yours
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateOne updates a collaborative area in the database, given its ID and the new data, it automatically share to peers if the workspace is shared
|
||||||
|
func (a *collaborativeAreaMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
|
res, code, err := utils.GenericUpdateOne(set.(*CollaborativeArea).Trim(), id, a, &CollaborativeArea{})
|
||||||
|
// a.deleteToPeer(res.(*CollaborativeArea)) // delete the collaborative area on the peer
|
||||||
|
a.sharedWorkflow(res.(*CollaborativeArea), id) // replace all shared workflows
|
||||||
|
a.sharedWorkspace(res.(*CollaborativeArea), id) // replace all collaborative areas (not shared worspace obj but workspace one)
|
||||||
|
// a.sendToPeer(res.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||||
|
return res, code, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared
|
||||||
|
func (a *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
_, id := (&peer.Peer{}).IsMySelf() // get the local peer
|
||||||
|
data.(*CollaborativeArea).Clear(id) // set the creator
|
||||||
|
// retrieve or proper peer
|
||||||
|
dd, code, err := a.peerAccessor.Search(nil, "0", true)
|
||||||
|
if code != 200 || len(dd) == 0 {
|
||||||
|
return nil, code, errors.New("Could not retrieve the peer" + err.Error())
|
||||||
|
}
|
||||||
|
data.(*CollaborativeArea).CollaborativeAreaRule.Creator = dd[0].GetID()
|
||||||
|
d, code, err := utils.GenericStoreOne(data.(*CollaborativeArea).Trim(), a)
|
||||||
|
if code == 200 {
|
||||||
|
a.sharedWorkflow(d.(*CollaborativeArea), d.GetID()) // create all shared workflows
|
||||||
|
a.sharedWorkspace(d.(*CollaborativeArea), d.GetID()) // create all collaborative areas
|
||||||
|
a.sendToPeer(d.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||||
|
}
|
||||||
|
return data, code, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CopyOne copies a CollaborativeArea in the database
|
||||||
|
func (a *collaborativeAreaMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return a.StoreOne(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterEnrich[T utils.ShallowDBObject](arr []string, isDrafted bool, a utils.Accessor) []T {
|
||||||
|
var new []T
|
||||||
|
res, code, _ := a.Search(&dbs.Filters{
|
||||||
|
Or: map[string][]dbs.Filter{
|
||||||
|
"abstractobject.id": {{Operator: dbs.IN.String(), Value: arr}},
|
||||||
|
},
|
||||||
|
}, "", isDrafted)
|
||||||
|
if code == 200 {
|
||||||
|
for _, r := range res {
|
||||||
|
new = append(new, r.(T))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new
|
||||||
|
}
|
||||||
|
|
||||||
|
// enrich is a function that enriches the CollaborativeArea with the shared objects
|
||||||
|
func (a *collaborativeAreaMongoAccessor) enrich(sharedWorkspace *CollaborativeArea, isDrafted bool) *CollaborativeArea {
|
||||||
|
sharedWorkspace.SharedWorkspaces = append(sharedWorkspace.SharedWorkspaces,
|
||||||
|
filterEnrich[*workspace.Workspace](sharedWorkspace.Workspaces, isDrafted, a.workspaceAccessor)...)
|
||||||
|
sharedWorkspace.SharedWorkflows = append(sharedWorkspace.SharedWorkflows,
|
||||||
|
filterEnrich[*workflow.Workflow](sharedWorkspace.Workflows, isDrafted, a.workflowAccessor)...)
|
||||||
|
peerskey := []string{}
|
||||||
|
for k := range sharedWorkspace.AllowedPeersGroup {
|
||||||
|
peerskey = append(peerskey, k)
|
||||||
|
}
|
||||||
|
sharedWorkspace.SharedPeers = append(sharedWorkspace.SharedPeers,
|
||||||
|
filterEnrich[*peer.Peer](peerskey, isDrafted, a.peerAccessor)...)
|
||||||
|
sharedWorkspace.SharedRules = append(sharedWorkspace.SharedRules,
|
||||||
|
filterEnrich[*rule.Rule](sharedWorkspace.Rules, isDrafted, a.ruleAccessor)...)
|
||||||
|
return sharedWorkspace
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *collaborativeAreaMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericLoadOne[*CollaborativeArea](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return a.enrich(d.(*CollaborativeArea), true), 200, nil
|
||||||
|
}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *collaborativeAreaMongoAccessor) LoadAll(isDrafted bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericLoadAll[*CollaborativeArea](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return a.enrich(d.(*CollaborativeArea), true)
|
||||||
|
}, isDrafted, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *collaborativeAreaMongoAccessor) Search(filters *dbs.Filters, search string, isDrafted bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericSearch[*CollaborativeArea](filters, search, (&CollaborativeArea{}).GetObjectFilters(search),
|
||||||
|
func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return a.enrich(d.(*CollaborativeArea), true)
|
||||||
|
}, isDrafted, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
sharedWorkspace is a function that shares the collaborative area to the peers
|
||||||
|
*/
|
||||||
|
func (a *collaborativeAreaMongoAccessor) sharedWorkspace(shared *CollaborativeArea, id string) {
|
||||||
|
eldest, code, _ := a.LoadOne(id) // get the eldest
|
||||||
|
if code == 200 {
|
||||||
|
eld := eldest.(*CollaborativeArea)
|
||||||
|
if eld.Workspaces != nil { // update all your workspaces in the eldest by replacing shared ref by an empty string
|
||||||
|
for _, v := range eld.Workspaces {
|
||||||
|
a.workspaceAccessor.UpdateOne(&workspace.Workspace{Shared: ""}, v)
|
||||||
|
if a.GetCaller() != nil || a.GetCaller().URLS == nil || a.GetCaller().URLS[tools.WORKSPACE] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
paccess := (&peer.Peer{}) // send to all peers
|
||||||
|
for k := range shared.AllowedPeersGroup { // delete the collaborative area on the peer
|
||||||
|
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKSPACE, tools.DELETE, nil, a.GetCaller())
|
||||||
|
if err != nil && b == nil {
|
||||||
|
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if shared.Workspaces != nil {
|
||||||
|
for _, v := range shared.Workspaces { // update all the collaborative areas
|
||||||
|
workspace, code, _ := a.workspaceAccessor.UpdateOne(&workspace.Workspace{Shared: shared.UUID}, v) // add the shared ref to workspace
|
||||||
|
if a.GetCaller() != nil || a.GetCaller().URLS == nil || a.GetCaller().URLS[tools.WORKSPACE] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for k := range shared.AllowedPeersGroup {
|
||||||
|
if code != 200 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
paccess := (&peer.Peer{}) // send to all peers, add the collaborative area on the peer
|
||||||
|
s := workspace.Serialize(workspace)
|
||||||
|
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + k
|
||||||
|
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKSPACE, tools.POST, s, a.GetCaller())
|
||||||
|
if err != nil && b == nil {
|
||||||
|
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// deleting on peers before adding, to avoid conflicts on peers side
|
||||||
|
// because you have no reference to the remote collaborative area
|
||||||
|
}
|
||||||
|
|
||||||
|
// sharedWorkflow is a function that shares the shared workflow to the peers
|
||||||
|
func (a *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeArea, id string) {
|
||||||
|
eldest, code, _ := a.LoadOne(id) // get the eldest
|
||||||
|
if code == 200 {
|
||||||
|
eld := eldest.(*CollaborativeArea)
|
||||||
|
if eld.Workflows != nil {
|
||||||
|
for _, v := range eld.Workflows {
|
||||||
|
data, code, _ := a.workflowAccessor.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
|
||||||
|
a.workflowAccessor.UpdateOne(n, v)
|
||||||
|
if a.GetCaller() != nil || a.GetCaller().URLS == nil || a.GetCaller().URLS[tools.WORKFLOW] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
paccess := (&peer.Peer{}) // send to all peers
|
||||||
|
for k := range shared.AllowedPeersGroup { // delete the shared workflow on the peer
|
||||||
|
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKFLOW, tools.DELETE, nil, a.GetCaller())
|
||||||
|
if err != nil && b == nil {
|
||||||
|
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if shared.Workflows != nil { // update all the shared workflows
|
||||||
|
for _, v := range shared.Workflows {
|
||||||
|
data, code, _ := a.workflowAccessor.LoadOne(v)
|
||||||
|
if code == 200 {
|
||||||
|
s := data.(*w.Workflow)
|
||||||
|
if !slices.Contains(s.Shared, id) {
|
||||||
|
s.Shared = append(s.Shared, id)
|
||||||
|
workflow, code, _ := a.workflowAccessor.UpdateOne(s, v)
|
||||||
|
if a.GetCaller() != nil || a.GetCaller().URLS == nil || a.GetCaller().URLS[tools.WORKFLOW] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
paccess := (&peer.Peer{})
|
||||||
|
for k := range shared.AllowedPeersGroup { // send to all peers
|
||||||
|
if code == 200 {
|
||||||
|
s := workflow.Serialize(workflow) // add the shared workflow on the peer
|
||||||
|
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + k
|
||||||
|
b, err := paccess.LaunchPeerExecution(k, shared.UUID, tools.WORKFLOW, tools.POST, s, a.GetCaller())
|
||||||
|
if err != nil && b == nil {
|
||||||
|
a.Logger.Error().Msg("Could not send to peer " + k + ". 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 collaborative area to the peers
|
||||||
|
func (a *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeArea) {
|
||||||
|
a.contactPeer(shared, tools.POST)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sharedWorkspace is a function that shares the collaborative area to the peers
|
||||||
|
func (a *collaborativeAreaMongoAccessor) sendToPeer(shared *CollaborativeArea) {
|
||||||
|
a.contactPeer(shared, tools.POST)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *collaborativeAreaMongoAccessor) contactPeer(shared *CollaborativeArea, meth tools.METHOD) {
|
||||||
|
if a.GetCaller() == nil || a.GetCaller().URLS == nil || a.GetCaller().URLS[tools.COLLABORATIVE_AREA] == nil || a.GetCaller().Disabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
paccess := (&peer.Peer{})
|
||||||
|
for k := range shared.AllowedPeersGroup {
|
||||||
|
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: k}}).IsMySelf(); ok || (shared.IsSent && meth == tools.POST) || (!shared.IsSent && meth != tools.POST) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shared.IsSent = meth == tools.POST
|
||||||
|
b, err := paccess.LaunchPeerExecution(k, k, tools.COLLABORATIVE_AREA, meth, shared.Serialize(shared), a.GetCaller())
|
||||||
|
if err != nil && b == nil {
|
||||||
|
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package rule
|
package rule
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -18,39 +16,14 @@ type Rule struct {
|
|||||||
Actions []string `json:"actions,omitempty" bson:"actions,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() {
|
func (r *Rule) GenerateID() {
|
||||||
r.UUID = uuid.New().String()
|
r.UUID = uuid.New().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Rule) GetName() string {
|
func (d *Rule) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
return d.Name
|
return NewAccessor(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Rule) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
func (d *Rule) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
data := New()
|
return true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
62
models/collaborative_area/rules/rule/rule_mongo_accessor.go
Normal file
62
models/collaborative_area/rules/rule/rule_mongo_accessor.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ruleMongoAccessor struct {
|
||||||
|
utils.AbstractAccessor
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the ruleMongoAccessor
|
||||||
|
func NewAccessor(request *tools.APIRequest) *ruleMongoAccessor {
|
||||||
|
return &ruleMongoAccessor{
|
||||||
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.RULE.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: tools.RULE,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Nothing special here, just the basic CRUD operations
|
||||||
|
*/
|
||||||
|
func (a *ruleMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericDeleteOne(id, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ruleMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericUpdateOne(set, id, a, &Rule{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ruleMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericStoreOne(data, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ruleMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericStoreOne(data, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericLoadOne[*Rule](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return d, 200, nil
|
||||||
|
}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ruleMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericLoadAll[*Rule](a.getExec(), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ruleMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericSearch[*Rule](filters, search, (&Rule{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ruleMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package shallow_collaborative_area
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ShallowCollaborativeArea struct {
|
||||||
|
utils.AbstractObject
|
||||||
|
IsSent bool `json:"is_sent" bson:"-"`
|
||||||
|
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" bson:"workspaces"`
|
||||||
|
Workflows []string `json:"workflows" bson:"workflows"`
|
||||||
|
Peers []string `json:"peers" bson:"peers"`
|
||||||
|
Rules []string `json:"rules,omitempty" bson:"rules,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ShallowCollaborativeArea) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor(request)
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package shallow_collaborative_area
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type shallowSharedWorkspaceMongoAccessor struct {
|
||||||
|
utils.AbstractAccessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAccessor(request *tools.APIRequest) *shallowSharedWorkspaceMongoAccessor {
|
||||||
|
return &shallowSharedWorkspaceMongoAccessor{
|
||||||
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.COLLABORATIVE_AREA.String()), // Create a logger with the data type
|
||||||
|
Request: request, // Set the caller
|
||||||
|
Type: tools.COLLABORATIVE_AREA,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *shallowSharedWorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericDeleteOne(id, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *shallowSharedWorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericUpdateOne(set.(*ShallowCollaborativeArea), id, a, &ShallowCollaborativeArea{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *shallowSharedWorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericStoreOne(data.(*ShallowCollaborativeArea), a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *shallowSharedWorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return a.StoreOne(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *shallowSharedWorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericLoadOne[*ShallowCollaborativeArea](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return d, 200, nil
|
||||||
|
}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *shallowSharedWorkspaceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericLoadAll[*ShallowCollaborativeArea](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return d
|
||||||
|
}, isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *shallowSharedWorkspaceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericSearch[*ShallowCollaborativeArea](filters, search, (&ShallowCollaborativeArea{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return d
|
||||||
|
}, isDraft, a)
|
||||||
|
}
|
||||||
17
models/common/access_configuration.go
Normal file
17
models/common/access_configuration.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
type Container struct {
|
||||||
|
Image string `json:"image,omitempty" bson:"image,omitempty"` // Image is the container image TEMPO
|
||||||
|
Command string `json:"command,omitempty" bson:"command,omitempty"` // Command is the container command
|
||||||
|
Args string `json:"args,omitempty" bson:"args,omitempty"` // Args is the container arguments
|
||||||
|
Env map[string]string `json:"env,omitempty" bson:"env,omitempty"` // Env is the container environment variables
|
||||||
|
Volumes map[string]string `json:"volumes,omitempty" bson:"volumes,omitempty"` // Volumes is the container volumes
|
||||||
|
|
||||||
|
Exposes []Expose `bson:"exposes,omitempty" json:"exposes,omitempty"` // Expose is the execution
|
||||||
|
}
|
||||||
|
|
||||||
|
type Expose struct {
|
||||||
|
Port int `json:"port,omitempty" bson:"port,omitempty"` // Port is the port
|
||||||
|
Reverse string `json:"reverse,omitempty" bson:"reverse,omitempty"` // Reverse is the reverse
|
||||||
|
PAT int `json:"pat,omitempty" bson:"pat,omitempty"` // PAT is the PAT
|
||||||
|
}
|
||||||
33
models/common/devices.go
Normal file
33
models/common/devices.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
// CPU is a struct that represents a CPU
|
||||||
|
type CPU struct {
|
||||||
|
Model string `bson:"platform,omitempty" json:"platform,omitempty"`
|
||||||
|
FrequencyGhz float64 `bson:"frenquency,omitempty" json:"frenquency,omitempty"`
|
||||||
|
Cores int `bson:"cores,omitempty" json:"cores,omitempty"`
|
||||||
|
Architecture string `bson:"architecture,omitempty" json:"architecture,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RAM struct {
|
||||||
|
SizeGb float64 `bson:"size,omitempty" json:"size,omitempty" description:"Units in MB"`
|
||||||
|
Ecc bool `bson:"ecc" json:"ecc" default:"true"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GPU struct {
|
||||||
|
Model string `bson:"platform,omitempty" json:"platform,omitempty"`
|
||||||
|
MemoryGb float64 `bson:"memory,omitempty" json:"memory,omitempty" description:"Units in MB"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InfrastructureType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
DOCKER InfrastructureType = iota
|
||||||
|
KUBERNETES
|
||||||
|
SLURM
|
||||||
|
HW
|
||||||
|
CONDOR
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t InfrastructureType) String() string {
|
||||||
|
return [...]string{"DOCKER", "KUBERNETES", "SLURM", "HW", "CONDOR"}[t]
|
||||||
|
}
|
||||||
20
models/common/pricing/interfaces.go
Normal file
20
models/common/pricing/interfaces.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package pricing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PricedItemITF interface {
|
||||||
|
GetID() string
|
||||||
|
GetType() tools.DataType
|
||||||
|
IsPurchased() bool
|
||||||
|
GetCreatorID() string
|
||||||
|
GetLocationStart() *time.Time
|
||||||
|
SetLocationStart(start time.Time)
|
||||||
|
SetLocationEnd(end time.Time)
|
||||||
|
GetLocationEnd() *time.Time
|
||||||
|
GetExplicitDurationInS() float64
|
||||||
|
GetPrice() (float64, error)
|
||||||
|
}
|
||||||
58
models/common/pricing/pricing_profile.go
Normal file
58
models/common/pricing/pricing_profile.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package pricing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PricingProfileITF interface {
|
||||||
|
GetID() string
|
||||||
|
GetPrice(quantity float64, val float64, start time.Time, end time.Time, params ...string) (float64, error)
|
||||||
|
IsPurchased() bool
|
||||||
|
GetOverrideStrategyValue() int
|
||||||
|
}
|
||||||
|
|
||||||
|
type RefundType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
REFUND_DEAD_END RefundType = iota
|
||||||
|
REFUND_ON_ERROR
|
||||||
|
REFUND_ON_EARLY_END
|
||||||
|
)
|
||||||
|
|
||||||
|
type AccessPricingProfile[T Strategy] struct { // only use for acces such as : DATA && PROCESSING
|
||||||
|
ID string `json:"id,omitempty" bson:"id,omitempty"` // ID is the ID of the pricing
|
||||||
|
Pricing PricingStrategy[T] `json:"price,omitempty" bson:"price,omitempty"` // Price is the price of the resource
|
||||||
|
DefaultRefund RefundType `json:"default_refund" bson:"default_refund"` // DefaultRefund is the default refund type of the pricing
|
||||||
|
RefundRatio int32 `json:"refund_ratio" bson:"refund_ratio" default:"0"` // RefundRatio is the refund ratio if missing
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *AccessPricingProfile[T]) GetID() string {
|
||||||
|
return b.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *AccessPricingProfile[T]) GetOverrideStrategyValue() int {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExploitPrivilegeStrategy int
|
||||||
|
|
||||||
|
const (
|
||||||
|
BASIC ExploitPrivilegeStrategy = iota
|
||||||
|
GARANTED_ON_DELAY
|
||||||
|
GARANTED
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t ExploitPrivilegeStrategy) String() string {
|
||||||
|
return [...]string{"BASIC", "GARANTED_ON_DELAY", "GARANTED"}[t]
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExploitPricingProfile[T Strategy] struct { // only use for exploit such as : STORAGE, COMPUTE, WORKFLOW
|
||||||
|
AccessPricingProfile[T]
|
||||||
|
AdditionnalRefundTypes []RefundType `json:"refund_types" bson:"refund_types"` // RefundTypes is the refund types of the pricing
|
||||||
|
|
||||||
|
PrivilegeStrategy ExploitPrivilegeStrategy `json:"privilege_strategy,omitempty" bson:"privilege_strategy,omitempty"` // Strategy is the strategy of the pricing
|
||||||
|
GarantedDelaySecond uint
|
||||||
|
|
||||||
|
Exceeding bool
|
||||||
|
ExceedingRatio int32 `json:"exceeding_ratio" bson:"exceeding_ratio" default:"0"` // ExceedingRatio is the exceeding ratio of the bill
|
||||||
|
}
|
||||||
136
models/common/pricing/pricing_strategy.go
Normal file
136
models/common/pricing/pricing_strategy.go
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
package pricing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BuyingStrategy int
|
||||||
|
|
||||||
|
const (
|
||||||
|
UNLIMITED BuyingStrategy = iota
|
||||||
|
SUBSCRIPTION
|
||||||
|
PAY_PER_USE
|
||||||
|
)
|
||||||
|
|
||||||
|
type Strategy interface {
|
||||||
|
GetStrategy() string
|
||||||
|
GetStrategyValue() int
|
||||||
|
}
|
||||||
|
|
||||||
|
type TimePricingStrategy int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ONCE TimePricingStrategy = iota
|
||||||
|
PER_SECOND
|
||||||
|
PER_MINUTE
|
||||||
|
PER_HOUR
|
||||||
|
PER_DAY
|
||||||
|
PER_WEEK
|
||||||
|
PER_MONTH
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t TimePricingStrategy) GetStrategy() string {
|
||||||
|
return [...]string{"ONCE", "PER_SECOND", "PER_MINUTE", "PER_HOUR", "PER_DAY", "PER_WEEK", "PER_MONTH"}[t]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TimePricingStrategy) GetStrategyValue() int {
|
||||||
|
return int(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAverageTimeInSecond(averageTimeInSecond float64, start time.Time, end *time.Time) float64 {
|
||||||
|
now := time.Now()
|
||||||
|
after := now.Add(time.Duration(averageTimeInSecond) * time.Second)
|
||||||
|
|
||||||
|
fromAverageDuration := after.Sub(now).Seconds()
|
||||||
|
var tEnd time.Time
|
||||||
|
if end == nil {
|
||||||
|
tEnd = start.Add(1 * time.Hour)
|
||||||
|
} else {
|
||||||
|
tEnd = *end
|
||||||
|
}
|
||||||
|
fromDateDuration := tEnd.Sub(start).Seconds()
|
||||||
|
|
||||||
|
if fromAverageDuration > fromDateDuration {
|
||||||
|
return fromAverageDuration
|
||||||
|
}
|
||||||
|
return fromDateDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
func BookingEstimation(t TimePricingStrategy, price float64, locationDurationInSecond float64, start time.Time, end *time.Time) (float64, error) {
|
||||||
|
locationDurationInSecond = getAverageTimeInSecond(locationDurationInSecond, start, end)
|
||||||
|
priceStr := fmt.Sprintf("%v", price)
|
||||||
|
p, err := strconv.ParseFloat(priceStr, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
switch t {
|
||||||
|
case ONCE:
|
||||||
|
return p, nil
|
||||||
|
case PER_HOUR:
|
||||||
|
return p * float64(locationDurationInSecond/3600), nil
|
||||||
|
case PER_MINUTE:
|
||||||
|
return p * float64(locationDurationInSecond/60), nil
|
||||||
|
case PER_SECOND:
|
||||||
|
return p * locationDurationInSecond, nil
|
||||||
|
case PER_DAY:
|
||||||
|
return p * float64(locationDurationInSecond/86400), nil
|
||||||
|
case PER_WEEK:
|
||||||
|
return p * float64(locationDurationInSecond/604800), nil
|
||||||
|
case PER_MONTH:
|
||||||
|
return p * float64(locationDurationInSecond/2592000), nil
|
||||||
|
}
|
||||||
|
return 0, errors.New("Pricing strategy not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// hmmmm
|
||||||
|
type PricingStrategy[T Strategy] struct {
|
||||||
|
Price float64 `json:"Price" bson:"Price" default:"0"` // Price is the Price of the pricing
|
||||||
|
BuyingStrategy BuyingStrategy `json:"buying_strategy" bson:"buying_strategy" default:"0"` // BuyingStrategy is the buying strategy of the pricing
|
||||||
|
TimePricingStrategy TimePricingStrategy `json:"time_pricing_strategy" bson:"time_pricing_strategy" default:"0"` // TimePricingStrategy is the time pricing strategy of the pricing
|
||||||
|
OverrideStrategy T `json:"override_strategy" bson:"override_strategy" default:"-1"` // Modulation is the modulation of the pricing
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PricingStrategy[T]) SetStrategy(Price float64, BuyingStrategy BuyingStrategy, TimePricingStrategy TimePricingStrategy) error {
|
||||||
|
if TimePricingStrategy == ONCE && (BuyingStrategy != UNLIMITED || BuyingStrategy != PAY_PER_USE) {
|
||||||
|
return errors.New("time pricing strategy can only be set to ONCE if buying strategy is UNLIMITED or PAY_PER_USE")
|
||||||
|
} else if BuyingStrategy == SUBSCRIPTION && (TimePricingStrategy == ONCE) {
|
||||||
|
return errors.New("subscription duration in second must be set if buying strategy is SUBSCRIPTION")
|
||||||
|
}
|
||||||
|
p.Price = Price
|
||||||
|
p.BuyingStrategy = BuyingStrategy
|
||||||
|
p.TimePricingStrategy = TimePricingStrategy
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PricingStrategy[T]) SetSpecificPerUseStrategy(strategy T) error {
|
||||||
|
if p.BuyingStrategy == UNLIMITED {
|
||||||
|
return errors.New("UNLIMITED buying strategy can't have a specific strategy, Price is set on buying")
|
||||||
|
}
|
||||||
|
p.OverrideStrategy = strategy
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// QUANTITY can be how many of gb core per example
|
||||||
|
func (p PricingStrategy[T]) GetPrice(amountOfData float64, bookingTimeDuration float64, start time.Time, end *time.Time) (float64, error) {
|
||||||
|
if p.BuyingStrategy == SUBSCRIPTION {
|
||||||
|
return BookingEstimation(p.GetTimePricingStrategy(), p.Price*float64(amountOfData), bookingTimeDuration, start, end)
|
||||||
|
} else if p.BuyingStrategy == UNLIMITED {
|
||||||
|
return p.Price, nil
|
||||||
|
}
|
||||||
|
return p.Price * float64(amountOfData), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PricingStrategy[T]) GetBuyingStrategy() BuyingStrategy {
|
||||||
|
return p.BuyingStrategy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PricingStrategy[T]) GetTimePricingStrategy() TimePricingStrategy {
|
||||||
|
return p.TimePricingStrategy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PricingStrategy[T]) GetOverrideStrategy() T {
|
||||||
|
return p.OverrideStrategy
|
||||||
|
}
|
||||||
38
models/common/schedule_type.go
Normal file
38
models/common/schedule_type.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
type ScheduledType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
DRAFT ScheduledType = iota
|
||||||
|
SCHEDULED
|
||||||
|
STARTED
|
||||||
|
FAILURE
|
||||||
|
SUCCESS
|
||||||
|
FORGOTTEN
|
||||||
|
DELAYED
|
||||||
|
CANCELLED
|
||||||
|
)
|
||||||
|
|
||||||
|
var str = [...]string{
|
||||||
|
"draft",
|
||||||
|
"scheduled",
|
||||||
|
"started",
|
||||||
|
"failure",
|
||||||
|
"success",
|
||||||
|
"forgotten",
|
||||||
|
"delayed",
|
||||||
|
"cancelled",
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
39
models/common/size.go
Normal file
39
models/common/size.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
type StorageSize int
|
||||||
|
|
||||||
|
// StorageType - Enum that defines the type of storage
|
||||||
|
const (
|
||||||
|
GB StorageSize = iota
|
||||||
|
MB
|
||||||
|
KB
|
||||||
|
)
|
||||||
|
|
||||||
|
var argoType = [...]string{
|
||||||
|
"Gi",
|
||||||
|
"Mi",
|
||||||
|
"Ki",
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the StorageResource struct
|
||||||
|
func (dma StorageSize) ToArgo() string {
|
||||||
|
return argoType[dma]
|
||||||
|
}
|
||||||
|
|
||||||
|
// enum of a data type
|
||||||
|
type StorageType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
FILE = iota
|
||||||
|
STREAM
|
||||||
|
API
|
||||||
|
DATABASE
|
||||||
|
S3
|
||||||
|
MEMORY
|
||||||
|
HARDWARE
|
||||||
|
)
|
||||||
|
|
||||||
|
// String() - Returns the string representation of the storage type
|
||||||
|
func (t StorageType) String() string {
|
||||||
|
return [...]string{"FILE", "STREAM", "API", "DATABASE", "S3", "MEMORY", "HARDWARE"}[t]
|
||||||
|
}
|
||||||
@@ -2,21 +2,19 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/order"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resource_model"
|
resource "cloud.o-forge.io/core/oc-lib/models/resources"
|
||||||
d "cloud.o-forge.io/core/oc-lib/models/resources/data"
|
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
|
||||||
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"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
|
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/workflow_execution"
|
||||||
w3 "cloud.o-forge.io/core/oc-lib/models/workspace"
|
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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -24,28 +22,31 @@ This package contains the models used in the application
|
|||||||
It's used to create the models dynamically
|
It's used to create the models dynamically
|
||||||
*/
|
*/
|
||||||
var models = map[string]func() utils.DBObject{
|
var models = map[string]func() utils.DBObject{
|
||||||
utils.WORKFLOW_RESOURCE.String(): func() utils.DBObject { return &w.WorkflowResource{} },
|
tools.WORKFLOW_RESOURCE.String(): func() utils.DBObject { return &resource.WorkflowResource{} },
|
||||||
utils.DATA_RESOURCE.String(): func() utils.DBObject { return &d.DataResource{} },
|
tools.DATA_RESOURCE.String(): func() utils.DBObject { return &resource.DataResource{} },
|
||||||
utils.DATACENTER_RESOURCE.String(): func() utils.DBObject { return &dc.DatacenterResource{} },
|
tools.COMPUTE_RESOURCE.String(): func() utils.DBObject { return &resource.ComputeResource{} },
|
||||||
utils.STORAGE_RESOURCE.String(): func() utils.DBObject { return &s.StorageResource{} },
|
tools.STORAGE_RESOURCE.String(): func() utils.DBObject { return &resource.StorageResource{} },
|
||||||
utils.PROCESSING_RESOURCE.String(): func() utils.DBObject { return &p.ProcessingResource{} },
|
tools.PROCESSING_RESOURCE.String(): func() utils.DBObject { return &resource.ProcessingResource{} },
|
||||||
utils.WORKFLOW.String(): func() utils.DBObject { return &w2.Workflow{} },
|
tools.WORKFLOW.String(): func() utils.DBObject { return &w2.Workflow{} },
|
||||||
utils.WORKFLOW_EXECUTION.String(): func() utils.DBObject { return &workflow_execution.WorkflowExecution{} },
|
tools.WORKFLOW_EXECUTION.String(): func() utils.DBObject { return &workflow_execution.WorkflowExecutions{} },
|
||||||
utils.WORKSPACE.String(): func() utils.DBObject { return &w3.Workspace{} },
|
tools.WORKSPACE.String(): func() utils.DBObject { return &w3.Workspace{} },
|
||||||
utils.RESOURCE_MODEL.String(): func() utils.DBObject { return &resource_model.ResourceModel{} },
|
tools.RESOURCE_MODEL.String(): func() utils.DBObject { return &resource_model.ResourceModel{} },
|
||||||
utils.PEER.String(): func() utils.DBObject { return &peer.Peer{} },
|
tools.PEER.String(): func() utils.DBObject { return &peer.Peer{} },
|
||||||
utils.SHARED_WORKSPACE.String(): func() utils.DBObject { return &shared_workspace.SharedWorkspace{} },
|
tools.COLLABORATIVE_AREA.String(): func() utils.DBObject { return &collaborative_area.CollaborativeArea{} },
|
||||||
utils.RULE.String(): func() utils.DBObject { return &rule.Rule{} },
|
tools.RULE.String(): func() utils.DBObject { return &rule.Rule{} },
|
||||||
utils.BOOKING.String(): func() utils.DBObject { return &booking.Booking{} },
|
tools.BOOKING.String(): func() utils.DBObject { return &booking.Booking{} },
|
||||||
|
tools.WORKFLOW_HISTORY.String(): func() utils.DBObject { return &w2.WorkflowHistory{} },
|
||||||
|
tools.WORKSPACE_HISTORY.String(): func() utils.DBObject { return &w3.WorkspaceHistory{} },
|
||||||
|
tools.ORDER.String(): func() utils.DBObject { return &order.Order{} },
|
||||||
}
|
}
|
||||||
|
|
||||||
// Model returns the model object based on the model type
|
// Model returns the model object based on the model type
|
||||||
func Model(model int) utils.DBObject {
|
func Model(model int) utils.DBObject {
|
||||||
log := logs.GetLogger()
|
log := logs.GetLogger()
|
||||||
if _, ok := models[utils.FromInt(model)]; ok {
|
if _, ok := models[tools.FromInt(model)]; ok {
|
||||||
return models[utils.FromInt(model)]()
|
return models[tools.FromInt(model)]()
|
||||||
}
|
}
|
||||||
log.Error().Msg("Can't find model " + utils.FromInt(model) + ".")
|
log.Error().Msg("Can't find model " + tools.FromInt(model) + ".")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
336
models/order/order.go
Normal file
336
models/order/order.go
Normal file
@@ -0,0 +1,336 @@
|
|||||||
|
package order
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Booking is a struct that represents a booking
|
||||||
|
*/
|
||||||
|
|
||||||
|
type OrderStatus = int
|
||||||
|
|
||||||
|
const (
|
||||||
|
DRAFT OrderStatus = iota
|
||||||
|
PENDING
|
||||||
|
CANCELLED
|
||||||
|
PARTIAL
|
||||||
|
PAID
|
||||||
|
DISPUTED
|
||||||
|
OVERDUE
|
||||||
|
REFUND
|
||||||
|
)
|
||||||
|
|
||||||
|
type Order struct {
|
||||||
|
utils.AbstractObject
|
||||||
|
OrderBy string `json:"order_by" bson:"order_by" validate:"required"`
|
||||||
|
WorkflowExecutionIDs []string `json:"workflow_execution_ids" bson:"workflow_execution_ids" validate:"required"`
|
||||||
|
Status OrderStatus `json:"status" bson:"status" default:"0"`
|
||||||
|
SubOrders map[string]*PeerOrder `json:"sub_orders" bson:"sub_orders"`
|
||||||
|
Total float64 `json:"total" bson:"total" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Order) StoreDraftDefault() {
|
||||||
|
r.IsDraft = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Order) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||||
|
if !r.IsDraft && r.Status != set.(*Order).Status {
|
||||||
|
return true, &Order{Status: set.(*Order).Status} // only state can be updated
|
||||||
|
}
|
||||||
|
return r.IsDraft, set
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Order) CanDelete() bool {
|
||||||
|
return r.IsDraft // only draft order can be deleted
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Order) DraftOrder(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) error {
|
||||||
|
// set the draft order from the model
|
||||||
|
if err := o.draftStoreFromModel(scheduler, request); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Order) Pay(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) error {
|
||||||
|
if _, err := o.draftBookOrder(scheduler, request); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
o.Status = PENDING
|
||||||
|
_, code, err := o.GetAccessor(request).UpdateOne(o, o.GetID())
|
||||||
|
if code != 200 || err != nil {
|
||||||
|
return errors.New("could not update the order" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
if err := o.pay(request); err != nil { // pay the order
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
o.IsDraft = false
|
||||||
|
}
|
||||||
|
for _, exec := range scheduler.WorkflowExecutions {
|
||||||
|
exec.IsDraft = false
|
||||||
|
_, code, err := utils.GenericUpdateOne(exec, exec.GetID(),
|
||||||
|
workflow_execution.NewAccessor(request), &workflow_execution.WorkflowExecutions{})
|
||||||
|
if code != 200 || err != nil {
|
||||||
|
return errors.New("could not update the workflow execution" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, code, err = o.GetAccessor(request).UpdateOne(o, o.GetID())
|
||||||
|
if code != 200 || err != nil {
|
||||||
|
return errors.New("could not update the order" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
TODO : TEMPORARY SET BOOKINGS TO UNDRAFT TO AVOID DELETION
|
||||||
|
BUT NEXT ONLY WHO IS PAYED WILL BE ALLOWED TO CHANGE IT
|
||||||
|
*/
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Order) draftStoreFromModel(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) error {
|
||||||
|
if request == nil {
|
||||||
|
return errors.New("no request found")
|
||||||
|
}
|
||||||
|
if scheduler.Workflow.Graph == nil { // if the workflow has no graph, return an error
|
||||||
|
return errors.New("no graph found")
|
||||||
|
}
|
||||||
|
o.SetName()
|
||||||
|
o.IsDraft = true
|
||||||
|
o.OrderBy = request.Username
|
||||||
|
o.WorkflowExecutionIDs = []string{} // create an array of ids
|
||||||
|
for _, exec := range scheduler.WorkflowExecutions {
|
||||||
|
o.WorkflowExecutionIDs = append(o.WorkflowExecutionIDs, exec.GetID())
|
||||||
|
}
|
||||||
|
// set the name of the order
|
||||||
|
resourcesByPeer := map[string][]pricing.PricedItemITF{} // create a map of resources by peer
|
||||||
|
|
||||||
|
processings := scheduler.Workflow.GetPricedItem(scheduler.Workflow.IsProcessing, request) // get the processing items
|
||||||
|
datas := scheduler.Workflow.GetPricedItem(scheduler.Workflow.IsData, request) // get the data items
|
||||||
|
storages := scheduler.Workflow.GetPricedItem(scheduler.Workflow.IsStorage, request) // get the storage items
|
||||||
|
workflows := scheduler.Workflow.GetPricedItem(scheduler.Workflow.IsWorkflow, request) // get the workflow items
|
||||||
|
for _, items := range []map[string]pricing.PricedItemITF{processings, datas, storages, workflows} {
|
||||||
|
for _, item := range items {
|
||||||
|
if _, ok := resourcesByPeer[item.GetCreatorID()]; !ok {
|
||||||
|
resourcesByPeer[item.GetCreatorID()] = []pricing.PricedItemITF{}
|
||||||
|
}
|
||||||
|
resourcesByPeer[item.GetCreatorID()] = append(resourcesByPeer[item.GetCreatorID()], item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for peerID, resources := range resourcesByPeer {
|
||||||
|
peerOrder := &PeerOrder{
|
||||||
|
Status: DRAFT,
|
||||||
|
PeerID: peerID,
|
||||||
|
}
|
||||||
|
peerOrder.GenerateID()
|
||||||
|
for _, resource := range resources {
|
||||||
|
peerOrder.AddItem(resource, len(resources)) // TODO SPECIALS REF ADDITIONALS NOTES
|
||||||
|
}
|
||||||
|
o.SubOrders[peerOrder.GetID()] = peerOrder
|
||||||
|
}
|
||||||
|
// search an order with same user name and same session id
|
||||||
|
err := o.sumUpBill(request)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// should store the order
|
||||||
|
res, code, err := o.GetAccessor(request).Search(&dbs.Filters{
|
||||||
|
And: map[string][]dbs.Filter{
|
||||||
|
"order_by": {{Operator: dbs.EQUAL.String(), Value: request.Username}},
|
||||||
|
},
|
||||||
|
}, "", o.IsDraft)
|
||||||
|
if code != 200 || err != nil {
|
||||||
|
return errors.New("could not search the order" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
if len(res) > 0 {
|
||||||
|
_, code, err := utils.GenericUpdateOne(o, res[0].GetID(), o.GetAccessor(request), o)
|
||||||
|
if code != 200 || err != nil {
|
||||||
|
return errors.New("could not update the order" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_, code, err := utils.GenericStoreOne(o, o.GetAccessor(request))
|
||||||
|
if code != 200 || err != nil {
|
||||||
|
return errors.New("could not store the order" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Order) draftBookOrder(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) ([]*booking.Booking, error) {
|
||||||
|
draftedBookings := []*booking.Booking{}
|
||||||
|
if request == nil {
|
||||||
|
return draftedBookings, errors.New("no request found")
|
||||||
|
}
|
||||||
|
for _, exec := range scheduler.WorkflowExecutions {
|
||||||
|
_, priceds, _, err := scheduler.Workflow.Planify(exec.ExecDate, exec.EndDate, request)
|
||||||
|
if err != nil {
|
||||||
|
return draftedBookings, errors.New("could not planify the workflow" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
bookings := exec.Book(scheduler.Workflow.UUID, priceds)
|
||||||
|
for _, booking := range bookings {
|
||||||
|
_, err := (&peer.Peer{}).LaunchPeerExecution(booking.DestPeerID, "",
|
||||||
|
tools.BOOKING, tools.POST, booking.Serialize(booking), request.Caller)
|
||||||
|
if err != nil {
|
||||||
|
return draftedBookings, errors.New("could not launch the peer execution : " + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
draftedBookings = append(draftedBookings, booking)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return draftedBookings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Order) Quantity() int {
|
||||||
|
return len(o.WorkflowExecutionIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Order) SetName() {
|
||||||
|
d.Name = d.UUID + "_order_" + "_" + time.Now().UTC().Format("2006-01-02T15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Order) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor(request) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Order) sumUpBill(request *tools.APIRequest) error {
|
||||||
|
for _, b := range d.SubOrders {
|
||||||
|
err := b.SumUpBill(request)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.Total += b.Total
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TO FINISH
|
||||||
|
func (d *Order) pay(request *tools.APIRequest) error {
|
||||||
|
responses := make(chan *PeerOrder, len(d.SubOrders))
|
||||||
|
var wg *sync.WaitGroup
|
||||||
|
wg.Add(len(d.SubOrders))
|
||||||
|
for _, b := range d.SubOrders {
|
||||||
|
go b.Pay(request, responses, wg)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
errs := ""
|
||||||
|
gotAnUnpaid := false
|
||||||
|
count := 0
|
||||||
|
for range responses {
|
||||||
|
res := <-responses
|
||||||
|
count++
|
||||||
|
if res != nil {
|
||||||
|
if res.Error != "" {
|
||||||
|
errs += res.Error
|
||||||
|
}
|
||||||
|
if res.Status != PAID {
|
||||||
|
gotAnUnpaid = true
|
||||||
|
}
|
||||||
|
d.Status = PARTIAL
|
||||||
|
d.SubOrders[res.GetID()] = res
|
||||||
|
if count == len(d.SubOrders) && !gotAnUnpaid {
|
||||||
|
d.Status = PAID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if errs != "" {
|
||||||
|
return errors.New(errs)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type PeerOrder struct {
|
||||||
|
utils.AbstractObject
|
||||||
|
Error string `json:"error,omitempty" bson:"error,omitempty"`
|
||||||
|
PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
|
||||||
|
Status OrderStatus `json:"status" bson:"status" default:"0"`
|
||||||
|
BillingAddress string `json:"billing_address,omitempty" bson:"billing_address,omitempty"`
|
||||||
|
Items []*PeerItemOrder `json:"items,omitempty" bson:"items,omitempty"`
|
||||||
|
Total float64 `json:"total,omitempty" bson:"total,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *PeerOrder) Pay(request *tools.APIRequest, response chan *PeerOrder, wg *sync.WaitGroup) {
|
||||||
|
d.Status = PENDING
|
||||||
|
go func() {
|
||||||
|
// DO SOMETHING TO PAY ON BLOCKCHAIN OR WHATEVER ON RETURN UPDATE STATUS
|
||||||
|
d.Status = PAID // TO REMOVE LATER IT'S A MOCK
|
||||||
|
if d.Status == PAID {
|
||||||
|
for _, b := range d.Items {
|
||||||
|
if !b.Item.IsPurchased() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
accessor := purchase_resource.NewAccessor(request)
|
||||||
|
accessor.StoreOne(&purchase_resource.PurchaseResource{
|
||||||
|
ResourceID: b.Item.GetID(),
|
||||||
|
ResourceType: b.Item.GetType(),
|
||||||
|
EndDate: b.Item.GetLocationEnd(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.Status != PENDING {
|
||||||
|
response <- d
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *PeerOrder) SumUpBill(request *tools.APIRequest) error {
|
||||||
|
for _, b := range d.Items {
|
||||||
|
tot, err := b.GetPrice(request) // missing something
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.Total += tot
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *PeerOrder) AddItem(item pricing.PricedItemITF, quantity int) {
|
||||||
|
d.Items = append(d.Items, &PeerItemOrder{
|
||||||
|
Quantity: quantity,
|
||||||
|
Item: item,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *PeerOrder) SetName() {
|
||||||
|
d.Name = d.UUID + "_order_" + d.PeerID + "_" + time.Now().UTC().Format("2006-01-02T15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
type PeerItemOrder struct {
|
||||||
|
Quantity int `json:"quantity,omitempty" bson:"quantity,omitempty"`
|
||||||
|
Purchase purchase_resource.PurchaseResource `json:"purchase,omitempty" bson:"purchase,omitempty"`
|
||||||
|
Item pricing.PricedItemITF `json:"item,omitempty" bson:"item,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *PeerItemOrder) GetPrice(request *tools.APIRequest) (float64, error) {
|
||||||
|
accessor := purchase_resource.NewAccessor(request)
|
||||||
|
search, code, _ := accessor.Search(&dbs.Filters{
|
||||||
|
And: map[string][]dbs.Filter{
|
||||||
|
"resource_id": {{Operator: dbs.EQUAL.String(), Value: d.Item.GetID()}},
|
||||||
|
},
|
||||||
|
}, "", d.Purchase.IsDraft)
|
||||||
|
if code == 200 && len(search) > 0 {
|
||||||
|
for _, s := range search {
|
||||||
|
if s.(*purchase_resource.PurchaseResource).EndDate == nil || time.Now().UTC().After(*s.(*purchase_resource.PurchaseResource).EndDate) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p, err := d.Item.GetPrice()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return p * float64(d.Quantity), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SHOULD SET A BUYING STATUS WHEN PAYMENT IS VALIDATED
|
||||||
64
models/order/order_mongo_accessor.go
Normal file
64
models/order/order_mongo_accessor.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package order
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type orderMongoAccessor struct {
|
||||||
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the orderMongoAccessor
|
||||||
|
func NewAccessor(request *tools.APIRequest) *orderMongoAccessor {
|
||||||
|
return &orderMongoAccessor{
|
||||||
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.ORDER.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: tools.ORDER,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Nothing special here, just the basic CRUD operations
|
||||||
|
*/
|
||||||
|
func (a *orderMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericDeleteOne(id, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *orderMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericUpdateOne(set, id, a, &Order{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *orderMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return nil, 404, errors.New("Not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *orderMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return nil, 404, errors.New("Not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *orderMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericLoadOne[*Order](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return d, 200, nil
|
||||||
|
}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *orderMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericLoadAll[*Order](a.getExec(), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *orderMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericSearch[*Order](filters, search, (&Order{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *orderMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +1,40 @@
|
|||||||
package peer
|
package peer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"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"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// now write a go enum for the state partner with self, blacklist, partner
|
||||||
|
|
||||||
|
type PeerState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
NONE PeerState = iota
|
||||||
|
SELF
|
||||||
|
PARTNER
|
||||||
|
BLACKLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m PeerState) String() string {
|
||||||
|
return [...]string{"NONE", "SELF", "PARTNER", "BLACKLIST"}[m]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m PeerState) EnumIndex() int {
|
||||||
|
return int(m)
|
||||||
|
}
|
||||||
|
|
||||||
// Peer is a struct that represents a peer
|
// Peer is a struct that represents a peer
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
utils.AbstractObject
|
utils.AbstractObject
|
||||||
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"` // Url is the URL of the peer (base64url)
|
Url string `json:"url" bson:"url" validate:"required"` // 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
|
WalletAddress string `json:"wallet_address" bson:"wallet_address" validate:"required"` // WalletAddress is the wallet address of the peer
|
||||||
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"` // Services is the services of the peer
|
PublicKey string `json:"public_key" bson:"public_key" validate:"required"` // PublicKey is the public key of the peer
|
||||||
FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"` // FailedExecution is the list of failed executions, to be retried
|
State PeerState `json:"state" bson:"state" default:"0"`
|
||||||
|
ServicesState map[string]int `json:"services_state,omitempty" bson:"services_state,omitempty"`
|
||||||
|
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
|
// AddExecution adds an execution to the list of failed executions
|
||||||
@@ -45,50 +63,25 @@ func (ao *Peer) RemoveExecution(exec PeerExecution) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsMySelf checks if the peer is the local peer
|
// IsMySelf checks if the peer is the local peer
|
||||||
func (ao *Peer) IsMySelf() bool {
|
func (p *Peer) IsMySelf() (bool, string) {
|
||||||
id, _ := static.GetMyLocalJsonPeer()
|
d, code, err := NewAccessor(nil).Search(nil, SELF.String(), p.IsDraft)
|
||||||
return ao.UUID == id
|
if code != 200 || err != nil || len(d) == 0 {
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
id := d[0].GetID()
|
||||||
|
return p.UUID == id, id
|
||||||
}
|
}
|
||||||
|
|
||||||
// LaunchPeerExecution launches an execution on a peer
|
// 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) {
|
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
|
||||||
p.UUID = peerID
|
p.UUID = peerID
|
||||||
return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache
|
return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache
|
||||||
}
|
}
|
||||||
|
func (d *Peer) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
func (ao *Peer) GetID() string {
|
data := NewAccessor(request) // Create a new instance of the accessor
|
||||||
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
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *Peer) Deserialize(j map[string]interface{}) utils.DBObject {
|
func (r *Peer) CanDelete() bool {
|
||||||
b, err := json.Marshal(j)
|
return false // only draft order can be deleted
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,18 +29,26 @@ type PeerCache struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// urlFormat formats the URL of the peer with the data type API function
|
// urlFormat formats the URL of the peer with the data type API function
|
||||||
func (p *PeerCache) urlFormat(url string, dt utils.DataType) string {
|
func (p *PeerCache) urlFormat(url string, dt tools.DataType) string {
|
||||||
// localhost is replaced by the local peer URL
|
// localhost is replaced by the local peer URL
|
||||||
// because localhost must collide on a web request security protocol
|
// because localhost must collide on a web request security protocol
|
||||||
if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") {
|
localhost := ""
|
||||||
url = strings.ReplaceAll(url, "localhost", dt.API())
|
if strings.Contains(url, "localhost") {
|
||||||
url = strings.ReplaceAll(url, "127.0.0.1", dt.API())
|
localhost = "localhost"
|
||||||
r := regexp.MustCompile("(:[0-9]+)")
|
}
|
||||||
|
if strings.Contains(url, "127.0.0.1") {
|
||||||
|
localhost = "127.0.0.1"
|
||||||
|
}
|
||||||
|
if localhost != "" {
|
||||||
|
r := regexp.MustCompile("(" + localhost + ":[0-9]+)")
|
||||||
t := r.FindString(url)
|
t := r.FindString(url)
|
||||||
if t != "" {
|
if t != "" {
|
||||||
url = strings.Replace(url, t, ":8080", -1)
|
url = strings.Replace(url, t, dt.API()+":8080/oc", -1)
|
||||||
|
} else {
|
||||||
|
url = strings.ReplaceAll(url, localhost, dt.API()+":8080/oc")
|
||||||
}
|
}
|
||||||
r.ReplaceAllString(url, ":8080")
|
} else {
|
||||||
|
url = url + "/" + dt.API()
|
||||||
}
|
}
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
@@ -49,12 +56,12 @@ func (p *PeerCache) urlFormat(url string, dt utils.DataType) string {
|
|||||||
// checkPeerStatus checks the status of a peer
|
// checkPeerStatus checks the status of a peer
|
||||||
func (p *PeerCache) checkPeerStatus(peerID string, appName string, caller *tools.HTTPCaller) (*Peer, bool) {
|
func (p *PeerCache) checkPeerStatus(peerID string, appName string, caller *tools.HTTPCaller) (*Peer, bool) {
|
||||||
api := tools.API{}
|
api := tools.API{}
|
||||||
access := (&Peer{}).GetAccessor(nil)
|
access := NewShallowAccessor()
|
||||||
res, code, _ := access.LoadOne(peerID) // Load the peer from db
|
res, code, _ := access.LoadOne(peerID) // Load the peer from db
|
||||||
if code != 200 { // no peer no party
|
if code != 200 { // no peer no party
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
methods := caller.URLS[utils.PEER.String()] // Get the methods url of the peer
|
methods := caller.URLS[tools.PEER] // Get the methods url of the peer
|
||||||
if methods == nil {
|
if methods == nil {
|
||||||
return res.(*Peer), false
|
return res.(*Peer), false
|
||||||
}
|
}
|
||||||
@@ -62,29 +69,29 @@ func (p *PeerCache) checkPeerStatus(peerID string, appName string, caller *tools
|
|||||||
if meth == "" {
|
if meth == "" {
|
||||||
return res.(*Peer), false
|
return res.(*Peer), false
|
||||||
}
|
}
|
||||||
url := p.urlFormat(res.(*Peer).Url+meth, utils.PEER) // Format the URL
|
url := p.urlFormat(res.(*Peer).Url, tools.PEER) + meth // Format the URL
|
||||||
state, services := api.CheckRemotePeer(url) // Check the status of the peer
|
fmt.Println("Checking peer status on", url, "...")
|
||||||
res.(*Peer).Services = services // Update the services states of the peer
|
state, services := api.CheckRemotePeer(url)
|
||||||
|
fmt.Println("Checking peer status on", url, state, services) // Check the status of the peer
|
||||||
|
res.(*Peer).ServicesState = services // Update the services states of the peer
|
||||||
access.UpdateOne(res, peerID) // Update the peer in the db
|
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
|
return res.(*Peer), state != tools.DEAD && services[appName] == 0 // Return the peer and its status
|
||||||
}
|
}
|
||||||
|
|
||||||
// LaunchPeerExecution launches an execution on a peer
|
// LaunchPeerExecution launches an execution on a peer
|
||||||
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
||||||
dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
|
dt tools.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
|
fmt.Println("Launching peer execution on", caller.URLS, dt, method)
|
||||||
if _, ok := methods[method]; !ok {
|
methods := caller.URLS[dt] // Get the methods url of the data type
|
||||||
|
if m, ok := methods[method]; !ok || m == "" {
|
||||||
return nil, errors.New("no path found")
|
return nil, errors.New("no path found")
|
||||||
}
|
}
|
||||||
meth := methods[method] // Get the method url to execute
|
meth := methods[method] // Get the method url to execute
|
||||||
if meth == "" {
|
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)
|
||||||
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 := ""
|
url := ""
|
||||||
|
|
||||||
// Check the status of the peer
|
// Check the status of the peer
|
||||||
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok {
|
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok && mypeer != nil {
|
||||||
// If the peer is not reachable, add the execution to the failed executions list
|
// If the peer is not reachable, add the execution to the failed executions list
|
||||||
pexec := &PeerExecution{
|
pexec := &PeerExecution{
|
||||||
Method: method.String(),
|
Method: method.String(),
|
||||||
@@ -94,18 +101,22 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
|||||||
DataID: dataID,
|
DataID: dataID,
|
||||||
}
|
}
|
||||||
mypeer.AddExecution(*pexec)
|
mypeer.AddExecution(*pexec)
|
||||||
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID) // Update the peer in the db
|
NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
|
||||||
return nil, errors.New("peer is not reachable")
|
return nil, errors.New("peer is not reachable")
|
||||||
} else {
|
} else {
|
||||||
|
if mypeer == nil {
|
||||||
|
return nil, errors.New("peer not found")
|
||||||
|
}
|
||||||
// If the peer is reachable, launch the execution
|
// If the peer is reachable, launch the execution
|
||||||
url = p.urlFormat((mypeer.Url)+meth, dt) // Format the URL
|
url = p.urlFormat((mypeer.Url)+meth, dt) // Format the URL
|
||||||
tmp := mypeer.FailedExecution // Get the failed executions list
|
tmp := mypeer.FailedExecution // Get the failed executions list
|
||||||
mypeer.FailedExecution = []PeerExecution{} // Reset the failed executions list
|
mypeer.FailedExecution = []PeerExecution{} // Reset the failed executions list
|
||||||
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID) // Update the peer in the db
|
NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
|
||||||
for _, v := range tmp { // Retry the failed executions
|
for _, v := range tmp { // Retry the failed executions
|
||||||
go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
|
go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Println("URL exec", url)
|
||||||
return nil, p.exec(url, method, body, caller) // Execute the method
|
return nil, p.exec(url, method, body, caller) // Execute the method
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,9 +138,8 @@ func (p *PeerCache) exec(url string, method tools.METHOD, body map[string]interf
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if e, ok := m["error"]; ok && e != "<nil>" && e != "" { // Check if there is an error in the response
|
||||||
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 errors.New(fmt.Sprintf("%v", m["error"]))
|
||||||
}
|
}
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package peer
|
package peer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"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/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type peerMongoAccessor struct {
|
type peerMongoAccessor struct {
|
||||||
@@ -11,8 +14,23 @@ type peerMongoAccessor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the peerMongoAccessor
|
// New creates a new instance of the peerMongoAccessor
|
||||||
func New() *peerMongoAccessor {
|
func NewShallowAccessor() *peerMongoAccessor {
|
||||||
return &peerMongoAccessor{}
|
return &peerMongoAccessor{
|
||||||
|
utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
||||||
|
Type: tools.PEER,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
|
||||||
|
return &peerMongoAccessor{
|
||||||
|
utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: tools.PEER,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -20,70 +38,53 @@ func New() *peerMongoAccessor {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
func (wfa *peerMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericDeleteOne(id, wfa)
|
return utils.GenericDeleteOne(id, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (wfa *peerMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericUpdateOne(set.(*Peer), id, wfa, &Peer{})
|
return utils.GenericUpdateOne(set.(*Peer), id, wfa, &Peer{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (wfa *peerMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data.(*Peer), wfa)
|
return utils.GenericStoreOne(data.(*Peer), wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (wfa *peerMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return utils.GenericStoreOne(data, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (dca *peerMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
var peer Peer
|
return utils.GenericLoadOne[*Peer](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
return d, 200, nil
|
||||||
if err != nil {
|
}, dca)
|
||||||
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) {
|
func (wfa *peerMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
objs := []utils.ShallowDBObject{}
|
return utils.GenericLoadAll[*Peer](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
return d
|
||||||
if err != nil {
|
}, isDraft, wfa)
|
||||||
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) {
|
func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
objs := []utils.ShallowDBObject{}
|
return utils.GenericSearch[*Peer](filters, search, wfa.getDefaultFilter(search),
|
||||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
filters = &dbs.Filters{
|
return d
|
||||||
|
}, isDraft, wfa)
|
||||||
|
}
|
||||||
|
func (a *peerMongoAccessor) getDefaultFilter(search string) *dbs.Filters {
|
||||||
|
s, err := strconv.Atoi(search)
|
||||||
|
if err == nil {
|
||||||
|
return &dbs.Filters{
|
||||||
|
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
||||||
|
"state": {{Operator: dbs.EQUAL.String(), Value: s}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return &dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
||||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||||
|
"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 []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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,131 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
166
models/resources/compute.go
Normal file
166
models/resources/compute.go
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ComputeResource is a struct that represents a compute resource
|
||||||
|
* it defines the resource compute
|
||||||
|
*/
|
||||||
|
type ComputeResource struct {
|
||||||
|
AbstractIntanciatedResource[*ComputeResourceInstance]
|
||||||
|
Architecture string `json:"architecture,omitempty" bson:"architecture,omitempty"` // Architecture is the architecture
|
||||||
|
Infrastructure common.InfrastructureType `json:"infrastructure,omitempty" bson:"infrastructure,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ComputeResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor[*ComputeResource](tools.COMPUTE_RESOURCE, request, func() utils.DBObject { return &ComputeResource{} })
|
||||||
|
}
|
||||||
|
|
||||||
|
type ComputeResourceInstance struct {
|
||||||
|
ResourceInstance[*ComputeResourcePartnership]
|
||||||
|
SecurityLevel string `json:"security_level,omitempty" bson:"security_level,omitempty"`
|
||||||
|
PowerSource string `json:"power_source,omitempty" bson:"power_source,omitempty"`
|
||||||
|
CPUs map[string]*common.CPU `bson:"cpus,omitempty" json:"cpus,omitempty"` // CPUs is the list of CPUs key is model
|
||||||
|
GPUs map[string]*common.GPU `bson:"gpus,omitempty" json:"gpus,omitempty"` // GPUs is the list of GPUs key is model
|
||||||
|
RAM *common.RAM `bson:"ram,omitempty" json:"ram,omitempty"` // RAM is the RAM
|
||||||
|
}
|
||||||
|
|
||||||
|
type ComputeResourcePartnership struct {
|
||||||
|
ResourcePartnerShip[*ComputeResourcePricingProfile]
|
||||||
|
MaxAllowedCPUsCores map[string]int `json:"allowed_cpus,omitempty" bson:"allowed_cpus,omitempty"`
|
||||||
|
MaxAllowedGPUsMemoryGB map[string]float64 `json:"allowed_gpus,omitempty" bson:"allowed_gpus,omitempty"`
|
||||||
|
MaxAllowedRAMSize float64 `json:"allowed_ram,omitempty" bson:"allowed_ram,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ComputeResourcePricingProfileOptions struct {
|
||||||
|
CPUCore int `json:"cpu_core" bson:"cpu_core" default:"1"`
|
||||||
|
GPUMemoryGB float64 `json:"gpu_memory_gb" bson:"gpu_memory_gb" default:"1"`
|
||||||
|
RAMSizeGB float64 `json:"ram_size_gb" bson:"ram_size_gb" default:"1"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ComputeResourcePricingProfile struct {
|
||||||
|
pricing.ExploitPricingProfile[pricing.TimePricingStrategy]
|
||||||
|
Options ComputeResourcePricingProfileOptions `json:"options,omitempty" bson:"options,omitempty"` // Options is the options of the pricing profile
|
||||||
|
// ExploitPricingProfile is the pricing profile of a compute it means that we exploit the resource for an amount of continuous time
|
||||||
|
OverrideCPUsPrices map[string]float64 `json:"cpus_prices,omitempty" bson:"cpus_prices,omitempty"` // CPUsPrices is the prices of the CPUs
|
||||||
|
OverrideGPUsPrices map[string]float64 `json:"gpus_prices,omitempty" bson:"gpus_prices,omitempty"` // GPUsPrices is the prices of the GPUs
|
||||||
|
OverrideRAMPrice float64 `json:"ram_price" bson:"ram_price" default:"-1"` // RAMPrice is the price of the RAM
|
||||||
|
}
|
||||||
|
|
||||||
|
// PROBLEM
|
||||||
|
|
||||||
|
func (p *ComputeResourcePricingProfile) IsPurchased() bool {
|
||||||
|
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ComputeResourcePricingProfile) GetOverrideStrategyValue() int {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOT A PROPER QUANTITY
|
||||||
|
// amountOfData is the number of CPUs, GPUs or RAM dependings on the params
|
||||||
|
func (p *ComputeResourcePricingProfile) GetPrice(amountOfData float64, explicitDuration float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||||
|
if len(params) < 1 {
|
||||||
|
return 0, errors.New("params must be set")
|
||||||
|
}
|
||||||
|
pp := float64(0)
|
||||||
|
model := params[1]
|
||||||
|
if strings.Contains(params[0], "cpus") && len(params) > 1 {
|
||||||
|
if _, ok := p.OverrideCPUsPrices[model]; ok {
|
||||||
|
p.Pricing.Price = p.OverrideCPUsPrices[model]
|
||||||
|
}
|
||||||
|
r, err := p.Pricing.GetPrice(amountOfData/float64(p.Options.CPUCore), explicitDuration, start, &end)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
pp += r
|
||||||
|
|
||||||
|
}
|
||||||
|
if strings.Contains(params[0], "gpus") && len(params) > 1 {
|
||||||
|
if _, ok := p.OverrideGPUsPrices[model]; ok {
|
||||||
|
p.Pricing.Price = p.OverrideGPUsPrices[model]
|
||||||
|
}
|
||||||
|
r, err := p.Pricing.GetPrice(amountOfData/float64(p.Options.GPUMemoryGB), explicitDuration, start, &end)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
pp += r
|
||||||
|
}
|
||||||
|
if strings.Contains(params[0], "ram") {
|
||||||
|
if p.OverrideRAMPrice >= 0 {
|
||||||
|
p.Pricing.Price = p.OverrideRAMPrice
|
||||||
|
}
|
||||||
|
r, err := p.Pricing.GetPrice(float64(amountOfData)/p.Options.RAMSizeGB, explicitDuration, start, &end)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
pp += r
|
||||||
|
}
|
||||||
|
return pp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type PricedComputeResource struct {
|
||||||
|
PricedResource
|
||||||
|
|
||||||
|
CPUsLocated map[string]float64 `json:"cpus_in_use" bson:"cpus_in_use"` // CPUsInUse is the list of CPUs in use
|
||||||
|
GPUsLocated map[string]float64 `json:"gpus_in_use" bson:"gpus_in_use"` // GPUsInUse is the list of GPUs in use
|
||||||
|
RAMLocated float64 `json:"ram_in_use" bson:"ram_in_use"` // RAMInUse is the RAM in use
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedComputeResource) GetType() tools.DataType {
|
||||||
|
return tools.COMPUTE_RESOURCE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedComputeResource) GetPrice() (float64, error) {
|
||||||
|
if r.UsageStart == nil || r.UsageEnd == nil {
|
||||||
|
return 0, errors.New("Usage start and end must be set")
|
||||||
|
}
|
||||||
|
if r.SelectedPricing == nil {
|
||||||
|
return 0, errors.New("Selected pricing must be set")
|
||||||
|
}
|
||||||
|
pricing := *r.SelectedPricing
|
||||||
|
price := float64(0)
|
||||||
|
for _, l := range []map[string]float64{r.CPUsLocated, r.GPUsLocated} {
|
||||||
|
for model, amountOfData := range l {
|
||||||
|
cpus, err := pricing.GetPrice(float64(amountOfData), r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd, "cpus", model)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
price += cpus
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ram, err := pricing.GetPrice(r.RAMLocated, r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd, "ram")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
price += ram
|
||||||
|
return price, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* FillWithDefaultProcessingUsage fills the order item with the default processing usage
|
||||||
|
* it depends on the processing usage only if nothing is set, during order
|
||||||
|
*/
|
||||||
|
func (i *PricedComputeResource) FillWithDefaultProcessingUsage(usage *ProcessingUsage) {
|
||||||
|
for _, cpu := range usage.CPUs {
|
||||||
|
if _, ok := i.CPUsLocated[cpu.Model]; !ok {
|
||||||
|
i.CPUsLocated[cpu.Model] = 0
|
||||||
|
}
|
||||||
|
if i.CPUsLocated[cpu.Model] < float64(cpu.Cores) {
|
||||||
|
i.CPUsLocated[cpu.Model] = float64(cpu.Cores)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, cpu := range usage.GPUs {
|
||||||
|
i.GPUsLocated[cpu.Model] = 1
|
||||||
|
}
|
||||||
|
i.RAMLocated = usage.RAM.SizeGb
|
||||||
|
}
|
||||||
130
models/resources/data.go
Normal file
130
models/resources/data.go
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
// enum of public private or licenced data
|
||||||
|
type DataLicense int
|
||||||
|
|
||||||
|
const (
|
||||||
|
PUBLIC DataLicense = iota
|
||||||
|
PRIVATE
|
||||||
|
LICENCED
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DataResource is a struct that represents a data resource
|
||||||
|
* it defines the resource data
|
||||||
|
*/
|
||||||
|
type DataResource struct {
|
||||||
|
AbstractIntanciatedResource[*ResourceInstance[*DataResourcePartnership]]
|
||||||
|
Type string `bson:"type,omitempty" json:"type,omitempty"`
|
||||||
|
Quality string `bson:"quality,omitempty" json:"quality,omitempty"`
|
||||||
|
OpenData bool `bson:"open_data" json:"open_data" default:"false"` // Type is the type of the storage
|
||||||
|
Static bool `bson:"static" json:"static" default:"false"`
|
||||||
|
UpdatePeriod time.Time `bson:"update_period,omitempty" json:"update_period,omitempty"`
|
||||||
|
PersonalData bool `bson:"personal_data,omitempty" json:"personal_data,omitempty"`
|
||||||
|
AnonymizedPersonalData bool `bson:"anonymized_personal_data,omitempty" json:"anonymized_personal_data,omitempty"`
|
||||||
|
SizeGB float64 `json:"size_gb,omitempty" bson:"size_gb,omitempty"` // SizeGB is the size of the data License DataLicense `json:"license" bson:"license" description:"license of the data" default:"0"` // License is the license of the data
|
||||||
|
// ? Interest DataLicense `json:"interest" bson:"interest" description:"interest of the data" default:"0"` // Interest is the interest of the data
|
||||||
|
Example string `json:"example,omitempty" bson:"example,omitempty" description:"base64 encoded data"` // Example is an example of the data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DataResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor[*DataResource](tools.DATA_RESOURCE, request, func() utils.DBObject { return &DataResource{} }) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
type DataResourcePartnership struct {
|
||||||
|
ResourcePartnerShip[*DataResourcePricingProfile]
|
||||||
|
MaxDownloadableGbAllowed float64 `json:"allowed_gb,omitempty" bson:"allowed_gb,omitempty"`
|
||||||
|
PersonalDataAllowed bool `json:"personal_data_allowed,omitempty" bson:"personal_data_allowed,omitempty"`
|
||||||
|
AnonymizedPersonalDataAllowed bool `json:"anonymized_personal_data_allowed,omitempty" bson:"anonymized_personal_data_allowed,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DataResourcePricingStrategy int
|
||||||
|
|
||||||
|
const (
|
||||||
|
PER_DOWNLOAD DataResourcePricingStrategy = iota
|
||||||
|
PER_TB_DOWNLOADED
|
||||||
|
PER_GB_DOWNLOADED
|
||||||
|
PER_MB_DOWNLOADED
|
||||||
|
PER_KB_DOWNLOADED
|
||||||
|
)
|
||||||
|
|
||||||
|
func ToDataResourcePricingStrategy(i int) DataResourcePricingStrategy {
|
||||||
|
return DataResourcePricingStrategy(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t DataResourcePricingStrategy) GetStrategy() string {
|
||||||
|
return [...]string{"PER_DOWNLOAD", "PER_GB", "PER_MB", "PER_KB"}[t]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t DataResourcePricingStrategy) GetStrategyValue() int {
|
||||||
|
return int(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t DataResourcePricingStrategy) GetQuantity(amountOfDataGB float64) (float64, error) {
|
||||||
|
switch t {
|
||||||
|
case PER_DOWNLOAD:
|
||||||
|
return 1, nil
|
||||||
|
case PER_TB_DOWNLOADED:
|
||||||
|
return amountOfDataGB * 1000, nil
|
||||||
|
case PER_GB_DOWNLOADED:
|
||||||
|
return amountOfDataGB, nil
|
||||||
|
case PER_MB_DOWNLOADED:
|
||||||
|
return amountOfDataGB / 1000, nil
|
||||||
|
case PER_KB_DOWNLOADED:
|
||||||
|
return amountOfDataGB / 1000000, nil
|
||||||
|
}
|
||||||
|
return 0, errors.New("Pricing strategy not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
type DataResourcePricingProfile struct {
|
||||||
|
pricing.AccessPricingProfile[DataResourcePricingStrategy] // AccessPricingProfile is the pricing profile of a data it means that we can access the data for an amount of time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DataResourcePricingProfile) GetOverrideStrategyValue() int {
|
||||||
|
return p.Pricing.OverrideStrategy.GetStrategyValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DataResourcePricingProfile) GetPrice(amountOfData float64, explicitDuration float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||||
|
return p.Pricing.GetPrice(amountOfData, explicitDuration, start, &end)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DataResourcePricingProfile) IsPurchased() bool {
|
||||||
|
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||||
|
}
|
||||||
|
|
||||||
|
type PricedDataResource struct {
|
||||||
|
PricedResource
|
||||||
|
UsageStorageGB float64 `json:"storage_gb,omitempty" bson:"storage_gb,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedDataResource) GetType() tools.DataType {
|
||||||
|
return tools.DATA_RESOURCE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedDataResource) GetPrice() (float64, error) {
|
||||||
|
if r.UsageStart == nil || r.UsageEnd == nil {
|
||||||
|
return 0, errors.New("Usage start and end must be set")
|
||||||
|
}
|
||||||
|
if r.SelectedPricing == nil {
|
||||||
|
return 0, errors.New("Selected pricing must be set")
|
||||||
|
}
|
||||||
|
pricing := *r.SelectedPricing
|
||||||
|
var err error
|
||||||
|
amountOfData := float64(1)
|
||||||
|
if pricing.GetOverrideStrategyValue() >= 0 {
|
||||||
|
amountOfData, err = ToDataResourcePricingStrategy(pricing.GetOverrideStrategyValue()).GetQuantity(r.UsageStorageGB)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pricing.GetPrice(amountOfData, r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd)
|
||||||
|
}
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
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"`
|
|
||||||
}
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
48
models/resources/interfaces.go
Normal file
48
models/resources/interfaces.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ShallowResourceInterface interface {
|
||||||
|
utils.DBObject
|
||||||
|
GetType() tools.DataType
|
||||||
|
GetCreatorID() string
|
||||||
|
GetPricingID() string
|
||||||
|
GetLocationStart() *time.Time
|
||||||
|
GetLocationEnd() *time.Time
|
||||||
|
GetExplicitDurationInS() float64
|
||||||
|
SetStartUsage(start time.Time)
|
||||||
|
SetEndUsage(end time.Time)
|
||||||
|
GetPartnership(request *tools.APIRequest) ResourcePartnerITF
|
||||||
|
SetResourceModel(model *resource_model.ResourceModel)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourceInterface interface {
|
||||||
|
utils.DBObject
|
||||||
|
Trim()
|
||||||
|
Transform() utils.DBObject
|
||||||
|
ConvertToPricedResource(t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF
|
||||||
|
SetAllowedInstances(request *tools.APIRequest)
|
||||||
|
SetResourceModel(model *resource_model.ResourceModel)
|
||||||
|
SetSelection(selection *int)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourceInstanceITF interface {
|
||||||
|
GetID() string
|
||||||
|
GetName() string
|
||||||
|
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
||||||
|
GetPeerGroups() ([]ResourcePartnerITF, []map[string][]string)
|
||||||
|
ClearPeerGroups()
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourcePartnerITF interface {
|
||||||
|
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
||||||
|
GetPeerGroups() map[string][]string
|
||||||
|
ClearPeerGroups()
|
||||||
|
}
|
||||||
64
models/resources/models.go
Normal file
64
models/resources/models.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
Computes []string `bson:"computes,omitempty" json:"computes,omitempty"`
|
||||||
|
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
|
||||||
|
|
||||||
|
DataResources []*DataResource `bson:"-" json:"data_resources,omitempty"`
|
||||||
|
StorageResources []*StorageResource `bson:"-" json:"storage_resources,omitempty"`
|
||||||
|
ProcessingResources []*ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
|
||||||
|
ComputeResources []*ComputeResource `bson:"-" json:"compute_resources,omitempty"`
|
||||||
|
WorkflowResources []*WorkflowResource `bson:"-" json:"workflow_resources,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ResourceSet) Clear() {
|
||||||
|
r.DataResources = nil
|
||||||
|
r.StorageResources = nil
|
||||||
|
r.ProcessingResources = nil
|
||||||
|
r.ComputeResources = nil
|
||||||
|
r.WorkflowResources = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ResourceSet) Fill(request *tools.APIRequest) {
|
||||||
|
for k, v := range map[utils.DBObject][]string{
|
||||||
|
(&DataResource{}): r.Datas,
|
||||||
|
(&ComputeResource{}): r.Computes,
|
||||||
|
(&StorageResource{}): r.Storages,
|
||||||
|
(&ProcessingResource{}): r.Processings,
|
||||||
|
(&WorkflowResource{}): r.Workflows,
|
||||||
|
} {
|
||||||
|
for _, id := range v {
|
||||||
|
d, _, e := k.GetAccessor(request).LoadOne(id)
|
||||||
|
if e == nil {
|
||||||
|
switch k.(type) {
|
||||||
|
case *DataResource:
|
||||||
|
r.DataResources = append(r.DataResources, d.(*DataResource))
|
||||||
|
case *ComputeResource:
|
||||||
|
r.ComputeResources = append(r.ComputeResources, d.(*ComputeResource))
|
||||||
|
case *StorageResource:
|
||||||
|
r.StorageResources = append(r.StorageResources, d.(*StorageResource))
|
||||||
|
case *ProcessingResource:
|
||||||
|
r.ProcessingResources = append(r.ProcessingResources, d.(*ProcessingResource))
|
||||||
|
case *WorkflowResource:
|
||||||
|
r.WorkflowResources = append(r.WorkflowResources, d.(*WorkflowResource))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ItemResource struct {
|
||||||
|
Data *DataResource `bson:"data,omitempty" json:"data,omitempty"`
|
||||||
|
Processing *ProcessingResource `bson:"processing,omitempty" json:"processing,omitempty"`
|
||||||
|
Storage *StorageResource `bson:"storage,omitempty" json:"storage,omitempty"`
|
||||||
|
Compute *ComputeResource `bson:"compute,omitempty" json:"compute,omitempty"`
|
||||||
|
Workflow *WorkflowResource `bson:"workflow,omitempty" json:"workflow,omitempty"`
|
||||||
|
}
|
||||||
90
models/resources/priced_resource.go
Normal file
90
models/resources/priced_resource.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PricedResource struct {
|
||||||
|
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
||||||
|
Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
|
||||||
|
InstancesRefs map[string]string `json:"instances_refs,omitempty" bson:"instances_refs,omitempty"`
|
||||||
|
PricingProfiles map[string][]pricing.PricingProfileITF `json:"pricing_profiles,omitempty" bson:"pricing_profiles,omitempty"`
|
||||||
|
SelectedPricing *pricing.PricingProfileITF `json:"selected_pricing,omitempty" bson:"selected_pricing,omitempty"`
|
||||||
|
ExplicitBookingDurationS float64 `json:"explicit_location_duration_s,omitempty" bson:"explicit_location_duration_s,omitempty"`
|
||||||
|
UsageStart *time.Time `json:"start,omitempty" bson:"start,omitempty"`
|
||||||
|
UsageEnd *time.Time `json:"end,omitempty" bson:"end,omitempty"`
|
||||||
|
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
|
||||||
|
ResourceID string `json:"resource_id,omitempty" bson:"resource_id,omitempty"`
|
||||||
|
ResourceType tools.DataType `json:"resource_type,omitempty" bson:"resource_type,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) GetID() string {
|
||||||
|
return abs.ResourceID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) GetType() tools.DataType {
|
||||||
|
return abs.ResourceType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) GetCreatorID() string {
|
||||||
|
return abs.CreatorID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) SetStartUsage(start time.Time) {
|
||||||
|
if abs.UsageStart == nil {
|
||||||
|
abs.UsageStart = &start
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) SetEndUsage(end time.Time) {
|
||||||
|
if abs.UsageEnd == nil {
|
||||||
|
abs.UsageEnd = &end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) IsPurchased() bool {
|
||||||
|
if abs.SelectedPricing == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return (*abs.SelectedPricing).IsPurchased()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) GetLocationEnd() *time.Time {
|
||||||
|
return abs.UsageEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) GetLocationStart() *time.Time {
|
||||||
|
return abs.UsageStart
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) SetLocationStart(start time.Time) {
|
||||||
|
abs.UsageStart = &start
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) SetLocationEnd(end time.Time) {
|
||||||
|
abs.UsageEnd = &end
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *PricedResource) GetExplicitDurationInS() float64 {
|
||||||
|
if abs.ExplicitBookingDurationS == 0 {
|
||||||
|
if abs.UsageEnd == nil || abs.UsageStart == nil {
|
||||||
|
return time.Duration(1 * time.Hour).Seconds()
|
||||||
|
}
|
||||||
|
return abs.UsageEnd.Sub(*abs.UsageStart).Seconds()
|
||||||
|
}
|
||||||
|
return abs.ExplicitBookingDurationS
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedResource) GetPrice() (float64, error) {
|
||||||
|
if r.UsageStart == nil || r.UsageEnd == nil {
|
||||||
|
return 0, errors.New("Usage start and end must be set")
|
||||||
|
}
|
||||||
|
if r.SelectedPricing == nil {
|
||||||
|
return 0, errors.New("Selected pricing must be set")
|
||||||
|
}
|
||||||
|
return (*r.SelectedPricing).GetPrice(1, 0, *r.UsageStart, *r.UsageEnd)
|
||||||
|
}
|
||||||
73
models/resources/processing.go
Normal file
73
models/resources/processing.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProcessingUsage struct {
|
||||||
|
CPUs map[string]*common.CPU `bson:"cpus,omitempty" json:"cpus,omitempty"` // CPUs is the list of CPUs key is model
|
||||||
|
GPUs map[string]*common.GPU `bson:"gpus,omitempty" json:"gpus,omitempty"` // GPUs is the list of GPUs key is model
|
||||||
|
RAM *common.RAM `bson:"ram,omitempty" json:"ram,omitempty"` // RAM is the RAM
|
||||||
|
|
||||||
|
StorageGb float64 `bson:"storage,omitempty" json:"storage,omitempty"` // Storage is the storage
|
||||||
|
Hypothesis string `bson:"hypothesis,omitempty" json:"hypothesis,omitempty"`
|
||||||
|
ScalingModel string `bson:"scaling_model,omitempty" json:"scaling_model,omitempty"` // ScalingModel is the scaling model
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ProcessingResource is a struct that represents a processing resource
|
||||||
|
* it defines the resource processing
|
||||||
|
*/
|
||||||
|
type ProcessingResource struct {
|
||||||
|
AbstractIntanciatedResource[*ResourceInstance[*ResourcePartnerShip[*ProcessingResourcePricingProfile]]]
|
||||||
|
Infrastructure common.InfrastructureType `json:"infrastructure,omitempty" bson:"infrastructure,omitempty"`
|
||||||
|
IsService bool `json:"is_service,omitempty" bson:"is_service,omitempty"` // IsService is a flag that indicates if the processing is a service
|
||||||
|
Usage *ProcessingUsage `bson:"usage,omitempty" json:"usage,omitempty"` // Usage is the usage of the processing
|
||||||
|
OpenSource bool `json:"open_source" bson:"open_source" default:"false"`
|
||||||
|
License string `json:"license,omitempty" bson:"license,omitempty"`
|
||||||
|
Maturity string `json:"maturity,omitempty" bson:"maturity,omitempty"`
|
||||||
|
Container *common.Container `json:"container,omitempty" bson:"container,omitempty"` // Container is the container
|
||||||
|
}
|
||||||
|
|
||||||
|
type PricedProcessingResource struct {
|
||||||
|
PricedResource
|
||||||
|
IsService bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedProcessingResource) GetType() tools.DataType {
|
||||||
|
return tools.PROCESSING_RESOURCE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *PricedProcessingResource) GetExplicitDurationInS() float64 {
|
||||||
|
if a.ExplicitBookingDurationS == 0 {
|
||||||
|
if a.IsService || a.UsageStart == nil {
|
||||||
|
if a.IsService {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return time.Duration(1 * time.Hour).Seconds()
|
||||||
|
}
|
||||||
|
return a.UsageEnd.Sub(*a.UsageStart).Seconds()
|
||||||
|
}
|
||||||
|
return a.ExplicitBookingDurationS
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ProcessingResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor[*ProcessingResource](tools.PROCESSING_RESOURCE, request, func() utils.DBObject { return &ProcessingResource{} }) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProcessingResourcePricingProfile struct {
|
||||||
|
pricing.AccessPricingProfile[pricing.TimePricingStrategy] // AccessPricingProfile is the pricing profile of a data it means that we can access the data for an amount of time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProcessingResourcePricingProfile) IsPurchased() bool {
|
||||||
|
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProcessingResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||||
|
return p.Pricing.GetPrice(amountOfData, val, start, &end)
|
||||||
|
}
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
30
models/resources/purchase_resource/purchase_resource.go
Normal file
30
models/resources/purchase_resource/purchase_resource.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package purchase_resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PurchaseResource struct {
|
||||||
|
utils.AbstractObject
|
||||||
|
EndDate *time.Time `json:"end_buying_date,omitempty" bson:"end_buying_date,omitempty"`
|
||||||
|
ResourceID string `json:"resource_id" bson:"resource_id" validate:"required"`
|
||||||
|
ResourceType tools.DataType `json:"resource_type" bson:"resource_type" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *PurchaseResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor(request) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PurchaseResource) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||||
|
return r.IsDraft, set // only draft buying can be updated
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PurchaseResource) CanDelete() bool { // ENDBuyingDate is passed
|
||||||
|
if r.EndDate != nil {
|
||||||
|
return time.Now().UTC().After(*r.EndDate)
|
||||||
|
}
|
||||||
|
return false // only draft bookings can be deleted
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package purchase_resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type purchaseResourceMongoAccessor struct {
|
||||||
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the bookingMongoAccessor
|
||||||
|
func NewAccessor(request *tools.APIRequest) *purchaseResourceMongoAccessor {
|
||||||
|
return &purchaseResourceMongoAccessor{
|
||||||
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.BUYING_STATUS.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: tools.BUYING_STATUS,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Nothing special here, just the basic CRUD operations
|
||||||
|
*/
|
||||||
|
func (a *purchaseResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericDeleteOne(id, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *purchaseResourceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericUpdateOne(set, id, a, &PurchaseResource{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *purchaseResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericStoreOne(data, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *purchaseResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericStoreOne(data, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *purchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericLoadOne[*PurchaseResource](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||||
|
utils.GenericDeleteOne(id, a)
|
||||||
|
return nil, 404, nil
|
||||||
|
}
|
||||||
|
return d, 200, nil
|
||||||
|
}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *purchaseResourceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericLoadAll[*PurchaseResource](a.getExec(), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *purchaseResourceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericSearch[*PurchaseResource](filters, search, (&PurchaseResource{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *purchaseResourceMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||||
|
utils.GenericDeleteOne(d.GetID(), a)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
package resources
|
package resources
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resource_model"
|
"slices"
|
||||||
"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/config"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||||
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
|
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/biter777/countries"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AbstractResource is the struct containing all of the attributes commons to all ressources
|
// AbstractResource is the struct containing all of the attributes commons to all ressources
|
||||||
@@ -14,45 +17,186 @@ import (
|
|||||||
// Resource is the interface to be implemented by all classes inheriting from Resource to have the same behavior
|
// 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
|
// 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"`
|
* AbstractResource is a struct that represents a resource
|
||||||
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
|
* it defines the resource data
|
||||||
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"`
|
type AbstractResource struct {
|
||||||
StorageResources []*storage.StorageResource `bson:"-" json:"storage_resources,omitempty"`
|
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||||
ProcessingResources []*processing.ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
|
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"` // Logo is the logo of the resource
|
||||||
DatacenterResources []*datacenter.DatacenterResource `bson:"-" json:"datacenter_resources,omitempty"`
|
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
||||||
WorkflowResources []*w.WorkflowResource `bson:"-" json:"workflow_resources,omitempty"`
|
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"` // ShortDescription is the short description of the resource
|
||||||
|
Owners []utils.Owner `json:"owners,omitempty" bson:"owners,omitempty"` // Owners is the list of owners of the resource
|
||||||
|
ResourceModel *resource_model.ResourceModel `json:"resource_model,omitempty" bson:"resource_model,omitempty"` // ResourceModel is the model of the resource
|
||||||
|
UsageRestrictions string `bson:"usage_restrictions,omitempty" json:"usage_restrictions,omitempty"`
|
||||||
|
SelectedInstanceIndex *int `json:"selected_instance_index,omitempty" bson:"selected_instance_index,omitempty"` // SelectedInstance is the selected instance
|
||||||
}
|
}
|
||||||
|
|
||||||
type ItemResource struct {
|
func (r *AbstractResource) SetSelection(selection *int) {
|
||||||
Data *data.DataResource `bson:"data,omitempty" json:"data,omitempty"`
|
r.SelectedInstanceIndex = selection
|
||||||
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 {
|
func (r *AbstractResource) Transform() utils.DBObject {
|
||||||
|
return r
|
||||||
if(i.Data != nil){
|
}
|
||||||
return &i.Data.AbstractResource
|
|
||||||
|
func (r *AbstractResource) StoreDraftDefault() {
|
||||||
|
r.IsDraft = true
|
||||||
|
}
|
||||||
|
func (r *AbstractResource) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||||
|
if r.IsDraft != set.IsDrafted() && set.IsDrafted() {
|
||||||
|
return true, set // only state can be updated
|
||||||
}
|
}
|
||||||
if(i.Processing != nil){
|
return r.IsDraft != set.IsDrafted() && set.IsDrafted(), set
|
||||||
return &i.Processing.AbstractResource
|
}
|
||||||
}
|
|
||||||
if(i.Storage != nil){
|
func (r *AbstractResource) CanDelete() bool {
|
||||||
return &i.Storage.AbstractResource
|
return r.IsDraft // only draft bookings can be deleted
|
||||||
}
|
}
|
||||||
if(i.Datacenter != nil){
|
|
||||||
return &i.Datacenter.AbstractResource
|
func (ao *AbstractResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
}
|
|
||||||
if(i.Workflow != nil){
|
|
||||||
return &i.Workflow.AbstractResource
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (abs *AbstractResource) SetResourceModel(model *resource_model.ResourceModel) {
|
||||||
|
abs.ResourceModel = model
|
||||||
|
}
|
||||||
|
|
||||||
|
type AbstractIntanciatedResource[T ResourceInstanceITF] struct {
|
||||||
|
AbstractResource // AbstractResource contains the basic fields of an object (id, name)
|
||||||
|
Instances []T `json:"instances,omitempty" bson:"instances,omitempty"` // Bill is the bill of the resource // Bill is the bill of the resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *AbstractIntanciatedResource[T]) ConvertToPricedResource(
|
||||||
|
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||||
|
instances := map[string]string{}
|
||||||
|
profiles := map[string][]pricing.PricingProfileITF{}
|
||||||
|
for _, instance := range abs.Instances {
|
||||||
|
instances[instance.GetID()] = instance.GetName()
|
||||||
|
profiles[instance.GetID()] = instance.GetPricingsProfiles(request.PeerID, request.Groups)
|
||||||
|
}
|
||||||
|
return &PricedResource{
|
||||||
|
Name: abs.Name,
|
||||||
|
Logo: abs.Logo,
|
||||||
|
ResourceID: abs.UUID,
|
||||||
|
ResourceType: t,
|
||||||
|
InstancesRefs: instances,
|
||||||
|
PricingProfiles: profiles,
|
||||||
|
CreatorID: abs.CreatorID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *AbstractIntanciatedResource[T]) SetAllowedInstances(request *tools.APIRequest) {
|
||||||
|
abs.Instances = verifyAuthAction[T](abs.Instances, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *AbstractIntanciatedResource[T]) Trim() {
|
||||||
|
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: d.CreatorID}}).IsMySelf(); !ok {
|
||||||
|
for _, instance := range d.Instances {
|
||||||
|
instance.ClearPeerGroups()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *AbstractIntanciatedResource[T]) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
|
return len(verifyAuthAction[T](abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
func verifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
||||||
|
instances := []T{}
|
||||||
|
for _, instance := range baseInstance {
|
||||||
|
_, peerGroups := instance.GetPeerGroups()
|
||||||
|
for _, peers := range peerGroups {
|
||||||
|
if request == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if grps, ok := peers[request.PeerID]; ok || config.GetConfig().Whitelist {
|
||||||
|
if (ok && slices.Contains(grps, "*")) || (!ok && config.GetConfig().Whitelist) {
|
||||||
|
instances = append(instances, instance)
|
||||||
|
}
|
||||||
|
for _, grp := range grps {
|
||||||
|
if slices.Contains(request.Groups, grp) {
|
||||||
|
instances = append(instances, instance)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instances
|
||||||
|
}
|
||||||
|
|
||||||
|
type GeoPoint struct {
|
||||||
|
Latitude float64 `json:"latitude,omitempty" bson:"latitude,omitempty"`
|
||||||
|
Longitude float64 `json:"longitude,omitempty" bson:"longitude,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourceInstance[T ResourcePartnerITF] struct {
|
||||||
|
UUID string `json:"id,omitempty" bson:"id,omitempty"`
|
||||||
|
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
||||||
|
Location GeoPoint `json:"location,omitempty" bson:"location,omitempty"`
|
||||||
|
Country countries.CountryCode `json:"country,omitempty" bson:"country,omitempty"`
|
||||||
|
AccessProtocol string `json:"access_protocol,omitempty" bson:"access_protocol,omitempty"`
|
||||||
|
Partnerships []T `json:"partnerships,omitempty" bson:"partnerships,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ri *ResourceInstance[T]) GetID() string {
|
||||||
|
return ri.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ri *ResourceInstance[T]) GetName() string {
|
||||||
|
return ri.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ri *ResourceInstance[T]) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||||
|
pricings := []pricing.PricingProfileITF{}
|
||||||
|
for _, p := range ri.Partnerships {
|
||||||
|
pricings = append(pricings, p.GetPricingsProfiles(peerID, groups)...)
|
||||||
|
}
|
||||||
|
return pricings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ri *ResourceInstance[T]) GetPeerGroups() ([]ResourcePartnerITF, []map[string][]string) {
|
||||||
|
groups := []map[string][]string{}
|
||||||
|
partners := []ResourcePartnerITF{}
|
||||||
|
for _, p := range ri.Partnerships {
|
||||||
|
partners = append(partners, p)
|
||||||
|
groups = append(groups, p.GetPeerGroups())
|
||||||
|
}
|
||||||
|
return partners, groups
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ri *ResourceInstance[T]) ClearPeerGroups() {
|
||||||
|
for _, p := range ri.Partnerships {
|
||||||
|
p.ClearPeerGroups()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourcePartnerShip[T pricing.PricingProfileITF] struct {
|
||||||
|
Namespace string `json:"namespace" bson:"namespace" default:"default-namespace"`
|
||||||
|
PeerGroups map[string][]string `json:"peer_groups,omitempty" bson:"peer_groups,omitempty"`
|
||||||
|
PricingProfiles map[string]T `json:"pricing,omitempty" bson:"pricing,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ri *ResourcePartnerShip[T]) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||||
|
if ri.PeerGroups[peerID] != nil {
|
||||||
|
for _, p := range ri.PeerGroups[peerID] {
|
||||||
|
if slices.Contains(groups, p) {
|
||||||
|
profiles := []pricing.PricingProfileITF{}
|
||||||
|
for _, ri := range ri.PricingProfiles {
|
||||||
|
profiles = append(profiles, ri)
|
||||||
|
}
|
||||||
|
return profiles
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []pricing.PricingProfileITF{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rp *ResourcePartnerShip[T]) GetPeerGroups() map[string][]string {
|
||||||
|
return rp.PeerGroups
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rp *ResourcePartnerShip[T]) ClearPeerGroups() {
|
||||||
|
rp.PeerGroups = map[string][]string{}
|
||||||
|
}
|
||||||
|
|||||||
105
models/resources/resource_accessor.go
Normal file
105
models/resources/resource_accessor.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resourceMongoAccessor[T ResourceInterface] struct {
|
||||||
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
generateData func() utils.DBObject
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the computeMongoAccessor
|
||||||
|
func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIRequest, g func() utils.DBObject) *resourceMongoAccessor[T] {
|
||||||
|
if !slices.Contains([]tools.DataType{tools.COMPUTE_RESOURCE, tools.STORAGE_RESOURCE, tools.PROCESSING_RESOURCE, tools.WORKFLOW_RESOURCE, tools.DATA_RESOURCE}, t) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &resourceMongoAccessor[T]{
|
||||||
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
|
ResourceModelAccessor: resource_model.NewAccessor(),
|
||||||
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: t,
|
||||||
|
},
|
||||||
|
generateData: g,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Nothing special here, just the basic CRUD operations
|
||||||
|
*/
|
||||||
|
|
||||||
|
func (dca *resourceMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericDeleteOne(id, dca)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dca *resourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
|
set.(T).SetResourceModel(nil)
|
||||||
|
set.(T).Trim()
|
||||||
|
return utils.GenericUpdateOne(set, id, dca, dca.generateData())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dca *resourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
data.(T).SetResourceModel(nil)
|
||||||
|
data.(T).Trim()
|
||||||
|
return utils.GenericStoreOne(data, dca)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dca *resourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return dca.StoreOne(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dca *resourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
resources, _, err := dca.ResourceModelAccessor.Search(nil, dca.GetType().String(), false)
|
||||||
|
if err == nil && len(resources) > 0 {
|
||||||
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
||||||
|
}
|
||||||
|
d.(T).SetAllowedInstances(dca.Request)
|
||||||
|
d = d.(T).Transform()
|
||||||
|
return d, 200, nil
|
||||||
|
}, dca)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wfa *resourceMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType().String(), isDraft)
|
||||||
|
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
if err == nil && len(resources) > 0 {
|
||||||
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
||||||
|
}
|
||||||
|
d.(T).SetAllowedInstances(wfa.Request)
|
||||||
|
d = d.(T).Transform()
|
||||||
|
return d
|
||||||
|
}, isDraft, wfa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wfa *resourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType().String(), false)
|
||||||
|
return utils.GenericSearch[T](filters, search, wfa.getResourceFilter(search),
|
||||||
|
func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
if err == nil && len(resources) > 0 {
|
||||||
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
||||||
|
}
|
||||||
|
d.(T).SetAllowedInstances(wfa.Request)
|
||||||
|
d = d.(T).Transform()
|
||||||
|
return d
|
||||||
|
}, isDraft, wfa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *resourceMongoAccessor[T]) getResourceFilter(search string) *dbs.Filters {
|
||||||
|
return &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}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
67
models/resources/resource_model/resource_model.go
Normal file
67
models/resources/resource_model/resource_model.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package resource_model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WebResource 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
|
||||||
|
}
|
||||||
|
|
||||||
|
type Model struct {
|
||||||
|
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type 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 {
|
||||||
|
utils.AbstractObject
|
||||||
|
ResourceType string `json:"resource_type,omitempty" bson:"resource_type,omitempty" validate:"required"`
|
||||||
|
VarRefs map[string]string `json:"var_refs,omitempty" bson:"var_refs,omitempty"` // VarRefs is the variable references of the model
|
||||||
|
Model map[string]map[string]Model `json:"model,omitempty" bson:"model,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ResourceModel) StoreDraftDefault() {
|
||||||
|
d.Name = d.ResourceType + " Resource Model"
|
||||||
|
d.IsDraft = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *ResourceModel) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ResourceModel) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return &ResourceModelMongoAccessor{
|
||||||
|
utils.AbstractAccessor{
|
||||||
|
Type: tools.RESOURCE_MODEL,
|
||||||
|
Request: request,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dma *ResourceModel) Deserialize(j map[string]interface{}, obj utils.DBObject) utils.DBObject {
|
||||||
|
b, err := json.Marshal(j)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
json.Unmarshal(b, obj)
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dma *ResourceModel) Serialize(obj utils.DBObject) map[string]interface{} {
|
||||||
|
var m map[string]interface{}
|
||||||
|
b, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
json.Unmarshal(b, &m)
|
||||||
|
return m
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package resource_model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ResourceModelMongoAccessor struct {
|
||||||
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Nothing special here, just the basic CRUD operations
|
||||||
|
*/
|
||||||
|
|
||||||
|
func NewAccessor() *ResourceModelMongoAccessor {
|
||||||
|
return &ResourceModelMongoAccessor{
|
||||||
|
utils.AbstractAccessor{
|
||||||
|
Type: tools.RESOURCE_MODEL,
|
||||||
|
Logger: logs.CreateLogger(tools.RESOURCE_MODEL.String()),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wfa *ResourceModelMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericDeleteOne(id, wfa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wfa *ResourceModelMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericUpdateOne(set, id, wfa, &ResourceModel{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wfa *ResourceModelMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericStoreOne(data, wfa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wfa *ResourceModelMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericStoreOne(data, wfa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ResourceModelMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
return utils.GenericLoadOne[*ResourceModel](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
|
return d, 200, nil
|
||||||
|
}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ResourceModelMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericLoadAll[*ResourceModel](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return d
|
||||||
|
}, isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ResourceModelMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
|
return utils.GenericSearch[*ResourceModel](filters, search,
|
||||||
|
&dbs.Filters{
|
||||||
|
Or: map[string][]dbs.Filter{
|
||||||
|
"resource_type": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||||
|
},
|
||||||
|
}, func(d utils.DBObject) utils.ShallowDBObject { return d }, isDraft, a)
|
||||||
|
}
|
||||||
142
models/resources/storage.go
Normal file
142
models/resources/storage.go
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* StorageResource is a struct that represents a storage resource
|
||||||
|
* it defines the resource storage
|
||||||
|
*/
|
||||||
|
type StorageResource struct {
|
||||||
|
AbstractIntanciatedResource[*StorageResourceInstance] // AbstractResource contains the basic fields of an object (id, name)
|
||||||
|
Type common.StorageType `bson:"type,omitempty"` // Type is the type of the storage
|
||||||
|
TypeJSON string `json:"type,omitempty"`
|
||||||
|
Acronym string `bson:"acronym,omitempty" json:"acronym,omitempty"` // Acronym is the acronym of the storage
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *StorageResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessor[*StorageResource](tools.STORAGE_RESOURCE, request, func() utils.DBObject { return &StorageResource{} }) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *StorageResource) Transform() utils.DBObject {
|
||||||
|
r.TypeJSON = r.Type.String()
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type StorageResourceInstance struct {
|
||||||
|
ResourceInstance[*StorageResourcePartnership]
|
||||||
|
Local bool `bson:"local" json:"local"`
|
||||||
|
SecurityLevel string `bson:"security_level,omitempty" json:"security_level,omitempty"`
|
||||||
|
SizeType common.StorageSize `bson:"size_type" json:"size_type" default:"0"` // SizeType is the type of the storage size
|
||||||
|
SizeGB uint `bson:"size,omitempty" json:"size,omitempty"` // Size is the size of the storage
|
||||||
|
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 (i *StorageResourceInstance) GetID() string {
|
||||||
|
return i.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
type StorageResourcePartnership struct {
|
||||||
|
ResourcePartnerShip[*StorageResourcePricingProfile]
|
||||||
|
MaxSizeGBAllowed float64 `json:"allowed_gb,omitempty" bson:"allowed_gb,omitempty"`
|
||||||
|
OnlyEncryptedAllowed bool `json:"personal_data_allowed,omitempty" bson:"personal_data_allowed,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PrivilegeStoragePricingStrategy int
|
||||||
|
|
||||||
|
const (
|
||||||
|
BASIC_STORAGE PrivilegeStoragePricingStrategy = iota
|
||||||
|
GARANTED_ON_DELAY_STORAGE
|
||||||
|
GARANTED_STORAGE
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t PrivilegeStoragePricingStrategy) String() string {
|
||||||
|
return [...]string{"BASIC_STORAGE", "GARANTED_ON_DELAY_STORAGE", "GARANTED_STORAGE"}[t]
|
||||||
|
}
|
||||||
|
|
||||||
|
type StorageResourcePricingStrategy int
|
||||||
|
|
||||||
|
const (
|
||||||
|
PER_DATA_STORED StorageResourcePricingStrategy = iota
|
||||||
|
PER_TB_STORED
|
||||||
|
PER_GB_STORED
|
||||||
|
PER_MB_STORED
|
||||||
|
PER_KB_STORED
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t StorageResourcePricingStrategy) GetStrategy() string {
|
||||||
|
return [...]string{"PER_DATA_STORED", "PER_GB_STORED", "PER_MB_STORED", "PER_KB_STORED"}[t]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t StorageResourcePricingStrategy) GetStrategyValue() int {
|
||||||
|
return int(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToStorageResourcePricingStrategy(i int) StorageResourcePricingStrategy {
|
||||||
|
return StorageResourcePricingStrategy(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t StorageResourcePricingStrategy) GetQuantity(amountOfDataGB float64) (float64, error) {
|
||||||
|
switch t {
|
||||||
|
case PER_DATA_STORED:
|
||||||
|
return amountOfDataGB, nil
|
||||||
|
case PER_TB_STORED:
|
||||||
|
return amountOfDataGB * 1000, nil
|
||||||
|
case PER_GB_STORED:
|
||||||
|
return amountOfDataGB, nil
|
||||||
|
case PER_MB_STORED:
|
||||||
|
return (amountOfDataGB * 1000), nil
|
||||||
|
case PER_KB_STORED:
|
||||||
|
return amountOfDataGB * 1000000, nil
|
||||||
|
}
|
||||||
|
return 0, errors.New("Pricing strategy not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
type StorageResourcePricingProfile struct {
|
||||||
|
pricing.ExploitPricingProfile[StorageResourcePricingStrategy] // ExploitPricingProfile is the pricing profile of a storage it means that we exploit the resource for an amount of continuous time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *StorageResourcePricingProfile) IsPurchased() bool {
|
||||||
|
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *StorageResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||||
|
return p.Pricing.GetPrice(amountOfData, val, start, &end)
|
||||||
|
}
|
||||||
|
|
||||||
|
type PricedStorageResource struct {
|
||||||
|
PricedResource
|
||||||
|
UsageStorageGB float64 `json:"storage_gb,omitempty" bson:"storage_gb,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedStorageResource) GetType() tools.DataType {
|
||||||
|
return tools.STORAGE_RESOURCE
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PricedStorageResource) GetPrice() (float64, error) {
|
||||||
|
if r.UsageStart == nil || r.UsageEnd == nil {
|
||||||
|
return 0, errors.New("Usage start and end must be set")
|
||||||
|
}
|
||||||
|
if r.SelectedPricing == nil {
|
||||||
|
return 0, errors.New("Selected pricing must be set")
|
||||||
|
}
|
||||||
|
pricing := *r.SelectedPricing
|
||||||
|
var err error
|
||||||
|
amountOfData := float64(1)
|
||||||
|
if pricing.GetOverrideStrategyValue() >= 0 {
|
||||||
|
amountOfData, err = ToStorageResourcePricingStrategy(pricing.GetOverrideStrategyValue()).GetQuantity(r.UsageStorageGB)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pricing.GetPrice(amountOfData, r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd)
|
||||||
|
}
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
45
models/resources/workflow.go
Normal file
45
models/resources/workflow.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
// we don't have any information about the accessor
|
||||||
|
type abstractWorkflowResource struct {
|
||||||
|
WorkflowID string `bson:"workflow_id,omitempty" json:"workflow_id,omitempty"` // WorkflowID is the ID of the native workflow
|
||||||
|
}
|
||||||
|
|
||||||
|
type WorkflowResourcePricingProfile struct{}
|
||||||
|
|
||||||
|
// WorkflowResource is a struct that represents a workflow resource
|
||||||
|
// it defines the resource workflow
|
||||||
|
type WorkflowResource struct {
|
||||||
|
AbstractResource
|
||||||
|
abstractWorkflowResource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *WorkflowResource) Trim() {
|
||||||
|
/* EMPTY */
|
||||||
|
}
|
||||||
|
func (abs *WorkflowResource) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (w *WorkflowResource) SetAllowedInstances(request *tools.APIRequest) {
|
||||||
|
/* EMPTY */
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WorkflowResource) ConvertToPricedResource(
|
||||||
|
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||||
|
instances := map[string]string{}
|
||||||
|
profiles := map[string][]pricing.PricingProfileITF{}
|
||||||
|
return &PricedResource{
|
||||||
|
Name: w.Name,
|
||||||
|
Logo: w.Logo,
|
||||||
|
ResourceID: w.UUID,
|
||||||
|
ResourceType: t,
|
||||||
|
InstancesRefs: instances,
|
||||||
|
PricingProfiles: profiles,
|
||||||
|
CreatorID: w.CreatorID,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
@@ -2,11 +2,9 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"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"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -16,48 +14,28 @@ import (
|
|||||||
// single instance of the validator used in every model Struct to validate the fields
|
// single instance of the validator used in every model Struct to validate the fields
|
||||||
var validate = validator.New(validator.WithRequiredStructEnabled())
|
var validate = validator.New(validator.WithRequiredStructEnabled())
|
||||||
|
|
||||||
|
type AccessMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Private AccessMode = iota
|
||||||
|
Public
|
||||||
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* AbstractObject is a struct that represents the basic fields of an object
|
* AbstractObject is a struct that represents the basic fields of an object
|
||||||
* it defines the object id and name
|
* it defines the object id and name
|
||||||
* every data in base root model should inherit from this struct (only exception is the ResourceModel)
|
* every data in base root model should inherit from this struct (only exception is the ResourceModel)
|
||||||
*/
|
*/
|
||||||
type AbstractObject struct {
|
type AbstractObject struct {
|
||||||
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
|
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
|
||||||
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
|
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
|
||||||
}
|
IsDraft bool `json:"is_draft" bson:"is_draft" default:"false"`
|
||||||
|
CreatorID string `json:"creator_id" bson:"creator_id" default:"unknown"`
|
||||||
|
|
||||||
// GetID returns the id of the object (abstract)
|
CreationDate time.Time `json:"creation_date" bson:"creation_date"`
|
||||||
func (ao *AbstractObject) GetID() string {
|
UpdateDate time.Time `json:"update_date" bson:"update_date"`
|
||||||
return ao.UUID
|
UpdaterID string `json:"updater_id" bson:"updater_id"`
|
||||||
}
|
AccessMode AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
|
||||||
|
|
||||||
// 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() {
|
func (r *AbstractObject) GenerateID() {
|
||||||
@@ -66,89 +44,115 @@ func (r *AbstractObject) GenerateID() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AbstractAccessor struct {
|
func (r *AbstractObject) StoreDraftDefault() {
|
||||||
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
|
r.IsDraft = false
|
||||||
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 {
|
func (r *AbstractObject) CanUpdate(set DBObject) (bool, DBObject) {
|
||||||
|
return true, set
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *AbstractObject) CanDelete() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *AbstractObject) IsDrafted() bool {
|
||||||
|
return r.IsDraft
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetID implements ShallowDBObject.
|
||||||
|
func (ao AbstractObject) GetID() string {
|
||||||
|
return ao.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetName implements ShallowDBObject.
|
||||||
|
func (ao AbstractObject) GetName() string {
|
||||||
|
return ao.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao *AbstractObject) GetCreatorID() string {
|
||||||
|
return ao.CreatorID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao *AbstractObject) UpToDate(user string, create bool) {
|
||||||
|
ao.UpdateDate = time.Now()
|
||||||
|
ao.UpdaterID = user
|
||||||
|
if create {
|
||||||
|
ao.CreationDate = time.Now()
|
||||||
|
ao.CreatorID = user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao *AbstractObject) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
|
return ao.AccessMode == Public || (request != nil && ao.CreatorID == request.Username)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
|
||||||
|
return &dbs.Filters{
|
||||||
|
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
|
||||||
|
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dma *AbstractObject) Deserialize(j map[string]interface{}, obj DBObject) DBObject {
|
||||||
|
b, err := json.Marshal(j)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
json.Unmarshal(b, obj)
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dma *AbstractObject) Serialize(obj DBObject) map[string]interface{} {
|
||||||
|
var m map[string]interface{}
|
||||||
|
b, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
json.Unmarshal(b, &m)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
type AbstractAccessor struct {
|
||||||
|
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
|
||||||
|
Type tools.DataType // Type is the data type of the accessor
|
||||||
|
Request *tools.APIRequest // Caller is the http caller of the accessor (optionnal) only need in a peer connection
|
||||||
|
ResourceModelAccessor Accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *AbstractAccessor) GetRequest() *tools.APIRequest {
|
||||||
|
return r.Request
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dma *AbstractAccessor) GetUser() string {
|
||||||
|
if dma.Request == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return dma.Request.Username
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dma *AbstractAccessor) GetPeerID() string {
|
||||||
|
if dma.Request == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return dma.Request.PeerID
|
||||||
|
}
|
||||||
|
func (dma *AbstractAccessor) GetGroups() []string {
|
||||||
|
if dma.Request == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
return dma.Request.Groups
|
||||||
|
}
|
||||||
|
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
|
||||||
|
return &dma.Logger
|
||||||
|
}
|
||||||
|
func (dma *AbstractAccessor) GetType() tools.DataType {
|
||||||
return dma.Type
|
return dma.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
||||||
return dma.Caller
|
if dma.Request == nil {
|
||||||
}
|
return nil
|
||||||
|
}
|
||||||
// Init initializes the accessor with the data type and the http caller
|
return dma.Request.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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,160 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
/*
|
import (
|
||||||
type Price struct {
|
"errors"
|
||||||
Price float64 `json:"price,omitempty" bson:"price,omitempty"`
|
"fmt"
|
||||||
Currency string `json:"currency,omitempty" bson:"currency,omitempty"`
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||||
|
mgb "go.mongodb.org/mongo-driver/mongo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Owner struct {
|
||||||
|
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
||||||
|
Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenericLoadOne loads one object from the database (generic)
|
||||||
|
func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
|
||||||
|
data.GenerateID()
|
||||||
|
data.StoreDraftDefault()
|
||||||
|
data.UpToDate(a.GetUser(), true)
|
||||||
|
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 !data.VerifyAuth(a.GetRequest()) {
|
||||||
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
||||||
|
}
|
||||||
|
if cursor, _, _ := a.Search(&f, "", data.IsDrafted()); len(cursor) > 0 {
|
||||||
|
return nil, 409, errors.New(a.GetType().String() + " 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(), a.GetType().String())
|
||||||
|
if err != nil {
|
||||||
|
a.GetLogger().Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
||||||
|
return nil, code, err
|
||||||
|
}
|
||||||
|
return a.LoadOne(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenericLoadOne loads one object from the database (generic)
|
||||||
|
func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
|
||||||
|
res, code, err := a.LoadOne(id)
|
||||||
|
if !res.CanDelete() {
|
||||||
|
return nil, 403, errors.New("you are not allowed to delete this collaborative area")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
||||||
|
return nil, code, err
|
||||||
|
}
|
||||||
|
if !res.VerifyAuth(a.GetRequest()) {
|
||||||
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
||||||
|
}
|
||||||
|
_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
|
||||||
|
if err != nil {
|
||||||
|
a.GetLogger().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 GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObject, int, error) {
|
||||||
|
r, c, err := a.LoadOne(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, c, err
|
||||||
|
}
|
||||||
|
ok, newSet := r.CanUpdate(set)
|
||||||
|
if !ok {
|
||||||
|
return nil, 403, errors.New("you are not allowed to delete this collaborative area")
|
||||||
|
}
|
||||||
|
set = newSet
|
||||||
|
r.UpToDate(a.GetUser(), false)
|
||||||
|
if !r.VerifyAuth(a.GetRequest()) {
|
||||||
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
||||||
|
}
|
||||||
|
change := set.Serialize(set) // get the changes
|
||||||
|
loaded := r.Serialize(r) // 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, new), id, a.GetType().String())
|
||||||
|
if err != nil {
|
||||||
|
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||||
|
return nil, code, err
|
||||||
|
}
|
||||||
|
return a.LoadOne(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), a Accessor) (DBObject, int, error) {
|
||||||
|
var data T
|
||||||
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
|
||||||
|
if err != nil {
|
||||||
|
a.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||||
|
return nil, code, err
|
||||||
|
}
|
||||||
|
res_mongo.Decode(&data)
|
||||||
|
if !data.VerifyAuth(a.GetRequest()) {
|
||||||
|
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
||||||
|
}
|
||||||
|
return f(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, onlyDraft bool, f func(DBObject) ShallowDBObject, a Accessor) ([]ShallowDBObject, int, error) {
|
||||||
|
objs := []ShallowDBObject{}
|
||||||
|
var results []T
|
||||||
|
if err != nil {
|
||||||
|
a.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||||
|
return nil, code, err
|
||||||
|
}
|
||||||
|
if err = res.All(mongo.MngoCtx, &results); err != nil {
|
||||||
|
return nil, 404, err
|
||||||
|
}
|
||||||
|
for _, r := range results {
|
||||||
|
if !r.VerifyAuth(a.GetRequest()) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
objs = append(objs, f(r))
|
||||||
|
}
|
||||||
|
return objs, 200, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||||
|
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
|
||||||
|
fmt.Println("res_mongo", res_mongo)
|
||||||
|
return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
|
||||||
|
f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||||
|
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||||
|
filters = defaultFilters
|
||||||
|
}
|
||||||
|
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
|
||||||
|
return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenericLoadOne loads one object from the database (generic)
|
||||||
|
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
||||||
|
func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, error) {
|
||||||
|
id, code, err := mongo.MONGOService.UpdateOne(set, id, a.GetType().String())
|
||||||
|
if err != nil {
|
||||||
|
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||||
|
return nil, code, err
|
||||||
|
}
|
||||||
|
return a.LoadOne(id)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ShallowDBObject is an interface that defines the basic methods shallowed version of a DBObject
|
// ShallowDBObject is an interface that defines the basic methods shallowed version of a DBObject
|
||||||
@@ -10,8 +11,8 @@ type ShallowDBObject interface {
|
|||||||
GenerateID()
|
GenerateID()
|
||||||
GetID() string
|
GetID() string
|
||||||
GetName() string
|
GetName() string
|
||||||
Deserialize(j map[string]interface{}) DBObject
|
Deserialize(j map[string]interface{}, obj DBObject) DBObject
|
||||||
Serialize() map[string]interface{}
|
Serialize(obj DBObject) map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DBObject is an interface that defines the basic methods for a DBObject
|
// DBObject is an interface that defines the basic methods for a DBObject
|
||||||
@@ -19,18 +20,29 @@ type DBObject interface {
|
|||||||
GenerateID()
|
GenerateID()
|
||||||
GetID() string
|
GetID() string
|
||||||
GetName() string
|
GetName() string
|
||||||
Deserialize(j map[string]interface{}) DBObject
|
GetCreatorID() string
|
||||||
Serialize() map[string]interface{}
|
IsDrafted() bool
|
||||||
GetAccessor(caller *tools.HTTPCaller) Accessor
|
StoreDraftDefault()
|
||||||
|
CanUpdate(set DBObject) (bool, DBObject)
|
||||||
|
CanDelete() bool
|
||||||
|
UpToDate(user string, create bool)
|
||||||
|
VerifyAuth(request *tools.APIRequest) bool
|
||||||
|
Deserialize(j map[string]interface{}, obj DBObject) DBObject
|
||||||
|
Serialize(obj DBObject) map[string]interface{}
|
||||||
|
GetAccessor(request *tools.APIRequest) Accessor
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accessor is an interface that defines the basic methods for an Accessor
|
// Accessor is an interface that defines the basic methods for an Accessor
|
||||||
type Accessor interface {
|
type Accessor interface {
|
||||||
Init(t DataType, caller *tools.HTTPCaller)
|
GetRequest() *tools.APIRequest
|
||||||
GetType() string
|
GetType() tools.DataType
|
||||||
|
GetUser() string
|
||||||
|
GetPeerID() string
|
||||||
|
GetGroups() []string
|
||||||
|
GetLogger() *zerolog.Logger
|
||||||
GetCaller() *tools.HTTPCaller
|
GetCaller() *tools.HTTPCaller
|
||||||
Search(filters *dbs.Filters, search string) ([]ShallowDBObject, int, error)
|
Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error)
|
||||||
LoadAll() ([]ShallowDBObject, int, error)
|
LoadAll(isDraft bool) ([]ShallowDBObject, int, error)
|
||||||
LoadOne(id string) (DBObject, int, error)
|
LoadOne(id string) (DBObject, int, error)
|
||||||
DeleteOne(id string) (DBObject, int, error)
|
DeleteOne(id string) (DBObject, int, error)
|
||||||
CopyOne(data DBObject) (DBObject, int, error)
|
CopyOne(data DBObject) (DBObject, int, error)
|
||||||
|
|||||||
175
models/workflow/graph/graph.go
Normal file
175
models/workflow/graph/graph.go
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
package graph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, processings []*resources.ProcessingResource, resource resources.ResourceInterface,
|
||||||
|
f func(GraphItem) resources.ResourceInterface, request *tools.APIRequest) (float64, float64) {
|
||||||
|
nearestStart := float64(10000000000)
|
||||||
|
oneIsInfinite := false
|
||||||
|
longestDuration := float64(0)
|
||||||
|
for _, link := range g.Links {
|
||||||
|
for _, processing := range processings {
|
||||||
|
var source string // source is the source of the link
|
||||||
|
if link.Destination.ID == processing.GetID() && f(g.Items[link.Source.ID]) != nil && f(g.Items[link.Source.ID]).GetID() == resource.GetID() { // if the destination is the processing and the source is not a compute
|
||||||
|
source = link.Source.ID
|
||||||
|
} else if link.Source.ID == processing.GetID() && f(g.Items[link.Source.ID]) != nil && f(g.Items[link.Source.ID]).GetID() == resource.GetID() { // if the source is the processing and the destination is not a compute
|
||||||
|
source = link.Destination.ID
|
||||||
|
}
|
||||||
|
priced := processing.ConvertToPricedResource(tools.PROCESSING_RESOURCE, request)
|
||||||
|
if source != "" {
|
||||||
|
if priced.GetLocationStart() != nil {
|
||||||
|
near := float64(priced.GetLocationStart().Sub(start).Seconds())
|
||||||
|
if near < nearestStart {
|
||||||
|
nearestStart = near
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if priced.GetLocationEnd() != nil {
|
||||||
|
duration := float64(priced.GetLocationEnd().Sub(*priced.GetLocationStart()).Seconds())
|
||||||
|
if longestDuration < duration {
|
||||||
|
longestDuration = duration
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
oneIsInfinite = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if oneIsInfinite {
|
||||||
|
return nearestStart, -1
|
||||||
|
}
|
||||||
|
return nearestStart, longestDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* GetAverageTimeBeforeStart is a function that returns the average time before the start of a processing
|
||||||
|
*/
|
||||||
|
func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingID string, request *tools.APIRequest) float64 {
|
||||||
|
currents := []float64{} // list of current time
|
||||||
|
for _, link := range g.Links { // for each link
|
||||||
|
var source string // source is the source of the link
|
||||||
|
if link.Destination.ID == processingID && g.Items[link.Source.ID].Processing == nil { // if the destination is the processing and the source is not a compute
|
||||||
|
source = link.Source.ID
|
||||||
|
} else if link.Source.ID == processingID && g.Items[link.Source.ID].Processing == nil { // if the source is the processing and the destination is not a compute
|
||||||
|
source = link.Destination.ID
|
||||||
|
}
|
||||||
|
if source == "" { // if source is empty, continue
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dt, r := g.GetResource(source) // get the resource of the source
|
||||||
|
if r == nil { // if item is nil, continue
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
priced := r.ConvertToPricedResource(dt, request)
|
||||||
|
current := priced.GetExplicitDurationInS() // get the explicit duration of the item
|
||||||
|
if current < 0 { // if current is negative, its means that duration of a before could be infinite continue
|
||||||
|
return current
|
||||||
|
}
|
||||||
|
current += g.GetAverageTimeProcessingBeforeStart(current, source, request) // get the average time before start of the source
|
||||||
|
currents = append(currents, current) // append the current to the currents
|
||||||
|
}
|
||||||
|
var max float64 // get the max time to wait dependancies to finish
|
||||||
|
for _, current := range currents {
|
||||||
|
if current > max {
|
||||||
|
max = current
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Graph) GetResource(id string) (tools.DataType, resources.ResourceInterface) {
|
||||||
|
if item, ok := g.Items[id]; ok {
|
||||||
|
if item.Data != nil {
|
||||||
|
return tools.DATA_RESOURCE, item.Data
|
||||||
|
} else if item.Compute != nil {
|
||||||
|
return tools.COMPUTE_RESOURCE, item.Compute
|
||||||
|
} else if item.Workflow != nil {
|
||||||
|
return tools.WORKFLOW_RESOURCE, item.Workflow
|
||||||
|
} else if item.Processing != nil {
|
||||||
|
return tools.PROCESSING_RESOURCE, item.Processing
|
||||||
|
} else if item.Storage != nil {
|
||||||
|
return tools.STORAGE_RESOURCE, item.Storage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tools.INVALID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GraphItem) GetResource() (tools.DataType, resources.ResourceInterface) {
|
||||||
|
if g.Data != nil {
|
||||||
|
return tools.DATA_RESOURCE, g.Data
|
||||||
|
} else if g.Compute != nil {
|
||||||
|
return tools.COMPUTE_RESOURCE, g.Compute
|
||||||
|
} else if g.Workflow != nil {
|
||||||
|
return tools.WORKFLOW_RESOURCE, g.Workflow
|
||||||
|
} else if g.Processing != nil {
|
||||||
|
return tools.PROCESSING_RESOURCE, g.Processing
|
||||||
|
} else if g.Storage != nil {
|
||||||
|
return tools.STORAGE_RESOURCE, g.Storage
|
||||||
|
}
|
||||||
|
return tools.INVALID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// tool function to check if a link is a link between a compute and a resource
|
||||||
|
func (l *GraphLink) IsComputeLink(g Graph) (bool, string) {
|
||||||
|
if g.Items == nil {
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
if d, ok := g.Items[l.Source.ID]; ok && d.Compute != nil {
|
||||||
|
return true, d.Compute.UUID
|
||||||
|
}
|
||||||
|
if d, ok := g.Items[l.Destination.ID]; ok && d.Compute != nil {
|
||||||
|
return true, d.Compute.UUID
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
package workflow
|
package workflow
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"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"
|
||||||
"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/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/workflow/graph"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,24 +22,87 @@ import (
|
|||||||
*/
|
*/
|
||||||
type AbstractWorkflow struct {
|
type AbstractWorkflow struct {
|
||||||
resources.ResourceSet
|
resources.ResourceSet
|
||||||
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"` // Graph UI & logic representation of the workflow
|
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
|
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
|
// 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
|
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 (d *Workflow) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) (bool, string) {
|
return NewAccessor(request) // Create a new instance of the accessor
|
||||||
if w.Graph == nil || w.Graph.Items == nil {
|
}
|
||||||
return false, ""
|
|
||||||
|
func (w *AbstractWorkflow) GetGraphItems(f func(item graph.GraphItem) bool) (list_datas []graph.GraphItem) {
|
||||||
|
for _, item := range w.Graph.Items {
|
||||||
|
if f(item) {
|
||||||
|
list_datas = append(list_datas, item)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if d, ok := w.Graph.Items[link.Source.ID]; ok && d.Datacenter != nil {
|
return
|
||||||
return true, d.Datacenter.UUID
|
}
|
||||||
|
|
||||||
|
func (w *AbstractWorkflow) GetResources(f func(item graph.GraphItem) bool) map[string]resources.ResourceInterface {
|
||||||
|
list_datas := map[string]resources.ResourceInterface{}
|
||||||
|
for _, item := range w.Graph.Items {
|
||||||
|
if f(item) {
|
||||||
|
_, res := item.GetResource()
|
||||||
|
list_datas[res.GetID()] = res
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if d, ok := w.Graph.Items[link.Destination.ID]; ok && d.Datacenter != nil {
|
return list_datas
|
||||||
return true, d.Datacenter.UUID
|
}
|
||||||
|
|
||||||
|
func (w *AbstractWorkflow) GetPricedItem(f func(item graph.GraphItem) bool, request *tools.APIRequest) map[string]pricing.PricedItemITF {
|
||||||
|
list_datas := map[string]pricing.PricedItemITF{}
|
||||||
|
for _, item := range w.Graph.Items {
|
||||||
|
if f(item) {
|
||||||
|
dt, res := item.GetResource()
|
||||||
|
ord := res.ConvertToPricedResource(dt, request)
|
||||||
|
list_datas[res.GetID()] = ord
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false, ""
|
return list_datas
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *AbstractWorkflow) GetByRelatedProcessing(processingID string, g func(item graph.GraphItem) bool) []resources.ResourceInterface {
|
||||||
|
storages := []resources.ResourceInterface{}
|
||||||
|
for _, link := range w.Graph.Links {
|
||||||
|
nodeID := link.Destination.ID
|
||||||
|
var node resources.ResourceInterface
|
||||||
|
if g(w.Graph.Items[link.Source.ID]) {
|
||||||
|
item := w.Graph.Items[link.Source.ID]
|
||||||
|
_, node = item.GetResource()
|
||||||
|
}
|
||||||
|
if node == nil && g(w.Graph.Items[link.Destination.ID]) { // if the source is not a storage, we consider that the destination is the storage
|
||||||
|
nodeID = link.Source.ID
|
||||||
|
item := w.Graph.Items[link.Destination.ID] // and the processing is the source
|
||||||
|
_, node = item.GetResource() // we are looking for the storage as destination
|
||||||
|
}
|
||||||
|
if processingID == nodeID && node != nil { // if the storage is linked to the processing
|
||||||
|
storages = append(storages, node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return storages
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wf *AbstractWorkflow) IsProcessing(item graph.GraphItem) bool {
|
||||||
|
return item.Processing != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wf *AbstractWorkflow) IsCompute(item graph.GraphItem) bool {
|
||||||
|
return item.Compute != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wf *AbstractWorkflow) IsData(item graph.GraphItem) bool {
|
||||||
|
return item.Data != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wf *AbstractWorkflow) IsStorage(item graph.GraphItem) bool {
|
||||||
|
return item.Storage != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wf *AbstractWorkflow) IsWorkflow(item graph.GraphItem) bool {
|
||||||
|
return item.Workflow != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -49,6 +114,28 @@ type Workflow struct {
|
|||||||
AbstractWorkflow // AbstractWorkflow contains the basic fields of a workflow
|
AbstractWorkflow // AbstractWorkflow contains the basic fields of a workflow
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Workflow) getPricedItem(item graph.GraphItem, request *tools.APIRequest) pricing.PricedItemITF {
|
||||||
|
dt, res := item.GetResource()
|
||||||
|
if dt == tools.INVALID {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return res.ConvertToPricedResource(dt, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao *Workflow) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
|
isAuthorized := false
|
||||||
|
if len(ao.Shared) > 0 {
|
||||||
|
for _, shared := range ao.Shared {
|
||||||
|
shared, code, _ := shallow_collaborative_area.NewAccessor(request).LoadOne(shared)
|
||||||
|
if code != 200 || shared == nil {
|
||||||
|
isAuthorized = false
|
||||||
|
}
|
||||||
|
isAuthorized = shared.VerifyAuth(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ao.AbstractObject.VerifyAuth(request) || isAuthorized
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* CheckBooking is a function that checks the booking of the workflow on peers (even ourselves)
|
* CheckBooking is a function that checks the booking of the workflow on peers (even ourselves)
|
||||||
*/
|
*/
|
||||||
@@ -57,19 +144,19 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
|
|||||||
if wfa.Graph == nil { // no graph no booking
|
if wfa.Graph == nil { // no graph no booking
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
accessor := (&datacenter.DatacenterResource{}).GetAccessor(nil)
|
accessor := (&resources.ComputeResource{}).GetAccessor(&tools.APIRequest{Caller: caller})
|
||||||
for _, link := range wfa.Graph.Links {
|
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
|
if ok, compute_id := link.IsComputeLink(*wfa.Graph); ok { // check if the link is a link between a compute and a resource
|
||||||
dc, code, _ := accessor.LoadOne(dc_id)
|
compute, code, _ := accessor.LoadOne(compute_id)
|
||||||
if code != 200 {
|
if code != 200 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// CHECK BOOKING ON PEER, datacenter could be a remote one
|
// CHECK BOOKING ON PEER, compute could be a remote one
|
||||||
peerID := dc.(*datacenter.DatacenterResource).PeerID
|
peerID := compute.(*resources.ComputeResource).CreatorID
|
||||||
if peerID == "" {
|
if peerID == "" {
|
||||||
return false, errors.New("no peer id")
|
return false, errors.New("no peer id")
|
||||||
} // no peer id no booking, we need to know where to book
|
} // 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)
|
_, err := (&peer.Peer{}).LaunchPeerExecution(peerID, compute_id, tools.BOOKING, tools.GET, nil, caller)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -78,31 +165,124 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Workflow) GetName() string {
|
func (wf *Workflow) Planify(start time.Time, end *time.Time, request *tools.APIRequest) (float64, map[tools.DataType][]pricing.PricedItemITF, *Workflow, error) {
|
||||||
return d.Name
|
processings := []*resources.ProcessingResource{}
|
||||||
}
|
priceds := map[tools.DataType][]pricing.PricedItemITF{}
|
||||||
|
priceds[tools.PROCESSING_RESOURCE] = []pricing.PricedItemITF{}
|
||||||
func (d *Workflow) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
for _, item := range wf.GetGraphItems(wf.IsProcessing) {
|
||||||
data := New() // Create a new instance of the accessor
|
dt, realItem := item.GetResource()
|
||||||
data.Init(utils.WORKFLOW, caller) // Initialize the accessor with the WORKFLOW model type
|
if realItem == nil {
|
||||||
return data
|
return 0, priceds, nil, errors.New("could not load the processing resource")
|
||||||
}
|
}
|
||||||
|
priced := realItem.ConvertToPricedResource(dt, request)
|
||||||
func (dma *Workflow) Deserialize(j map[string]interface{}) utils.DBObject {
|
timeFromStartS := wf.Graph.GetAverageTimeProcessingBeforeStart(0, realItem.GetID(), request)
|
||||||
b, err := json.Marshal(j)
|
started := start.Add(time.Duration(timeFromStartS) * time.Second)
|
||||||
if err != nil {
|
priced.SetLocationStart(started)
|
||||||
return nil
|
priced.SetLocationEnd(started.Add(time.Duration(priced.GetExplicitDurationInS())))
|
||||||
|
processings = append(processings, realItem.(*resources.ProcessingResource))
|
||||||
|
priceds[tools.PROCESSING_RESOURCE] = append(priceds[tools.PROCESSING_RESOURCE], priced)
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, dma)
|
priceds[tools.DATA_RESOURCE] = []pricing.PricedItemITF{}
|
||||||
return dma
|
for _, item := range wf.GetGraphItems(wf.IsData) {
|
||||||
|
dt, realItem := item.GetResource()
|
||||||
|
if realItem == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
priced := realItem.ConvertToPricedResource(dt, request)
|
||||||
|
priced.SetLocationStart(start)
|
||||||
|
priced.SetLocationEnd(*end)
|
||||||
|
priceds[tools.PROCESSING_RESOURCE] = append(priceds[tools.PROCESSING_RESOURCE], priced)
|
||||||
|
}
|
||||||
|
for _, f := range []func(graph.GraphItem) bool{wf.IsStorage, wf.IsCompute} {
|
||||||
|
for _, item := range wf.GetGraphItems(f) {
|
||||||
|
dt, r := item.GetResource()
|
||||||
|
if r == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if priceds[dt] == nil {
|
||||||
|
priceds[dt] = []pricing.PricedItemITF{}
|
||||||
|
}
|
||||||
|
priced := r.ConvertToPricedResource(dt, request)
|
||||||
|
nearestStart, longestDuration := wf.Graph.GetAverageTimeRelatedToProcessingActivity(start, processings, r,
|
||||||
|
func(i graph.GraphItem) resources.ResourceInterface {
|
||||||
|
if f(i) {
|
||||||
|
_, r := i.GetResource()
|
||||||
|
return r
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}, request)
|
||||||
|
started := start.Add(time.Duration(nearestStart) * time.Second)
|
||||||
|
priced.SetLocationStart(started)
|
||||||
|
if longestDuration >= 0 {
|
||||||
|
priced.SetLocationEnd(started.Add(time.Duration(longestDuration)))
|
||||||
|
}
|
||||||
|
priceds[dt] = append(priceds[dt], priced)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
longest := wf.getLongestTime(end, priceds, request)
|
||||||
|
priceds[tools.WORKFLOW_RESOURCE] = []pricing.PricedItemITF{}
|
||||||
|
for _, item := range wf.GetGraphItems(wf.IsWorkflow) {
|
||||||
|
access := NewAccessor(nil)
|
||||||
|
_, r := item.GetResource()
|
||||||
|
if r == nil {
|
||||||
|
return 0, priceds, nil, errors.New("could not load the workflow")
|
||||||
|
}
|
||||||
|
priced := r.ConvertToPricedResource(tools.WORKFLOW_RESOURCE, request)
|
||||||
|
res, code, err := access.LoadOne(r.GetID())
|
||||||
|
if code != 200 || err != nil {
|
||||||
|
return 0, priceds, nil, errors.New("could not load the workflow with id: " + fmt.Sprintf("%v", err.Error()))
|
||||||
|
}
|
||||||
|
neoLongest := float64(0)
|
||||||
|
innerWF := res.(*Workflow)
|
||||||
|
neoLongest, _, innerWF, err = innerWF.Planify(start, end, request)
|
||||||
|
if neoLongest > longest {
|
||||||
|
longest = neoLongest
|
||||||
|
}
|
||||||
|
started := start.Add(time.Duration(wf.getNearestStart(start, priceds, request)) * time.Second)
|
||||||
|
priced.SetLocationStart(started)
|
||||||
|
durationE := time.Duration(longest)
|
||||||
|
if durationE < 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ended := start.Add(durationE * time.Second)
|
||||||
|
priced.SetLocationEnd(ended)
|
||||||
|
priceds[tools.WORKFLOW_RESOURCE] = append(priceds[tools.WORKFLOW_RESOURCE], priced)
|
||||||
|
}
|
||||||
|
return longest, priceds, wf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *Workflow) Serialize() map[string]interface{} {
|
func (wf *Workflow) getNearestStart(start time.Time, priceds map[tools.DataType][]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||||
var m map[string]interface{}
|
near := float64(10000000000)
|
||||||
b, err := json.Marshal(dma)
|
for _, items := range priceds {
|
||||||
if err != nil {
|
for _, priced := range items {
|
||||||
return nil
|
if priced.GetLocationStart() == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newS := priced.GetLocationStart()
|
||||||
|
if newS.Sub(start).Seconds() < near {
|
||||||
|
near = newS.Sub(start).Seconds()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// get the nearest start from start var
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, &m)
|
return near
|
||||||
return m
|
}
|
||||||
|
|
||||||
|
func (wf *Workflow) getLongestTime(end *time.Time, priceds map[tools.DataType][]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||||
|
if end == nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
longestTime := float64(0)
|
||||||
|
for _, priced := range priceds[tools.PROCESSING_RESOURCE] {
|
||||||
|
if priced.GetLocationEnd() == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newS := priced.GetLocationEnd()
|
||||||
|
if longestTime < newS.Sub(*end).Seconds() {
|
||||||
|
longestTime = newS.Sub(*end).Seconds()
|
||||||
|
}
|
||||||
|
// get the nearest start from start var
|
||||||
|
}
|
||||||
|
return longestTime
|
||||||
}
|
}
|
||||||
|
|||||||
32
models/workflow/workflow_history_mongo_accessor.go
Normal file
32
models/workflow/workflow_history_mongo_accessor.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WorkflowHistory struct{ Workflow }
|
||||||
|
|
||||||
|
func (d *WorkflowHistory) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessorHistory(request) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
func (r *WorkflowHistory) GenerateID() {
|
||||||
|
r.UUID = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workspace is a struct that represents a workspace
|
||||||
|
type workflowHistoryMongoAccessor struct {
|
||||||
|
workflowMongoAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the workspaceMongoAccessor
|
||||||
|
func NewHistory() *workflowHistoryMongoAccessor {
|
||||||
|
return &workflowHistoryMongoAccessor{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *workflowHistoryMongoAccessor) MapFromWorkflow(w *Workflow) *WorkflowHistory {
|
||||||
|
wh := &WorkflowHistory{Workflow: *w}
|
||||||
|
wh.GenerateID()
|
||||||
|
return wh
|
||||||
|
}
|
||||||
@@ -1,373 +1,177 @@
|
|||||||
package workflow
|
package workflow
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"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/collaborative_area/shallow_collaborative_area"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"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"
|
||||||
"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/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"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/shallow_shared_workspace"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
cron "github.com/robfig/cron/v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type workflowMongoAccessor struct {
|
type workflowMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
computeResourceAccessor utils.Accessor
|
||||||
|
collaborativeAreaAccessor utils.Accessor
|
||||||
|
workspaceAccessor utils.Accessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAccessorHistory(request *tools.APIRequest) *workflowMongoAccessor {
|
||||||
|
return new(tools.WORKFLOW_HISTORY, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAccessor(request *tools.APIRequest) *workflowMongoAccessor {
|
||||||
|
return new(tools.WORKFLOW, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the workflowMongoAccessor
|
// New creates a new instance of the workflowMongoAccessor
|
||||||
func New() *workflowMongoAccessor {
|
func new(t tools.DataType, request *tools.APIRequest) *workflowMongoAccessor {
|
||||||
return &workflowMongoAccessor{}
|
return &workflowMongoAccessor{
|
||||||
}
|
computeResourceAccessor: (&resources.ComputeResource{}).GetAccessor(request),
|
||||||
|
collaborativeAreaAccessor: (&shallow_collaborative_area.ShallowCollaborativeArea{}).GetAccessor(request),
|
||||||
/*
|
workspaceAccessor: (&workspace.Workspace{}).GetAccessor(request),
|
||||||
* THERE IS A LOT IN THIS FILE SHOULD BE AWARE OF THE COMMENTS
|
AbstractAccessor: utils.AbstractAccessor{
|
||||||
*/
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
/*
|
Type: t,
|
||||||
* 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
|
// DeleteOne deletes a workflow from the database, delete depending executions and bookings
|
||||||
func (wfa *workflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
func (a *workflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
wfa.execution(id, &Workflow{
|
res, code, err := utils.GenericDeleteOne(id, a)
|
||||||
AbstractWorkflow: AbstractWorkflow{ScheduleActive: false},
|
|
||||||
}, true) // delete the executions
|
|
||||||
res, code, err := wfa.GenericDeleteOne(id, wfa)
|
|
||||||
if res != nil && code == 200 {
|
if res != nil && code == 200 {
|
||||||
wfa.execute(res.(*Workflow), false) // up to date the workspace for the workflow
|
a.execute(res.(*Workflow), true, false) // up to date the workspace for the workflow
|
||||||
|
a.share(res.(*Workflow), true, a.GetCaller())
|
||||||
}
|
}
|
||||||
wfa.share(res.(*Workflow), true, wfa.Caller) // send the deletion to the peers where workflow is shared
|
|
||||||
return res, code, err
|
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
|
* 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) {
|
func (a *workflowMongoAccessor) share(realData *Workflow, delete bool, caller *tools.HTTPCaller) {
|
||||||
if realData.Shared == nil || len(realData.Shared) == 0 { // no shared no sharing
|
if realData == nil || realData.Shared == nil || len(realData.Shared) == 0 || caller == nil || caller.Disabled { // no shared no sharing
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, sharedID := range realData.Shared { // loop through the shared ids
|
for _, sharedID := range realData.Shared { // loop through the shared ids
|
||||||
access := (&shallow_shared_workspace.ShallowSharedWorkspace{}).GetAccessor(nil)
|
res, code, _ := a.collaborativeAreaAccessor.LoadOne(sharedID)
|
||||||
res, code, _ := access.LoadOne(sharedID)
|
|
||||||
if code != 200 {
|
if code != 200 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
paccess := &peer.Peer{}
|
paccess := &peer.Peer{}
|
||||||
for _, p := range res.(*shallow_shared_workspace.ShallowSharedWorkspace).Peers {
|
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
||||||
paccess.UUID = p
|
paccess.UUID = p
|
||||||
if paccess.IsMySelf() { // if the peer is the current peer, never share because it will create a loop
|
if ok, _ := paccess.IsMySelf(); ok { // if the peer is the current peer, never share because it will create a loop
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if delete { // if the workflow is deleted, share the deletion
|
if delete { // if the workflow is deleted, share the deletion orderResourceAccessor utils.Accessor
|
||||||
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKFLOW, tools.DELETE, map[string]interface{}{}, caller)
|
|
||||||
|
history := NewHistory()
|
||||||
|
history.StoreOne(history.MapFromWorkflow(res.(*Workflow)))
|
||||||
|
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKFLOW, tools.DELETE,
|
||||||
|
map[string]interface{}{}, caller)
|
||||||
} else { // if the workflow is updated, share the update
|
} else { // if the workflow is updated, share the update
|
||||||
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKFLOW, tools.PUT, res.Serialize(), caller)
|
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKFLOW, tools.PUT,
|
||||||
|
res.Serialize(res), caller)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wfa.Logger.Error().Msg(err.Error())
|
a.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
|
|
||||||
return 200, err
|
|
||||||
}
|
|
||||||
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
|
// UpdateOne updates a workflow in the database
|
||||||
func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (a *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 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 := utils.GenericUpdateOne(set, id, a, &Workflow{})
|
||||||
res, code, err = wfa.GenericUpdateOne(set, id, wfa, &Workflow{})
|
|
||||||
if code != 200 {
|
if code != 200 {
|
||||||
return nil, code, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
if !avoid { // if the schedule is not avoided, update the executions
|
workflow := res.(*Workflow)
|
||||||
if code, err := wfa.execution(id, res.(*Workflow), true); err != nil {
|
a.execute(workflow, false, false) // update the workspace for the workflow
|
||||||
return nil, code, err
|
a.share(workflow, false, a.GetCaller()) // share the update to the peers
|
||||||
}
|
return res, code, nil
|
||||||
}
|
|
||||||
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
|
// StoreOne stores a workflow in the database
|
||||||
func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (a *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
res, code, err := wfa.GenericStoreOne(data, wfa)
|
d := data.(*Workflow)
|
||||||
if err != nil {
|
res, code, err := utils.GenericStoreOne(d, a)
|
||||||
|
if err != nil || code != 200 {
|
||||||
return nil, code, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
wfa.share(res.(*Workflow), false, wfa.Caller) // share the creation to the peers
|
workflow := res.(*Workflow)
|
||||||
//store the executions
|
|
||||||
if code, err := wfa.execution(res.GetID(), res.(*Workflow), false); err != nil {
|
a.share(workflow, false, a.GetCaller()) // share the creation to the peers
|
||||||
return nil, code, err
|
a.execute(workflow, false, false) // store the workspace for the workflow
|
||||||
}
|
return res, code, nil
|
||||||
wfa.execute(res.(*Workflow), false) // store the workspace for the workflow
|
|
||||||
return res, code, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyOne copies a workflow in the database
|
// CopyOne copies a workflow in the database
|
||||||
func (wfa *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (a *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return utils.GenericStoreOne(data, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute is a function that executes a workflow
|
// 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
|
// 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) {
|
func (a *workflowMongoAccessor) execute(workflow *Workflow, delete bool, active bool) {
|
||||||
|
|
||||||
accessor := (&workspace.Workspace{}).GetAccessor(nil)
|
|
||||||
filters := &dbs.Filters{
|
filters := &dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{ // filter by standard workspace name attached to a workflow
|
Or: map[string][]dbs.Filter{ // filter by standard workspace name attached to a workflow
|
||||||
"abstractobject.name": {{dbs.LIKE.String(), workflow.Name + "_workspace"}},
|
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: workflow.Name + "_workspace"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
resource, _, err := accessor.Search(filters, "")
|
resource, _, err := a.workspaceAccessor.Search(filters, "", workflow.IsDraft)
|
||||||
if delete { // if delete is set to true, delete the workspace
|
if delete { // if delete is set to true, delete the workspace
|
||||||
for _, r := range resource {
|
for _, r := range resource {
|
||||||
accessor.DeleteOne(r.GetID())
|
a.workspaceAccessor.DeleteOne(r.GetID())
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err == nil && len(resource) > 0 { // if the workspace already exists, update it
|
if err == nil && len(resource) > 0 { // if the workspace already exists, update it
|
||||||
accessor.UpdateOne(&workspace.Workspace{
|
a.workspaceAccessor.UpdateOne(&workspace.Workspace{
|
||||||
Active: true,
|
Active: active,
|
||||||
ResourceSet: resources.ResourceSet{
|
ResourceSet: resources.ResourceSet{
|
||||||
Datas: workflow.Datas,
|
Datas: workflow.Datas,
|
||||||
Processings: workflow.Processings,
|
Processings: workflow.Processings,
|
||||||
Storages: workflow.Storages,
|
Storages: workflow.Storages,
|
||||||
Workflows: workflow.Workflows,
|
Workflows: workflow.Workflows,
|
||||||
Datacenters: workflow.Datacenters,
|
Computes: workflow.Computes,
|
||||||
},
|
},
|
||||||
}, resource[0].GetID())
|
}, resource[0].GetID())
|
||||||
} else { // if the workspace does not exist, create it
|
} else { // if the workspace does not exist, create it
|
||||||
accessor.StoreOne(&workspace.Workspace{
|
a.workspaceAccessor.StoreOne(&workspace.Workspace{
|
||||||
Active: true,
|
Active: active,
|
||||||
AbstractObject: utils.AbstractObject{Name: workflow.Name + "_workspace"},
|
AbstractObject: utils.AbstractObject{Name: workflow.Name + "_workspace"},
|
||||||
ResourceSet: resources.ResourceSet{
|
ResourceSet: resources.ResourceSet{
|
||||||
Datas: workflow.Datas,
|
Datas: workflow.Datas,
|
||||||
Processings: workflow.Processings,
|
Processings: workflow.Processings,
|
||||||
Storages: workflow.Storages,
|
Storages: workflow.Storages,
|
||||||
Workflows: workflow.Workflows,
|
Workflows: workflow.Workflows,
|
||||||
Datacenters: workflow.Datacenters,
|
Computes: workflow.Computes,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadOne loads a workflow from the database
|
func (a *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
func (wfa *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
return utils.GenericLoadOne[*Workflow](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
var workflow Workflow
|
w := d.(*Workflow)
|
||||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
a.execute(w, false, true) // if no workspace is attached to the workflow, create it
|
||||||
if err != nil {
|
return d, 200, nil
|
||||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
}, a)
|
||||||
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 (a *workflowMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
func (wfa workflowMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
return utils.GenericLoadAll[*Workflow](func(d utils.DBObject) utils.ShallowDBObject { return &d.(*Workflow).AbstractObject }, isDraft, a)
|
||||||
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) {
|
func (a *workflowMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
objs := []utils.ShallowDBObject{}
|
return utils.GenericSearch[*Workflow](filters, search, (&Workflow{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject { return d }, isDraft, a)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,7 @@ func TestStoreOneWorkflow(t *testing.T) {
|
|||||||
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
|
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
|
||||||
}
|
}
|
||||||
|
|
||||||
wma := New()
|
wma := NewAccessor(nil)
|
||||||
id, _, _ := wma.StoreOne(&w)
|
id, _, _ := wma.StoreOne(&w)
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
assert.NotEmpty(t, id)
|
||||||
@@ -23,7 +23,7 @@ func TestLoadOneWorkflow(t *testing.T) {
|
|||||||
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
|
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
|
||||||
}
|
}
|
||||||
|
|
||||||
wma := New()
|
wma := NewAccessor(nil)
|
||||||
new_w, _, _ := wma.StoreOne(&w)
|
new_w, _, _ := wma.StoreOne(&w)
|
||||||
assert.Equal(t, w, new_w)
|
assert.Equal(t, w, new_w)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,141 +1,134 @@
|
|||||||
package workflow_execution
|
package workflow_execution
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
* 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
|
* Warning: No user can write (del, post, put) a workflow execution, it is only used by the system
|
||||||
* workflows generate their own executions
|
* workflows generate their own executions
|
||||||
*/
|
*/
|
||||||
type WorkflowExecutions struct {
|
type WorkflowExecutions struct {
|
||||||
WorkflowID string `json:"workflow_id" bson:"workflow_id"`
|
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||||
ResourceID string `json:"resource_id" bson:"resource_id"`
|
ExecDate time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"` // ExecDate is the execution date of the workflow, is required
|
||||||
Executions []*WorkflowExecution `json:"executions" bson:"executions"`
|
EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"` // EndDate is the end date of the workflow
|
||||||
|
State common.ScheduledType `json:"state" bson:"state" default:"0"` // State is the state of the workflow
|
||||||
|
WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
|
||||||
}
|
}
|
||||||
|
|
||||||
// New - Creates a new instance of the WorkflowExecutions from a map
|
func (r *WorkflowExecutions) StoreDraftDefault() {
|
||||||
func (dma *WorkflowExecutions) Deserialize(j map[string]interface{}) *WorkflowExecutions {
|
r.IsDraft = true
|
||||||
b, err := json.Marshal(j)
|
}
|
||||||
if err != nil {
|
|
||||||
return nil
|
func (r *WorkflowExecutions) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||||
|
if r.State != set.(*WorkflowExecutions).State {
|
||||||
|
return true, &WorkflowExecutions{State: set.(*WorkflowExecutions).State} // only state can be updated
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, dma)
|
return r.IsDraft, set // only draft buying can be updated
|
||||||
return dma
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize - Returns the WorkflowExecutions as a map
|
func (r *WorkflowExecutions) CanDelete() bool {
|
||||||
func (dma *WorkflowExecutions) Serialize() map[string]interface{} {
|
return r.IsDraft // only draft bookings can be deleted
|
||||||
var m map[string]interface{}
|
}
|
||||||
b, err := json.Marshal(dma)
|
|
||||||
if err != nil {
|
func (wfa *WorkflowExecutions) Equals(we *WorkflowExecutions) bool {
|
||||||
return nil
|
return wfa.ExecDate.Equal(we.ExecDate) && wfa.WorkflowID == we.WorkflowID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WorkflowExecutions) PurgeDraft(request *tools.APIRequest) error {
|
||||||
|
if ws.EndDate == nil {
|
||||||
|
// if no end... then Book like a savage
|
||||||
|
e := ws.ExecDate.Add(time.Hour)
|
||||||
|
ws.EndDate = &e
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, &m)
|
accessor := ws.GetAccessor(request)
|
||||||
return m
|
res, code, err := accessor.Search(&dbs.Filters{
|
||||||
}
|
And: map[string][]dbs.Filter{ // check if there is a booking on the same compute resource by filtering on the compute_resource_id, the state and the execution date
|
||||||
|
"state": {{Operator: dbs.EQUAL.String(), Value: common.DRAFT.EnumIndex()}},
|
||||||
/*
|
"workflow_id": {{Operator: dbs.EQUAL.String(), Value: ws.WorkflowID}},
|
||||||
* WorkflowExecution is a struct that represents a workflow execution
|
"execution_date": {
|
||||||
* Warning: No user can write (del, post, put) a workflow execution, it is only used by the system
|
{Operator: dbs.LTE.String(), Value: primitive.NewDateTimeFromTime(*ws.EndDate)},
|
||||||
* workflows generate their own executions
|
{Operator: dbs.GTE.String(), Value: primitive.NewDateTimeFromTime(ws.ExecDate)},
|
||||||
*/
|
},
|
||||||
type WorkflowExecution struct {
|
},
|
||||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
}, "", ws.IsDraft)
|
||||||
ExecDate *time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"` // ExecDate is the execution date of the workflow, is required
|
if code != 200 || err != nil {
|
||||||
EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"` // EndDate is the end date of the workflow
|
return err
|
||||||
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
|
for _, r := range res {
|
||||||
|
accessor.DeleteOne(r.GetID())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// tool to transform the argo status to a state
|
// tool to transform the argo status to a state
|
||||||
func (wfa *WorkflowExecution) ArgoStatusToState(status string) *WorkflowExecution {
|
func (wfa *WorkflowExecutions) ArgoStatusToState(status string) *WorkflowExecutions {
|
||||||
status = strings.ToLower(status)
|
status = strings.ToLower(status)
|
||||||
switch status {
|
switch status {
|
||||||
case "succeeded": // Succeeded
|
case "succeeded": // Succeeded
|
||||||
wfa.State = int64(SUCCESS.EnumIndex())
|
wfa.State = common.SUCCESS
|
||||||
case "pending": // Pending
|
case "pending": // Pending
|
||||||
wfa.State = int64(SCHEDULED.EnumIndex())
|
wfa.State = common.SCHEDULED
|
||||||
case "running": // Running
|
case "running": // Running
|
||||||
wfa.State = int64(STARTED.EnumIndex())
|
wfa.State = common.STARTED
|
||||||
default: // Failed
|
default: // Failed
|
||||||
wfa.State = int64(FAILURE.EnumIndex())
|
wfa.State = common.FAILURE
|
||||||
}
|
}
|
||||||
return wfa
|
return wfa
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *WorkflowExecution) GetID() string {
|
func (r *WorkflowExecutions) GenerateID() {
|
||||||
return ao.UUID
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *WorkflowExecution) GenerateID() {
|
|
||||||
r.UUID = uuid.New().String()
|
r.UUID = uuid.New().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *WorkflowExecution) GetName() string {
|
func (d *WorkflowExecutions) GetName() string {
|
||||||
return d.UUID + "_" + d.ExecDate.String()
|
return d.UUID + "_" + d.ExecDate.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *WorkflowExecution) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
func (d *WorkflowExecutions) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
data := New() // Create a new instance of the accessor
|
return NewAccessor(request) // 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 (d *WorkflowExecutions) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
func (dma *WorkflowExecution) Deserialize(j map[string]interface{}) utils.DBObject {
|
return true
|
||||||
b, err := json.Marshal(j)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
json.Unmarshal(b, dma)
|
|
||||||
return dma
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize returns the WorkflowExecution as a map
|
func (d *WorkflowExecutions) Book(wfID string, priceds map[tools.DataType][]pricing.PricedItemITF) []*booking.Booking {
|
||||||
func (dma *WorkflowExecution) Serialize() map[string]interface{} {
|
booking := d.bookEach(wfID, tools.STORAGE_RESOURCE, priceds[tools.STORAGE_RESOURCE])
|
||||||
var m map[string]interface{}
|
booking = append(booking, d.bookEach(wfID, tools.PROCESSING_RESOURCE, priceds[tools.PROCESSING_RESOURCE])...)
|
||||||
b, err := json.Marshal(dma)
|
return booking
|
||||||
if err != nil {
|
}
|
||||||
return nil
|
|
||||||
}
|
func (d *WorkflowExecutions) bookEach(wfID string, dt tools.DataType, priceds []pricing.PricedItemITF) []*booking.Booking {
|
||||||
json.Unmarshal(b, &m)
|
items := []*booking.Booking{}
|
||||||
return m
|
for _, priced := range priceds {
|
||||||
|
start := d.ExecDate
|
||||||
|
if s := priced.GetLocationStart(); s != nil {
|
||||||
|
start = *s
|
||||||
|
}
|
||||||
|
end := start.Add(time.Duration(priced.GetExplicitDurationInS()) * time.Second)
|
||||||
|
bookingItem := &booking.Booking{
|
||||||
|
State: common.DRAFT,
|
||||||
|
ResourceID: priced.GetID(),
|
||||||
|
ResourceType: dt,
|
||||||
|
DestPeerID: priced.GetCreatorID(),
|
||||||
|
WorkflowID: wfID,
|
||||||
|
ExecutionID: d.GetID(),
|
||||||
|
ExpectedStartDate: start,
|
||||||
|
ExpectedEndDate: &end,
|
||||||
|
}
|
||||||
|
items = append(items, bookingItem)
|
||||||
|
}
|
||||||
|
return items
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,84 +1,78 @@
|
|||||||
package workflow_execution
|
package workflow_execution
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"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/common"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type workflowExecutionMongoAccessor struct {
|
type workflowExecutionMongoAccessor struct {
|
||||||
utils.AbstractAccessor
|
utils.AbstractAccessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *workflowExecutionMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *workflowExecutionMongoAccessor {
|
||||||
return &workflowExecutionMongoAccessor{}
|
return &workflowExecutionMongoAccessor{
|
||||||
|
utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: tools.WORKFLOW_EXECUTION,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *workflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
func (wfa *workflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericDeleteOne(id, wfa)
|
return nil, 404, errors.New("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *workflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (wfa *workflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericUpdateOne(set, id, wfa, &WorkflowExecution{})
|
return nil, 404, errors.New("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *workflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (wfa *workflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return nil, 404, errors.New("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *workflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (wfa *workflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return nil, 404, errors.New("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *workflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *workflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
var workflow WorkflowExecution
|
return utils.GenericLoadOne[*WorkflowExecutions](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
if d.(*WorkflowExecutions).State == common.DRAFT && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||||
if err != nil {
|
utils.GenericDeleteOne(d.GetID(), a)
|
||||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
return nil, 404, errors.New("Not found")
|
||||||
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}},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
if d.(*WorkflowExecutions).State == common.SCHEDULED && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
|
d.(*WorkflowExecutions).State = common.FORGOTTEN
|
||||||
if err != nil {
|
utils.GenericRawUpdateOne(d, id, a)
|
||||||
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
}
|
||||||
return nil, code, err
|
return d, 200, nil
|
||||||
}
|
}, a)
|
||||||
var results []WorkflowExecution
|
}
|
||||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
|
||||||
return nil, 404, err
|
func (a *workflowExecutionMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
}
|
return utils.GenericLoadAll[*WorkflowExecutions](a.getExec(), isDraft, a)
|
||||||
for _, r := range results {
|
}
|
||||||
objs = append(objs, &r)
|
|
||||||
}
|
func (a *workflowExecutionMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
return objs, 200, nil
|
return utils.GenericSearch[*WorkflowExecutions](filters, search, (&WorkflowExecutions{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *workflowExecutionMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
if d.(*WorkflowExecutions).State == common.DRAFT && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||||
|
utils.GenericDeleteOne(d.GetID(), a)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if d.(*WorkflowExecutions).State == common.SCHEDULED && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||||
|
d.(*WorkflowExecutions).State = common.FORGOTTEN
|
||||||
|
utils.GenericRawUpdateOne(d, d.GetID(), a)
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
196
models/workflow_execution/workflow_scheduler.go
Normal file
196
models/workflow_execution/workflow_scheduler.go
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
package workflow_execution
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/robfig/cron"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
// it's a flying object only use in a session time. It's not stored in the database
|
||||||
|
type WorkflowSchedule struct {
|
||||||
|
Workflow *workflow.Workflow `json:"workflow,omitempty"` // Workflow is the workflow dependancy of the schedule
|
||||||
|
WorkflowExecutions []*WorkflowExecutions `json:"workflow_executions,omitempty"` // WorkflowExecutions is the list of executions of the workflow
|
||||||
|
Message string `json:"message,omitempty"` // Message is the message of the schedule
|
||||||
|
Warning string `json:"warning,omitempty"` // Warning is the warning message of the schedule
|
||||||
|
Start time.Time `json:"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"` // End is the end time of the schedule, is required and must be greater than the Start time
|
||||||
|
DurationS float64 `json:"duration_s" default:"-1"` // End is the end time of the schedule
|
||||||
|
Cron string `json:"cron,omitempty"` // here the cron format : ss mm hh dd MM dw task
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewScheduler(start string, end string, durationInS float64, cron string) *WorkflowSchedule {
|
||||||
|
s, err := time.Parse("2006-01-02T15:04:05", start)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ws := &WorkflowSchedule{
|
||||||
|
Start: s,
|
||||||
|
DurationS: durationInS,
|
||||||
|
Cron: cron,
|
||||||
|
}
|
||||||
|
e, err := time.Parse("2006-01-02T15:04:05", end)
|
||||||
|
if err == nil {
|
||||||
|
ws.End = &e
|
||||||
|
}
|
||||||
|
return ws
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WorkflowSchedule) CheckBooking(wfID string, request *tools.APIRequest) (bool, *workflow.Workflow, []*WorkflowExecutions, error) {
|
||||||
|
if request.Caller == nil && request.Caller.URLS == nil && request.Caller.URLS[tools.BOOKING] == nil || request.Caller.URLS[tools.BOOKING][tools.POST] == "" {
|
||||||
|
return false, nil, []*WorkflowExecutions{}, errors.New("no caller defined")
|
||||||
|
}
|
||||||
|
access := workflow.NewAccessor(nil)
|
||||||
|
res, code, err := access.LoadOne(wfID)
|
||||||
|
if code != 200 {
|
||||||
|
return false, nil, []*WorkflowExecutions{}, errors.New("could not load the workflow with id: " + err.Error())
|
||||||
|
}
|
||||||
|
wf := res.(*workflow.Workflow)
|
||||||
|
longest, priceds, wf, err := wf.Planify(ws.Start, ws.End, request)
|
||||||
|
if err != nil {
|
||||||
|
return false, wf, []*WorkflowExecutions{}, err
|
||||||
|
}
|
||||||
|
ws.DurationS = longest
|
||||||
|
ws.Message = "We estimate that the workflow will start at " + ws.Start.String() + " and last " + fmt.Sprintf("%v", ws.DurationS) + "seconds."
|
||||||
|
if ws.End != nil && ws.Start.Add(time.Duration(longest)*time.Second).After(*ws.End) {
|
||||||
|
ws.Warning = "The workflow may be too long to be executed in the given time frame, we will try to book it anyway\n"
|
||||||
|
}
|
||||||
|
execs, err := ws.getExecutions(wf)
|
||||||
|
if err != nil {
|
||||||
|
return false, wf, []*WorkflowExecutions{}, err
|
||||||
|
}
|
||||||
|
for _, exec := range execs {
|
||||||
|
bookings := exec.Book(wfID, priceds)
|
||||||
|
for _, booking := range bookings {
|
||||||
|
_, err := (&peer.Peer{}).LaunchPeerExecution(booking.DestPeerID, "",
|
||||||
|
tools.BOOKING, tools.POSTCHECK, booking.Serialize(booking), request.Caller)
|
||||||
|
if err != nil {
|
||||||
|
return false, wf, execs, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true, wf, execs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WorkflowSchedule) Schedules(wfID string, request *tools.APIRequest) (*workflow.Workflow, []*WorkflowExecutions, error) {
|
||||||
|
if request == nil {
|
||||||
|
return nil, []*WorkflowExecutions{}, errors.New("no request found")
|
||||||
|
}
|
||||||
|
c := request.Caller
|
||||||
|
if c == nil || c.URLS == nil || c.URLS[tools.BOOKING] == nil {
|
||||||
|
return nil, []*WorkflowExecutions{}, errors.New("no caller defined")
|
||||||
|
}
|
||||||
|
methods := c.URLS[tools.BOOKING]
|
||||||
|
if _, ok := methods[tools.POST]; !ok {
|
||||||
|
return nil, []*WorkflowExecutions{}, errors.New("no path found")
|
||||||
|
}
|
||||||
|
ok, wf, executions, err := ws.CheckBooking(wfID, request)
|
||||||
|
if !ok || err != nil {
|
||||||
|
return nil, []*WorkflowExecutions{}, errors.New("could not book the workflow" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.Workflow = wf
|
||||||
|
ws.WorkflowExecutions = executions
|
||||||
|
|
||||||
|
for _, exec := range executions {
|
||||||
|
err := exec.PurgeDraft(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, []*WorkflowExecutions{}, errors.New("could not book the workflow" + fmt.Sprintf("%v", err))
|
||||||
|
}
|
||||||
|
exec.GenerateID()
|
||||||
|
// Should DELETE the previous execution2
|
||||||
|
utils.GenericStoreOne(exec, NewAccessor(request))
|
||||||
|
}
|
||||||
|
return wf, executions, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
BOOKING IMPLIED TIME, not of subscription but of execution
|
||||||
|
so is processing time execution time applied on computes
|
||||||
|
data can improve the processing time
|
||||||
|
time should implied a security time border (10sec) if not from the same executions
|
||||||
|
VERIFY THAT WE HANDLE DIFFERENCE BETWEEN LOCATION TIME && BOOKING
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getExecutions is a function that returns the executions of a workflow
|
||||||
|
* it returns an array of workflow_execution.WorkflowExecution
|
||||||
|
*/
|
||||||
|
func (ws *WorkflowSchedule) getExecutions(workflow *workflow.Workflow) ([]*WorkflowExecutions, error) {
|
||||||
|
workflows_executions := []*WorkflowExecutions{}
|
||||||
|
dates, err := ws.getDates()
|
||||||
|
if err != nil {
|
||||||
|
return workflows_executions, err
|
||||||
|
}
|
||||||
|
for _, date := range dates {
|
||||||
|
obj := &WorkflowExecutions{
|
||||||
|
AbstractObject: utils.AbstractObject{
|
||||||
|
Name: workflow.Name + "_execution_" + date.Start.String(), // set the name of the execution
|
||||||
|
},
|
||||||
|
ExecDate: date.Start, // set the execution date
|
||||||
|
EndDate: date.End, // set the end date
|
||||||
|
State: common.DRAFT, // set the state to 1 (scheduled)
|
||||||
|
WorkflowID: workflow.GetID(), // set the workflow id dependancy of the execution
|
||||||
|
}
|
||||||
|
workflows_executions = append(workflows_executions, obj)
|
||||||
|
}
|
||||||
|
return workflows_executions, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WorkflowSchedule) getDates() ([]Schedule, error) {
|
||||||
|
schedule := []Schedule{}
|
||||||
|
if len(ws.Cron) > 0 { // if cron is set then end date should be set
|
||||||
|
if ws.End == nil {
|
||||||
|
return schedule, errors.New("a cron task should have an end date.")
|
||||||
|
}
|
||||||
|
if ws.DurationS <= 0 {
|
||||||
|
ws.DurationS = ws.End.Sub(ws.Start).Seconds()
|
||||||
|
}
|
||||||
|
cronStr := strings.Split(ws.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 schedule, errors.New("Bad cron message: (" + ws.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 schedule, errors.New("Bad cron message: " + err.Error())
|
||||||
|
}
|
||||||
|
// loop through the cron schedule to set the executions
|
||||||
|
for s := sched.Next(ws.Start); !s.IsZero() && s.Before(*ws.End); s = sched.Next(s) {
|
||||||
|
e := s.Add(time.Duration(ws.DurationS) * time.Second)
|
||||||
|
schedule = append(schedule, Schedule{
|
||||||
|
Start: s,
|
||||||
|
End: &e,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else { // if no cron, set the execution to the start date
|
||||||
|
schedule = append(schedule, Schedule{
|
||||||
|
Start: ws.Start,
|
||||||
|
End: ws.End,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return schedule, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Schedule struct {
|
||||||
|
Start time.Time
|
||||||
|
End *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO : LARGEST GRAIN PLANIFYING THE WORKFLOW WHEN OPTION IS SET
|
||||||
|
* SET PROTECTION BORDER TIME
|
||||||
|
*/
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,330 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,60 +1,37 @@
|
|||||||
package workspace
|
package workspace
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"fmt"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
"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/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Workspace is a struct that represents a workspace
|
// Workspace is a struct that represents a workspace
|
||||||
type Workspace struct {
|
type Workspace struct {
|
||||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
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)
|
resources.ResourceSet // ResourceSet contains the resources of the workspace (data, compute, processing, storage, workflow)
|
||||||
IsContextual bool `json:"is_contextual" bson:"is_contextual" default:"false"` // IsContextual is a flag that indicates if the workspace is contextual
|
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
|
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
|
Shared string `json:"shared,omitempty" bson:"shared,omitempty"` // Shared is the ID of the shared workspace
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *Workspace) GetID() string {
|
func (d *Workspace) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
return ao.UUID
|
return NewAccessor(request) // Create a new instance of the accessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Workspace) GenerateID() {
|
func (ao *Workspace) VerifyAuth(request *tools.APIRequest) bool {
|
||||||
if r.UUID == "" {
|
fmt.Println("Workspace.VerifyAuth", ao.Shared)
|
||||||
r.UUID = uuid.New().String()
|
if ao.Shared != "" {
|
||||||
|
shared, code, _ := shallow_collaborative_area.NewAccessor(request).LoadOne(ao.Shared)
|
||||||
|
fmt.Println("Workspace.VerifyAuth", shared, code)
|
||||||
|
if code != 200 || shared == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return shared.VerifyAuth(request)
|
||||||
}
|
}
|
||||||
}
|
fmt.Println("Workspace.VerifyAuth", ao.AbstractObject.VerifyAuth(request))
|
||||||
|
return ao.AbstractObject.VerifyAuth(request)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
32
models/workspace/workspace_history_mongo_accessor.go
Normal file
32
models/workspace/workspace_history_mongo_accessor.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package workspace
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WorkspaceHistory struct{ Workspace }
|
||||||
|
|
||||||
|
func (d *WorkspaceHistory) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
|
return NewAccessorHistory(request) // Create a new instance of the accessor
|
||||||
|
}
|
||||||
|
func (r *WorkspaceHistory) GenerateID() {
|
||||||
|
r.UUID = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workspace is a struct that represents a workspace
|
||||||
|
type workspaceHistoryMongoAccessor struct {
|
||||||
|
workspaceMongoAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the workspaceMongoAccessor
|
||||||
|
func NewHistory() *workspaceHistoryMongoAccessor {
|
||||||
|
return &workspaceHistoryMongoAccessor{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *workspaceHistoryMongoAccessor) MapFromWorkspace(w *Workspace) *WorkspaceHistory {
|
||||||
|
wh := &WorkspaceHistory{Workspace: *w}
|
||||||
|
wh.GenerateID()
|
||||||
|
return wh
|
||||||
|
}
|
||||||
@@ -2,17 +2,13 @@ package workspace
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"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/collaborative_area/shallow_collaborative_area"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"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/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/shallow_shared_workspace"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,207 +18,130 @@ type workspaceMongoAccessor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the workspaceMongoAccessor
|
// New creates a new instance of the workspaceMongoAccessor
|
||||||
func New() *workspaceMongoAccessor {
|
func NewAccessorHistory(request *tools.APIRequest) *workspaceMongoAccessor {
|
||||||
return &workspaceMongoAccessor{}
|
return new(tools.WORKSPACE_HISTORY, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAccessor(request *tools.APIRequest) *workspaceMongoAccessor {
|
||||||
|
return new(tools.WORKSPACE, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of the workspaceMongoAccessor
|
||||||
|
func new(t tools.DataType, request *tools.APIRequest) *workspaceMongoAccessor {
|
||||||
|
return &workspaceMongoAccessor{
|
||||||
|
utils.AbstractAccessor{
|
||||||
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||||
|
Request: request,
|
||||||
|
Type: t,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteOne deletes a workspace from the database, given its ID, it automatically share to peers if the workspace is shared
|
// 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
|
// it checks if a workspace with the same name already exists
|
||||||
func (wfa *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
func (a *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
res, code, err := wfa.GenericDeleteOne(id, wfa)
|
res, code, err := utils.GenericDeleteOne(id, a)
|
||||||
wfa.share(res.(*Workspace), true, wfa.Caller) // Share the deletion to the peers
|
if code == 200 && res != nil {
|
||||||
|
a.share(res.(*Workspace), tools.DELETE, a.GetCaller()) // Share the deletion to the peers
|
||||||
|
}
|
||||||
return res, code, err
|
return res, code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateOne updates a workspace in the database, given its ID, it automatically share to peers if the workspace is shared
|
// 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) {
|
func (a *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
d := set.(*Workspace) // Get the workspace from the set
|
d := set.(*Workspace) // Get the workspace from the set
|
||||||
d.DataResources = nil // Reset the resources
|
d.Clear()
|
||||||
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
|
if d.Active { // If the workspace is active, deactivate all the other workspaces
|
||||||
res, _, err := wfa.LoadAll()
|
res, _, err := a.LoadAll(true)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, r := range res {
|
for _, r := range res {
|
||||||
if r.GetID() != id {
|
if r.GetID() != id {
|
||||||
r.(*Workspace).Active = false
|
r.(*Workspace).Active = false
|
||||||
wfa.UpdateOne(r.(*Workspace), r.GetID())
|
a.UpdateOne(r.(*Workspace), r.GetID())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res, code, err := wfa.GenericUpdateOne(set, id, wfa, &Workspace{})
|
res, code, err := utils.GenericUpdateOne(set, id, a, &Workspace{})
|
||||||
wfa.share(res.(*Workspace), false, wfa.Caller)
|
if code == 200 && res != nil {
|
||||||
|
a.share(res.(*Workspace), tools.PUT, a.GetCaller())
|
||||||
|
}
|
||||||
return res, code, err
|
return res, code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// StoreOne stores a workspace in the database, it checks if a workspace with the same name already exists
|
// 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) {
|
func (a *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
filters := &dbs.Filters{
|
filters := &dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{
|
Or: map[string][]dbs.Filter{
|
||||||
"abstractobject.name": {{dbs.LIKE.String(), data.GetName() + "_workspace"}},
|
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: data.GetName() + "_workspace"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
res, _, err := wfa.Search(filters, "") // Search for the workspace
|
res, _, err := a.Search(filters, "", true) // Search for the workspace
|
||||||
if err == nil && len(res) > 0 { // If the workspace already exists, return an error
|
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")
|
return nil, 409, errors.New("A workspace with the same name already exists")
|
||||||
}
|
}
|
||||||
// reset the resources
|
// reset the resources
|
||||||
d := data.(*Workspace)
|
d := data.(*Workspace)
|
||||||
d.DataResources = nil
|
d.Clear()
|
||||||
d.DatacenterResources = nil
|
return utils.GenericStoreOne(d, a)
|
||||||
d.StorageResources = nil
|
|
||||||
d.ProcessingResources = nil
|
|
||||||
d.WorkflowResources = nil
|
|
||||||
return wfa.GenericStoreOne(d, wfa)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyOne copies a workspace in the database
|
// CopyOne copies a workspace in the database
|
||||||
func (wfa *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (a *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return utils.GenericStoreOne(data, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func (a *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
This function is used to fill the workspace with the resources
|
return utils.GenericLoadOne[*Workspace](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
*/
|
d.(*Workspace).Fill(a.GetRequest())
|
||||||
func (wfa *workspaceMongoAccessor) fill(workflow *Workspace) *Workspace {
|
return d, 200, nil
|
||||||
// Fill the workspace with the resources
|
}, a)
|
||||||
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 (a *workspaceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
func (wfa *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
return utils.GenericLoadAll[*Workspace](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
var workflow Workspace
|
d.(*Workspace).Fill(a.GetRequest())
|
||||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
return d
|
||||||
if err != nil {
|
}, isDraft, a)
|
||||||
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 (a *workspaceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
func (wfa workspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
return utils.GenericSearch[*Workspace](filters, search, (&Workspace{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
objs := []utils.ShallowDBObject{}
|
d.(*Workspace).Fill(a.GetRequest())
|
||||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
return d
|
||||||
if err != nil {
|
}, isDraft, a)
|
||||||
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
|
This function is used to share the workspace with the peers
|
||||||
*/
|
*/
|
||||||
func (wfa *workspaceMongoAccessor) share(realData *Workspace, delete bool, caller *tools.HTTPCaller) {
|
func (a *workspaceMongoAccessor) share(realData *Workspace, method tools.METHOD, caller *tools.HTTPCaller) {
|
||||||
if realData.Shared == "" {
|
fmt.Println("Sharing workspace", realData, caller)
|
||||||
|
if realData == nil || realData.Shared == "" || caller == nil || caller.Disabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
access := (&shallow_shared_workspace.ShallowSharedWorkspace{}).GetAccessor(nil)
|
shallow := &shallow_collaborative_area.ShallowCollaborativeArea{}
|
||||||
|
access := (shallow).GetAccessor(a.GetRequest())
|
||||||
res, code, _ := access.LoadOne(realData.Shared)
|
res, code, _ := access.LoadOne(realData.Shared)
|
||||||
if code != 200 {
|
if code != 200 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
paccess := &peer.Peer{}
|
paccess := &peer.Peer{}
|
||||||
for _, p := range res.(*shallow_shared_workspace.ShallowSharedWorkspace).Peers {
|
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
||||||
paccess.UUID = p
|
paccess.UUID = p
|
||||||
if paccess.IsMySelf() { // If the peer is the current peer, never share because it will create a loop
|
if ok, _ := paccess.IsMySelf(); ok { // If the peer is the current peer, never share because it will create a loop
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if delete { // If the workspace is deleted, share the deletion
|
if method == tools.DELETE { // If the workspace is deleted, share the deletion
|
||||||
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKSPACE, tools.DELETE, map[string]interface{}{}, caller)
|
history := NewHistory()
|
||||||
|
history.StoreOne(history.MapFromWorkspace(res.(*Workspace)))
|
||||||
|
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKSPACE, tools.DELETE, map[string]interface{}{}, caller)
|
||||||
} else { // If the workspace is updated, share the update
|
} else { // If the workspace is updated, share the update
|
||||||
_, err = paccess.LaunchPeerExecution(p, res.GetID(), utils.WORKSPACE, tools.PUT, res.Serialize(), caller)
|
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKSPACE, tools.PUT, res.Serialize(res), caller)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wfa.Logger.Error().Msg(err.Error())
|
a.Logger.Error().Msg(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
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",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BIN
swagger/favicon-16x16.png
Normal file
BIN
swagger/favicon-16x16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 665 B |
BIN
swagger/favicon-32x32.png
Normal file
BIN
swagger/favicon-32x32.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 628 B |
60
swagger/index.html
Normal file
60
swagger/index.html
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<!-- HTML for static distribution bundle build -->
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Swagger UI</title>
|
||||||
|
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
|
||||||
|
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
|
||||||
|
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
|
||||||
|
<style>
|
||||||
|
html
|
||||||
|
{
|
||||||
|
box-sizing: border-box;
|
||||||
|
overflow: -moz-scrollbars-vertical;
|
||||||
|
overflow-y: scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
*,
|
||||||
|
*:before,
|
||||||
|
*:after
|
||||||
|
{
|
||||||
|
box-sizing: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
body
|
||||||
|
{
|
||||||
|
margin:0;
|
||||||
|
background: #fafafa;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="swagger-ui"></div>
|
||||||
|
|
||||||
|
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
|
||||||
|
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
|
||||||
|
<script>
|
||||||
|
window.onload = function() {
|
||||||
|
// Begin Swagger UI call region
|
||||||
|
const ui = SwaggerUIBundle({
|
||||||
|
url: "https://petstore.swagger.io/v2/swagger.json",
|
||||||
|
dom_id: '#swagger-ui',
|
||||||
|
deepLinking: true,
|
||||||
|
presets: [
|
||||||
|
SwaggerUIBundle.presets.apis,
|
||||||
|
SwaggerUIStandalonePreset
|
||||||
|
],
|
||||||
|
plugins: [
|
||||||
|
SwaggerUIBundle.plugins.DownloadUrl
|
||||||
|
],
|
||||||
|
layout: "StandaloneLayout"
|
||||||
|
});
|
||||||
|
// End Swagger UI call region
|
||||||
|
|
||||||
|
window.ui = ui;
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
79
swagger/oauth2-redirect.html
Normal file
79
swagger/oauth2-redirect.html
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en-US">
|
||||||
|
<head>
|
||||||
|
<title>Swagger UI: OAuth2 Redirect</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
'use strict';
|
||||||
|
function run () {
|
||||||
|
var oauth2 = window.opener.swaggerUIRedirectOauth2;
|
||||||
|
var sentState = oauth2.state;
|
||||||
|
var redirectUrl = oauth2.redirectUrl;
|
||||||
|
var isValid, qp, arr;
|
||||||
|
|
||||||
|
if (/code|token|error/.test(window.location.hash)) {
|
||||||
|
qp = window.location.hash.substring(1);
|
||||||
|
} else {
|
||||||
|
qp = location.search.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
arr = qp.split("&");
|
||||||
|
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';});
|
||||||
|
qp = qp ? JSON.parse('{' + arr.join() + '}',
|
||||||
|
function (key, value) {
|
||||||
|
return key === "" ? value : decodeURIComponent(value);
|
||||||
|
}
|
||||||
|
) : {};
|
||||||
|
|
||||||
|
isValid = qp.state === sentState;
|
||||||
|
|
||||||
|
if ((
|
||||||
|
oauth2.auth.schema.get("flow") === "accessCode" ||
|
||||||
|
oauth2.auth.schema.get("flow") === "authorizationCode" ||
|
||||||
|
oauth2.auth.schema.get("flow") === "authorization_code"
|
||||||
|
) && !oauth2.auth.code) {
|
||||||
|
if (!isValid) {
|
||||||
|
oauth2.errCb({
|
||||||
|
authId: oauth2.auth.name,
|
||||||
|
source: "auth",
|
||||||
|
level: "warning",
|
||||||
|
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (qp.code) {
|
||||||
|
delete oauth2.state;
|
||||||
|
oauth2.auth.code = qp.code;
|
||||||
|
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
|
||||||
|
} else {
|
||||||
|
let oauthErrorMsg;
|
||||||
|
if (qp.error) {
|
||||||
|
oauthErrorMsg = "["+qp.error+"]: " +
|
||||||
|
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
|
||||||
|
(qp.error_uri ? "More info: "+qp.error_uri : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
oauth2.errCb({
|
||||||
|
authId: oauth2.auth.name,
|
||||||
|
source: "auth",
|
||||||
|
level: "error",
|
||||||
|
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
|
||||||
|
}
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (document.readyState !== 'loading') {
|
||||||
|
run();
|
||||||
|
} else {
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
run();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
3
swagger/swagger-ui-bundle.js
Normal file
3
swagger/swagger-ui-bundle.js
Normal file
File diff suppressed because one or more lines are too long
1
swagger/swagger-ui-bundle.js.map
Normal file
1
swagger/swagger-ui-bundle.js.map
Normal file
File diff suppressed because one or more lines are too long
2
swagger/swagger-ui-es-bundle-core.js
Normal file
2
swagger/swagger-ui-es-bundle-core.js
Normal file
File diff suppressed because one or more lines are too long
3
swagger/swagger-ui-es-bundle.js
Normal file
3
swagger/swagger-ui-es-bundle.js
Normal file
File diff suppressed because one or more lines are too long
3
swagger/swagger-ui-standalone-preset.js
Normal file
3
swagger/swagger-ui-standalone-preset.js
Normal file
File diff suppressed because one or more lines are too long
1
swagger/swagger-ui-standalone-preset.js.map
Normal file
1
swagger/swagger-ui-standalone-preset.js.map
Normal file
File diff suppressed because one or more lines are too long
4
swagger/swagger-ui.css
Normal file
4
swagger/swagger-ui.css
Normal file
File diff suppressed because one or more lines are too long
1
swagger/swagger-ui.css.map
Normal file
1
swagger/swagger-ui.css.map
Normal file
File diff suppressed because one or more lines are too long
2
swagger/swagger-ui.js
Normal file
2
swagger/swagger-ui.js
Normal file
File diff suppressed because one or more lines are too long
1
swagger/swagger-ui.js.map
Normal file
1
swagger/swagger-ui.js.map
Normal file
File diff suppressed because one or more lines are too long
58
tools/api.go
58
tools/api.go
@@ -3,11 +3,21 @@ package tools
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/config"
|
"cloud.o-forge.io/core/oc-lib/config"
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||||
|
beego "github.com/beego/beego/v2/server/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type APIRequest struct {
|
||||||
|
Username string
|
||||||
|
PeerID string
|
||||||
|
Groups []string
|
||||||
|
Caller *HTTPCaller
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* API is the Health Check API
|
* API is the Health Check API
|
||||||
* it defines the health check methods
|
* it defines the health check methods
|
||||||
@@ -49,6 +59,16 @@ func (s State) String() string {
|
|||||||
|
|
||||||
type API struct{}
|
type API struct{}
|
||||||
|
|
||||||
|
func (a *API) Discovered(infos []*beego.ControllerInfo) {
|
||||||
|
respondToDiscovery := func(m map[string]interface{}) {
|
||||||
|
if len(m) == 0 {
|
||||||
|
a.SubscribeRouter(infos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a.ListenRouter(respondToDiscovery)
|
||||||
|
a.SubscribeRouter(infos)
|
||||||
|
}
|
||||||
|
|
||||||
// GetState returns the state of the API
|
// GetState returns the state of the API
|
||||||
func (a *API) GetState() (State, int, error) {
|
func (a *API) GetState() (State, int, error) {
|
||||||
// Check if the database is up
|
// Check if the database is up
|
||||||
@@ -70,12 +90,35 @@ func (a *API) GetState() (State, int, error) {
|
|||||||
return ALIVE, 200, nil // If everything is up, return alive
|
return ALIVE, 200, nil // If everything is up, return alive
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) ListenRouter(exec func(msg map[string]interface{})) {
|
||||||
|
go NewNATSCaller().ListenNats(DISCOVERY.GenerateKey("api"), exec)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) SubscribeRouter(infos []*beego.ControllerInfo) {
|
||||||
|
nats := NewNATSCaller()
|
||||||
|
discovery := map[string][]string{}
|
||||||
|
for _, info := range infos {
|
||||||
|
path := strings.ReplaceAll(info.GetPattern(), "/oc/", "/"+strings.ReplaceAll(config.GetAppName(), "oc-", ""))
|
||||||
|
for k, v := range info.GetMethod() {
|
||||||
|
if discovery[path] == nil {
|
||||||
|
discovery[path] = []string{}
|
||||||
|
}
|
||||||
|
if strings.Contains(strings.ToLower(v), "internal") {
|
||||||
|
discovery[path] = append(discovery[path], "INTERNAL"+k)
|
||||||
|
} else {
|
||||||
|
discovery[path] = append(discovery[path], k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nats.SetNATSPub("api", DISCOVERY, discovery)
|
||||||
|
}
|
||||||
|
|
||||||
// CheckRemotePeer checks the state of a remote peer
|
// CheckRemotePeer checks the state of a remote peer
|
||||||
func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
|
func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
|
||||||
// Check if the database is up
|
// Check if the database is up
|
||||||
caller := NewHTTPCaller(map[string]map[METHOD]string{}) // Create a new http caller
|
caller := NewHTTPCaller(map[DataType]map[METHOD]string{}) // Create a new http caller
|
||||||
var resp APIStatusResponse
|
var resp APIStatusResponse
|
||||||
b, err := caller.CallPost(url, "/status", map[string]interface{}{}) // Call the status endpoint of the peer
|
b, err := caller.CallPost(url, "", map[string]interface{}{}) // Call the status endpoint of the peer
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DEAD, map[string]int{} // If the peer is not reachable, return dead
|
return DEAD, map[string]int{} // If the peer is not reachable, return dead
|
||||||
}
|
}
|
||||||
@@ -91,27 +134,28 @@ func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckRemoteAPIs checks the state of remote APIs from your proper OC
|
// CheckRemoteAPIs checks the state of remote APIs from your proper OC
|
||||||
func (a *API) CheckRemoteAPIs(urls map[string]string) (State, map[string]string, error) {
|
func (a *API) CheckRemoteAPIs(apis []DataType) (State, map[string]string, error) {
|
||||||
// Check if the database is up
|
// Check if the database is up
|
||||||
new := map[string]string{}
|
new := map[string]string{}
|
||||||
caller := NewHTTPCaller(map[string]map[METHOD]string{}) // Create a new http caller
|
caller := NewHTTPCaller(map[DataType]map[METHOD]string{}) // Create a new http caller
|
||||||
code := 0
|
code := 0
|
||||||
e := ""
|
e := ""
|
||||||
state := ALIVE
|
state := ALIVE
|
||||||
reachable := false
|
reachable := false
|
||||||
for appName, url := range urls { // Check the state of each remote API in the list
|
for _, api := range apis { // Check the state of each remote API in the list
|
||||||
var resp APIStatusResponse
|
var resp APIStatusResponse
|
||||||
b, err := caller.CallGet(url, "/version/status") // Call the status endpoint of the remote API (standard OC status endpoint)
|
b, err := caller.CallGet("http://"+api.API()+":8080", "/oc/version/status") // Call the status endpoint of the remote API (standard OC status endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state = REDUCED_SERVICE // If a remote API is not reachable, return reduced service
|
state = REDUCED_SERVICE // If a remote API is not reachable, return reduced service
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, &resp)
|
json.Unmarshal(b, &resp)
|
||||||
|
fmt.Println(string(b))
|
||||||
if resp.Data == nil { //
|
if resp.Data == nil { //
|
||||||
state = REDUCED_SERVICE // If the response is empty, return reduced service
|
state = REDUCED_SERVICE // If the response is empty, return reduced service
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
new[appName] = resp.Data.State
|
new[api.String()] = resp.Data.State
|
||||||
if resp.Data.Code > code {
|
if resp.Data.Code > code {
|
||||||
code = resp.Data.Code
|
code = resp.Data.Code
|
||||||
e += resp.Error
|
e += resp.Error
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package tools
|
||||||
|
|
||||||
type DataType int
|
type DataType int
|
||||||
|
|
||||||
@@ -8,34 +8,50 @@ const (
|
|||||||
DATA_RESOURCE
|
DATA_RESOURCE
|
||||||
PROCESSING_RESOURCE
|
PROCESSING_RESOURCE
|
||||||
STORAGE_RESOURCE
|
STORAGE_RESOURCE
|
||||||
DATACENTER_RESOURCE
|
COMPUTE_RESOURCE
|
||||||
WORKFLOW_RESOURCE
|
WORKFLOW_RESOURCE
|
||||||
WORKFLOW
|
WORKFLOW
|
||||||
WORKFLOW_EXECUTION
|
WORKFLOW_EXECUTION
|
||||||
WORKSPACE
|
WORKSPACE
|
||||||
RESOURCE_MODEL
|
RESOURCE_MODEL
|
||||||
PEER
|
PEER
|
||||||
SHARED_WORKSPACE
|
COLLABORATIVE_AREA
|
||||||
RULE
|
RULE
|
||||||
BOOKING
|
BOOKING
|
||||||
|
WORKFLOW_HISTORY
|
||||||
|
WORKSPACE_HISTORY
|
||||||
|
ORDER
|
||||||
|
BUYING_STATUS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var NOAPI = ""
|
||||||
|
var CATALOGAPI = "oc-catalog"
|
||||||
|
var SHAREDAPI = "oc-shared"
|
||||||
|
var WORKFLOWAPI = "oc-workflow"
|
||||||
|
var WORKSPACEAPI = "oc-workspace"
|
||||||
|
var PEERSAPI = "oc-peer"
|
||||||
|
var DATACENTERAPI = "oc-datacenter"
|
||||||
|
|
||||||
// Bind the standard API name to the data type
|
// Bind the standard API name to the data type
|
||||||
var DefaultAPI = [...]string{
|
var DefaultAPI = [...]string{
|
||||||
"",
|
NOAPI,
|
||||||
"oc-catalog",
|
CATALOGAPI,
|
||||||
"oc-catalog",
|
CATALOGAPI,
|
||||||
"oc-catalog",
|
CATALOGAPI,
|
||||||
"oc-catalog",
|
CATALOGAPI,
|
||||||
"oc-catalog",
|
CATALOGAPI,
|
||||||
"oc-workflow",
|
WORKFLOWAPI,
|
||||||
"",
|
NOAPI,
|
||||||
"oc-workspace",
|
WORKSPACEAPI,
|
||||||
"",
|
CATALOGAPI,
|
||||||
"oc-peers",
|
PEERSAPI,
|
||||||
"oc-shared",
|
SHAREDAPI,
|
||||||
"oc-shared",
|
SHAREDAPI,
|
||||||
"oc-datacenter",
|
DATACENTERAPI,
|
||||||
|
NOAPI,
|
||||||
|
NOAPI,
|
||||||
|
NOAPI,
|
||||||
|
NOAPI,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind the standard data name to the data type
|
// Bind the standard data name to the data type
|
||||||
@@ -44,16 +60,20 @@ var Str = [...]string{
|
|||||||
"data_resource",
|
"data_resource",
|
||||||
"processing_resource",
|
"processing_resource",
|
||||||
"storage_resource",
|
"storage_resource",
|
||||||
"datacenter_resource",
|
"compute_resource",
|
||||||
"workflow_resource",
|
"workflow_resource",
|
||||||
"workflow",
|
"workflow",
|
||||||
"workflow_execution",
|
"workflow_execution",
|
||||||
"workspace",
|
"workspace",
|
||||||
"resource_model",
|
"resource_model",
|
||||||
"peer",
|
"peer",
|
||||||
"shared_workspace",
|
"collaborative_area",
|
||||||
"rule",
|
"rule",
|
||||||
"booking",
|
"booking",
|
||||||
|
"workflow_history",
|
||||||
|
"workspace_history",
|
||||||
|
"order",
|
||||||
|
"buying_status",
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromInt(i int) string {
|
func FromInt(i int) string {
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user