-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_workspace_email_gateway.go
222 lines (178 loc) · 5.65 KB
/
resource_workspace_email_gateway.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
220
221
222
package provider
import (
"context"
"encoding/xml"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"io"
"log"
"net/http"
"strings"
)
// Google Workspace Admin SDK -> Admin Settings API
const apiEndPoint string = "https://apps-apis.google.com/a/feeds/domain/2.0/%s/email/gateway"
func resourceWorkspaceEmailGateway() *schema.Resource {
return &schema.Resource{
Description: "The outbound email gateway provides outbound routing of mail from users in your domain.",
CreateContext: resourceGatewayCreate,
ReadContext: resourceWorkspaceRead,
DeleteContext: resourceGatewayDelete,
Schema: map[string]*schema.Schema{
"domain_name": {
Description: "Your Google Workspace domain name.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"smart_host": {
Description: "Either the IP address or hostname of your SMTP server. Google Workspace routes outgoing mail to this server.",
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"smtp_mode": {
Description: "The default value is SMTP. Another value, SMTP_TLS, secures a connection with TLS when delivering the message.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"response": {
Description: "The default value is SMTP. Another value, SMTP_TLS, secures a connection with TLS when delivering the message.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
type Property struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
type Response struct {
XMLName xml.Name `xml:"entry"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Apps string `xml:"apps,attr"`
Property []struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
} `xml:"property"`
}
type Request struct {
XMLName xml.Name `xml:"atom:entry"`
Text string `xml:",chardata"`
Atom string `xml:"xmlns:atom,attr"`
Apps string `xml:"xmlns:apps,attr"`
Property []Property //`xml:"property"`
}
func resourceGatewayCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
domainName := d.Get("domain_name").(string)
d.SetId(domainName)
c := meta.(*apiClient)
// TODO create XML and output it as a string
p1 := Property{
Name: "smartHost",
Value: d.Get("smart_host").(string),
}
p2 := Property{
Name: "smtpMode",
Value: d.Get("smtp_mode").(string),
}
p := []Property{p1, p2}
r := Request{
Atom: "http://www.w3.org/2005/Atom",
Apps: "http://schemas.google.com/apps/2006",
Property: p,
}
bXml, err := xml.Marshal(r)
if err != nil {
fmt.Printf("Marshaling error: %v\n", err)
return nil
}
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf(apiEndPoint, domainName), strings.NewReader(string(bXml)))
if err != nil {
return diag.FromErr(err)
}
resp, err := c.client.Do(req)
if err != nil {
return diag.FromErr(err)
}
if resp.StatusCode != http.StatusOK {
return diag.Errorf("Error in HTTP response. Status code: %v, Status: %v. Full response: %#v", resp.StatusCode, resp.Status, resp)
}
// TODO no error, process the response
return diag.Diagnostics{}
}
func resourceWorkspaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
domainName := d.Get("domain_name").(string)
d.SetId(domainName)
c := meta.(*apiClient)
// Send an HTTP request to this legacy API and process the response
resp, err := c.client.Get(fmt.Sprintf(apiEndPoint, domainName))
if err != nil {
return diag.FromErr(err)
}
if resp.StatusCode != http.StatusOK {
return diag.Errorf("Error in HTTP response. Status code: %v, Status: %v. Full response: %#v", resp.StatusCode, resp.Status, resp)
}
// TODO: trim the XML parser struct to only have relevant fields and remove the unnecessary declarations
body, err := io.ReadAll(resp.Body)
if err != nil {
return diag.FromErr(err)
}
respRead := Response{}
if err := xml.Unmarshal([]byte(body), &respRead); err != nil {
fmt.Printf("Unmarshaling error: %v\n", err)
return diag.FromErr(err)
}
if err := d.Set("smart_host", respRead.Property[0].Value); err != nil {
return diag.FromErr(err)
}
if err := d.Set("smtp_mode", respRead.Property[1].Value); err != nil {
return diag.FromErr(err)
}
return diag.Diagnostics{}
}
func resourceGatewayDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
domainName := d.Get("domain_name").(string)
d.SetId(domainName)
log.Printf("[DEBUG] Deleting Domain %q: %#v", d.Id(), domainName)
c := meta.(*apiClient)
// TODO create XML and output it as a string
p1 := Property{
Name: "smartHost",
Value: "",
}
p2 := Property{
Name: "smtpMode",
Value: "",
}
p := []Property{p1, p2}
r := Request{
Atom: "http://www.w3.org/2005/Atom",
Apps: "http://schemas.google.com/apps/2006",
Property: p,
}
bXml, err := xml.Marshal(r)
if err != nil {
fmt.Printf("Marshaling error: %v\n", err)
return nil
}
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf(apiEndPoint, domainName), strings.NewReader(string(bXml)))
if err != nil {
return diag.FromErr(err)
}
resp, err := c.client.Do(req)
if err != nil {
return diag.FromErr(err)
}
if resp.StatusCode != http.StatusOK {
return diag.Errorf("Error in HTTP response. Status code: %v, Status: %v. Full response: %#v", resp.StatusCode, resp.Status, resp)
}
log.Printf("[DEBUG] Finished deleting Domain %q: %#v", d.Id(), domainName)
return diag.Diagnostics{}
}