-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AUTH-5428 added tags to access namespace
- Loading branch information
1 parent
f968533
commit 9f86c39
Showing
5 changed files
with
243 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:enhancement | ||
access_application: Add support for tags | ||
``` | ||
|
||
```release-note:enhancement | ||
access_tag: Add support for tags | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/goccy/go-json" | ||
) | ||
|
||
type AccessTag struct { | ||
Name string `json:"name,omitempty"` | ||
AppCount int `json:"app_count,omitempty"` | ||
} | ||
|
||
type AccessTagListResponse struct { | ||
Response | ||
Result []AccessTag `json:"result"` | ||
ResultInfo `json:"result_info"` | ||
} | ||
|
||
type AccessTagResponse struct { | ||
Response | ||
Result AccessTag `json:"result"` | ||
} | ||
|
||
type ListAccessTagsParams struct{} | ||
|
||
type CreateAccessTagParams struct { | ||
Name string `json:"name,omitempty"` | ||
} | ||
|
||
func (api *API) ListAccessTags(ctx context.Context, rc *ResourceContainer, params ListAccessTagsParams) ([]AccessTag, error) { | ||
uri := buildURI(fmt.Sprintf("/%s/%s/access/tags", rc.Level, rc.Identifier), params) | ||
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return []AccessTag{}, err | ||
} | ||
|
||
var TagsResponse AccessTagListResponse | ||
err = json.Unmarshal(res, &TagsResponse) | ||
if err != nil { | ||
return []AccessTag{}, err | ||
} | ||
return TagsResponse.Result, nil | ||
} | ||
|
||
func (api *API) GetAccessTag(ctx context.Context, rc *ResourceContainer, tagName string) (AccessTag, error) { | ||
uri := fmt.Sprintf("/%s/%s/access/tags/%s", rc.Level, rc.Identifier, tagName) | ||
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return AccessTag{}, err | ||
} | ||
|
||
var TagResponse AccessTagResponse | ||
err = json.Unmarshal(res, &TagResponse) | ||
if err != nil { | ||
return AccessTag{}, err | ||
} | ||
return TagResponse.Result, nil | ||
} | ||
|
||
func (api *API) CreateAccessTag(ctx context.Context, rc *ResourceContainer, params CreateAccessTagParams) (AccessTag, error) { | ||
uri := fmt.Sprintf("/%s/%s/access/tags", rc.Level, rc.Identifier) | ||
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) | ||
if err != nil { | ||
return AccessTag{}, err | ||
} | ||
|
||
var TagResponse AccessTagResponse | ||
err = json.Unmarshal(res, &TagResponse) | ||
if err != nil { | ||
return AccessTag{}, err | ||
} | ||
return TagResponse.Result, nil | ||
} | ||
|
||
func (api *API) DeleteAccessTag(ctx context.Context, rc *ResourceContainer, tagName string) error { | ||
uri := fmt.Sprintf("/%s/%s/access/tags/%s", rc.Level, rc.Identifier, tagName) | ||
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAccessTags(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodGet, "HTTP method") | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": [ | ||
{ | ||
"name": "engineers", | ||
"app_count": 0 | ||
} | ||
], | ||
"result_info": { | ||
"page": 1, | ||
"per_page": 20, | ||
"count": 1, | ||
"total_count": 1 | ||
} | ||
}`) | ||
} | ||
|
||
want := []AccessTag{ | ||
{ | ||
Name: "engineers", | ||
AppCount: 0, | ||
}, | ||
} | ||
mux.HandleFunc("/accounts/"+testAccountID+"/access/tags", handler) | ||
actual, err := client.ListAccessTags(context.Background(), AccountIdentifier(testAccountID), ListAccessTagsParams{}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestAccessTag(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodGet, "HTTP method") | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"name": "engineers", | ||
"app_count": 0 | ||
} | ||
}`) | ||
} | ||
|
||
want := AccessTag{ | ||
Name: "engineers", | ||
AppCount: 0, | ||
} | ||
mux.HandleFunc("/accounts/"+testAccountID+"/access/tags/engineers", handler) | ||
actual, err := client.GetAccessTag(context.Background(), AccountIdentifier(testAccountID), "engineers") | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestCreateAccessTag(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodPost, "HTTP method") | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"name": "sales", | ||
"app_count": 0 | ||
} | ||
}`) | ||
} | ||
|
||
Tag := AccessTag{ | ||
Name: "sales", | ||
AppCount: 0, | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/access/tags", handler) | ||
actual, err := client.CreateAccessTag(context.Background(), AccountIdentifier(testAccountID), CreateAccessTagParams{ | ||
Name: "sales", | ||
}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, Tag, actual) | ||
} | ||
} | ||
|
||
func TestDeleteAccessTag(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodDelete, "HTTP method") | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
result: { | ||
"id": "engineers" | ||
} | ||
}`) | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/access/tags/engineers", handler) | ||
err := client.DeleteAccessTag(context.Background(), AccountIdentifier(testAccountID), "engineers") | ||
|
||
assert.NoError(t, err) | ||
} |