Skip to content

Commit 567678a

Browse files
Tanchwankvuong
authored andcommitted
added skelleton for databrikcs permission resource
1 parent 45188da commit 567678a

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

internal/providers/pluginfw/pluginfw_rollout_utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/products/serving"
2525
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/products/sharing"
2626
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/products/volume"
27+
"github.com/databricks/terraform-provider-databricks/permission"
2728
"github.com/hashicorp/terraform-plugin-framework/datasource"
2829
"github.com/hashicorp/terraform-plugin-framework/resource"
2930
)
@@ -46,6 +47,7 @@ var migratedDataSources = []func() datasource.DataSource{
4647
var pluginFwOnlyResources = append(
4748
[]func() resource.Resource{
4849
app.ResourceApp,
50+
permission.ResourcePermission,
4951
sharing.ResourceShare,
5052
},
5153
autoGeneratedResources...,
@@ -60,6 +62,7 @@ var pluginFwOnlyDataSources = append(
6062
catalog.DataSourceFunctions,
6163
dashboards.DataSourceDashboards,
6264
notificationdestinations.DataSourceNotificationDestinations,
65+
permission.DataSourcePermission,
6366
registered_model.DataSourceRegisteredModel,
6467
registered_model.DataSourceRegisteredModelVersions,
6568
serving.DataSourceServingEndpoints,
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package permission
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"path"
8+
"strings"
9+
10+
"github.com/databricks/databricks-sdk-go/apierr"
11+
"github.com/databricks/databricks-sdk-go/service/iam"
12+
"github.com/databricks/terraform-provider-databricks/common"
13+
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"
14+
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
15+
"github.com/databricks/terraform-provider-databricks/permissions/entity"
16+
17+
"github.com/hashicorp/terraform-plugin-framework/resource"
18+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
19+
"github.com/hashicorp/terraform-plugin-framework/types"
20+
)
21+
22+
const resourceName = "permission"
23+
24+
var (
25+
_ resource.Resource = &permissionResource{}
26+
_ resource.ResourceWithConfigure = &permissionResource{}
27+
)
28+
29+
func NewPermissionResource() resource.Resource {
30+
return &permissionResource{}
31+
}
32+
33+
type permissionResource struct {
34+
client *common.DatabricksClient
35+
context context.Context
36+
}
37+
38+
func (r *permissionResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
39+
if req.ProviderData == nil {
40+
return
41+
}
42+
43+
client, ok := req.ProviderData.(*common.DatabricksClient)
44+
45+
if !ok {
46+
resp.Diagnostics.AddError(
47+
//TODO ADD ERROR MESSAGE IN LINE WITH PROVIDER
48+
"Unable to configure the Databricks client",
49+
fmt.Sprintf("Expected *common.DatabricksClient, got %T", req.ProviderData),
50+
)
51+
52+
return
53+
}
54+
55+
r.client = client
56+
}
57+
58+
func (r *permissionResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
59+
resp.TypeName = pluginfwcommon.GetDatabrikcsProductionName(resourceName)
60+
}
61+
62+
63+
type permissionResourceModel struct {
64+
ObjectID types.String `tfsdk:"object_id"`
65+
ObjectType types.String `tfsdk:"object_type"`
66+
AccessControlList []accessControlListModel `tfsdk:"access_control_list"`
67+
LastUpdated types.String `tfsdk:"last_updated"`
68+
}
69+
70+
type accessControlListModel struct {
71+
ServicePrincipalName types.String `tfsdk:"service_principal_name"`
72+
GroupName types.String `tfsdk:"group_name"`
73+
UserName types.String `tfsdk:"user_name"`
74+
PermissionLevel types.String `tfsdk:"permission_level"`
75+
}
76+
77+
78+
//TODO set some attributes as optional, required
79+
func (r *permissionResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
80+
attrs, blocks := tfschema.ResourceStructToSchemaMap(ctx, permissionResourceModel{}, nil)
81+
resp.Schema = schema.Schema{
82+
Attributes: attrs,
83+
Blocks: blocks,
84+
}
85+
}
86+
87+
88+
func (r *permissionResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
89+
ctx := pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
90+
w, diags := r.client.WorkspaceClient()
91+
resp.Diagnostics.Append(diags...)
92+
}
93+
94+
func (r *permissionResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
95+
ctx := pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
96+
}
97+
98+
func (r *permissionResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
99+
ctx := pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
100+
}
101+
102+
func (r *permissionResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
103+
ctx := pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
104+
}
105+
106+
107+
// safePutWithOwner is a workaround for the limitation where warehouse without owners cannot have IS_OWNER set
108+
func (r *permissionResource) safePutWithOwner(ctx context.Context, objectID string, objectACL []iam.AccessControlRequest, mapping resourcePermissions, ownerOpt string) error {
109+
w, err := r.client.WorkspaceClient()
110+
if err != nil {
111+
return err
112+
}
113+
idParts := strings.Split(objectID, "/")
114+
id := idParts[len(idParts)-1]
115+
withOwner := mapping.addOwnerPermissionIfNeeded(objectACL, ownerOpt)
116+
_, err = w.Permissions.Set(ctx, iam.PermissionsRequest{
117+
RequestObjectId: id,
118+
RequestObjectType: mapping.requestObjectType,
119+
AccessControlList: withOwner,
120+
})
121+
if err != nil {
122+
if strings.Contains(err.Error(), "with no existing owner must provide a new owner") {
123+
_, err = w.Permissions.Set(ctx, iam.PermissionsRequest{
124+
RequestObjectId: id,
125+
RequestObjectType: mapping.requestObjectType,
126+
AccessControlList: objectACL,
127+
})
128+
}
129+
return err
130+
}
131+
return nil
132+
}
133+
134+
func (r *permissionResource) getCurrentUser(ctx context.Context) (string, error) {
135+
w, err := r.client.WorkspaceClient()
136+
if err != nil {
137+
return "", err
138+
}
139+
me, err := w.CurrentUser.Me(ctx)
140+
if err != nil {
141+
return "", err
142+
}
143+
return me.UserName, nil
144+
}

0 commit comments

Comments
 (0)