From 33679d8017ff9f89564e204e81bde1c02dbc9b4d Mon Sep 17 00:00:00 2001 From: Gianmaria Del Monte Date: Tue, 9 May 2023 15:35:19 +0200 Subject: [PATCH] add discovery endpoint in the ocm client --- pkg/ocm/client/client.go | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pkg/ocm/client/client.go b/pkg/ocm/client/client.go index a0a91914128..3657b976129 100644 --- a/pkg/ocm/client/client.go +++ b/pkg/ocm/client/client.go @@ -228,3 +228,50 @@ func (c *OCMClient) parseNewShareResponse(r *http.Response) (*NewShareResponse, } return nil, errtypes.InternalError(string(body)) } + +// Capabilities contains a set of properties exposed by +// a remote cloud storage. +type Capabilities struct { + Enabled bool `json:"enabled"` + APIVersion string `json:"apiVersion"` + EndPoint string `json:"endPoint"` + Provider string `json:"provider"` + ResourceTypes []struct { + Name string `json:"name"` + ShareTypes []string `json:"shareTypes"` + Protocols struct { + Webdav *string `json:"webdav"` + Webapp *string `json:"webapp"` + Datatx *string `json:"datatx"` + } `json:"protocols"` + } `json:"resourceTypes"` + Capabilities []string `json:"capabilities"` +} + +// Discovery returns a number of properties used to discover the capabilities offered by a remote cloud storage. +// https://cs3org.github.io/OCM-API/docs.html?branch=develop&repo=OCM-API&user=cs3org#/paths/~1ocm-provider/get +func (c *OCMClient) Discovery(ctx context.Context, endpoint string) (*Capabilities, error) { + url, err := url.JoinPath(endpoint, "shares") + if err != nil { + return nil, err + } + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, errors.Wrap(err, "error creating request") + } + req.Header.Set("Content-Type", "application/json") + + resp, err := c.client.Do(req) + if err != nil { + return nil, errors.Wrap(err, "error doing request") + } + defer resp.Body.Close() + + var cap Capabilities + if err := json.NewDecoder(resp.Body).Decode(&c); err != nil { + return nil, err + } + + return &cap, nil +}