-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtraffics.go
More file actions
93 lines (83 loc) · 3.5 KB
/
Copy pathtraffics.go
File metadata and controls
93 lines (83 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2021-2026 The sacloud/apprun-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 apprun
import (
"context"
"errors"
"net/http"
v1 "github.com/sacloud/apprun-api-go/apis/v1"
)
type TrafficAPI interface {
// List アプリケーショントラフィック分散を取得
List(ctx context.Context, appId string) (*v1.HandlerListTraffics, error)
// Update アプリケーショントラフィック分散を変更
Update(ctx context.Context, appId string, params *v1.PutTrafficsBody) (*v1.HandlerPutTraffics, error)
}
var _ TrafficAPI = (*trafficOp)(nil)
type trafficOp struct {
client *v1.Client
}
// NewTrafficOp アプリケーショントラフィック分散関連API
func NewTrafficOp(client *v1.Client) TrafficAPI {
return &trafficOp{client: client}
}
func (op *trafficOp) List(ctx context.Context, appId string) (*v1.HandlerListTraffics, error) {
const methodName = "Traffics.List"
res, err := op.client.ListApplicationTraffics(ctx, v1.ListApplicationTrafficsParams{ID: appId})
if err != nil {
return nil, NewAPIError(methodName, 0, err)
}
switch result := res.(type) {
case *v1.HandlerListTraffics:
return result, nil
case *v1.ListApplicationTrafficsBadRequest:
return nil, apiErrorFromModel(methodName, http.StatusBadRequest, result)
case *v1.ListApplicationTrafficsUnauthorized:
return nil, apiErrorFromModel(methodName, http.StatusUnauthorized, result)
case *v1.ListApplicationTrafficsForbidden:
return nil, apiErrorFromModel(methodName, http.StatusForbidden, result)
case *v1.ListApplicationTrafficsNotFound:
return nil, apiErrorFromModel(methodName, http.StatusNotFound, result)
case *v1.ListApplicationTrafficsInternalServerError:
return nil, apiErrorFromModel(methodName, http.StatusInternalServerError, result)
default:
return nil, NewAPIError(methodName, 0, errors.New("unknown error"))
}
}
func (op *trafficOp) Update(ctx context.Context, appId string, params *v1.PutTrafficsBody) (*v1.HandlerPutTraffics, error) {
const methodName = "Traffics.Update"
if params == nil {
return nil, NewError(methodName, errors.New("params is nil"))
}
res, err := op.client.PutApplicationTraffic(ctx, *params, v1.PutApplicationTrafficParams{ID: appId})
if err != nil {
return nil, NewAPIError(methodName, 0, err)
}
switch result := res.(type) {
case *v1.HandlerPutTraffics:
return result, nil
case *v1.PutApplicationTrafficBadRequest:
return nil, apiErrorFromModel(methodName, http.StatusBadRequest, result)
case *v1.PutApplicationTrafficUnauthorized:
return nil, apiErrorFromModel(methodName, http.StatusUnauthorized, result)
case *v1.PutApplicationTrafficForbidden:
return nil, apiErrorFromModel(methodName, http.StatusForbidden, result)
case *v1.PutApplicationTrafficNotFound:
return nil, apiErrorFromModel(methodName, http.StatusNotFound, result)
case *v1.PutApplicationTrafficInternalServerError:
return nil, apiErrorFromModel(methodName, http.StatusInternalServerError, result)
default:
return nil, NewAPIError(methodName, 0, errors.New("unknown error"))
}
}