generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
user_settings.go
128 lines (119 loc) · 3.81 KB
/
user_settings.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
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strconv"
)
const userSettingsEndpoint = "v1/settings/user"
type UserSettings struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
ApplyOnOrg bool `json:"apply_on_org"`
ApplyToEntities []string `json:"apply_to_entities"`
AllowedFactors []string `json:"allowed_factors,omitempty"`
MaxDevicesPerUser *int `json:"max_devices_per_user,omitempty"`
MfaRequired *bool `json:"mfa_required,omitempty"`
PasswordExpiration *int `json:"password_expiration,omitempty"`
ProhibitedOs []string `json:"prohibited_os,omitempty"`
ProxyPops *string `json:"proxy_pops,omitempty"`
SsoMandatory *bool `json:"sso_mandatory,omitempty"`
}
func NewUserSettings(d *schema.ResourceData) *UserSettings {
res := &UserSettings{}
if d.HasChange("name") {
res.Name = d.Get("name").(string)
}
res.Description = d.Get("description").(string)
res.Enabled = d.Get("enabled").(bool)
res.ApplyOnOrg = d.Get("apply_on_org").(bool)
res.ApplyToEntities = ConfigToStringSlice("apply_to_entities", d)
_, exists := d.GetOk("allowed_factors")
if exists {
res.AllowedFactors = ConfigToStringSlice("allowed_factors", d)
}
mdpu := d.Get("max_devices_per_user")
exists = mdpu.(string) != ""
if exists {
maxDevicesPerUser, _ := strconv.Atoi(mdpu.(string))
res.MaxDevicesPerUser = &maxDevicesPerUser
} else {
res.MaxDevicesPerUser = nil
}
mfar, exists := d.GetOkExists("mfa_required")
if exists {
mfaRequired := mfar.(bool)
res.MfaRequired = &mfaRequired
}
pw, exists := d.GetOk("password_expiration")
if exists {
passwordExpiration := pw.(int)
res.PasswordExpiration = &passwordExpiration
}
_, exists = d.GetOk("prohibited_os")
if exists {
res.ProhibitedOs = ConfigToStringSlice("prohibited_os", d)
}
pp, exists := d.GetOk("proxy_pops")
if exists {
proxyPops := pp.(string)
res.ProxyPops = &proxyPops
}
ssoM, exists := d.GetOkExists("sso_mandatory")
if exists {
ssoMandatory := ssoM.(bool)
res.SsoMandatory = &ssoMandatory
}
return res
}
func parseUserSettings(resp []byte) (*UserSettings, error) {
ds := &UserSettings{}
err := json.Unmarshal(resp, ds)
if err != nil {
return nil, fmt.Errorf("could not parse user settings response: %v", err)
}
return ds, nil
}
func CreateUserSettings(ctx context.Context, c *Client, ds *UserSettings) (*UserSettings, error) {
url := fmt.Sprintf("%s/%s", c.BaseURL, userSettingsEndpoint)
body, err := json.Marshal(ds)
if err != nil {
return nil, fmt.Errorf("could not convert user settings to json: %v", err)
}
resp, err := c.Post(ctx, url, body)
if err != nil {
return nil, err
}
return parseUserSettings(resp)
}
func UpdateUserSettings(ctx context.Context, c *Client, dsID string, ds *UserSettings) (*UserSettings, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, userSettingsEndpoint, dsID)
body, err := json.Marshal(ds)
if err != nil {
return nil, fmt.Errorf("could not convert user settings to json: %v", err)
}
resp, err := c.Patch(ctx, url, body)
if err != nil {
return nil, err
}
return parseUserSettings(resp)
}
func GetUserSettings(ctx context.Context, c *Client, dsID string) (*UserSettings, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, userSettingsEndpoint, dsID)
resp, err := c.Get(ctx, url, nil)
if err != nil {
return nil, err
}
return parseUserSettings(resp)
}
func DeleteUserSettings(ctx context.Context, c *Client, dsID string) (*UserSettings, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, userSettingsEndpoint, dsID)
resp, err := c.Delete(ctx, url, nil)
if err != nil {
return nil, err
}
return parseUserSettings(resp)
}