-
Notifications
You must be signed in to change notification settings - Fork 0
/
region.go
105 lines (93 loc) · 2.74 KB
/
region.go
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
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2022 API7.ai, Inc
//
// 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 cloud
import (
"context"
"encoding/json"
"path"
"time"
)
// Region is the specification of the deploy region for Cloud.
type Region struct {
// ID is the unique identify to mark an object.
ID ID `json:"id"`
// Name is the object name.
Name string `json:"name"`
// CreatedAt is the object creation time.
CreatedAt time.Time `json:"created_at"`
// UpdatedAt is the last modified time of this object.
UpdatedAt time.Time `json:"updated_at"`
// Provider is the cloud vendor we use, like aws, gcp
Provider string `json:"provider"`
// Status is the region status
Status EntityStatus `json:"status,omitempty"`
}
// RegionInterface is the interface for manipulating Region.
type RegionInterface interface {
// ListRegions returns an iterator for listing Regions with the
// given list conditions.
// Users need to specify the Paging and Filter conditions (if necessary)
// in the `opts`.
ListRegions(ctx context.Context, opts *ResourceListOptions) (RegionListIterator, error)
}
// RegionListIterator is an iterator for listing Regions.
type RegionListIterator interface {
// Next returns the next Region according to the filter conditions.
Next() (*Region, error)
}
type regionImpl struct {
client httpClient
}
type regionListIterator struct {
iter listIterator
}
func newRegion(client httpClient) RegionInterface {
return ®ionImpl{
client: client,
}
}
func (iter *regionListIterator) Next() (*Region, error) {
var region Region
rawData, err := iter.iter.Next()
if err != nil {
return nil, err
}
if rawData == nil {
return nil, nil
}
if err = json.Unmarshal(rawData, ®ion); err != nil {
return nil, err
}
return ®ion, nil
}
func (impl *regionImpl) ListRegions(ctx context.Context, opts *ResourceListOptions) (RegionListIterator, error) {
var (
paging *Pagination
filter *Filter
)
if opts != nil {
paging = opts.Pagination
filter = opts.Filter
}
iter := listIterator{
ctx: ctx,
resource: "region",
client: impl.client,
path: path.Join(_apiPathPrefix, "regions"),
paging: mergePagination(paging),
filter: filter,
headers: appendHeader(mapClusterIdFromOpts(opts)),
}
return ®ionListIterator{iter: iter}, nil
}