@@ -59,7 +59,31 @@ type ComponentFilterOptions struct {
5959 OnlyDirect bool
6060}
6161
62+ type ComponentProperty struct {
63+ Group string `json:"groupName,omitempty"`
64+ Name string `json:"propertyName,omitempty"`
65+ Value string `json:"propertyValue,omitempty"`
66+ Type string `json:"propertyType"`
67+ Description string `json:"description,omitempty"`
68+ UUID uuid.UUID `json:"uuid"`
69+ }
70+
71+ type ComponentIdentityQueryOptions struct {
72+ Group string
73+ Name string
74+ Version string
75+ PURL string
76+ CPE string
77+ SWIDTagID string
78+ Project uuid.UUID
79+ }
80+
6281func (cs ComponentService ) Get (ctx context.Context , componentUUID uuid.UUID ) (c Component , err error ) {
82+ err = cs .client .assertServerVersionAtLeast ("3.0.0" )
83+ if err != nil {
84+ return
85+ }
86+
6387 req , err := cs .client .newRequest (ctx , http .MethodGet , fmt .Sprintf ("/api/v1/component/%s" , componentUUID ))
6488 if err != nil {
6589 return
@@ -70,6 +94,11 @@ func (cs ComponentService) Get(ctx context.Context, componentUUID uuid.UUID) (c
7094}
7195
7296func (cs ComponentService ) GetAll (ctx context.Context , projectUUID uuid.UUID , po PageOptions , filterOptions ComponentFilterOptions ) (p Page [Component ], err error ) {
97+ err = cs .client .assertServerVersionAtLeast ("4.0.0" )
98+ if err != nil {
99+ return
100+ }
101+
73102 req , err := cs .client .newRequest (ctx , http .MethodGet , fmt .Sprintf ("/api/v1/component/project/%s" , projectUUID ), withPageOptions (po ), withComponentFilterOptions (filterOptions ))
74103 if err != nil {
75104 return
@@ -98,10 +127,13 @@ func withComponentFilterOptions(filterOptions ComponentFilterOptions) requestOpt
98127 }
99128}
100129
101- func (cs ComponentService ) Create (ctx context.Context , projectUUID string , component Component ) (c Component , err error ) {
102- req , err := cs .client .newRequest (ctx , http .MethodPut ,
103- fmt .Sprintf ("/api/v1/component/project/%s" , projectUUID ),
104- withBody (component ))
130+ func (cs ComponentService ) Create (ctx context.Context , projectUUID uuid.UUID , component Component ) (c Component , err error ) {
131+ err = cs .client .assertServerVersionAtLeast ("3.0.0" )
132+ if err != nil {
133+ return
134+ }
135+
136+ req , err := cs .client .newRequest (ctx , http .MethodPut , fmt .Sprintf ("/api/v1/component/project/%s" , projectUUID ), withBody (component ))
105137 if err != nil {
106138 return
107139 }
@@ -111,12 +143,164 @@ func (cs ComponentService) Create(ctx context.Context, projectUUID string, compo
111143}
112144
113145func (cs ComponentService ) Update (ctx context.Context , component Component ) (c Component , err error ) {
114- req , err := cs .client .newRequest (ctx , http .MethodPost ,
115- "/api/v1/component" ,
116- withBody (component ))
146+ err = cs .client .assertServerVersionAtLeast ("3.0.0" )
117147 if err != nil {
118148 return
119149 }
150+
151+ req , err := cs .client .newRequest (ctx , http .MethodPost , "/api/v1/component" , withBody (component ))
152+ if err != nil {
153+ return
154+ }
155+
120156 _ , err = cs .client .doRequest (req , & c )
121157 return
122158}
159+
160+ func (cs ComponentService ) Delete (ctx context.Context , componentUUID uuid.UUID ) (err error ) {
161+ err = cs .client .assertServerVersionAtLeast ("3.0.0" )
162+ if err != nil {
163+ return
164+ }
165+
166+ req , err := cs .client .newRequest (ctx , http .MethodDelete , fmt .Sprintf ("/api/v1/component/%s" , componentUUID ))
167+ if err != nil {
168+ return
169+ }
170+
171+ _ , err = cs .client .doRequest (req , nil )
172+ return
173+ }
174+
175+ func (cs ComponentService ) GetProperties (ctx context.Context , componentUUID uuid.UUID ) (ps []ComponentProperty , err error ) {
176+ err = cs .client .assertServerVersionAtLeast ("4.11.0" )
177+ if err != nil {
178+ return
179+ }
180+
181+ req , err := cs .client .newRequest (ctx , http .MethodGet , fmt .Sprintf ("/api/v1/component/%s/property" , componentUUID ))
182+ if err != nil {
183+ return
184+ }
185+
186+ _ , err = cs .client .doRequest (req , & ps )
187+ return
188+ }
189+
190+ func (cs ComponentService ) CreateProperty (ctx context.Context , componentUUID uuid.UUID , property ComponentProperty ) (p ComponentProperty , err error ) {
191+ err = cs .client .assertServerVersionAtLeast ("4.11.0" )
192+ if err != nil {
193+ return
194+ }
195+
196+ req , err := cs .client .newRequest (ctx , http .MethodPut , fmt .Sprintf ("/api/v1/component/%s/property" , componentUUID ), withBody (property ))
197+ if err != nil {
198+ return
199+ }
200+
201+ _ , err = cs .client .doRequest (req , & p )
202+ return
203+ }
204+
205+ func (cs ComponentService ) DeleteProperty (ctx context.Context , componentUUID , propertyUUID uuid.UUID ) (err error ) {
206+ err = cs .client .assertServerVersionAtLeast ("4.11.0" )
207+ if err != nil {
208+ return
209+ }
210+
211+ req , err := cs .client .newRequest (ctx , http .MethodDelete , fmt .Sprintf ("/api/v1/component/%s/property/%s" , componentUUID , propertyUUID ))
212+ if err != nil {
213+ return
214+ }
215+
216+ _ , err = cs .client .doRequest (req , nil )
217+ return
218+ }
219+
220+ func (cs ComponentService ) GetByHash (ctx context.Context , hash string , po PageOptions , so SortOptions ) (p Page [Component ], err error ) {
221+ err = cs .client .assertServerVersionAtLeast ("3.0.0" )
222+ if err != nil {
223+ return
224+ }
225+
226+ req , err := cs .client .newRequest (ctx , http .MethodGet , fmt .Sprintf ("/api/v1/component/hash/%s" , hash ), withPageOptions (po ), withSortOptions (so ))
227+ if err != nil {
228+ return
229+ }
230+
231+ res , err := cs .client .doRequest (req , & p .Items )
232+ if err != nil {
233+ return
234+ }
235+
236+ if cs .client .isServerVersionAtLeast ("4.0.0" ) {
237+ p .TotalCount = res .TotalCount
238+ } else {
239+ p .TotalCount = len (p .Items )
240+ }
241+ return
242+ }
243+
244+ func (cs ComponentService ) GetByIdentity (ctx context.Context , po PageOptions , so SortOptions , io ComponentIdentityQueryOptions ) (p Page [Component ], err error ) {
245+ err = cs .client .assertServerVersionAtLeast ("4.0.0" )
246+ if err != nil {
247+ return
248+ }
249+
250+ req , err := cs .client .newRequest (ctx , http .MethodGet , "/api/v1/component/identity" , withPageOptions (po ), withSortOptions (so ), withComponentIdentityOptions (io ))
251+ if err != nil {
252+ return
253+ }
254+
255+ res , err := cs .client .doRequest (req , & p .Items )
256+ if err != nil {
257+ return
258+ }
259+
260+ p .TotalCount = res .TotalCount
261+ return
262+ }
263+
264+ func withComponentIdentityOptions (identityOptions ComponentIdentityQueryOptions ) requestOption {
265+ return func (req * http.Request ) error {
266+ query := req .URL .Query ()
267+ if len (identityOptions .Group ) > 0 {
268+ query .Set ("group" , identityOptions .Group )
269+ }
270+ if len (identityOptions .Name ) > 0 {
271+ query .Set ("name" , identityOptions .Name )
272+ }
273+ if len (identityOptions .Version ) > 0 {
274+ query .Set ("version" , identityOptions .Version )
275+ }
276+ if len (identityOptions .PURL ) > 0 {
277+ query .Set ("purl" , identityOptions .PURL )
278+ }
279+ if len (identityOptions .CPE ) > 0 {
280+ query .Set ("cpe" , identityOptions .CPE )
281+ }
282+ if len (identityOptions .SWIDTagID ) > 0 {
283+ query .Set ("swidTagId" , identityOptions .SWIDTagID )
284+ }
285+ if identityOptions .Project != uuid .Nil {
286+ query .Set ("project" , identityOptions .Project .String ())
287+ }
288+ req .URL .RawQuery = query .Encode ()
289+ return nil
290+ }
291+ }
292+
293+ func (cs ComponentService ) IdentifyInternal (ctx context.Context ) (err error ) {
294+ err = cs .client .assertServerVersionAtLeast ("4.0.0" )
295+ if err != nil {
296+ return
297+ }
298+
299+ req , err := cs .client .newRequest (ctx , http .MethodGet , "/api/v1/component/internal/identify" )
300+ if err != nil {
301+ return
302+ }
303+
304+ _ , err = cs .client .doRequest (req , nil )
305+ return
306+ }
0 commit comments