generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 55
/
provider_config.go
169 lines (128 loc) · 4.3 KB
/
provider_config.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
package googleworkspace
import (
"context"
"log"
"net/http"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"golang.org/x/oauth2"
googleoauth "golang.org/x/oauth2/google"
"google.golang.org/api/chromepolicy/v1"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
directory "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/groupssettings/v1"
)
type apiClient struct {
client *http.Client
ClientScopes []string
Credentials string
Customer string
ImpersonatedUserEmail string
UserAgent string
}
func (c *apiClient) loadAndValidate(ctx context.Context) diag.Diagnostics {
var diags diag.Diagnostics
if len(c.ClientScopes) == 0 {
c.ClientScopes = DefaultClientScopes
}
if c.Credentials != "" {
contents, _, err := pathOrContents(c.Credentials)
if err != nil {
return diag.FromErr(err)
}
jwtConfig, err := googleoauth.JWTConfigFromJSON([]byte(contents), c.ClientScopes...)
if err != nil {
return diag.FromErr(err)
}
jwtConfig.Subject = c.ImpersonatedUserEmail
cleanCtx := context.WithValue(ctx, oauth2.HTTPClient, cleanhttp.DefaultClient())
// 1. OAUTH2 TRANSPORT/CLIENT - sets up proper auth headers
client := jwtConfig.Client(cleanCtx)
// 2. Logging Transport - ensure we log HTTP requests to admin APIs.
loggingTransport := logging.NewTransport("Google Workspace", client.Transport)
// Set final transport value.
client.Transport = loggingTransport
c.client = client
}
return diags
}
func (c *apiClient) NewChromePolicyService() (*chromepolicy.Service, diag.Diagnostics) {
var diags diag.Diagnostics
log.Printf("[INFO] Instantiating Google Admin Chrome Policy service")
chromePolicyService, err := chromepolicy.NewService(context.Background(), option.WithHTTPClient(c.client))
if err != nil {
return nil, diag.FromErr(err)
}
if chromePolicyService == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Directory Service could not be created.",
})
return nil, diags
}
return chromePolicyService, diags
}
func (c *apiClient) NewDirectoryService() (*directory.Service, diag.Diagnostics) {
var diags diag.Diagnostics
log.Printf("[INFO] Instantiating Google Admin Directory service")
directoryService, err := directory.NewService(context.Background(), option.WithHTTPClient(c.client))
if err != nil {
return nil, diag.FromErr(err)
}
if directoryService == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Directory Service could not be created.",
})
return nil, diags
}
return directoryService, diags
}
func (c *apiClient) NewGmailService(ctx context.Context, userId string) (*gmail.Service, diag.Diagnostics) {
var diags diag.Diagnostics
log.Printf("[INFO] Instantiating Google Admin Gmail service")
// the send-as-alias resource requires the oauth token impersonate the user
// the alias is being created for.
log.Printf("[INFO] Creating Google Admin Gmail client that impersonates %q", userId)
newClient := &apiClient{
Credentials: c.Credentials,
ClientScopes: c.ClientScopes,
Customer: c.Customer,
UserAgent: c.UserAgent,
ImpersonatedUserEmail: userId,
}
diags = newClient.loadAndValidate(ctx)
if diags.HasError() {
return nil, diags
}
gmailService, err := gmail.NewService(ctx, option.WithHTTPClient(newClient.client))
if err != nil {
return nil, diag.FromErr(err)
}
if gmailService == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Gmail Service could not be created.",
})
return nil, diags
}
return gmailService, diags
}
func (c *apiClient) NewGroupsSettingsService() (*groupssettings.Service, diag.Diagnostics) {
var diags diag.Diagnostics
log.Printf("[INFO] Instantiating Google Admin Groups Settings service")
groupsSettingsService, err := groupssettings.NewService(context.Background(), option.WithHTTPClient(c.client))
if err != nil {
return nil, diag.FromErr(err)
}
if groupsSettingsService == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Groups Settings Service could not be created.",
})
return nil, diags
}
return groupsSettingsService, diags
}