generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
url_filtering_rule.go
116 lines (106 loc) · 4.42 KB
/
url_filtering_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
u "net/url"
)
const urlFilteringRulesEndpoint string = "v1/url_filtering_rules"
type UrlFilteringRule struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description"`
Action string `json:"action"`
ApplyToOrg bool `json:"apply_to_org"`
Enabled bool `json:"enabled"`
Sources []string `json:"sources,omitempty"`
ExemptSources []string `json:"exempt_sources,omitempty"`
AdvancedThreatProtection bool `json:"advanced_threat_protection"`
CatalogAppCategories []string `json:"catalog_app_categories"`
CloudApps []string `json:"cloud_apps"`
Countries []string `json:"countries,omitempty"`
ExpiresAt string `json:"expires_at,omitempty"`
FilterExpression string `json:"filter_expression,omitempty"`
ForbiddenContentCategories []string `json:"forbidden_content_categories"`
Networks []string `json:"networks,omitempty"`
Priority int `json:"priority"`
Schedule []string `json:"schedule"`
TenantRestriction string `json:"tenant_restriction,omitempty"`
ThreatCategories []string `json:"threat_categories"`
WarnTtl int `json:"warn_ttl"`
}
func NewUrlFilteringRule(d *schema.ResourceData) *UrlFilteringRule {
res := &UrlFilteringRule{}
if d.HasChange("name") {
res.Name = d.Get("name").(string)
}
res.Description = d.Get("description").(string)
res.Action = d.Get("action").(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.AdvancedThreatProtection = d.Get("advanced_threat_protection").(bool)
res.CatalogAppCategories = ConfigToStringSlice("catalog_app_categories", d)
res.CloudApps = ConfigToStringSlice("cloud_apps", d)
res.Countries = ConfigToStringSlice("countries", d)
res.ExpiresAt = d.Get("expires_at").(string)
res.FilterExpression = d.Get("filter_expression").(string)
res.ForbiddenContentCategories = ConfigToStringSlice("forbidden_content_categories", d)
res.Networks = ConfigToStringSlice("networks", d)
res.Priority = d.Get("priority").(int)
res.Schedule = ConfigToStringSlice("schedule", d)
res.TenantRestriction = d.Get("tenant_restriction").(string)
res.ThreatCategories = ConfigToStringSlice("threat_categories", d)
res.WarnTtl = d.Get("warn_ttl").(int)
return res
}
func parseUrlFilteringRule(resp []byte) (*UrlFilteringRule, error) {
pg := &UrlFilteringRule{}
err := json.Unmarshal(resp, pg)
if err != nil {
return nil, fmt.Errorf("could not parse url filtering rule response: %v", err)
}
return pg, nil
}
func CreateUrlFilteringRule(ctx context.Context, c *Client, rg *UrlFilteringRule) (*UrlFilteringRule, error) {
rgUrl := fmt.Sprintf("%s/%s", c.BaseURL, urlFilteringRulesEndpoint)
body, err := json.Marshal(rg)
if err != nil {
return nil, fmt.Errorf("could not convert url filtering rule to json: %v", err)
}
resp, err := c.Post(ctx, rgUrl, body)
if err != nil {
return nil, err
}
return parseUrlFilteringRule(resp)
}
func UpdateUrlFilteringRule(ctx context.Context, c *Client, rgID string, rg *UrlFilteringRule) (*UrlFilteringRule, error) {
rgUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, urlFilteringRulesEndpoint, rgID)
body, err := json.Marshal(rg)
if err != nil {
return nil, fmt.Errorf("could not convert url filtering rule to json: %v", err)
}
resp, err := c.Patch(ctx, rgUrl, body)
if err != nil {
return nil, err
}
return parseUrlFilteringRule(resp)
}
func GetUrlFilteringRule(ctx context.Context, c *Client, rgID string) (*UrlFilteringRule, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, urlFilteringRulesEndpoint, rgID)
resp, err := c.Get(ctx, url, u.Values{"expand": {"true"}})
if err != nil {
return nil, err
}
return parseUrlFilteringRule(resp)
}
func DeleteUrlFilteringRule(ctx context.Context, c *Client, pgID string) (*UrlFilteringRule, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, urlFilteringRulesEndpoint, pgID)
resp, err := c.Delete(ctx, url, nil)
if err != nil {
return nil, err
}
return parseUrlFilteringRule(resp)
}