Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/devtooling 831 #1269

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 +32,8 @@ func getAllContacts(ctx context.Context, clientConfig *platformclientv2.Configur
}

for _, contact := range contacts {
//id := createCustomContactId(*contact.ContactListId, *contact.Id)
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 @@ -60,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 @@ -73,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 @@ -100,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 @@ -111,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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

return fmt.Sprintf("%s:%s", contactListId, contactId)
}

func splitComplexContact(complexContact string) (string, string) {
if strings.Contains(complexContact, ":") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add delimiter as a constant, in the package so that test cases also would refer the same everywhere.

split := strings.SplitN(complexContact, ":", 2)
return split[0], split[1]
}
return "", complexContact
}
Loading