forked from IBM-Cloud/bluemix-go
/
api_keys.go
151 lines (128 loc) · 3.89 KB
/
api_keys.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
package iamv1
import (
"net/http"
"net/url"
"github.com/IBM-Cloud/bluemix-go/client"
"github.com/IBM-Cloud/bluemix-go/models"
"github.com/IBM-Cloud/bluemix-go/rest"
)
type APIKeyResource struct {
Metadata APIKeyMetadata `json:"metadata"`
Entity APIKeyEntity `json:"entity"`
}
type APIKeyMetadata struct {
UUID string `json:"uuid"`
Version string `json:"version"`
Crn string `json:"crn"`
CreatedAt string `json:"createdAt"`
ModifiedAt string `json:"modifiedAt"`
}
type APIKeyEntity struct {
Name string `json:"name"`
Description string `json:"description"`
BoundTo string `json:"boundTo"`
Format string `json:"format"`
APIKey string `json:"apiKey"`
APIKeyID string `json:"apiKeyId"`
APIKeySecret string `json:"apiKeySecret"`
}
func (r APIKeyResource) ToModel() models.APIKey {
meta := r.Metadata
entity := r.Entity
return models.APIKey{
UUID: meta.UUID,
Version: meta.Version,
Crn: meta.Crn,
CreatedAt: meta.CreatedAt,
ModifiedAt: meta.ModifiedAt,
Name: entity.Name,
Description: entity.Description,
BoundTo: entity.BoundTo,
Format: entity.Format,
APIKey: entity.APIKey,
APIKeyID: entity.APIKeyID,
APIKeySecret: entity.APIKeySecret,
}
}
const (
_API_Key_Operation_Path_Root = "/apikeys/"
)
type APIKeyRepository interface {
Get(uuid string) (*models.APIKey, error)
List(boundTo string) ([]models.APIKey, error)
FindByName(name string, boundTo string) ([]models.APIKey, error)
Create(key models.APIKey) (*models.APIKey, error)
Delete(uuid string) error
Update(uuid string, version string, key models.APIKey) (*models.APIKey, error)
}
type apiKeyRepository struct {
client *client.Client
}
func NewAPIKeyRepository(c *client.Client) APIKeyRepository {
return &apiKeyRepository{
client: c,
}
}
func (r *apiKeyRepository) Get(uuid string) (*models.APIKey, error) {
key := APIKeyResource{}
_, err := r.client.Get(_API_Key_Operation_Path_Root+uuid, &key)
if err != nil {
return nil, err
}
result := key.ToModel()
return &result, nil
}
func (r *apiKeyRepository) List(boundTo string) ([]models.APIKey, error) {
var keys []models.APIKey
resp, err := r.client.GetPaginated("/apikeys?boundTo="+url.QueryEscape(boundTo), NewIAMPaginatedResources(APIKeyResource{}), func(resource interface{}) bool {
if apiKeyResource, ok := resource.(APIKeyResource); ok {
keys = append(keys, apiKeyResource.ToModel())
return true
}
return false
})
if resp.StatusCode == http.StatusNotFound {
return []models.APIKey{}, nil
}
return keys, err
}
func (r *apiKeyRepository) FindByName(name string, boundTo string) ([]models.APIKey, error) {
var keys []models.APIKey
resp, err := r.client.GetPaginated("/apikeys?boundTo="+url.QueryEscape(boundTo), NewIAMPaginatedResources(APIKeyResource{}), func(resource interface{}) bool {
if apiKeyResource, ok := resource.(APIKeyResource); ok {
if apiKeyResource.Entity.Name == name {
keys = append(keys, apiKeyResource.ToModel())
}
return true
}
return false
})
if resp.StatusCode == http.StatusNotFound {
return []models.APIKey{}, nil
}
return keys, err
}
func (r *apiKeyRepository) Create(key models.APIKey) (*models.APIKey, error) {
var keyCreated APIKeyResource
_, err := r.client.Post("/apikeys", &key, &keyCreated)
if err != nil {
return nil, err
}
keyToReturn := keyCreated.ToModel()
return &keyToReturn, err
}
func (r *apiKeyRepository) Delete(uuid string) error {
_, err := r.client.Delete("/apikeys/" + uuid)
return err
}
func (r *apiKeyRepository) Update(uuid string, version string, key models.APIKey) (*models.APIKey, error) {
req := rest.PutRequest(*r.client.Config.Endpoint + "/apikeys/" + uuid).Body(&key)
req.Set("If-Match", version)
var keyUpdated APIKeyResource
_, err := r.client.SendRequest(req, &keyUpdated)
if err != nil {
return nil, err
}
keyToReturn := keyUpdated.ToModel()
return &keyToReturn, nil
}