|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + gapi "github.com/grafana/grafana-api-golang-client" |
| 8 | + "github.com/grafana/terraform-provider-grafana/internal/common" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func DatasourceRole() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Description: ` |
| 16 | +**Note:** This resource is available only with Grafana Enterprise 8.+. |
| 17 | +
|
| 18 | +* [Official documentation](https://grafana.com/docs/grafana/latest/administration/roles-and-permissions/access-control/) |
| 19 | +* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/access_control/) |
| 20 | +`, |
| 21 | + ReadContext: dataSourceRoleRead, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "uid": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Computed: true, |
| 26 | + Description: "Unique identifier of the role. Used for assignments.", |
| 27 | + }, |
| 28 | + "version": { |
| 29 | + Type: schema.TypeInt, |
| 30 | + Computed: true, |
| 31 | + Description: "Version of the role. A role is updated only on version increase.", |
| 32 | + }, |
| 33 | + "name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + Description: "Name of the role", |
| 37 | + }, |
| 38 | + "description": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "Description of the role.", |
| 42 | + }, |
| 43 | + "display_name": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Description: "Display name of the role. Available with Grafana 8.5+.", |
| 47 | + }, |
| 48 | + "group": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + Description: "Group of the role. Available with Grafana 8.5+.", |
| 52 | + }, |
| 53 | + "hidden": { |
| 54 | + Type: schema.TypeBool, |
| 55 | + Computed: true, |
| 56 | + Description: "Boolean to state whether the role should be visible in the Grafana UI or not. Available with Grafana 8.5+.", |
| 57 | + }, |
| 58 | + "global": { |
| 59 | + Type: schema.TypeBool, |
| 60 | + Computed: true, |
| 61 | + Description: "Boolean to state whether the role is available across all organizations or not.", |
| 62 | + }, |
| 63 | + "permissions": { |
| 64 | + Type: schema.TypeSet, |
| 65 | + Computed: true, |
| 66 | + Description: "Specific set of actions granted by the role.", |
| 67 | + Elem: &schema.Resource{ |
| 68 | + Schema: map[string]*schema.Schema{ |
| 69 | + "action": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "Specific action users granted with the role will be allowed to perform (for example: `users:read`)", |
| 73 | + }, |
| 74 | + "scope": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "Scope to restrict the action to a set of resources (for example: `users:*` or `roles:customrole1`)", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func findRoleWithName(client *gapi.Client, name string) (*gapi.Role, error) { |
| 87 | + roles, err := client.GetRoles() |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + for _, r := range roles { |
| 93 | + if r.Name == name { |
| 94 | + // Query the role by UID, that API has additional information |
| 95 | + return client.GetRole(r.UID) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return nil, fmt.Errorf("no role with name %q", name) |
| 100 | +} |
| 101 | + |
| 102 | +func dataSourceRoleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 103 | + client := meta.(*common.Client).GrafanaAPI |
| 104 | + name := d.Get("name").(string) |
| 105 | + role, err := findRoleWithName(client, name) |
| 106 | + if err != nil { |
| 107 | + return diag.FromErr(err) |
| 108 | + } |
| 109 | + |
| 110 | + d.SetId(role.UID) |
| 111 | + d.Set("uid", role.UID) |
| 112 | + d.Set("version", role.Version) |
| 113 | + d.Set("name", role.Name) |
| 114 | + if role.Description != "" { |
| 115 | + d.Set("description", role.Description) |
| 116 | + } |
| 117 | + if role.DisplayName != "" { |
| 118 | + d.Set("displayName", role.DisplayName) |
| 119 | + } |
| 120 | + if role.Group != "" { |
| 121 | + d.Set("group", role.Group) |
| 122 | + } |
| 123 | + d.Set("hidden", role.Hidden) |
| 124 | + d.Set("global", role.Global) |
| 125 | + perms := make([]interface{}, 0) |
| 126 | + for _, p := range role.Permissions { |
| 127 | + pMap := map[string]interface{}{ |
| 128 | + "action": p.Action, |
| 129 | + "scope": p.Scope, |
| 130 | + } |
| 131 | + perms = append(perms, pMap) |
| 132 | + } |
| 133 | + err = d.Set("permissions", perms) |
| 134 | + if err != nil { |
| 135 | + return diag.FromErr(err) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
0 commit comments