Skip to content

Commit

Permalink
service: accountkey (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc committed Mar 23, 2022
1 parent 8a98715 commit 17f1a8d
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions account/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})

Expand Down
27 changes: 27 additions & 0 deletions accountkey/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 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)
}
34 changes: 34 additions & 0 deletions accountkey/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 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)
}
28 changes: 28 additions & 0 deletions accountkey/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 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)
}
41 changes: 41 additions & 0 deletions accountkey/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 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)
}
25 changes: 25 additions & 0 deletions accountkey/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 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)
}
38 changes: 38 additions & 0 deletions accountkey/find_service.go
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 28 additions & 0 deletions accountkey/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 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)
}
43 changes: 43 additions & 0 deletions accountkey/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 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
}
27 changes: 27 additions & 0 deletions accountkey/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 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}
}
Loading

0 comments on commit 17f1a8d

Please sign in to comment.