Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit ed4a6cb

Browse files
authored
Merge pull request #153 from philips-software/feature/blr-configuration
BLR: Implement BlobStorePolicy APIs
2 parents 6531d12 + 0e53ff7 commit ed4a6cb

File tree

5 files changed

+223
-2
lines changed

5 files changed

+223
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The current implement covers only a subset of HSDP APIs. Basically, we implement
1414
- [x] Access Policy
1515
- [x] Access URL
1616
- [x] Multipart Upload
17+
- [x] BlobStore Policy management
1718
- [ ] Topic management
1819
- [ ] Store Access
1920
- [ ] Bucket management

blr/client.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ type Client struct {
4747

4848
validate *validator.Validate
4949

50-
Blobs *BlobsService
50+
Blobs *BlobsService
51+
Configurations *ConfigurationsService
5152
}
5253

5354
// NewClient returns a new BLR client
@@ -67,6 +68,7 @@ func NewClient(iamClient *iam.Client, config *Config) (*Client, error) {
6768
}
6869

6970
c.Blobs = &BlobsService{Client: c, validate: validator.New()}
71+
c.Configurations = &ConfigurationsService{Client: c, validate: validator.New()}
7072

7173
return c, nil
7274
}

blr/configurations_service.go

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package blr
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/go-playground/validator/v10"
7+
"github.com/philips-software/go-hsdp-api/internal"
8+
"net/http"
9+
)
10+
11+
var (
12+
blobConfigurationAPIVersion = "1"
13+
)
14+
15+
type BlobStorePolicy struct {
16+
ResourceType string `json:"resourceType"`
17+
ID string `json:"id,omitempty"`
18+
Statement []BlobStorePolicyStatement `json:"statement"`
19+
}
20+
21+
type BlobStorePolicyStatement struct {
22+
Effect string `json:"effect"`
23+
Action []string `json:"action"`
24+
Principal []string `json:"principal"`
25+
Resource []string `json:"resource"`
26+
}
27+
28+
type GetBlobStorePolicyOptions struct {
29+
ID *string `url:"_id,omitempty"`
30+
}
31+
32+
type ConfigurationsService struct {
33+
*Client
34+
validate *validator.Validate
35+
}
36+
37+
func (b *ConfigurationsService) CreateBlobStorePolicy(policy BlobStorePolicy) (*BlobStorePolicy, *Response, error) {
38+
policy.ResourceType = "BlobStorePolicy"
39+
if err := b.validate.Struct(policy); err != nil {
40+
return nil, nil, err
41+
}
42+
43+
req, _ := b.NewRequest(http.MethodPost, "/configuration/BlobStorePolicy", policy, nil)
44+
req.Header.Set("api-version", blobConfigurationAPIVersion)
45+
req.Header.Set("Content-Type", "application/json")
46+
47+
var created BlobStorePolicy
48+
49+
resp, err := b.Do(req, &created)
50+
51+
if err != nil {
52+
return nil, resp, err
53+
}
54+
if created.ID == "" {
55+
return nil, resp, fmt.Errorf("the 'ID' field is missing")
56+
}
57+
return &created, resp, nil
58+
}
59+
60+
func (b *ConfigurationsService) GetBlobStorePolicyByID(id string) (*Blob, *Response, error) {
61+
req, err := b.NewRequest(http.MethodGet, "/configuration/BlobStorePolicy/"+id, nil)
62+
if err != nil {
63+
return nil, nil, err
64+
}
65+
req.Header.Set("api-version", blobAPIVersion)
66+
req.Header.Set("Content-Type", "application/json")
67+
68+
var resource Blob
69+
70+
resp, err := b.Do(req, &resource)
71+
if err != nil {
72+
return nil, resp, err
73+
}
74+
err = internal.CheckResponse(resp.Response)
75+
if err != nil {
76+
return nil, resp, fmt.Errorf("GetByID: %w", err)
77+
}
78+
if resource.ID != id {
79+
return nil, nil, fmt.Errorf("returned resource does not match")
80+
}
81+
return &resource, resp, nil
82+
}
83+
84+
func (b *ConfigurationsService) FindBlobStorePolicy(opt *GetBlobStorePolicyOptions, options ...OptionFunc) (*[]Blob, *Response, error) {
85+
req, err := b.NewRequest(http.MethodGet, "/configuration/BlobStorePolicy", opt, options...)
86+
if err != nil {
87+
return nil, nil, err
88+
}
89+
req.Header.Set("api-version", blobConfigurationAPIVersion)
90+
req.Header.Set("Content-Type", "application/json")
91+
92+
var bundleResponse internal.Bundle
93+
94+
resp, err := b.Do(req, &bundleResponse)
95+
if err != nil {
96+
return nil, resp, err
97+
}
98+
var resources []Blob
99+
for _, c := range bundleResponse.Entry {
100+
var resource Blob
101+
if err := json.Unmarshal(c.Resource, &resource); err == nil {
102+
resources = append(resources, resource)
103+
}
104+
}
105+
return &resources, resp, err
106+
}
107+
108+
func (b *ConfigurationsService) DeleteBlobStorePolicy(policy BlobStorePolicy) (bool, *Response, error) {
109+
req, err := b.NewRequest(http.MethodDelete, "/configuration/BlobStorePolicy/"+policy.ID, nil, nil)
110+
if err != nil {
111+
return false, nil, err
112+
}
113+
req.Header.Set("api-version", blobConfigurationAPIVersion)
114+
115+
var deleteResponse interface{}
116+
117+
resp, err := b.Do(req, &deleteResponse)
118+
if resp == nil || resp.StatusCode() != http.StatusNoContent {
119+
return false, resp, err
120+
}
121+
return true, resp, nil
122+
}

