Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 0e0d9a2

Browse files
Cloud Access Policy support (#129)
* Add support for cloud access policies * Add Token field * Update is a post * Update is a POST for policies as well * Remove unnecessary newline
1 parent 9a63390 commit 0e0d9a2

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

cloud_access_policy.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/url"
8+
"time"
9+
)
10+
11+
type CloudAccessPolicyLabelPolicy struct {
12+
Selector string `json:"selector"`
13+
}
14+
15+
type CloudAccessPolicyRealm struct {
16+
Type string `json:"type"`
17+
Identifier string `json:"identifier"`
18+
LabelPolicies []CloudAccessPolicyLabelPolicy `json:"labelPolicies"`
19+
}
20+
21+
type CreateCloudAccessPolicyInput struct {
22+
Name string `json:"name"`
23+
DisplayName string `json:"displayName"`
24+
Scopes []string `json:"scopes"`
25+
Realms []CloudAccessPolicyRealm `json:"realms"`
26+
}
27+
28+
type UpdateCloudAccessPolicyInput struct {
29+
DisplayName string `json:"displayName"`
30+
Scopes []string `json:"scopes"`
31+
Realms []CloudAccessPolicyRealm `json:"realms"`
32+
}
33+
34+
type CloudAccessPolicy struct {
35+
Name string `json:"name"`
36+
DisplayName string `json:"displayName"`
37+
Scopes []string `json:"scopes"`
38+
Realms []CloudAccessPolicyRealm `json:"realms"`
39+
40+
// The following fields are not part of the input, but are returned by the API.
41+
ID string `json:"id"`
42+
OrgID string `json:"orgId"`
43+
CreatedAt time.Time `json:"createdAt"`
44+
UpdatedAt time.Time `json:"updatedAt"`
45+
}
46+
47+
type CloudAccessPolicyItems struct {
48+
Items []*CloudAccessPolicy `json:"items"`
49+
}
50+
51+
func (c *Client) CloudAccessPolicies(region string) (CloudAccessPolicyItems, error) {
52+
policies := CloudAccessPolicyItems{}
53+
err := c.request("GET", "/api/v1/accesspolicies", url.Values{
54+
"region": []string{region},
55+
}, nil, &policies)
56+
57+
return policies, err
58+
}
59+
60+
func (c *Client) CloudAccessPolicyByID(region, id string) (CloudAccessPolicy, error) {
61+
policy := CloudAccessPolicy{}
62+
err := c.request("GET", fmt.Sprintf("/api/v1/accesspolicies/%s", id), url.Values{
63+
"region": []string{region},
64+
}, nil, &policy)
65+
66+
return policy, err
67+
}
68+
69+
func (c *Client) CreateCloudAccessPolicy(region string, input CreateCloudAccessPolicyInput) (CloudAccessPolicy, error) {
70+
result := CloudAccessPolicy{}
71+
72+
data, err := json.Marshal(input)
73+
if err != nil {
74+
return result, err
75+
}
76+
77+
err = c.request("POST", "/api/v1/accesspolicies", url.Values{
78+
"region": []string{region},
79+
}, bytes.NewBuffer(data), &result)
80+
81+
return result, err
82+
}
83+
84+
func (c *Client) UpdateCloudAccessPolicy(region, id string, input UpdateCloudAccessPolicyInput) (CloudAccessPolicy, error) {
85+
result := CloudAccessPolicy{}
86+
87+
data, err := json.Marshal(input)
88+
if err != nil {
89+
return result, err
90+
}
91+
92+
err = c.request("POST", fmt.Sprintf("/api/v1/accesspolicies/%s", id), url.Values{
93+
"region": []string{region},
94+
}, bytes.NewBuffer(data), &result)
95+
96+
return result, err
97+
}
98+
99+
func (c *Client) DeleteCloudAccessPolicy(region, id string) error {
100+
return c.request("DELETE", fmt.Sprintf("/api/v1/accesspolicies/%s", id), url.Values{
101+
"region": []string{region},
102+
}, nil, nil)
103+
}

cloud_access_policy_token.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/url"
8+
"time"
9+
)
10+
11+
type CreateCloudAccessPolicyTokenInput struct {
12+
AccessPolicyID string `json:"accessPolicyId"`
13+
Name string `json:"name"`
14+
DisplayName string `json:"displayName"`
15+
ExpiresAt time.Time `json:"expiresAt"`
16+
}
17+
18+
type UpdateCloudAccessPolicyTokenInput struct {
19+
DisplayName string `json:"displayName"`
20+
}
21+
22+
type CloudAccessPolicyToken struct {
23+
ID string `json:"id"`
24+
AccessPolicyID string `json:"accessPolicyId"`
25+
Name string `json:"name"`
26+
DisplayName string `json:"displayName"`
27+
ExpiresAt time.Time `json:"expiresAt"`
28+
FirstUsedAt time.Time `json:"firstUsedAt"`
29+
CreatedAt time.Time `json:"createdAt"`
30+
UpdatedAt time.Time `json:"updatedAt"`
31+
32+
Token string `json:"token,omitempty"` // Only returned when creating a token.
33+
}
34+
35+
type CloudAccessPolicyTokenItems struct {
36+
Items []*CloudAccessPolicyToken `json:"items"`
37+
}
38+
39+
func (c *Client) CloudAccessPolicyTokens(region, accessPolicyID string) (CloudAccessPolicyTokenItems, error) {
40+
tokens := CloudAccessPolicyTokenItems{}
41+
err := c.request("GET", "/api/v1/tokens", url.Values{
42+
"region": []string{region},
43+
"accessPolicyId": []string{accessPolicyID},
44+
}, nil, &tokens)
45+
46+
return tokens, err
47+
}
48+
49+
func (c *Client) CloudAccessPolicyTokenByID(region, id string) (CloudAccessPolicyToken, error) {
50+
token := CloudAccessPolicyToken{}
51+
err := c.request("GET", fmt.Sprintf("/api/v1/tokens/%s", id), url.Values{
52+
"region": []string{region},
53+
}, nil, &token)
54+
55+
return token, err
56+
}
57+
58+
func (c *Client) CreateCloudAccessPolicyToken(region string, input CreateCloudAccessPolicyTokenInput) (CloudAccessPolicyToken, error) {
59+
token := CloudAccessPolicyToken{}
60+
61+
data, err := json.Marshal(input)
62+
if err != nil {
63+
return token, err
64+
}
65+
66+
err = c.request("POST", "/api/v1/tokens", url.Values{
67+
"region": []string{region},
68+
}, bytes.NewBuffer(data), &token)
69+
70+
return token, err
71+
}
72+
73+
func (c *Client) UpdateCloudAccessPolicyToken(region, id string, input UpdateCloudAccessPolicyTokenInput) (CloudAccessPolicyToken, error) {
74+
token := CloudAccessPolicyToken{}
75+
76+
data, err := json.Marshal(input)
77+
if err != nil {
78+
return token, err
79+
}
80+
81+
err = c.request("POST", fmt.Sprintf("/api/v1/tokens/%s", id), url.Values{
82+
"region": []string{region},
83+
}, bytes.NewBuffer(data), &token)
84+
85+
return token, err
86+
}
87+
88+
func (c *Client) DeleteCloudAccessPolicyToken(region, id string) error {
89+
return c.request("DELETE", fmt.Sprintf("/api/v1/tokens/%s", id), url.Values{
90+
"region": []string{region},
91+
}, nil, nil)
92+
}

0 commit comments

Comments
 (0)