/
path_regions.go
141 lines (118 loc) · 3.61 KB
/
path_regions.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
package keystoneauth
import (
"fmt"
"context"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathListRegions(b *backend) *framework.Path {
return &framework.Path{
Pattern: "regions/?$",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathRegionList,
},
}
}
func pathRegions(b *backend) *framework.Path {
return &framework.Path{
Pattern: "regions/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"id": &framework.FieldSchema{
Type: framework.TypeString,
Description: "id",
},
"description": &framework.FieldSchema{
Type: framework.TypeString,
Description: "description",
},
"parent_region_id": &framework.FieldSchema{
Type: framework.TypeString,
Description: "parent_region_id",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathRegionWrite,
logical.ReadOperation: b.pathRegionRead,
},
}
}
func (b *backend) Region(ctx context.Context, s logical.Storage, n string) (*regionEntry, error) {
entry, err := s.Get(ctx, "region/" + n)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result regionEntry
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
return &result, nil
}
func (b *backend) pathRegionRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
id := data.Get("id").(string)
description := data.Get("description").(string)
parentRegionID := data.Get("parent_region_id").(string)
region, err := b.Region(ctx, req.Storage, id)
if err != nil {
return nil, err
}
if region == nil {
return logical.ErrorResponse(fmt.Sprintf("unknown region: %s", id)), nil
}
conf, err2 := getconfig(ctx, req)
if err2 != nil {
return nil, fmt.Errorf("configure the Keystone connection with config/connection first")
}
keystoneURL := conf[0]
token := conf[1]
createdRegion, err2 := CreateRegion(id, description, parentRegionID, keystoneURL, token)
if err2 != nil {
return nil, fmt.Errorf("creation of the region failed")
}
return &logical.Response{
Data: map[string]interface{}{
"id": createdRegion[0],
"description": createdRegion[1],
"parent_region_id": createdRegion[2],
},
}, nil
}
func (b *backend) pathRegionList(
ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "region/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
func (b *backend) pathRegionWrite(
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
id := data.Get("id").(string)
description := data.Get("description").(string)
parentRegionID := data.Get("parent_region_id").(string)
// Store it
entry, err := logical.StorageEntryJSON("region/"+id, ®ionEntry{
Region_id: id,
Region_description: description,
Region_parent_region_id: parentRegionID,
})
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
"id": id,
"created": true,
},
}, nil
}
type regionEntry struct {
Region_id string `json:"id" structs:"id" mapstructure:"id"`
Region_description string `json:"description" structs:"description" mapstructure:"description"`
Region_parent_region_id string `json:"parent_region_id" structs:"parent_region_id" mapstructure:"parent_region_id"`
}