|
| 1 | +package scim |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/databricks/terraform-provider-databricks/common" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +// ResourceGroup manages user groups |
| 13 | +func ResourceEntitlements() *schema.Resource { |
| 14 | + type entity struct { |
| 15 | + GroupId string `json:"group_id,omitempty" tf:"force_new"` |
| 16 | + UserId string `json:"user_id,omitempty" tf:"force_new"` |
| 17 | + SpnId string `json:"service_principal_id,omitempty" tf:"force_new"` |
| 18 | + } |
| 19 | + entitlementSchema := common.StructToSchema(entity{}, |
| 20 | + func(m map[string]*schema.Schema) map[string]*schema.Schema { |
| 21 | + addEntitlementsToSchema(&m) |
| 22 | + alof := []string{"group_id", "user_id", "service_principal_id"} |
| 23 | + for _, field := range alof { |
| 24 | + m[field].AtLeastOneOf = alof |
| 25 | + } |
| 26 | + return m |
| 27 | + }) |
| 28 | + addEntitlementsToSchema(&entitlementSchema) |
| 29 | + return common.Resource{ |
| 30 | + Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 31 | + return patchEntitlements(ctx, d, c, "add") |
| 32 | + }, |
| 33 | + Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 34 | + split := strings.SplitN(d.Id(), "/", 2) |
| 35 | + if len(split) != 2 { |
| 36 | + return fmt.Errorf("ID must be two elements: %s", d.Id()) |
| 37 | + } |
| 38 | + switch strings.ToLower(split[0]) { |
| 39 | + case "group": |
| 40 | + group, err := NewGroupsAPI(ctx, c).Read(split[1]) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + return group.Entitlements.readIntoData(d) |
| 45 | + case "user": |
| 46 | + user, err := NewUsersAPI(ctx, c).Read(split[1]) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + return user.Entitlements.readIntoData(d) |
| 51 | + case "spn": |
| 52 | + spn, err := NewServicePrincipalsAPI(ctx, c).Read(split[1]) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + return spn.Entitlements.readIntoData(d) |
| 57 | + } |
| 58 | + return nil |
| 59 | + }, |
| 60 | + Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 61 | + return enforceEntitlements(ctx, d, c) |
| 62 | + }, |
| 63 | + Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 64 | + return patchEntitlements(ctx, d, c, "remove") |
| 65 | + }, |
| 66 | + Schema: entitlementSchema, |
| 67 | + }.ToResource() |
| 68 | +} |
| 69 | + |
| 70 | +func patchEntitlements(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient, op string) error { |
| 71 | + groupId := d.Get("group_id").(string) |
| 72 | + userId := d.Get("user_id").(string) |
| 73 | + spnId := d.Get("service_principal_id").(string) |
| 74 | + request := PatchRequestComplexValue([]patchOperation{ |
| 75 | + { |
| 76 | + op, |
| 77 | + "entitlements", |
| 78 | + readEntitlementsFromData(d), |
| 79 | + }, |
| 80 | + }) |
| 81 | + if groupId != "" { |
| 82 | + groupsAPI := NewGroupsAPI(ctx, c) |
| 83 | + err := groupsAPI.UpdateEntitlements(groupId, request) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + d.SetId("group/" + groupId) |
| 88 | + } |
| 89 | + if userId != "" { |
| 90 | + usersAPI := NewUsersAPI(ctx, c) |
| 91 | + err := usersAPI.UpdateEntitlements(userId, request) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + d.SetId("user/" + userId) |
| 96 | + } |
| 97 | + if spnId != "" { |
| 98 | + spnAPI := NewServicePrincipalsAPI(ctx, c) |
| 99 | + err := spnAPI.UpdateEntitlements(spnId, request) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + d.SetId("spn/" + spnId) |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func enforceEntitlements(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 109 | + split := strings.SplitN(d.Id(), "/", 2) |
| 110 | + if len(split) != 2 { |
| 111 | + return fmt.Errorf("ID must be two elements: %s", d.Id()) |
| 112 | + } |
| 113 | + identity := strings.ToLower(split[0]) |
| 114 | + id := strings.ToLower(split[1]) |
| 115 | + request := PatchRequestComplexValue( |
| 116 | + []patchOperation{ |
| 117 | + { |
| 118 | + "remove", "entitlements", generateFullEntitlements(), |
| 119 | + }, |
| 120 | + { |
| 121 | + "add", "entitlements", readEntitlementsFromData(d), |
| 122 | + }, |
| 123 | + }, |
| 124 | + ) |
| 125 | + switch identity { |
| 126 | + case "group": |
| 127 | + NewGroupsAPI(ctx, c).UpdateEntitlements(id, request) |
| 128 | + case "user": |
| 129 | + NewUsersAPI(ctx, c).UpdateEntitlements(id, request) |
| 130 | + case "spn": |
| 131 | + NewServicePrincipalsAPI(ctx, c).UpdateEntitlements(id, request) |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
0 commit comments