blr/configurations_service_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package blr_test
2+
3+
import (
4+
"fmt"
5+
"github.com/philips-software/go-hsdp-api/blr"
6+
"github.com/stretchr/testify/assert"
7+
"io"
8+
"net/http"
9+
"testing"
10+
)
11+
12+
func blobStorePolicyBody(id, effect, action, principal, resource string) string {
13+
return fmt.Sprintf(`{
14+
"resourceType": "BlobStorePolicy",
15+
"meta": {
16+
"lastUpdated": "2022-05-25T19:36:10Z",
17+
"versionId": "1"
18+
},
19+
"id": "%s",
20+
"statement": [
21+
{
22+
"effect": "%s",
23+
"action": [
24+
"%s"
25+
],
26+
"principal": [
27+
"%s"
28+
],
29+
"resource": [
30+
"%s"
31+
]
32+
}
33+
]
34+
}`, id, effect, action, principal, resource)
35+
}
36+
37+
func TestBlobStorePolicyCRUD(t *testing.T) {
38+
teardown := setup(t)
39+
defer teardown()
40+
41+
blobStorePolicyID := "dbf1d779-ab9f-4c27-b4aa-ea75f9efbbc1"
42+
muxBLR.HandleFunc("/connect/blobrepository/configuration/BlobStorePolicy", func(w http.ResponseWriter, r *http.Request) {
43+
w.Header().Set("Content-Type", "application/json")
44+
switch r.Method {
45+
case "POST":
46+
w.Header().Set("Etag", "1")
47+
w.WriteHeader(http.StatusCreated)
48+
_, _ = io.WriteString(w, blobStorePolicyBody(blobStorePolicyID, "effect", "action", "principal", "resource"))
49+
}
50+
})
51+
muxBLR.HandleFunc("/connect/blobrepository/configuration/BlobStorePolicy/"+blobStorePolicyID, func(w http.ResponseWriter, r *http.Request) {
52+
w.Header().Set("Content-Type", "application/json")
53+
switch r.Method {
54+
case "GET":
55+
w.WriteHeader(http.StatusOK)
56+
_, _ = io.WriteString(w, blobStorePolicyBody(blobStorePolicyID, "effect", "action", "principal", "resource"))
57+
case "PUT":
58+
w.WriteHeader(http.StatusOK)
59+
_, _ = io.WriteString(w, blobStorePolicyBody(blobStorePolicyID, "effect", "action", "principal", "resource"))
60+
case "DELETE":
61+
w.WriteHeader(http.StatusNoContent)
62+
}
63+
})
64+
65+
created, resp, err := blrClient.Configurations.CreateBlobStorePolicy(blr.BlobStorePolicy{
66+
Statement: []blr.BlobStorePolicyStatement{
67+
{
68+
Effect: "effect",
69+
Action: []string{"action"},
70+
Principal: []string{"principal"},
71+
Resource: []string{"resource"},
72+
},
73+
},
74+
})
75+
if !assert.Nil(t, err) {
76+
return
77+
}
78+
if !assert.NotNil(t, resp) {
79+
return
80+
}
81+
if !assert.NotNil(t, created) {
82+
return
83+
}
84+
assert.Equal(t, []string{"action"}, created.Statement[0].Action)
85+
assert.Equal(t, blobStorePolicyID, created.ID)
86+
87+
res, resp, err := blrClient.Configurations.DeleteBlobStorePolicy(*created)
88+
if !assert.Nil(t, err) {
89+
return
90+
}
91+
if !assert.NotNil(t, resp) {
92+
return
93+
}
94+
assert.True(t, res)
95+
assert.Equal(t, http.StatusNoContent, resp.StatusCode())
96+
}

internal/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package internal
22

33
const (
4-
LibraryVersion = "0.81.3"
4+
LibraryVersion = "0.82.0"
55
)

0 commit comments

Comments
 (0)