generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pac_file.go
219 lines (200 loc) · 5.99 KB
/
pac_file.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"net/http"
)
const pacFilesEndpoint string = "v1/pac_files"
const pacTypeManaged string = "managed"
const pacTypeBringYourOwn string = "bring_your_own"
type ManagedContent struct {
Domains *[]string `json:"domains,omitempty"`
CloudApps *[]string `json:"cloud_apps,omitempty"`
IpNetworks *[]string `json:"ip_networks,omitempty"`
}
type PacFile struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
ApplyToOrg bool `json:"apply_to_org"`
Sources []string `json:"sources"`
ExemptSources []string `json:"exempt_sources"`
HasContent bool `json:"has_content,omitempty"`
Priority int `json:"priority"`
Type string `json:"type,omitempty"`
ManagedContent *ManagedContent `json:"managed_content,omitempty"`
}
func NewManagedContent(d *schema.ResourceData) *ManagedContent {
mc := d.Get("managed_content").([]interface{})
if len(mc) == 0 {
return nil
}
res := &ManagedContent{
Domains: &[]string{},
CloudApps: &[]string{},
IpNetworks: &[]string{},
}
if mc[0] == nil {
return res
}
conf := mc[0].(map[string]interface{})
rng := conf["domains"].([]interface{})
if len(rng) > 0 {
tmpDomains := make([]string, len(rng))
for i, val := range rng {
tmpDomains[i] = val.(string)
}
res.Domains = &tmpDomains
}
rng = conf["cloud_apps"].([]interface{})
if len(rng) > 0 {
tmpCloudApps := make([]string, len(rng))
for i, val := range rng {
tmpCloudApps[i] = val.(string)
}
res.CloudApps = &tmpCloudApps
}
rng = conf["ip_networks"].([]interface{})
if len(rng) > 0 {
tmpIpNetworks := make([]string, len(rng))
for i, val := range rng {
tmpIpNetworks[i] = val.(string)
}
res.IpNetworks = &tmpIpNetworks
}
return res
}
func PacFileBase(d *schema.ResourceData) *PacFile {
res := &PacFile{}
res.Description = d.Get("description").(string)
res.ApplyToOrg = d.Get("apply_to_org").(bool)
res.Enabled = d.Get("enabled").(bool)
res.Sources = ConfigToStringSlice("sources", d)
res.ExemptSources = ConfigToStringSlice("exempt_sources", d)
res.Priority = d.Get("priority").(int)
return res
}
func NewPacFile(d *schema.ResourceData) *PacFile {
res := PacFileBase(d)
res.Name = d.Get("name").(string)
res.Type = d.Get("type").(string)
return res
}
func ModifiedPacFile(d *schema.ResourceData) *PacFile {
res := PacFileBase(d)
if d.HasChange("name") {
res.Name = d.Get("name").(string)
}
return res
}
func parsePacFile(resp []byte) (*PacFile, error) {
pg := &PacFile{}
err := json.Unmarshal(resp, pg)
if err != nil {
return nil, fmt.Errorf("could not parse PAC file response: %v", err)
}
return pg, nil
}
func parseManagedContent(resp []byte) (*ManagedContent, error) {
mc := &ManagedContent{}
err := json.Unmarshal(resp, mc)
if err != nil {
return nil, fmt.Errorf("could not parse managed content response: %v", err)
}
return mc, nil
}
func CreatePacFile(ctx context.Context, c *Client, pf *PacFile) (*PacFile, error) {
pfUrl := fmt.Sprintf("%s/%s", c.BaseURL, pacFilesEndpoint)
body, err := json.Marshal(pf)
if err != nil {
return nil, fmt.Errorf("could not convert PAC file to json: %v", err)
}
resp, err := c.Post(ctx, pfUrl, body)
if err != nil {
return nil, err
}
return parsePacFile(resp)
}
func UpdatePacFile(ctx context.Context, c *Client, pfID string, pf *PacFile) (*PacFile, error) {
pfUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, pacFilesEndpoint, pfID)
body, err := json.Marshal(pf)
if err != nil {
return nil, fmt.Errorf("could not convert PAC file to json: %v", err)
}
resp, err := c.Patch(ctx, pfUrl, body)
if err != nil {
return nil, err
}
return parsePacFile(resp)
}
func GetPacFile(ctx context.Context, c *Client, pfID string) (*PacFile, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, pacFilesEndpoint, pfID)
resp, err := c.Get(ctx, url, nil)
if err != nil {
return nil, err
}
return parsePacFile(resp)
}
func DeletePacFile(ctx context.Context, c *Client, pfID string) (*PacFile, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, pacFilesEndpoint, pfID)
resp, err := c.Delete(ctx, url, nil)
if err != nil {
return nil, err
}
return parsePacFile(resp)
}
func GetPacFileContent(ctx context.Context, c *Client, pfID string) (*string, error) {
pfUrl := fmt.Sprintf("%s/%s/%s/content", c.BaseURL, pacFilesEndpoint, pfID)
resp, err := c.Get(ctx, pfUrl, nil)
if err != nil {
return nil, err
}
strResp := string(resp)
return &strResp, nil
}
func DeletePacFileContent(ctx context.Context, c *Client, pfID string) error {
url := fmt.Sprintf("%s/%s/%s/content", c.BaseURL, pacFilesEndpoint, pfID)
_, err := c.Delete(ctx, url, nil)
if err != nil {
return err
}
return nil
}
func PutPacFileContent(ctx context.Context, c *Client, pfID, pfContent string) error {
pfUrl := fmt.Sprintf("%s/%s/%s/content", c.BaseURL, pacFilesEndpoint, pfID)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, pfUrl, bytes.NewReader([]byte(pfContent)))
req.Header.Set("Content-Type", "text/plain")
if err != nil {
return err
}
_, err = c.SendRequest(req)
if err != nil {
return err
}
return nil
}
func GetPacFileManagedContent(ctx context.Context, c *Client, pfID string) (*ManagedContent, error) {
pfUrl := fmt.Sprintf("%s/%s/%s/content/managed", c.BaseURL, pacFilesEndpoint, pfID)
resp, err := c.Get(ctx, pfUrl, nil)
if err != nil {
return nil, err
}
return parseManagedContent(resp)
}
func PatchPacFileManagedContent(ctx context.Context, c *Client, pfID string, mc *ManagedContent) error {
pfUrl := fmt.Sprintf("%s/%s/%s/content/managed", c.BaseURL, pacFilesEndpoint, pfID)
body, err := json.Marshal(mc)
if err != nil {
fmt.Println("could not convert PAC file managed content to json")
return err
}
_, err = c.Patch(ctx, pfUrl, body)
if err != nil {
return err
}
return nil
}