From 17f1a8dbbd2e4dbf567f670965f76b3f23ef2d44 Mon Sep 17 00:00:00 2001 From: Kazumichi Yamamoto Date: Thu, 24 Mar 2022 08:55:10 +0900 Subject: [PATCH] service: accountkey (#5) --- Makefile | 2 +- account/service_test.go | 1 + accountkey/create_request.go | 27 +++++++ accountkey/create_service.go | 34 +++++++++ accountkey/delete_request.go | 28 +++++++ accountkey/delete_service.go | 41 +++++++++++ accountkey/find_request.go | 25 +++++++ accountkey/find_service.go | 38 ++++++++++ accountkey/read_request.go | 28 +++++++ accountkey/read_service.go | 43 +++++++++++ accountkey/service.go | 27 +++++++ accountkey/service_test.go | 137 +++++++++++++++++++++++++++++++++++ bucket/read_service.go | 4 - go.mod | 2 +- go.sum | 4 +- 15 files changed, 433 insertions(+), 8 deletions(-) create mode 100644 accountkey/create_request.go create mode 100644 accountkey/create_service.go create mode 100644 accountkey/delete_request.go create mode 100644 accountkey/delete_service.go create mode 100644 accountkey/find_request.go create mode 100644 accountkey/find_service.go create mode 100644 accountkey/read_request.go create mode 100644 accountkey/read_service.go create mode 100644 accountkey/service.go create mode 100644 accountkey/service_test.go diff --git a/Makefile b/Makefile index 9f36d8b..6169192 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ test: .PHONY: testacc testacc: - TESTACC= go test ./... $(TESTARGS) -v -timeout=120m -parallel=8 -race + TESTACC=1 go test ./... $(TESTARGS) --tags=acctest -v -timeout=120m -parallel=8 -race .PHONY: tools tools: diff --git a/account/service_test.go b/account/service_test.go index 1a0c2ac..ae03a6d 100644 --- a/account/service_test.go +++ b/account/service_test.go @@ -73,6 +73,7 @@ func TestService_CRUD_plus_L(t *testing.T) { }) require.NoError(t, err) require.Len(t, found, 1) + require.Equal(t, account, found[0]) }) diff --git a/accountkey/create_request.go b/accountkey/create_request.go new file mode 100644 index 0000000..3d81d58 --- /dev/null +++ b/accountkey/create_request.go @@ -0,0 +1,27 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "github.com/sacloud/packages-go/validate" +) + +type CreateRequest struct { + SiteId string `service:"-" validate:"required"` +} + +func (req *CreateRequest) Validate() error { + return validate.New().Struct(req) +} diff --git a/accountkey/create_service.go b/accountkey/create_service.go new file mode 100644 index 0000000..9b080e7 --- /dev/null +++ b/accountkey/create_service.go @@ -0,0 +1,34 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "context" + + objectstorage "github.com/sacloud/object-storage-api-go" + v1 "github.com/sacloud/object-storage-api-go/apis/v1" +) + +func (s *Service) Create(req *CreateRequest) (*v1.AccountKey, error) { + return s.CreateWithContext(context.Background(), req) +} + +func (s *Service) CreateWithContext(ctx context.Context, req *CreateRequest) (*v1.AccountKey, error) { + if err := req.Validate(); err != nil { + return nil, err + } + client := objectstorage.NewAccountOp(s.client) + return client.CreateAccessKey(ctx, req.SiteId) +} diff --git a/accountkey/delete_request.go b/accountkey/delete_request.go new file mode 100644 index 0000000..2b834a6 --- /dev/null +++ b/accountkey/delete_request.go @@ -0,0 +1,28 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "github.com/sacloud/packages-go/validate" +) + +type DeleteRequest struct { + SiteId string `service:"-" validate:"required"` + Id string `service:"-" validate:"required"` // リソースId +} + +func (req *DeleteRequest) Validate() error { + return validate.New().Struct(req) +} diff --git a/accountkey/delete_service.go b/accountkey/delete_service.go new file mode 100644 index 0000000..9317e7f --- /dev/null +++ b/accountkey/delete_service.go @@ -0,0 +1,41 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "context" + + objectstorage "github.com/sacloud/object-storage-api-go" +) + +func (s *Service) Delete(req *DeleteRequest) error { + return s.DeleteWithContext(context.Background(), req) +} + +func (s *Service) DeleteWithContext(ctx context.Context, req *DeleteRequest) error { + if err := req.Validate(); err != nil { + return err + } + _, err := s.ReadWithContext(ctx, &ReadRequest{ + SiteId: req.SiteId, + Id: req.Id, + }) + if err != nil { + return err + } + + client := objectstorage.NewAccountOp(s.client) + return client.DeleteAccessKey(ctx, req.SiteId, req.Id) +} diff --git a/accountkey/find_request.go b/accountkey/find_request.go new file mode 100644 index 0000000..1a04211 --- /dev/null +++ b/accountkey/find_request.go @@ -0,0 +1,25 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import "github.com/sacloud/packages-go/validate" + +type FindRequest struct { + SiteId string `service:"-" validate:"required"` +} + +func (req *FindRequest) Validate() error { + return validate.New().Struct(req) +} diff --git a/accountkey/find_service.go b/accountkey/find_service.go new file mode 100644 index 0000000..121db26 --- /dev/null +++ b/accountkey/find_service.go @@ -0,0 +1,38 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "context" + + objectstorage "github.com/sacloud/object-storage-api-go" + v1 "github.com/sacloud/object-storage-api-go/apis/v1" +) + +func (s *Service) Find(req *FindRequest) ([]*v1.AccountKey, error) { + return s.FindWithContext(context.Background(), req) +} + +func (s *Service) FindWithContext(ctx context.Context, req *FindRequest) ([]*v1.AccountKey, error) { + if req == nil { + req = &FindRequest{} + } + if err := req.Validate(); err != nil { + return nil, err + } + + client := objectstorage.NewAccountOp(s.client) + return client.ListAccessKeys(ctx, req.SiteId) +} diff --git a/accountkey/read_request.go b/accountkey/read_request.go new file mode 100644 index 0000000..1170584 --- /dev/null +++ b/accountkey/read_request.go @@ -0,0 +1,28 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "github.com/sacloud/packages-go/validate" +) + +type ReadRequest struct { + SiteId string `service:"-" validate:"required"` + Id string `service:"-" validate:"required"` // リソースId +} + +func (req *ReadRequest) Validate() error { + return validate.New().Struct(req) +} diff --git a/accountkey/read_service.go b/accountkey/read_service.go new file mode 100644 index 0000000..5f1e2b5 --- /dev/null +++ b/accountkey/read_service.go @@ -0,0 +1,43 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "context" + "fmt" + + objectstorage "github.com/sacloud/object-storage-api-go" + v1 "github.com/sacloud/object-storage-api-go/apis/v1" + service "github.com/sacloud/object-storage-service-go" +) + +func (s *Service) Read(req *ReadRequest) (*v1.AccountKey, error) { + return s.ReadWithContext(context.Background(), req) +} + +func (s *Service) ReadWithContext(ctx context.Context, req *ReadRequest) (*v1.AccountKey, error) { + if err := req.Validate(); err != nil { + return nil, err + } + client := objectstorage.NewAccountOp(s.client) + accountKey, err := client.ReadAccessKey(ctx, req.SiteId, req.Id) + if err != nil { + if v1.IsError404(err) { + return nil, service.NotFoundError(fmt.Errorf("account-key %q not found", req.Id)) + } + return nil, err + } + return accountKey, nil +} diff --git a/accountkey/service.go b/accountkey/service.go new file mode 100644 index 0000000..25b8bbc --- /dev/null +++ b/accountkey/service.go @@ -0,0 +1,27 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import objectstorage "github.com/sacloud/object-storage-api-go" + +// Service provides a high-level API of for Site +type Service struct { + client *objectstorage.Client +} + +// New returns new service instance of Archive +func New(client *objectstorage.Client) *Service { + return &Service{client: client} +} diff --git a/accountkey/service_test.go b/accountkey/service_test.go new file mode 100644 index 0000000..177e3a3 --- /dev/null +++ b/accountkey/service_test.go @@ -0,0 +1,137 @@ +// Copyright 2022 The sacloud/object-storage-service-go Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package accountkey + +import ( + "net/http/httptest" + "testing" + "time" + + objectstorage "github.com/sacloud/object-storage-api-go" + v1 "github.com/sacloud/object-storage-api-go/apis/v1" + "github.com/sacloud/object-storage-api-go/fake" + "github.com/sacloud/object-storage-api-go/fake/server" + service "github.com/sacloud/object-storage-service-go" + "github.com/stretchr/testify/require" +) + +var ( + siteId = "isk01" + accountId = "100000000001" +) + +func TestService_CRUD_plus_L(t *testing.T) { + fakeServer := initFakeServer() + client := &objectstorage.Client{ + APIRootURL: fakeServer.URL, + } + + svc := New(client) + var accountKey *v1.AccountKey + + t.Run("create", func(t *testing.T) { + created, err := svc.Create(&CreateRequest{ + SiteId: siteId, + }) + require.NoError(t, err) + require.NotNil(t, created) + accountKey = created + }) + + t.Run("read", func(t *testing.T) { + read, err := svc.Read(&ReadRequest{ + SiteId: siteId, + Id: accountKey.Id.String(), + }) + require.NoError(t, err) + require.NotNil(t, read) + }) + + t.Run("read return NotFoundError when account is not found", func(t *testing.T) { + id := "not-exists-account-id" + read, err := svc.Read(&ReadRequest{ + SiteId: siteId, + Id: id, + }) + require.Nil(t, read) + require.EqualError(t, err, `account-key "`+id+`" not found`) + + _, errIsNotFoundError := err.(service.NotFoundError) + require.True(t, errIsNotFoundError) + }) + + t.Run("list", func(t *testing.T) { + found, err := svc.Find(&FindRequest{ + SiteId: siteId, + }) + require.NoError(t, err) + require.Len(t, found, 1) + + // Note: AccountKey.SecretKeyは作成時のみ参照可能なのでそれ以外の項目を比較する + key := *accountKey + key.Secret = "" + require.Equal(t, &key, found[0]) + }) + + t.Run("delete return NotFoundError when account is not found", func(t *testing.T) { + id := "not-exists-account-id" + err := svc.Delete(&DeleteRequest{ + SiteId: siteId, + Id: id, + }) + require.EqualError(t, err, `account-key "`+id+`" not found`) + + _, errIsNotFoundError := err.(service.NotFoundError) + require.True(t, errIsNotFoundError) + }) + + t.Run("delete", func(t *testing.T) { + err := svc.Delete(&DeleteRequest{ + SiteId: siteId, + Id: accountKey.Id.String(), + }) + require.NoError(t, err) + + found, err := svc.Find(&FindRequest{ + SiteId: siteId, + }) + require.NoError(t, err) + require.Len(t, found, 0) + }) +} + +func initFakeServer() *httptest.Server { + fakeServer := &server.Server{ + Engine: &fake.Engine{ + Clusters: []*v1.Cluster{ + { + Id: siteId, + ControlPanelUrl: "https://secure.sakura.ad.jp/objectstorage/", + DisplayNameEnUs: "Ishikari Site #1", + DisplayNameJa: "石狩第1サイト", + DisplayName: "石狩第1サイト", + DisplayOrder: 1, + EndpointBase: "isk01.sakurastorage.jp", + }, + }, + Account: &v1.Account{ + Code: v1.Code("member@account@isk01"), + CreatedAt: v1.CreatedAt(time.Now()), + ResourceId: v1.ResourceID(accountId), + }, + }, + } + return httptest.NewServer(fakeServer.Handler()) +} diff --git a/bucket/read_service.go b/bucket/read_service.go index 4645d1e..3022394 100644 --- a/bucket/read_service.go +++ b/bucket/read_service.go @@ -51,9 +51,5 @@ func (s *Service) ReadWithContext(ctx context.Context, req *ReadRequest) (*Bucke return bucket, nil } } - var e error = service.NotFoundError(fmt.Errorf("bucket %q not found", req.Id)) - if _, ok := e.(service.NotFoundError); ok { - fmt.Println("foo") - } return nil, service.NotFoundError(fmt.Errorf("bucket %q not found", req.Id)) } diff --git a/go.mod b/go.mod index f570d10..fa8b160 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/aws/aws-sdk-go-v2/credentials v1.9.0 github.com/aws/aws-sdk-go-v2/service/s3 v1.25.0 github.com/sacloud/api-client-go v0.0.3 - github.com/sacloud/object-storage-api-go v0.0.5-0.20220323131139-89c78dc904c0 + github.com/sacloud/object-storage-api-go v0.0.5 github.com/sacloud/packages-go v0.0.2 github.com/stretchr/testify v1.7.0 ) diff --git a/go.sum b/go.sum index 94301c7..9104c80 100644 --- a/go.sum +++ b/go.sum @@ -136,8 +136,8 @@ github.com/sacloud/api-client-go v0.0.3 h1:9xGKuBzWaYribd4jmq1Fe8Uijtr3taL9dwYKF github.com/sacloud/api-client-go v0.0.3/go.mod h1:fisKcJ5DUIPx+PpNrH+118tZ2ejp+pM6R+xEiICuv/Q= github.com/sacloud/go-http v0.0.4 h1:+vgx/uCctcGiHMe8jE+qirMKd3+d73MNZXK7nrUo0po= github.com/sacloud/go-http v0.0.4/go.mod h1:aYTXNuAnPmD6Ar3ktDaR1gPxJCPv2CqpppcYciy1hmo= -github.com/sacloud/object-storage-api-go v0.0.5-0.20220323131139-89c78dc904c0 h1:qG/A91XdlwRoPyJvN2JdkYEuh/K8C6iSOFp/8lKU1bo= -github.com/sacloud/object-storage-api-go v0.0.5-0.20220323131139-89c78dc904c0/go.mod h1:zbPyMvPwRZA8PToJotYkOv1o9btdHdGjWkeHj58PhfE= +github.com/sacloud/object-storage-api-go v0.0.5 h1:tPhM8Authxm4yshdhZzLtAX/D5bj6kE4nx0PdKYI4rA= +github.com/sacloud/object-storage-api-go v0.0.5/go.mod h1:zbPyMvPwRZA8PToJotYkOv1o9btdHdGjWkeHj58PhfE= github.com/sacloud/packages-go v0.0.2 h1:tvA/V8tjzaCeuQQVwby1Olq6RmjMI/LEdAe65+qcpYM= github.com/sacloud/packages-go v0.0.2/go.mod h1:Eny1qnbKr9n/5yfXDiwOC+0YMiBhK1HBYGjjmxes+CI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=