|
| 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