-
Notifications
You must be signed in to change notification settings - Fork 360
/
resource_ip_access_list.go
77 lines (72 loc) · 2.35 KB
/
resource_ip_access_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 ipAccessListUpdateRequest struct {
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
func ResourceIPAccessList() common.Resource {
s := common.StructToSchema(ipAccessListUpdateRequest{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
// nolint
s["list_type"].ValidateFunc = validation.StringInSlice([]string{"ALLOW", "BLOCK"}, false)
s["ip_addresses"].Elem = &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.Any(validation.IsIPv4Address, validation.IsCIDR),
}
return s
})
return common.Resource{
Schema: s,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var iacl settings.CreateIpAccessList
common.DataToStructPointer(d, s, &iacl)
status, err := w.IpAccessLists.Create(ctx, iacl)
if err != nil {
return err
}
d.SetId(status.IpAccessList.ListId)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
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.IpAccessList, s, d)
return nil
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var iacl settings.UpdateIpAccessList
common.DataToStructPointer(d, s, &iacl)
iacl.IpAccessListId = d.Id()
return w.IpAccessLists.Update(ctx, iacl)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
return w.IpAccessLists.DeleteByIpAccessListId(ctx, d.Id())
},
}
}