forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_setting_smtp.go
116 lines (95 loc) · 3.23 KB
/
admin_setting_smtp.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
)
// Compile-time proof of interface implementation.
var _ SMTPSettings = (*adminSMTPSettings)(nil)
// SMTPSettings describes all the SMTP admin settings for the Admin Setting API https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/settings
type SMTPSettings interface {
// Read returns the SMTP settings.
Read(ctx context.Context) (*AdminSMTPSetting, error)
// Update updates SMTP settings.
Update(ctx context.Context, options AdminSMTPSettingsUpdateOptions) (*AdminSMTPSetting, error)
}
type adminSMTPSettings struct {
client *Client
}
// SMTPAuthType represents valid SMTP Auth types.
type SMTPAuthType string
// List of all SMTP auth types.
const (
SMTPAuthNone SMTPAuthType = "none"
SMTPAuthPlain SMTPAuthType = "plain"
SMTPAuthLogin SMTPAuthType = "login"
)
// AdminSMTPSetting represents a the SMTP settings in Terraform Enterprise.
type AdminSMTPSetting struct {
ID string `jsonapi:"primary,smtp-settings"`
Enabled bool `jsonapi:"attr,enabled"`
Host string `jsonapi:"attr,host"`
Port int `jsonapi:"attr,port"`
Sender string `jsonapi:"attr,sender"`
Auth SMTPAuthType `jsonapi:"attr,auth"`
Username string `jsonapi:"attr,username"`
}
// Read returns the SMTP settings.
func (a *adminSMTPSettings) Read(ctx context.Context) (*AdminSMTPSetting, error) {
req, err := a.client.NewRequest("GET", "admin/smtp-settings", nil)
if err != nil {
return nil, err
}
smtp := &AdminSMTPSetting{}
err = req.Do(ctx, smtp)
if err != nil {
return nil, err
}
return smtp, nil
}
// AdminSMTPSettingsUpdateOptions represents the admin options for updating
// SMTP settings.
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/settings#request-body-3
type AdminSMTPSettingsUpdateOptions struct {
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
Host *string `jsonapi:"attr,host,omitempty"`
Port *int `jsonapi:"attr,port,omitempty"`
Sender *string `jsonapi:"attr,sender,omitempty"`
Auth *SMTPAuthType `jsonapi:"attr,auth,omitempty"`
Username *string `jsonapi:"attr,username,omitempty"`
Password *string `jsonapi:"attr,password,omitempty"`
TestEmailAddress *string `jsonapi:"attr,test-email-address,omitempty"`
}
// Update updates the SMTP settings.
func (a *adminSMTPSettings) Update(ctx context.Context, options AdminSMTPSettingsUpdateOptions) (*AdminSMTPSetting, error) {
if err := options.valid(); err != nil {
return nil, err
}
req, err := a.client.NewRequest("PATCH", "admin/smtp-settings", &options)
if err != nil {
return nil, err
}
smtp := &AdminSMTPSetting{}
err = req.Do(ctx, smtp)
if err != nil {
return nil, err
}
return smtp, nil
}
func (o AdminSMTPSettingsUpdateOptions) valid() error {
if validString((*string)(o.Auth)) {
if err := validateAdminSettingSMTPAuth(*o.Auth); err != nil {
return err
}
}
return nil
}
func validateAdminSettingSMTPAuth(authVal SMTPAuthType) error {
switch authVal {
case SMTPAuthNone, SMTPAuthPlain, SMTPAuthLogin:
// do nothing
default:
return ErrInvalidSMTPAuth
}
return nil
}