forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition.go
167 lines (140 loc) · 4.12 KB
/
partition.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"context"
"fmt"
"time"
)
// Partition is the configuration of a single admin partition. Admin Partitions are a Consul Enterprise feature.
type Partition struct {
// Name is the name of the Partition.
Name string `json:"Name"`
// Description is where the user puts any information they want
// about the admin partition. It is not used internally.
Description string `json:"Description,omitempty"`
// DeletedAt is the time when the Partition was marked for deletion
// This is nullable so that we can omit if empty when encoding in JSON
DeletedAt *time.Time `json:"DeletedAt,omitempty" alias:"deleted_at"`
// CreateIndex is the Raft index at which the Partition was created
CreateIndex uint64 `json:"CreateIndex,omitempty"`
// ModifyIndex is the latest Raft index at which the Partition was modified.
ModifyIndex uint64 `json:"ModifyIndex,omitempty"`
}
// PartitionDefaultName is the default partition value.
const PartitionDefaultName = "default"
// Partitions can be used to manage Partitions in Consul Enterprise.
type Partitions struct {
c *Client
}
// Operator returns a handle to the operator endpoints.
func (c *Client) Partitions() *Partitions {
return &Partitions{c}
}
func (p *Partitions) Create(ctx context.Context, partition *Partition, q *WriteOptions) (*Partition, *WriteMeta, error) {
if partition.Name == "" {
return nil, nil, fmt.Errorf("Must specify a Name for Partition creation")
}
r := p.c.newRequest("PUT", "/v1/partition")
r.setWriteOptions(q)
r.ctx = ctx
r.obj = partition
rtt, resp, err := p.c.doRequest(r)
if err != nil {
return nil, nil, err
}
defer closeResponseBody(resp)
if err := requireOK(resp); err != nil {
return nil, nil, err
}
wm := &WriteMeta{RequestTime: rtt}
var out Partition
if err := decodeBody(resp, &out); err != nil {
return nil, nil, err
}
return &out, wm, nil
}
func (p *Partitions) Update(ctx context.Context, partition *Partition, q *WriteOptions) (*Partition, *WriteMeta, error) {
if partition.Name == "" {
return nil, nil, fmt.Errorf("Must specify a Name for Partition updating")
}
r := p.c.newRequest("PUT", "/v1/partition/"+partition.Name)
r.setWriteOptions(q)
r.ctx = ctx
r.obj = partition
rtt, resp, err := p.c.doRequest(r)
if err != nil {
return nil, nil, err
}
defer closeResponseBody(resp)
if err := requireOK(resp); err != nil {
return nil, nil, err
}
wm := &WriteMeta{RequestTime: rtt}
var out Partition
if err := decodeBody(resp, &out); err != nil {
return nil, nil, err
}
return &out, wm, nil
}
func (p *Partitions) Read(ctx context.Context, name string, q *QueryOptions) (*Partition, *QueryMeta, error) {
var out Partition
r := p.c.newRequest("GET", "/v1/partition/"+name)
r.setQueryOptions(q)
r.ctx = ctx
rtt, resp, err := p.c.doRequest(r)
if err != nil {
return nil, nil, err
}
defer closeResponseBody(resp)
found, resp, err := requireNotFoundOrOK(resp)
if err != nil {
return nil, nil, err
}
qm := &QueryMeta{}
parseQueryMeta(resp, qm)
qm.RequestTime = rtt
if !found {
return nil, qm, nil
}
if err := decodeBody(resp, &out); err != nil {
return nil, nil, err
}
return &out, qm, nil
}
func (p *Partitions) Delete(ctx context.Context, name string, q *WriteOptions) (*WriteMeta, error) {
r := p.c.newRequest("DELETE", "/v1/partition/"+name)
r.setWriteOptions(q)
r.ctx = ctx
rtt, resp, err := p.c.doRequest(r)
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
if err := requireOK(resp); err != nil {
return nil, err
}
wm := &WriteMeta{RequestTime: rtt}
return wm, nil
}
func (p *Partitions) List(ctx context.Context, q *QueryOptions) ([]*Partition, *QueryMeta, error) {
var out []*Partition
r := p.c.newRequest("GET", "/v1/partitions")
r.setQueryOptions(q)
r.ctx = ctx
rtt, resp, err := p.c.doRequest(r)
if err != nil {
return nil, nil, err
}
defer closeResponseBody(resp)
if err := requireOK(resp); err != nil {
return nil, nil, err
}
qm := &QueryMeta{}
parseQueryMeta(resp, qm)
qm.RequestTime = rtt
if err := decodeBody(resp, &out); err != nil {
return nil, nil, err
}
return out, qm, nil
}