|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/grafana/grafana-openapi-client-go/models" |
| 8 | + |
| 9 | + "github.com/grafana/terraform-provider-grafana/v3/internal/common" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func datasourceFolderPermission() *common.DataSource { |
| 15 | + schema := &schema.Resource{ |
| 16 | + Description: ` |
| 17 | +* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/manage-dashboards/) |
| 18 | +* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/folder_permissions/) |
| 19 | +`, |
| 20 | + ReadContext: dataSourceFolderPermissionRead, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "org_id": orgIDAttribute(), |
| 23 | + "folder_uid": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "The UID of the folder.", |
| 27 | + }, |
| 28 | + "permissions": { |
| 29 | + Type: schema.TypeSet, |
| 30 | + Computed: true, |
| 31 | + Elem: &schema.Resource{ |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "team_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Description: "ID of the team to manage permissions for.", |
| 37 | + }, |
| 38 | + "user_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "ID of the user or service account to manage permissions for.", |
| 42 | + }, |
| 43 | + "permission": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Description: "Permission to associate with item. Must be one of `View`, `Edit`, or `Admin`.", |
| 47 | + }, |
| 48 | + "role": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + Description: "Role to associate with item. Must be one of `Viewer`, `Editor`, or `Admin`.", |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + return common.NewLegacySDKDataSource(common.CategoryGrafanaOSS, "grafana_folder_permission", schema) |
| 59 | +} |
| 60 | + |
| 61 | +func dataSourceFolderPermissionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 62 | + client, orgID := OAPIClientFromNewOrgResource(meta, d) |
| 63 | + uid := d.Get("folder_uid").(string) |
| 64 | + |
| 65 | + resp, err := client.FolderPermissions.GetFolderPermissionList(uid) |
| 66 | + if err != nil { |
| 67 | + return diag.FromErr(err) |
| 68 | + } |
| 69 | + |
| 70 | + var resourcePermissions []models.DashboardACLInfoDTO |
| 71 | + for _, perm := range resp.Payload { |
| 72 | + resourcePermissions = append(resourcePermissions, *perm) |
| 73 | + } |
| 74 | + |
| 75 | + var permissionItems []interface{} |
| 76 | + for _, permission := range resourcePermissions { |
| 77 | + permissionItem := make(map[string]interface{}) |
| 78 | + if permission.Role != "" { |
| 79 | + permissionItem["role"] = permission.Role |
| 80 | + } |
| 81 | + permissionItem["team_id"] = permission.TeamUID |
| 82 | + permissionItem["user_id"] = permission.UserUID |
| 83 | + permissionItem["permission"] = permission.PermissionName |
| 84 | + |
| 85 | + permissionItems = append(permissionItems, permissionItem) |
| 86 | + } |
| 87 | + |
| 88 | + d.SetId(MakeOrgResourceID(orgID, uid)) |
| 89 | + d.Set("org_id", strconv.FormatInt(orgID, 10)) |
| 90 | + d.Set("permissions", permissionItems) |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
0 commit comments