Skip to content

Commit bb0db6a

Browse files
Add Essentials Contacts resource (#4469) (#2943)
Co-authored-by: upodroid <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: upodroid <[email protected]>
1 parent 02f4412 commit bb0db6a

10 files changed

+704
-2
lines changed

.changelog/4469.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
`google_essential_contacts_contact`
3+
```

google-beta/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type Config struct {
115115
DeploymentManagerBasePath string
116116
DialogflowBasePath string
117117
DNSBasePath string
118+
EssentialContactsBasePath string
118119
FilestoreBasePath string
119120
FirebaseBasePath string
120121
FirestoreBasePath string
@@ -204,6 +205,7 @@ var DatastoreDefaultBasePath = "https://datastore.googleapis.com/v1/"
204205
var DeploymentManagerDefaultBasePath = "https://www.googleapis.com/deploymentmanager/v2/"
205206
var DialogflowDefaultBasePath = "https://dialogflow.googleapis.com/v2/"
206207
var DNSDefaultBasePath = "https://dns.googleapis.com/dns/v1beta2/"
208+
var EssentialContactsDefaultBasePath = "https://essentialcontacts.googleapis.com/v1beta1/"
207209
var FilestoreDefaultBasePath = "https://file.googleapis.com/v1beta1/"
208210
var FirebaseDefaultBasePath = "https://firebase.googleapis.com/v1beta1/"
209211
var FirestoreDefaultBasePath = "https://firestore.googleapis.com/v1/"
@@ -1005,6 +1007,7 @@ func ConfigureBasePaths(c *Config) {
10051007
c.DeploymentManagerBasePath = DeploymentManagerDefaultBasePath
10061008
c.DialogflowBasePath = DialogflowDefaultBasePath
10071009
c.DNSBasePath = DNSDefaultBasePath
1010+
c.EssentialContactsBasePath = EssentialContactsDefaultBasePath
10081011
c.FilestoreBasePath = FilestoreDefaultBasePath
10091012
c.FirebaseBasePath = FirebaseDefaultBasePath
10101013
c.FirestoreBasePath = FirestoreDefaultBasePath

google-beta/provider.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,14 @@ func Provider() *schema.Provider {
391391
"GOOGLE_DNS_CUSTOM_ENDPOINT",
392392
}, DNSDefaultBasePath),
393393
},
394+
"essential_contacts_custom_endpoint": {
395+
Type: schema.TypeString,
396+
Optional: true,
397+
ValidateFunc: validateCustomEndpoint,
398+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
399+
"GOOGLE_ESSENTIAL_CONTACTS_CUSTOM_ENDPOINT",
400+
}, EssentialContactsDefaultBasePath),
401+
},
394402
"filestore_custom_endpoint": {
395403
Type: schema.TypeString,
396404
Optional: true,
@@ -799,9 +807,9 @@ func Provider() *schema.Provider {
799807
return provider
800808
}
801809

802-
// Generated resources: 208
810+
// Generated resources: 209
803811
// Generated IAM resources: 108
804-
// Total generated resources: 316
812+
// Total generated resources: 317
805813
func ResourceMap() map[string]*schema.Resource {
806814
resourceMap, _ := ResourceMapWithErrors()
807815
return resourceMap
@@ -994,6 +1002,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
9941002
"google_dialogflow_entity_type": resourceDialogflowEntityType(),
9951003
"google_dns_managed_zone": resourceDNSManagedZone(),
9961004
"google_dns_policy": resourceDNSPolicy(),
1005+
"google_essential_contacts_contact": resourceEssentialContactsContact(),
9971006
"google_filestore_instance": resourceFilestoreInstance(),
9981007
"google_firebase_project": resourceFirebaseProject(),
9991008
"google_firebase_project_location": resourceFirebaseProjectLocation(),
@@ -1347,6 +1356,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
13471356
config.DeploymentManagerBasePath = d.Get("deployment_manager_custom_endpoint").(string)
13481357
config.DialogflowBasePath = d.Get("dialogflow_custom_endpoint").(string)
13491358
config.DNSBasePath = d.Get("dns_custom_endpoint").(string)
1359+
config.EssentialContactsBasePath = d.Get("essential_contacts_custom_endpoint").(string)
13501360
config.FilestoreBasePath = d.Get("filestore_custom_endpoint").(string)
13511361
config.FirebaseBasePath = d.Get("firebase_custom_endpoint").(string)
13521362
config.FirestoreBasePath = d.Get("firestore_custom_endpoint").(string)
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Magic Modules and manual
8+
// changes will be clobbered when the file is regenerated.
9+
//
10+
// Please read more about how to change this file in
11+
// .github/CONTRIBUTING.md.
12+
//
13+
// ----------------------------------------------------------------------------
14+
15+
package google
16+
17+
import (
18+
"fmt"
19+
"log"
20+
"reflect"
21+
"strings"
22+
"time"
23+
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25+
)
26+
27+
func resourceEssentialContactsContact() *schema.Resource {
28+
return &schema.Resource{
29+
Create: resourceEssentialContactsContactCreate,
30+
Read: resourceEssentialContactsContactRead,
31+
Update: resourceEssentialContactsContactUpdate,
32+
Delete: resourceEssentialContactsContactDelete,
33+
34+
Importer: &schema.ResourceImporter{
35+
State: resourceEssentialContactsContactImport,
36+
},
37+
38+
Timeouts: &schema.ResourceTimeout{
39+
Create: schema.DefaultTimeout(4 * time.Minute),
40+
Update: schema.DefaultTimeout(4 * time.Minute),
41+
Delete: schema.DefaultTimeout(4 * time.Minute),
42+
},
43+
44+
Schema: map[string]*schema.Schema{
45+
"email": {
46+
Type: schema.TypeString,
47+
Required: true,
48+
Description: `The email address to send notifications to. This does not need to be a Google account.`,
49+
},
50+
"notification_category_subscriptions": {
51+
Type: schema.TypeList,
52+
Required: true,
53+
Description: `The categories of notifications that the contact will receive communications for.`,
54+
Elem: &schema.Schema{
55+
Type: schema.TypeString,
56+
},
57+
},
58+
"parent": {
59+
Type: schema.TypeString,
60+
Required: true,
61+
ForceNew: true,
62+
Description: `The resource to save this contact for. Format: organizations/{organization_id}, folders/{folder_id} or projects/{project_id}`,
63+
},
64+
"language_tag": {
65+
Type: schema.TypeString,
66+
Optional: true,
67+
Description: `The preferred language for notifications, as a ISO 639-1 language code. See Supported languages for a list of supported languages.`,
68+
},
69+
"name": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: `The identifier for the contact. Format: {resourceType}/{resource_id}/contacts/{contact_id}`,
73+
},
74+
},
75+
UseJSONNumber: true,
76+
}
77+
}
78+
79+
func resourceEssentialContactsContactCreate(d *schema.ResourceData, meta interface{}) error {
80+
config := meta.(*Config)
81+
userAgent, err := generateUserAgentString(d, config.userAgent)
82+
if err != nil {
83+
return err
84+
}
85+
86+
obj := make(map[string]interface{})
87+
emailProp, err := expandEssentialContactsContactEmail(d.Get("email"), d, config)
88+
if err != nil {
89+
return err
90+
} else if v, ok := d.GetOkExists("email"); !isEmptyValue(reflect.ValueOf(emailProp)) && (ok || !reflect.DeepEqual(v, emailProp)) {
91+
obj["email"] = emailProp
92+
}
93+
notificationCategorySubscriptionsProp, err := expandEssentialContactsContactNotificationCategorySubscriptions(d.Get("notification_category_subscriptions"), d, config)
94+
if err != nil {
95+
return err
96+
} else if v, ok := d.GetOkExists("notification_category_subscriptions"); !isEmptyValue(reflect.ValueOf(notificationCategorySubscriptionsProp)) && (ok || !reflect.DeepEqual(v, notificationCategorySubscriptionsProp)) {
97+
obj["notificationCategorySubscriptions"] = notificationCategorySubscriptionsProp
98+
}
99+
languageTagProp, err := expandEssentialContactsContactLanguageTag(d.Get("language_tag"), d, config)
100+
if err != nil {
101+
return err
102+
} else if v, ok := d.GetOkExists("language_tag"); !isEmptyValue(reflect.ValueOf(languageTagProp)) && (ok || !reflect.DeepEqual(v, languageTagProp)) {
103+
obj["languageTag"] = languageTagProp
104+
}
105+
106+
url, err := replaceVars(d, config, "{{EssentialContactsBasePath}}{{parent}}/contacts")
107+
if err != nil {
108+
return err
109+
}
110+
111+
log.Printf("[DEBUG] Creating new Contact: %#v", obj)
112+
billingProject := ""
113+
114+
// err == nil indicates that the billing_project value was found
115+
if bp, err := getBillingProject(d, config); err == nil {
116+
billingProject = bp
117+
}
118+
119+
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
120+
if err != nil {
121+
return fmt.Errorf("Error creating Contact: %s", err)
122+
}
123+
if err := d.Set("name", flattenEssentialContactsContactName(res["name"], d, config)); err != nil {
124+
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
125+
}
126+
127+
// Store the ID now
128+
id, err := replaceVars(d, config, "{{name}}")
129+
if err != nil {
130+
return fmt.Errorf("Error constructing id: %s", err)
131+
}
132+
d.SetId(id)
133+
134+
log.Printf("[DEBUG] Finished creating Contact %q: %#v", d.Id(), res)
135+
136+
return resourceEssentialContactsContactRead(d, meta)
137+
}
138+
139+
func resourceEssentialContactsContactRead(d *schema.ResourceData, meta interface{}) error {
140+
config := meta.(*Config)
141+
userAgent, err := generateUserAgentString(d, config.userAgent)
142+
if err != nil {
143+
return err
144+
}
145+
146+
url, err := replaceVars(d, config, "{{EssentialContactsBasePath}}{{name}}")
147+
if err != nil {
148+
return err
149+
}
150+
151+
billingProject := ""
152+
153+
// err == nil indicates that the billing_project value was found
154+
if bp, err := getBillingProject(d, config); err == nil {
155+
billingProject = bp
156+
}
157+
158+
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
159+
if err != nil {
160+
return handleNotFoundError(err, d, fmt.Sprintf("EssentialContactsContact %q", d.Id()))
161+
}
162+
163+
if err := d.Set("name", flattenEssentialContactsContactName(res["name"], d, config)); err != nil {
164+
return fmt.Errorf("Error reading Contact: %s", err)
165+
}
166+
if err := d.Set("email", flattenEssentialContactsContactEmail(res["email"], d, config)); err != nil {
167+
return fmt.Errorf("Error reading Contact: %s", err)
168+
}
169+
if err := d.Set("notification_category_subscriptions", flattenEssentialContactsContactNotificationCategorySubscriptions(res["notificationCategorySubscriptions"], d, config)); err != nil {
170+
return fmt.Errorf("Error reading Contact: %s", err)
171+
}
172+
if err := d.Set("language_tag", flattenEssentialContactsContactLanguageTag(res["languageTag"], d, config)); err != nil {
173+
return fmt.Errorf("Error reading Contact: %s", err)
174+
}
175+
176+
return nil
177+
}
178+
179+
func resourceEssentialContactsContactUpdate(d *schema.ResourceData, meta interface{}) error {
180+
config := meta.(*Config)
181+
userAgent, err := generateUserAgentString(d, config.userAgent)
182+
if err != nil {
183+
return err
184+
}
185+
186+
billingProject := ""
187+
188+
obj := make(map[string]interface{})
189+
emailProp, err := expandEssentialContactsContactEmail(d.Get("email"), d, config)
190+
if err != nil {
191+
return err
192+
} else if v, ok := d.GetOkExists("email"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, emailProp)) {
193+
obj["email"] = emailProp
194+
}
195+
notificationCategorySubscriptionsProp, err := expandEssentialContactsContactNotificationCategorySubscriptions(d.Get("notification_category_subscriptions"), d, config)
196+
if err != nil {
197+
return err
198+
} else if v, ok := d.GetOkExists("notification_category_subscriptions"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, notificationCategorySubscriptionsProp)) {
199+
obj["notificationCategorySubscriptions"] = notificationCategorySubscriptionsProp
200+
}
201+
languageTagProp, err := expandEssentialContactsContactLanguageTag(d.Get("language_tag"), d, config)
202+
if err != nil {
203+
return err
204+
} else if v, ok := d.GetOkExists("language_tag"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, languageTagProp)) {
205+
obj["languageTag"] = languageTagProp
206+
}
207+
208+
url, err := replaceVars(d, config, "{{EssentialContactsBasePath}}{{name}}")
209+
if err != nil {
210+
return err
211+
}
212+
213+
log.Printf("[DEBUG] Updating Contact %q: %#v", d.Id(), obj)
214+
updateMask := []string{}
215+
216+
if d.HasChange("email") {
217+
updateMask = append(updateMask, "email")
218+
}
219+
220+
if d.HasChange("notification_category_subscriptions") {
221+
updateMask = append(updateMask, "notificationCategorySubscriptions")
222+
}
223+
224+
if d.HasChange("language_tag") {
225+
updateMask = append(updateMask, "languageTag")
226+
}
227+
// updateMask is a URL parameter but not present in the schema, so replaceVars
228+
// won't set it
229+
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
230+
if err != nil {
231+
return err
232+
}
233+
234+
// err == nil indicates that the billing_project value was found
235+
if bp, err := getBillingProject(d, config); err == nil {
236+
billingProject = bp
237+
}
238+
239+
res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
240+
241+
if err != nil {
242+
return fmt.Errorf("Error updating Contact %q: %s", d.Id(), err)
243+
} else {
244+
log.Printf("[DEBUG] Finished updating Contact %q: %#v", d.Id(), res)
245+
}
246+
247+
return resourceEssentialContactsContactRead(d, meta)
248+
}
249+
250+
func resourceEssentialContactsContactDelete(d *schema.ResourceData, meta interface{}) error {
251+
config := meta.(*Config)
252+
userAgent, err := generateUserAgentString(d, config.userAgent)
253+
if err != nil {
254+
return err
255+
}
256+
257+
billingProject := ""
258+
259+
url, err := replaceVars(d, config, "{{EssentialContactsBasePath}}{{name}}")
260+
if err != nil {
261+
return err
262+
}
263+
264+
var obj map[string]interface{}
265+
log.Printf("[DEBUG] Deleting Contact %q", d.Id())
266+
267+
// err == nil indicates that the billing_project value was found
268+
if bp, err := getBillingProject(d, config); err == nil {
269+
billingProject = bp
270+
}
271+
272+
res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete))
273+
if err != nil {
274+
return handleNotFoundError(err, d, "Contact")
275+
}
276+
277+
log.Printf("[DEBUG] Finished deleting Contact %q: %#v", d.Id(), res)
278+
return nil
279+
}
280+
281+
func resourceEssentialContactsContactImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
282+
config := meta.(*Config)
283+
if err := parseImportId([]string{
284+
"(?P<name>[^/]+)",
285+
}, d, config); err != nil {
286+
return nil, err
287+
}
288+
289+
// Replace import id for the resource id
290+
id, err := replaceVars(d, config, "{{name}}")
291+
if err != nil {
292+
return nil, fmt.Errorf("Error constructing id: %s", err)
293+
}
294+
d.SetId(id)
295+
296+
return []*schema.ResourceData{d}, nil
297+
}
298+
299+
func flattenEssentialContactsContactName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
300+
return v
301+
}
302+
303+
func flattenEssentialContactsContactEmail(v interface{}, d *schema.ResourceData, config *Config) interface{} {
304+
return v
305+
}
306+
307+
func flattenEssentialContactsContactNotificationCategorySubscriptions(v interface{}, d *schema.ResourceData, config *Config) interface{} {
308+
return v
309+
}
310+
311+
func flattenEssentialContactsContactLanguageTag(v interface{}, d *schema.ResourceData, config *Config) interface{} {
312+
return v
313+
}
314+
315+
func expandEssentialContactsContactEmail(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
316+
return v, nil
317+
}
318+
319+
func expandEssentialContactsContactNotificationCategorySubscriptions(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
320+
return v, nil
321+
}
322+
323+
func expandEssentialContactsContactLanguageTag(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
324+
return v, nil
325+
}

0 commit comments

Comments
 (0)