Skip to content

Commit

Permalink
service: account (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc committed Mar 23, 2022
1 parent 7dd5ba4 commit 8a98715
Show file tree
Hide file tree
Showing 15 changed files with 451 additions and 10 deletions.
27 changes: 27 additions & 0 deletions account/create_request.go
Original file line number Diff line number Diff line change
@@ -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 account

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)
}
34 changes: 34 additions & 0 deletions account/create_service.go
Original file line number Diff line number Diff line change
@@ -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 account

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.Account, error) {
return s.CreateWithContext(context.Background(), req)
}

func (s *Service) CreateWithContext(ctx context.Context, req *CreateRequest) (*v1.Account, error) {
if err := req.Validate(); err != nil {
return nil, err
}
client := objectstorage.NewAccountOp(s.client)
return client.Create(ctx, req.SiteId)
}
28 changes: 28 additions & 0 deletions account/delete_request.go
Original file line number Diff line number Diff line change
@@ -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 account

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)
}
41 changes: 41 additions & 0 deletions account/delete_service.go
Original file line number Diff line number Diff line change
@@ -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 account

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.Delete(ctx, req.SiteId)
}
25 changes: 25 additions & 0 deletions account/find_request.go
Original file line number Diff line number Diff line change
@@ -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 account

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)
}
46 changes: 46 additions & 0 deletions account/find_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 account

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.Account, error) {
return s.FindWithContext(context.Background(), req)
}

func (s *Service) FindWithContext(ctx context.Context, req *FindRequest) ([]*v1.Account, error) {
if req == nil {
req = &FindRequest{}
}
if err := req.Validate(); err != nil {
return nil, err
}

client := objectstorage.NewAccountOp(s.client)
account, err := client.Read(ctx, req.SiteId)
if err != nil {
// Note: serviceインターフェースを満たすためにclient.Read()が404エラーを返した時はエラーではなく[]*v1.Account{}を返す
if _, ok := err.(*v1.Error404); ok {
return []*v1.Account{}, nil
}
return nil, err
}
return []*v1.Account{account}, nil
}
28 changes: 28 additions & 0 deletions account/read_request.go
Original file line number Diff line number Diff line change
@@ -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 account

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)
}
43 changes: 43 additions & 0 deletions account/read_service.go
Original file line number Diff line number Diff line change
@@ -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 account

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.Account, error) {
return s.ReadWithContext(context.Background(), req)
}

func (s *Service) ReadWithContext(ctx context.Context, req *ReadRequest) (*v1.Account, error) {
if err := req.Validate(); err != nil {
return nil, err
}
client := objectstorage.NewAccountOp(s.client)
account, err := client.Read(ctx, req.SiteId)
if err != nil {
return nil, err
}
if account.ResourceId.String() == req.Id {
return account, nil
}
return nil, service.NotFoundError(fmt.Errorf("account %q not found", req.Id))
}
27 changes: 27 additions & 0 deletions account/service.go
Original file line number Diff line number Diff line change
@@ -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 account

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}
}
Loading

0 comments on commit 8a98715

Please sign in to comment.