Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add azuread_directory_roles data source #945

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/data-sources/directory_roles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
subcategory: "Directory Roles"
---

# Resource: azuread_directory_roles

Use this data source to access information about activated directory roles within Azure Active Directory.

## API Permissions

The following API permissions are required in order to use this resource.

When authenticated with a service principal, this resource requires one of the following application roles: `RoleManagement.Read.Directory` or `Directory.Read.All`

When authenticated with a user principal, this data source does not require any additional roles.

## Example Usage

```terraform
data "azuread_directory_roles" "current" {}

output "roles" {
value = data.azuread_directory_roles.current.object_ids
}
```

## Argument Reference

This data source does not have any arguments.

## Attributes Reference

The following attributes are exported:

* `object_ids` - The object IDs of the roles.
* `roles` - A list of users. Each `role` object provides the attributes documented below.

---

`role` object exports the following:

* `display_name` - The display name of the directory role.
* `template_id` - The template ID of the directory role.
* `description` - The description of the directory role.
* `object_id` - The object ID of the directory role.

## Import

This resource does not support importing.
manicminer marked this conversation as resolved.
Show resolved Hide resolved
109 changes: 109 additions & 0 deletions internal/services/directoryroles/directory_roles_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package directoryroles

import (
"context"
"crypto/sha1"
"encoding/base64"
"errors"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
)

func directoryRoleDataSource() *schema.Resource {
manicminer marked this conversation as resolved.
Show resolved Hide resolved
return &schema.Resource{
ReadContext: directoryRoleDataSourceRead,
manicminer marked this conversation as resolved.
Show resolved Hide resolved

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"object_ids": {
Description: "The object IDs of the roles",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"roles": {
Description: "A list of roles",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"display_name": {
Description: "The display name of the directory role",
Type: schema.TypeString,
Computed: true,
},

"template_id": {
Description: "The object ID of the template associated with the directory role",
Type: schema.TypeString,
Computed: true,
},

"description": {
Description: "The description of the directory role",
Type: schema.TypeString,
Computed: true,
},

"object_id": {
Description: "The object ID of the directory role",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func directoryRoleDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
manicminer marked this conversation as resolved.
Show resolved Hide resolved
client := meta.(*clients.Client).DirectoryRoles.DirectoryRolesClient

directoryRoles, _, err := client.List(ctx)
if err != nil {
return tf.ErrorDiagF(err, "Could not retrieve roles")
}
if directoryRoles == nil {
return tf.ErrorDiagF(errors.New("API error: nil directoryRoles were returned"), "Retrieving all directory roles")
}

objectIds := make([]string, 0)
roleList := make([]map[string]interface{}, 0)

for _, r := range *directoryRoles {
objectIds = append(objectIds, *r.ID())

role := make(map[string]interface{})
role["description"] = r.Description
role["display_name"] = r.DisplayName
role["object_id"] = r.ID()
role["template_id"] = r.RoleTemplateId
roleList = append(roleList, role)
}

// Generate a unique ID based on result
h := sha1.New()
if _, err := h.Write([]byte(strings.Join(objectIds, "/"))); err != nil {
return tf.ErrorDiagF(err, "Unable to compute hash for Object IDs")
}

d.SetId("roles#" + base64.URLEncoding.EncodeToString(h.Sum(nil)))

tf.Set(d, "roles", roleList)
tf.Set(d, "object_ids", objectIds)

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package directoryroles_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/hashicorp/terraform-provider-azuread/internal/acceptance"
"github.com/hashicorp/terraform-provider-azuread/internal/acceptance/check"
)

type DirectoryRolesDataSource struct{}

func TestAccDomainsDataSource_basic(t *testing.T) {
manicminer marked this conversation as resolved.
Show resolved Hide resolved
data := acceptance.BuildTestData(t, "data.azuread_directory_roles", "test")
r := DirectoryRolesDataSource{}

data.DataSourceTest(t, []resource.TestStep{
{
Config: r.basic(),
Check: r.testCheckFunc(data),
},
})
}

func (DirectoryRolesDataSource) testCheckFunc(data acceptance.TestData, additionalChecks ...resource.TestCheckFunc) resource.TestCheckFunc {
checks := []resource.TestCheckFunc{
check.That(data.ResourceName).Key("roles.0.description").Exists(),
check.That(data.ResourceName).Key("roles.0.display_name").Exists(),
check.That(data.ResourceName).Key("roles.0.object_id").Exists(),
check.That(data.ResourceName).Key("roles.0.template_id").Exists(),
}
checks = append(checks, additionalChecks...)
return resource.ComposeTestCheckFunc(checks...)
}

func (DirectoryRolesDataSource) basic() string {
return `data "azuread_directory_roles" "test" {}`
}
4 changes: 3 additions & 1 deletion internal/services/directoryroles/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func (r Registration) WebsiteCategories() []string {

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{}
return map[string]*schema.Resource{
"azuread_directory_roles": directoryRoleDataSource(),
manicminer marked this conversation as resolved.
Show resolved Hide resolved
}
}

// SupportedResources returns the supported Resources supported by this Service
Expand Down