-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_grant.go
196 lines (168 loc) · 5.43 KB
/
resource_grant.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"log"
"bytes"
"errors"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGrant() *schema.Resource {
return &schema.Resource{
Create: resourceGrantCreate,
Read: resourceGrantRead,
Update: resourceGrantUpdate,
Delete: resourceGrantDelete,
Schema: map[string]*schema.Schema{
"grant_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"client_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"audience": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"scope": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
},
},
}
}
type Grant struct {
Id string `json:"id,omitempty"`
Client_id string `json:"client_id,omitempty"`
Audience string `json:"audience,omitempty"`
Scope []interface{} `json:"scope"`
}
func resourceGrantCreate(d *schema.ResourceData, m interface{}) error {
reqClient := Grant{
Client_id: d.Get("client_id").(string),
Audience: d.Get("audience").(string),
Scope: d.Get("scope").([]interface{}),
}
jsonValue, err := json.Marshal(reqClient)
if err != nil {
return err
}
log.Println("Request JSON: " + string(jsonValue))
config := m.(Config)
client := http.Client{}
req, err := http.NewRequest("POST", "https://" + config.domain + "/api/v2/client-grants", bytes.NewBuffer(jsonValue))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer " + config.accessToken)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Println("Response JSON:" + string(data))
if(resp.StatusCode != 201) {
return errors.New("Error: Invalid status code during create: "+string(data))
}
var respClient Grant
err = json.Unmarshal(data, &respClient)
if err != nil {
return err
}
log.Println("New client_id:" + respClient.Id)
d.Set("grant_id", respClient.Id)
d.SetId(respClient.Id)
return nil
}
func resourceGrantRead(d *schema.ResourceData, m interface{}) error {
config := m.(Config)
client := http.Client{}
req, err := http.NewRequest("GET", "https://" + config.domain + "/api/v2/client-grants?client_id="+d.Get("client_id").(string)+"&audience="+d.Get("audience").(string), nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer " + config.accessToken)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Println("Response JSON:" + string(data))
if(resp.StatusCode != 200) {
return errors.New("Error: Invalid status code during read: " + string(data))
}
var respClient[] Grant
err = json.Unmarshal(data, &respClient)
if err != nil {
return err
}
if(len(respClient) > 1) {
return errors.New("Error: Multiple grants found for the same client and audience: " + string(data))
}
if(len(respClient) <= 0) {
d.SetId("")
return nil
}
return nil
}
func resourceGrantUpdate(d *schema.ResourceData, m interface{}) error {
reqClient := Grant{
Client_id: d.Get("client_id").(string),
Audience: d.Get("audience").(string),
Scope: d.Get("scope").([]interface{}),
}
jsonValue, err := json.Marshal(reqClient)
if err != nil {
return err
}
log.Println("Request JSON: " + string(jsonValue))
config := m.(Config)
client := http.Client{}
req, err := http.NewRequest("PATCH", "https://" + config.domain + "/api/v2/client-grants/" + d.Id(), bytes.NewBuffer(jsonValue))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer " + config.accessToken)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Println("Response JSON:" + string(data))
if(resp.StatusCode != 200) {
return errors.New("Error: Invalid status code during update: " + string(data))
}
var respClient Grant
err = json.Unmarshal(data, &respClient)
if err != nil {
return err
}
return nil
}
func resourceGrantDelete(d *schema.ResourceData, m interface{}) error {
config := m.(Config)
client := http.Client{}
req, err := http.NewRequest("DELETE", "https://" + config.domain + "/api/v2/client-grants/" + d.Id(), nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer " + config.accessToken)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
log.Println("Response Status Code:" + string(resp.StatusCode))
if(resp.StatusCode != 204) {
return errors.New("Error: Invalid status code during delete")
}
return nil
}