Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nkvuong committed Nov 15, 2024
1 parent 4492533 commit 3a87eb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
42 changes: 18 additions & 24 deletions access/resource_ip_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func isRetriable(err error) *retry.RetryError {
var apiErr *apierr.APIError
if !errors.As(err, &apiErr) {
return retry.NonRetryableError(err)
}
if apiErr.StatusCode == 404 {
return retry.RetryableError(err)
} else {
return retry.NonRetryableError(err)
}
}

var ipAclTimeout = 10 * time.Minute

// ResourceIPAccessList manages IP access lists
func ResourceIPAccessList() common.Resource {
s := common.StructToSchema(settings.IpAccessListInfo{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
Expand All @@ -33,10 +47,6 @@ func ResourceIPAccessList() common.Resource {
})
return common.Resource{
Schema: s,
CanSkipReadAfterCreateAndUpdate: func(d *schema.ResourceData) bool {
//only skip read after create
return d.IsNewResource()
},
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var iacl settings.CreateIpAccessList
var updateIacl settings.UpdateIpAccessList
Expand All @@ -49,32 +59,16 @@ func ResourceIPAccessList() common.Resource {
}
ipAclId := status.IpAccessList.ListId
// need to wait until the ip access list is available from get
retry.RetryContext(ctx, 10*time.Minute, func() *retry.RetryError {
retry.RetryContext(ctx, ipAclTimeout, func() *retry.RetryError {
_, err := acc.IpAccessLists.GetByIpAccessListId(ctx, ipAclId)
var apiErr *apierr.APIError
if !errors.As(err, &apiErr) {
return retry.NonRetryableError(err)
}
if apiErr.StatusCode == 404 {
return retry.RetryableError(err)
} else {
return retry.NonRetryableError(err)
}
return isRetriable(err)
})
//need to enable the IP Access List with update, retry if 404 is returned due to eventual consistency
if d.Get("enabled").(bool) {
updateIacl.IpAccessListId = ipAclId
retry.RetryContext(ctx, 10*time.Minute, func() *retry.RetryError {
retry.RetryContext(ctx, ipAclTimeout, func() *retry.RetryError {
err = acc.IpAccessLists.Update(ctx, updateIacl)
var apiErr *apierr.APIError
if !errors.As(err, &apiErr) {
return retry.NonRetryableError(err)
}
if apiErr.StatusCode == 404 {
return retry.RetryableError(err)
} else {
return retry.NonRetryableError(err)
}
return isRetriable(err)
})
}
d.SetId(ipAclId)
Expand Down
3 changes: 3 additions & 0 deletions access/resource_ip_access_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func TestIPACLCreate(t *testing.T) {
}).Return(&settings.CreateIpAccessListResponse{
IpAccessList: TestIpAccessList,
}, nil)
e.GetByIpAccessListId(mock.Anything, TestingId).Return(&settings.FetchIpAccessListResponse{
IpAccessList: TestIpAccessList,
}, nil)
},
Resource: ResourceIPAccessList(),
State: map[string]any{
Expand Down

0 comments on commit 3a87eb5

Please sign in to comment.