-
Notifications
You must be signed in to change notification settings - Fork 409
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
Migrated databricks_ip_access_list
resource to Go SDK
#2306
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b52cd26
migrate to Go SDK
tanmay-db 61a8bef
cleanup
tanmay-db 0727071
Merge branch 'master' of github.com:databricks/terraform-provider-dat…
tanmay-db 396ed91
migrate more structs
tanmay-db e19d61a
cleanup
tanmay-db 6a7e2bc
update exporter tests
tanmay-db 71016d2
use update request
tanmay-db 5c30ff2
fix
tanmay-db 9656c58
Merge branch 'master' into ipacl
nfx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,92 +3,18 @@ package access | |
import ( | ||
"context" | ||
|
||
"github.com/databricks/databricks-sdk-go/service/settings" | ||
"github.com/databricks/terraform-provider-databricks/common" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
type ListIPAccessListsResponse struct { | ||
ListIPAccessListsResponse []IpAccessListStatus `json:"ip_access_lists,omitempty"` | ||
} | ||
|
||
type createIPAccessListRequest struct { | ||
Label string `json:"label"` | ||
ListType string `json:"list_type"` | ||
IPAddresses []string `json:"ip_addresses"` | ||
} | ||
|
||
type IpAccessListStatus struct { | ||
ListID string `json:"list_id"` | ||
Label string `json:"label"` | ||
ListType string `json:"list_type"` | ||
IPAddresses []string `json:"ip_addresses"` | ||
AddressCount int `json:"address_count,omitempty"` | ||
CreatedAt int64 `json:"created_at,omitempty"` | ||
CreatorUserID int64 `json:"creator_user_id,omitempty"` | ||
UpdatedAt int64 `json:"updated_at,omitempty"` | ||
UpdatorUserID int64 `json:"updator_user_id,omitempty"` | ||
Enabled bool `json:"enabled,omitempty"` | ||
} | ||
|
||
type IpAccessListStatusWrapper struct { | ||
IPAccessList IpAccessListStatus `json:"ip_access_list,omitempty"` | ||
} | ||
|
||
type ipAccessListUpdateRequest struct { | ||
Label string `json:"label"` | ||
ListType string `json:"list_type"` | ||
IPAddresses []string `json:"ip_addresses"` | ||
Enabled bool `json:"enabled,omitempty" tf:"default:true"` | ||
} | ||
|
||
// Preview feature: https://docs.databricks.com/security/network/ip-access-list.html | ||
// REST API: https://docs.databricks.com/dev-tools/api/latest/ip-access-list.html#operation/create-list | ||
type ipAccessListsAPI struct { | ||
client *common.DatabricksClient | ||
context context.Context | ||
} | ||
|
||
// NewIPAccessListsAPI ... | ||
func NewIPAccessListsAPI(ctx context.Context, m any) ipAccessListsAPI { | ||
return ipAccessListsAPI{ | ||
client: m.(*common.DatabricksClient), | ||
context: ctx, | ||
} | ||
} | ||
|
||
// Create creates the IP Access List to given the instance pool configuration | ||
func (a ipAccessListsAPI) Create(cr createIPAccessListRequest) (status IpAccessListStatus, err error) { | ||
wrapper := IpAccessListStatusWrapper{} | ||
err = a.client.Post(a.context, "/ip-access-lists", cr, &wrapper) | ||
if err != nil { | ||
return | ||
} | ||
status = wrapper.IPAccessList | ||
return | ||
} | ||
|
||
func (a ipAccessListsAPI) Update(objectID string, ur ipAccessListUpdateRequest) error { | ||
return a.client.Put(a.context, "/ip-access-lists/"+objectID, ur) | ||
} | ||
|
||
func (a ipAccessListsAPI) Delete(objectID string) (err error) { | ||
err = a.client.Delete(a.context, "/ip-access-lists/"+objectID, map[string]any{}) | ||
return | ||
} | ||
|
||
func (a ipAccessListsAPI) Read(objectID string) (status IpAccessListStatus, err error) { | ||
wrapper := IpAccessListStatusWrapper{} | ||
err = a.client.Get(a.context, "/ip-access-lists/"+objectID, nil, &wrapper) | ||
status = wrapper.IPAccessList | ||
return | ||
} | ||
|
||
func (a ipAccessListsAPI) List() (listResponse ListIPAccessListsResponse, err error) { | ||
listResponse = ListIPAccessListsResponse{} | ||
err = a.client.Get(a.context, "/ip-access-lists", nil, &listResponse) | ||
return | ||
Label string `json:"label"` | ||
ListType settings.ListType `json:"list_type"` | ||
IpAddresses []string `json:"ip_addresses"` | ||
Enabled bool `json:"enabled,omitempty" tf:"default:true"` | ||
} | ||
|
||
// ResourceIPAccessList manages IP access lists | ||
|
@@ -105,30 +31,47 @@ func ResourceIPAccessList() *schema.Resource { | |
return common.Resource{ | ||
Schema: s, | ||
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { | ||
var iacl createIPAccessListRequest | ||
w, err := c.WorkspaceClient() | ||
if err != nil { | ||
return err | ||
} | ||
var iacl settings.CreateIpAccessList | ||
common.DataToStructPointer(d, s, &iacl) | ||
status, err := NewIPAccessListsAPI(ctx, c).Create(iacl) | ||
status, err := w.IpAccessLists.Create(ctx, iacl) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId(status.ListID) | ||
d.SetId(status.IpAccessList.ListId) | ||
return nil | ||
}, | ||
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { | ||
status, err := NewIPAccessListsAPI(ctx, c).Read(d.Id()) | ||
w, err := c.WorkspaceClient() | ||
if err != nil { | ||
return err | ||
} | ||
status, err := w.IpAccessLists.GetByIpAccessListId(ctx, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
common.StructToData(status, s, d) | ||
return nil | ||
}, | ||
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { | ||
var iacl ipAccessListUpdateRequest | ||
w, err := c.WorkspaceClient() | ||
if err != nil { | ||
return err | ||
} | ||
var iacl settings.UpdateIpAccessList | ||
common.DataToStructPointer(d, s, &iacl) | ||
return NewIPAccessListsAPI(ctx, c).Update(d.Id(), iacl) | ||
iacl.IpAccessListId = d.Id() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to do this because Update call doesn't take id so we have to pass it through the resource data. |
||
return w.IpAccessLists.Update(ctx, iacl) | ||
}, | ||
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { | ||
return NewIPAccessListsAPI(ctx, c).Delete(d.Id()) | ||
w, err := c.WorkspaceClient() | ||
if err != nil { | ||
return err | ||
} | ||
return w.IpAccessLists.DeleteByIpAccessListId(ctx, d.Id()) | ||
}, | ||
}.ToResource() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since all these are same, I don't think updates is needed in docs.