-
Notifications
You must be signed in to change notification settings - Fork 3
/
trustFrameworkKeySetClient.go
165 lines (139 loc) · 4.02 KB
/
trustFrameworkKeySetClient.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
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/pjfebbraro/terraform-provider-azureadb2cief/internal/models"
"io"
"net/http"
)
type TrustFrameworkKeySetClient struct {
*baseClient
}
func newKeySetClient(baseClient *baseClient) *TrustFrameworkKeySetClient {
return &TrustFrameworkKeySetClient{
baseClient,
}
}
func (c *TrustFrameworkKeySetClient) GetKeySet(ctx context.Context, id string) (*models.TrustFrameworkKeySet, int, error) {
path := fmt.Sprintf("/trustFramework/keySets/%s", id)
response, err := c.doRequest(ctx, path, http.MethodGet, http.NoBody, nil)
var status int
if err != nil {
return nil, status, err
}
status = response.StatusCode
if status >= 400 {
return nil, status, formatHttpErrorResponse(response)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, status, err
}
keyset := models.TrustFrameworkKeySet{}
if err := json.Unmarshal(body, &keyset); err != nil {
return nil, status, err
}
return &keyset, status, nil
}
func (c *TrustFrameworkKeySetClient) GetActiveKey(ctx context.Context, id string) (*models.TrustFrameworkKey, int, error) {
path := fmt.Sprintf("/trustFramework/keySets/%s/getActiveKey", id)
response, err := c.doRequest(ctx, path, http.MethodGet, http.NoBody, nil)
var status int
if err != nil {
return nil, status, err
}
status = response.StatusCode
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, status, err
}
key := models.TrustFrameworkKey{}
if err := json.Unmarshal(body, &key); err != nil {
return nil, status, err
}
return &key, status, nil
}
func (c *TrustFrameworkKeySetClient) CreateKey(ctx context.Context, id string) (*models.TrustFrameworkKeySet, int, error) {
path := "/trustFramework/keySets"
keyId := map[string]string{
"id": id,
}
var status int
body, err := json.Marshal(keyId)
if err != nil {
return nil, status, err
}
contentType := "application/json"
response, err := c.doRequest(ctx, path, http.MethodPost, bytes.NewBuffer(body), &contentType)
if err != nil {
return nil, status, err
}
status = response.StatusCode
if status != 201 {
return nil, status, formatHttpErrorResponse(response)
}
defer response.Body.Close()
body, err = io.ReadAll(response.Body)
if err != nil {
return nil, status, err
}
keyset := models.TrustFrameworkKeySet{}
err = json.Unmarshal(body, &keyset)
if err != nil {
return nil, status, err
}
return &keyset, status, nil
}
func (c *TrustFrameworkKeySetClient) GenerateKey(ctx context.Context, id string, key models.TrustFrameworkKey) (int, error) {
path := fmt.Sprintf("/trustFramework/keySets/%s/generateKey", id)
body, err := json.Marshal(key)
var status int
if err != nil {
return status, err
}
contentType := "application/json"
response, err := c.doRequest(ctx, path, http.MethodPost, bytes.NewBuffer(body), &contentType)
if err != nil {
return status, err
}
status = response.StatusCode
if status != 200 {
return status, formatHttpErrorResponse(response)
}
return status, nil
}
func (c *TrustFrameworkKeySetClient) UploadSecret(ctx context.Context, id string, key models.TrustFrameworkKey) (int, error) {
path := fmt.Sprintf("/trustFramework/keySets/%s/uploadSecret", id)
body, err := json.Marshal(key)
var status int
if err != nil {
return status, err
}
contentType := "application/json"
response, err := c.doRequest(ctx, path, http.MethodPost, bytes.NewBuffer(body), &contentType)
if err != nil {
return status, err
}
status = response.StatusCode
if status != 200 {
return status, formatHttpErrorResponse(response)
}
return status, nil
}
func (c *TrustFrameworkKeySetClient) DeleteKey(ctx context.Context, id string) (int, error) {
path := fmt.Sprintf("/trustFramework/keySets/%s", id)
response, err := c.doRequest(ctx, path, http.MethodDelete, http.NoBody, nil)
var status int
if err != nil {
return status, err
}
status = response.StatusCode
if status != 204 {
return status, formatHttpErrorResponse(response)
}
return status, nil
}