Skip to content

Commit

Permalink
ACLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Nov 3, 2022
1 parent dfd0544 commit 0b4ba41
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 121 deletions.
34 changes: 11 additions & 23 deletions fastly/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (c *Client) ListACLs(i *ListACLsInput) ([]*ACL, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}

if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
Expand All @@ -72,7 +71,7 @@ func (c *Client) ListACLs(i *ListACLsInput) ([]*ACL, error) {
// CreateACLInput is used as input to the CreateACL function.
type CreateACLInput struct {
// Name is the name of the ACL to create (required)
Name string `url:"name"`
Name *string `url:"name,omitempty"`
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
Expand All @@ -84,7 +83,6 @@ func (c *Client) CreateACL(i *CreateACLInput) (*ACL, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}

if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
Expand Down Expand Up @@ -115,18 +113,16 @@ type DeleteACLInput struct {

// DeleteACL deletes the specified resource.
func (c *Client) DeleteACL(i *DeleteACLInput) error {
if i.Name == "" {
return ErrMissingName
}
if i.ServiceID == "" {
return ErrMissingServiceID
}

if i.ServiceVersion == 0 {
return ErrMissingServiceVersion
}

if i.Name == "" {
return ErrMissingName
}

path := fmt.Sprintf("/service/%s/version/%d/acl/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
resp, err := c.Delete(path, nil)
if err != nil {
Expand Down Expand Up @@ -156,18 +152,16 @@ type GetACLInput struct {

// GetACL retrieves the specified resource.
func (c *Client) GetACL(i *GetACLInput) (*ACL, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}

if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}

if i.Name == "" {
return nil, ErrMissingName
}

path := fmt.Sprintf("/service/%s/version/%d/acl/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
resp, err := c.Get(path, nil)
if err != nil {
Expand All @@ -187,7 +181,7 @@ type UpdateACLInput struct {
// Name is the name of the ACL to update (required).
Name string
// NewName is the new name of the ACL to update (required).
NewName string `url:"name"`
NewName *string `url:"name,omitempty"`
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
Expand All @@ -196,22 +190,16 @@ type UpdateACLInput struct {

// UpdateACL updates the specified resource.
func (c *Client) UpdateACL(i *UpdateACLInput) (*ACL, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}

if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}

if i.Name == "" {
return nil, ErrMissingName
}

if i.NewName == "" {
return nil, ErrMissingNewName
}

path := fmt.Sprintf("/service/%s/version/%d/acl/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
resp, err := c.PutForm(path, i, nil)
if err != nil {
Expand Down
75 changes: 40 additions & 35 deletions fastly/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ func TestClient_ACLs(t *testing.T) {
a, err = c.CreateACL(&CreateACLInput{
ServiceID: testServiceID,
ServiceVersion: testVersion.Number,
Name: "test_acl",
Name: String("test_acl"),
})
})
if err != nil {
t.Fatal(err)
}

// Create with expected error
var errExpected error
record(t, fixtureBase+"create_expected_error", func(c *Client) {
_, errExpected = c.CreateACL(&CreateACLInput{
ServiceID: testServiceID,
ServiceVersion: testVersion.Number,
})
})
if errExpected == nil {
t.Error("expected API error, got nil")
}

// Ensure deleted
defer func() {
record(t, fixtureBase+"cleanup", func(c *Client) {
Expand Down Expand Up @@ -84,7 +96,7 @@ func TestClient_ACLs(t *testing.T) {
ServiceID: testServiceID,
ServiceVersion: testVersion.Number,
Name: "test_acl",
NewName: "new_test_acl",
NewName: String("new_test_acl"),
})
})
if err != nil {
Expand Down Expand Up @@ -150,89 +162,82 @@ func TestClient_CreateACL_validation(t *testing.T) {
func TestClient_GetACL_validation(t *testing.T) {
var err error
_, err = testClient.GetACL(&GetACLInput{
ServiceID: "",
ServiceID: "foo",
ServiceVersion: 1,
})
if err != ErrMissingServiceID {
if err != ErrMissingName {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetACL(&GetACLInput{
ServiceID: "foo",
ServiceVersion: 0,
Name: "test",
ServiceVersion: 1,
})
if err != ErrMissingServiceVersion {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetACL(&GetACLInput{
ServiceID: "foo",
ServiceVersion: 1,
Name: "",
Name: "test",
ServiceID: "foo",
})
if err != ErrMissingName {
if err != ErrMissingServiceVersion {
t.Errorf("bad error: %s", err)
}
}

func TestClient_UpdateACL_validation(t *testing.T) {
var err error
_, err = testClient.UpdateACL(&UpdateACLInput{
ServiceID: "",
})
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.UpdateACL(&UpdateACLInput{
ServiceID: "foo",
ServiceVersion: 0,
ServiceVersion: 1,
})
if err != ErrMissingServiceVersion {
if err != ErrMissingName {
t.Errorf("bad error: %s", err)
}

_, err = testClient.UpdateACL(&UpdateACLInput{
ServiceID: "foo",
Name: "test",
ServiceVersion: 1,
Name: "",
})
if err != ErrMissingName {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.UpdateACL(&UpdateACLInput{
ServiceID: "foo",
ServiceVersion: 1,
Name: "acl",
NewName: "",
Name: "test",
ServiceID: "foo",
})
if err != ErrMissingNewName {
if err != ErrMissingServiceVersion {
t.Errorf("bad error: %s", err)
}
}

func TestClient_DeleteACL_validation(t *testing.T) {
var err error

err = testClient.DeleteACL(&DeleteACLInput{
ServiceID: "",
ServiceID: "foo",
ServiceVersion: 1,
})
if err != ErrMissingServiceID {
if err != ErrMissingName {
t.Errorf("bad error: %s", err)
}

err = testClient.DeleteACL(&DeleteACLInput{
ServiceID: "foo",
Name: "test",
ServiceVersion: 0,
})
if err != ErrMissingServiceVersion {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

err = testClient.DeleteACL(&DeleteACLInput{
ServiceID: "foo",
ServiceVersion: 1,
Name: "",
Name: "test",
ServiceID: "foo",
})
if err != ErrMissingName {
if err != ErrMissingServiceVersion {
t.Errorf("bad error: %s", err)
}
}
2 changes: 1 addition & 1 deletion fastly/fastly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func createTestACL(t *testing.T, createFixture string, serviceID string, version
acl, err = client.CreateACL(&CreateACLInput{
ServiceID: serviceID,
ServiceVersion: version,
Name: fmt.Sprintf("test_acl_%s", aclNameSuffix),
Name: String(fmt.Sprintf("test_acl_%s", aclNameSuffix)),
})
})
if err != nil {
Expand Down
32 changes: 16 additions & 16 deletions fastly/fixtures/acls/cleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ interactions:
form: {}
headers:
User-Agent:
- FastlyGo/5.1.1 (+github.com/fastly/go-fastly; go1.17)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/version/41/acl/test_acl
- FastlyGo/6.8.0 (+github.com/fastly/go-fastly; go1.16.15)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/version/24/acl/test_acl
method: DELETE
response:
body: '{"msg":"Record not found","detail":"Couldn''t find ACL ''{ deleted =\u003e
0000-00-00 00:00:00, name =\u003e test_acl, service =\u003e 7i6HN3TK9wS159v2gPAZ8A,
version =\u003e 41 }''"}'
version =\u003e 24 }''"}'
headers:
Accept-Ranges:
- bytes
Expand All @@ -21,11 +21,11 @@ interactions:
Content-Type:
- application/json
Date:
- Wed, 03 Nov 2021 14:27:07 GMT
- Thu, 03 Nov 2022 17:11:12 GMT
Fastly-Ratelimit-Remaining:
- "4995"
- "9988"
Fastly-Ratelimit-Reset:
- "1635951600"
- "1667498400"
Status:
- 404 Not Found
Strict-Transport-Security:
Expand All @@ -39,9 +39,9 @@ interactions:
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-slwdc9037-CONTROL-SLWDC, cache-man4137-MAN
- cache-control-cp-aws-us-east-1-prod-5-CONTROL-AWS, cache-man4143-MAN
X-Timer:
- S1635949627.124914,VS0,VE143
- S1667495472.160631,VS0,VE304
status: 404 Not Found
code: 404
duration: ""
Expand All @@ -50,13 +50,13 @@ interactions:
form: {}
headers:
User-Agent:
- FastlyGo/5.1.1 (+github.com/fastly/go-fastly; go1.17)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/version/41/acl/new_test_acl
- FastlyGo/6.8.0 (+github.com/fastly/go-fastly; go1.16.15)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/version/24/acl/new_test_acl
method: DELETE
response:
body: '{"msg":"Record not found","detail":"Couldn''t find ACL ''{ deleted =\u003e
0000-00-00 00:00:00, name =\u003e new_test_acl, service =\u003e 7i6HN3TK9wS159v2gPAZ8A,
version =\u003e 41 }''"}'
version =\u003e 24 }''"}'
headers:
Accept-Ranges:
- bytes
Expand All @@ -65,11 +65,11 @@ interactions:
Content-Type:
- application/json
Date:
- Wed, 03 Nov 2021 14:27:07 GMT
- Thu, 03 Nov 2022 17:11:12 GMT
Fastly-Ratelimit-Remaining:
- "4994"
- "9987"
Fastly-Ratelimit-Reset:
- "1635951600"
- "1667498400"
Status:
- 404 Not Found
Strict-Transport-Security:
Expand All @@ -83,9 +83,9 @@ interactions:
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-slwdc9037-CONTROL-SLWDC, cache-man4137-MAN
- cache-control-cp-aws-us-east-1-prod-5-CONTROL-AWS, cache-man4143-MAN
X-Timer:
- S1635949627.303422,VS0,VE124
- S1667495472.491100,VS0,VE144
status: 404 Not Found
code: 404
duration: ""
Loading

0 comments on commit 0b4ba41

Please sign in to comment.