generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 56
/
resource_domain.go
193 lines (154 loc) · 5.16 KB
/
resource_domain.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
package googleworkspace
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
directory "google.golang.org/api/admin/directory/v1"
)
func resourceDomain() *schema.Resource {
return &schema.Resource{
// This description is used by the documentation generator and the language server.
Description: "Domain resource manages Google Workspace Domains. Domain resides under the " +
"`https://www.googleapis.com/auth/admin.directory.domain` client scope.",
CreateContext: resourceDomainCreate,
ReadContext: resourceDomainRead,
DeleteContext: resourceDomainDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"domain_aliases": {
Description: "asps.list of domain alias objects.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"verified": {
Description: "Indicates the verification state of a domain.",
Type: schema.TypeBool,
Computed: true,
},
"etag": {
Description: "ETag of the resource.",
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Description: "Creation time of the domain. Expressed in Unix time format.",
Type: schema.TypeInt,
Computed: true,
},
"is_primary": {
Description: "Indicates if the domain is a primary domain.",
Type: schema.TypeBool,
Computed: true,
},
"domain_name": {
Description: "The domain name of the customer.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
// Adding a computed id simply to override the `optional` id that gets added in the SDK
// that will then display improperly in the docs
"id": {
Description: "The ID of this resource.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceDomainCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
// use the meta value to retrieve your client from the provider configure method
client := meta.(*apiClient)
domainName := d.Get("domain_name").(string)
log.Printf("[DEBUG] Creating Domain %q: %#v", d.Id(), domainName)
directoryService, diags := client.NewDirectoryService()
if diags.HasError() {
return diags
}
domainsService, diags := GetDomainsService(directoryService)
if diags.HasError() {
return diags
}
domainObj := directory.Domains{
DomainName: d.Get("domain_name").(string),
}
domain, err := domainsService.Insert(client.Customer, &domainObj).Do()
if err != nil {
return diag.FromErr(err)
}
// Use the domain name as the ID, as it should be unique
d.SetId(domain.DomainName)
log.Printf("[DEBUG] Finished creating Domain %q: %#v", d.Id(), domainName)
return resourceDomainRead(ctx, d, meta)
}
func resourceDomainRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
// use the meta value to retrieve your client from the provider configure method
client := meta.(*apiClient)
directoryService, diags := client.NewDirectoryService()
if diags.HasError() {
return diags
}
domainsService, diags := GetDomainsService(directoryService)
if diags.HasError() {
return diags
}
log.Printf("[DEBUG] Getting Domain %q: %#v", d.Id(), d.Id())
domain, err := domainsService.Get(client.Customer, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, d.Id())
}
if domain == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("No domain was returned for %s.", d.Get("domain_name").(string)),
})
return diags
}
if err := d.Set("domain_aliases", flattenDomainAliases(domain.DomainAliases, d)); err != nil {
return diag.FromErr(err)
}
d.Set("verified", domain.Verified)
d.Set("creation_time", domain.CreationTime)
d.Set("is_primary", domain.IsPrimary)
d.Set("domain_name", domain.DomainName)
d.SetId(domain.DomainName)
log.Printf("[DEBUG] Finished getting Domain %q: %#v", d.Id(), domain.DomainName)
return diags
}
func resourceDomainDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
// use the meta value to retrieve your client from the provider configure method
client := meta.(*apiClient)
domainName := d.Get("domain_name").(string)
log.Printf("[DEBUG] Deleting Domain %q: %#v", d.Id(), domainName)
directoryService, diags := client.NewDirectoryService()
if diags.HasError() {
return diags
}
domainsService, diags := GetDomainsService(directoryService)
if diags.HasError() {
return diags
}
err := domainsService.Delete(client.Customer, domainName).Do()
if err != nil {
return handleNotFoundError(err, d, domainName)
}
log.Printf("[DEBUG] Finished deleting Domain %q: %#v", d.Id(), domainName)
return diags
}
func flattenDomainAliases(domainAliases []*directory.DomainAlias, d *schema.ResourceData) interface{} {
var v []string
for _, domainAlias := range domainAliases {
v = append(v, domainAlias.DomainAliasName)
}
return v
}