forked from labd/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.go
140 lines (113 loc) · 2.93 KB
/
role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// RolesService service
type RolesService service
// Role model
type Role struct {
Sys *Sys `json:"sys"`
Name string `json:"name"`
Description string `json:"description"`
Policies []Policies `json:"policies"`
Permissions Permissions `json:"permissions"`
}
// Policies model
type Policies struct {
Effect string `json:"effect"`
Actions []string `json:"actions"`
Constraint Constraint `json:"constraint"`
}
// Permissions model
type Permissions struct {
ContentModel []string `json:"ContentModel"`
Settings string `json:"Settings"`
ContentDelivery string `json:"ContentDelivery"`
Environments string `json:"Environments"`
EnvironmentAliases string `json:"EnvironmentAliases"`
}
// Constraint model
type Constraint struct {
And []ConstraintDetail `json:"and"`
}
// ConstraintDetail model
type ConstraintDetail struct {
Equals DetailItem `json:"equals"`
}
// DetailItem model
type DetailItem struct {
Doc map[string]interface{}
ItemType string
}
// GetVersion returns entity version
func (r *Role) GetVersion() int {
version := 1
if r.Sys != nil {
version = r.Sys.Version
}
return version
}
// List returns an environments collection
func (service *RolesService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/roles", spaceID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single role
func (service *RolesService) Get(spaceID, roleID string) (*Role, error) {
path := fmt.Sprintf("/spaces/%s/roles/%s", spaceID, roleID)
query := url.Values{}
method := "GET"
req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &Role{}, err
}
var role Role
if ok := service.c.do(req, &role); ok != nil {
return nil, err
}
return &role, err
}
// Upsert updates or creates a new role
func (service *RolesService) Upsert(spaceID string, r *Role) error {
bytesArray, err := json.Marshal(r)
if err != nil {
return err
}
var path string
var method string
if r.Sys != nil && r.Sys.ID != "" {
path = fmt.Sprintf("/spaces/%s/roles/%s", spaceID, r.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/roles", spaceID)
method = "POST"
}
req, err := service.c.newRequest(method, path, nil, bytes.NewReader(bytesArray))
if err != nil {
return err
}
req.Header.Set("X-Contentful-Version", strconv.Itoa(r.GetVersion()))
return service.c.do(req, r)
}
// Delete the role
func (service *RolesService) Delete(spaceID string, roleID string) error {
path := fmt.Sprintf("/spaces/%s/roles/%s", spaceID, roleID)
method := "DELETE"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
return service.c.do(req, nil)
}