Skip to content

Commit

Permalink
Bug/devtooling 831 (#1269)
Browse files Browse the repository at this point in the history
* introduced contactListId and contactId for kv pair

* added complex contact ID

* removed redundant assignment
  • Loading branch information
kavinbalagen authored Oct 1, 2024
1 parent 72cf71b commit 6957981
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func createContactFn(_ context.Context, p *contactProxy, contactListId string, c
}

func readContactByIdFn(_ context.Context, p *contactProxy, contactListId, contactId string) (*platformclientv2.Dialercontact, *platformclientv2.APIResponse, error) {
if contact := rc.GetCacheItem(p.contactCache, contactId); contact != nil {
if contact := rc.GetCacheItem(p.contactCache, createComplexContact(contactListId, contactId)); contact != nil {
return contact, nil, nil
}
return p.outboundApi.GetOutboundContactlistContact(contactListId, contactId)
Expand All @@ -90,6 +90,7 @@ func deleteContactFn(_ context.Context, p *contactProxy, contactListId, contactI

func getAllContactsFn(ctx context.Context, p *contactProxy) ([]platformclientv2.Dialercontact, *platformclientv2.APIResponse, error) {
var allContacts []platformclientv2.Dialercontact
contactMatrix := make(map[string][]platformclientv2.Dialercontact)

contactListIds, resp, err := p.getAllContactListIds(ctx)
if err != nil {
Expand All @@ -102,10 +103,14 @@ func getAllContactsFn(ctx context.Context, p *contactProxy) ([]platformclientv2.
return nil, resp, err
}
allContacts = append(allContacts, contacts...)
contactMatrix[contactListId] = contacts
}

for _, contact := range allContacts {
rc.SetCache(p.contactCache, *contact.Id, contact)
for contactListId, contactList := range contactMatrix {
for _, contact := range contactList {
rc.SetCache(p.contactCache, createComplexContact(contactListId, *contact.Id), contact)

}
}

return allContacts, nil, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func getAllContacts(ctx context.Context, clientConfig *platformclientv2.Configur
}

for _, contact := range contacts {
resources[*contact.Id] = &resourceExporter.ResourceMeta{Name: *contact.Id}
id := createComplexContact(*contact.ContactListId, *contact.Id)
resources[id] = &resourceExporter.ResourceMeta{Name: id}
}

return resources, nil
Expand All @@ -59,9 +60,9 @@ func createOutboundContactListContact(ctx context.Context, d *schema.ResourceDat
msg := fmt.Sprintf("expected to receive one dialer contact object in contact creation response. Received %v", len(contactResponseBody))
return util.BuildDiagnosticError(resourceName, msg, fmt.Errorf("%v", msg))
}

d.SetId(*contactResponseBody[0].Id)
log.Printf("Finished creating contact '%s' in contact list '%s'", d.Id(), contactListId)
id := createComplexContact(contactListId, *contactResponseBody[0].Id)
d.SetId(id)
log.Printf("Finished creating contact '%s' in contact list '%s'", *contactResponseBody[0].Id, contactListId)
return readOutboundContactListContact(ctx, d, meta)
}

Expand All @@ -72,17 +73,20 @@ func readOutboundContactListContact(ctx context.Context, d *schema.ResourceData,

sdkConfig = meta.(*provider.ProviderMeta).ClientConfig
cp = getContactProxy(sdkConfig)
)

contactListId, contactId := splitComplexContact(d.Id())
if contactListId == "" {
contactListId = d.Get("contact_list_id").(string)
)
}

cc := consistency_checker.NewConsistencyCheck(ctx, d, meta, ResourceOutboundContactListContact(), constants.DefaultConsistencyChecks, resourceName)

retryErr := util.WithRetriesForRead(ctx, d, func() *retry.RetryError {
var contactResponseBody *platformclientv2.Dialercontact

log.Printf("Reading contact '%s' in contact list '%s'", d.Id(), contactListId)
contactResponseBody, resp, err = cp.readContactById(ctx, contactListId, d.Id())
log.Printf("Reading contact '%s' in contact list '%s'", contactId, contactListId)
contactResponseBody, resp, err = cp.readContactById(ctx, contactListId, contactId)
if err != nil {
if util.IsStatus404(resp) {
return retry.RetryableError(err)
Expand All @@ -99,9 +103,9 @@ func readOutboundContactListContact(ctx context.Context, d *schema.ResourceData,
return cc.CheckState(d)
})
if retryErr != nil {
return util.BuildAPIDiagnosticError(resourceName, fmt.Sprintf("failed to read contact by ID '%s' from contact list '%s'. Error: %v", d.Id(), contactListId, retryErr), resp)
return util.BuildAPIDiagnosticError(resourceName, fmt.Sprintf("failed to read contact by ID '%s' from contact list '%s'. Error: %v", d.Id(), contactListId, contactId), resp)
}
log.Printf("Done reading contact '%s' in contact list '%s'", d.Id(), contactListId)
log.Printf("Done reading contact '%s' in contact list '%s'", contactId, contactListId)
return nil
}

Expand All @@ -110,44 +114,50 @@ func updateOutboundContactListContact(ctx context.Context, d *schema.ResourceDat
cp := getContactProxy(sdkConfig)

contactRequestBody := buildDialerContactFromResourceData(d)
contactListId := *contactRequestBody.ContactListId
contactListId, contactId := splitComplexContact(d.Id())
if contactListId == "" {
contactListId = d.Get("contact_list_id").(string)
}

log.Printf("Updating contact '%s' in contact list '%s'", d.Id(), contactListId)
_, resp, err := cp.updateContact(ctx, contactListId, d.Id(), contactRequestBody)
log.Printf("Updating contact '%s' in contact list '%s'", contactId, contactListId)
_, resp, err := cp.updateContact(ctx, contactListId, contactId, contactRequestBody)
if err != nil {
msg := fmt.Sprintf("failed to update contact '%s' for contact list '%s'. Error: %v", d.Id(), contactListId, err)
msg := fmt.Sprintf("failed to update contact '%s' for contact list '%s'. Error: %v", contactId, contactListId, err)
return util.BuildAPIDiagnosticError(resourceName, msg, resp)
}

log.Printf("Finished updating contact '%s' in contact list '%s'", d.Id(), contactListId)
log.Printf("Finished updating contact '%s' in contact list '%s'", contactId, contactListId)
return readOutboundContactListContact(ctx, d, meta)
}

func deleteOutboundContactListContact(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
cp := getContactProxy(sdkConfig)

contactListId := d.Get("contact_list_id").(string)
contactListId, contactId := splitComplexContact(d.Id())
if contactListId == "" {
contactListId = d.Get("contact_list_id").(string)
}

log.Printf("Deleting contact '%s' from contact list '%s'", d.Id(), contactListId)
resp, err := cp.deleteContact(ctx, contactListId, d.Id())
log.Printf("Deleting contact '%s' from contact list '%s'", contactId, contactListId)
resp, err := cp.deleteContact(ctx, contactListId, contactId)
if err != nil {
msg := fmt.Sprintf("failed to delete contact '%s' from contact list '%s'. Error: %v", d.Id(), contactListId, err)
msg := fmt.Sprintf("failed to delete contact '%s' from contact list '%s'. Error: %v", contactId, contactListId, err)
return util.BuildAPIDiagnosticError(resourceName, msg, resp)
}

return util.WithRetries(ctx, 60*time.Second, func() *retry.RetryError {
log.Printf("Reading contact '%s'", d.Id())
_, resp, err := cp.readContactById(ctx, contactListId, d.Id())
_, resp, err := cp.readContactById(ctx, contactListId, contactId)
if err != nil {
if util.IsStatus404(resp) {
log.Printf("Contact '%s' deleted", d.Id())
log.Printf("Contact '%s' deleted", contactId)
return nil
}
msg := fmt.Sprintf("failed to delete contact '%s'. Error: %v", d.Id(), err)
msg := fmt.Sprintf("failed to delete contact '%s'. Error: %v", contactId, err)
return retry.NonRetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, msg, resp))
}
msg := fmt.Sprintf("contact '%s' still exists in contact list '%s'", d.Id(), contactListId)
msg := fmt.Sprintf("contact '%s' still exists in contact list '%s'", contactId, contactListId)
return retry.RetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, msg, resp))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,15 @@ func GenerateColumnStatus(column, contactable string) string {
contactable = %s
}`, column, contactable)
}

func createComplexContact(contactListId, contactId string) string {
return fmt.Sprintf("%s:%s", contactListId, contactId)
}

func splitComplexContact(complexContact string) (string, string) {
if strings.Contains(complexContact, ":") {
split := strings.SplitN(complexContact, ":", 2)
return split[0], split[1]
}
return "", complexContact
}

0 comments on commit 6957981

Please sign in to comment.