generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssl_bypass_rule.go
98 lines (88 loc) · 3.15 KB
/
ssl_bypass_rule.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
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
u "net/url"
)
const sslBypassRulesEndpoint = "v1/ssl_bypass_rules"
type SSLBypassRule struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
ApplyToOrg bool `json:"apply_to_org"`
Sources []string `json:"sources"`
ExemptSources []string `json:"exempt_sources"`
BypassUncategorizedUrls bool `json:"bypass_uncategorized_urls"`
ContentTypes []string `json:"content_types"`
Domains []string `json:"domains"`
Priority int `json:"priority"`
Action string `json:"action"`
}
func NewSSLBypassRule(d *schema.ResourceData) *SSLBypassRule {
res := &SSLBypassRule{}
if d.HasChange("name") {
res.Name = d.Get("name").(string)
}
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)
res.Domains = ConfigToStringSlice("domains", d)
res.ContentTypes = ConfigToStringSlice("content_types", d)
res.BypassUncategorizedUrls = d.Get("bypass_uncategorized_urls").(bool)
res.Action = d.Get("action").(string)
return res
}
func parseSSLBypassRule(resp []byte) (*SSLBypassRule, error) {
pg := &SSLBypassRule{}
err := json.Unmarshal(resp, pg)
if err != nil {
return nil, fmt.Errorf("could not parse ssl bypass rule response: %v", err)
}
return pg, nil
}
func CreateSSLBypassRule(ctx context.Context, c *Client, rg *SSLBypassRule) (*SSLBypassRule, error) {
rgUrl := fmt.Sprintf("%s/%s", c.BaseURL, sslBypassRulesEndpoint)
body, err := json.Marshal(rg)
if err != nil {
return nil, fmt.Errorf("could not convert ssl bypass rule to json: %v", err)
}
resp, err := c.Post(ctx, rgUrl, body)
if err != nil {
return nil, err
}
return parseSSLBypassRule(resp)
}
func UpdateSSLBypassRule(ctx context.Context, c *Client, rgID string, rg *SSLBypassRule) (*SSLBypassRule, error) {
rgUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, sslBypassRulesEndpoint, rgID)
body, err := json.Marshal(rg)
if err != nil {
return nil, fmt.Errorf("could not convert ssl bypass rule to json: %v", err)
}
resp, err := c.Patch(ctx, rgUrl, body)
if err != nil {
return nil, err
}
return parseSSLBypassRule(resp)
}
func GetSSLBypassRule(ctx context.Context, c *Client, rgID string) (*SSLBypassRule, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, sslBypassRulesEndpoint, rgID)
resp, err := c.Get(ctx, url, u.Values{"expand": {"true"}})
if err != nil {
return nil, err
}
return parseSSLBypassRule(resp)
}
func DeleteSSLBypassRule(ctx context.Context, c *Client, pgID string) (*SSLBypassRule, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, sslBypassRulesEndpoint, pgID)
resp, err := c.Delete(ctx, url, nil)
if err != nil {
return nil, err
}
return parseSSLBypassRule(resp)
}