Skip to content

Commit

Permalink
fakeサーバ実装: SiteStatus (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc authored Mar 2, 2022
1 parent 92a6ebf commit edd720e
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 39 deletions.
41 changes: 41 additions & 0 deletions apis/v1/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,44 @@ func Example_permissionKeys() {
// output:
// secret
}

// Example_siteStatus サイトステータス確認の例
func Example_siteStatus() {
token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")

client, err := v1.NewClientWithResponses(serverURL, func(c *v1.Client) error {
c.RequestEditors = []v1.RequestEditorFn{
v1.OjsAuthInterceptor(token, secret),
}
return nil
})
if err != nil {
panic(err)
}

// サイトIDが必要になるためまずサイト一覧を取得
sitesResp, err := client.ListClustersWithResponse(context.Background())
if err != nil {
panic(err)
}

sites, err := sitesResp.Result()
if err != nil {
panic(err)
}
siteId := sites.Data[0].Id

statusResp, err := client.ReadSiteStatusWithResponse(context.Background(), siteId)
if err != nil {
panic(err)
}
status, err := statusResp.Result()
if err != nil {
panic(err)
}

fmt.Println(status.Data.StatusCode.Status)
// output:
// ok
}
71 changes: 46 additions & 25 deletions apis/v1/spec/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Status'
$ref: '#/components/schemas/StatusResponseBody'
'401':
description: 認証に失敗しました
content:
Expand Down Expand Up @@ -1506,6 +1506,16 @@ components:
required:
- data

# Note: Statusから分離
StatusResponseBody:
description: Status
type: object
properties:
data:
$ref: "#/components/schemas/Status"
required:
- data

# Note: model.Bucketからリネーム
Bucket:
type: object
Expand Down Expand Up @@ -1960,33 +1970,44 @@ components:
# user:
# type: string
# example: user_name

# Note: StatusResponseBodyを切り出し
Status:
description: Status
description: data type
type: object
properties:
data:
description: data type
type: object
properties:
accept_new:
type: boolean
example: true
message:
type: string
example: some extra info
started_at:
type: string
format: date-time
example: '2020-01-11T01:11:23.123456+09:00'
status_code:
type: object
properties:
id:
type: integer
example: 1
status:
type: string
example: available
accept_new:
type: boolean
example: true
message:
type: string
example: some extra info
started_at:
type: string
format: date-time
example: '2020-01-11T01:11:23.123456+09:00'
status_code:
$ref: "#/components/schemas/StatusCode"
required:
- accept_new
- message
- started_at
- status_code

# Note: Statusから分離
StatusCode:
type: object
properties:
id:
type: integer
example: 1
status:
type: string
example: available
required:
- id
- status

ErrorCode:
description: |
エラーコード。
Expand Down
6 changes: 3 additions & 3 deletions apis/v1/zz_client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions apis/v1/zz_types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion fake/server/site_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@
package server

import (
"net/http"

"github.com/gin-gonic/gin"
v1 "github.com/sacloud/object-storage-api-go/apis/v1"
)

// ReadSiteStatus サイトのステータスの取得
// (GET /{site_name}/v2/status)
func (s *Server) ReadSiteStatus(c *gin.Context, siteId string) {}
func (s *Server) ReadSiteStatus(c *gin.Context, siteId string) {
status, err := s.Engine.ReadSiteStatus(siteId)
if err != nil {
s.handleError(c, err)
return
}

c.JSON(http.StatusOK, &v1.StatusResponseBody{
Data: *status,
})
}
43 changes: 43 additions & 0 deletions fake/site_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022 The sacloud/object-storage-api-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 fake

import (
"time"

v1 "github.com/sacloud/object-storage-api-go/apis/v1"
)

// ReadSiteStatus サイトのステータスの取得
// (GET /{site_name}/v2/status)
func (engine *Engine) ReadSiteStatus(siteId string) (*v1.Status, error) {
defer engine.rLock()()

if err := engine.siteExist(siteId); err != nil {
return nil, err
}

// Note: fakeでは固定値を返す
jst, _ := time.LoadLocation("Asia/Tokyo")
return &v1.Status{
AcceptNew: true,
Message: "Fake server for sacloud/object-storage-api",
StartedAt: time.Date(2020, 7, 17, 9, 0, 0, 0, jst),
StatusCode: v1.StatusCode{
Id: 1,
Status: "ok",
},
}, nil
}

0 comments on commit edd720e

Please sign in to comment.