Skip to content

Commit

Permalink
SDK Migration: migrate identitygovernance to go-azure-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Sep 23, 2024
1 parent 90c2d56 commit 9b88821
Show file tree
Hide file tree
Showing 27 changed files with 1,586 additions and 1,192 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package identitygovernance_test
import (
"context"
"fmt"
"net/http"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/common-types/beta"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/identitygovernance/beta/entitlementmanagementaccesspackageassignmentpolicy"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-azuread/internal/acceptance"
"github.com/hashicorp/terraform-provider-azuread/internal/acceptance/check"
Expand Down Expand Up @@ -124,16 +125,16 @@ func TestAccAccessPackageAssignmentPolicy_removeQuestion(t *testing.T) {

func (AccessPackageAssignmentPolicyResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
client := clients.IdentityGovernance.AccessPackageAssignmentPolicyClient
client.BaseClient.DisableRetries = true
defer func() { client.BaseClient.DisableRetries = false }()
id := beta.NewIdentityGovernanceEntitlementManagementAccessPackageAssignmentPolicyID(state.ID)

_, status, err := client.Get(ctx, state.ID, odata.Query{})
resp, err := client.GetEntitlementManagementAccessPackageAssignmentPolicy(ctx, id, entitlementmanagementaccesspackageassignmentpolicy.DefaultGetEntitlementManagementAccessPackageAssignmentPolicyOperationOptions())
if err != nil {
if status == http.StatusNotFound {
if response.WasNotFound(resp.HttpResponse) {
return pointer.To(false), nil
}
return nil, fmt.Errorf("failed to retrieve Access package assignment policy with ID %q: %+v", state.ID, err)
return nil, fmt.Errorf("failed to retrieve %s: %+v", id, err)
}

return pointer.To(true), nil
}

Expand Down Expand Up @@ -170,18 +171,18 @@ resource "azuread_group" "test" {
}
resource "azuread_access_package_catalog" "test_catalog" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "TestAcc Catalog %[1]d for access assignment policy"
}
resource "azuread_access_package" "test" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "TestAcc Access Package %[1]d for access assignment policy"
catalog_id = azuread_access_package_catalog.test_catalog.id
}
resource "azuread_access_package_assignment_policy" "test" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "TestAcc Access Package Assignnment Policy %[1]d"
duration_in_days = 90
access_package_id = azuread_access_package.test.id
Expand All @@ -197,7 +198,7 @@ resource "azuread_access_package_assignment_policy" "test" {
primary_approver {
object_id = azuread_group.test.object_id
subject_type = "groupMembers"
subject_type = "GroupMembers"
}
}
}
Expand Down Expand Up @@ -229,18 +230,18 @@ resource "azuread_group" "test" {
}
resource "azuread_access_package_catalog" "test_catalog" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "TestAcc Catalog %[1]d for access assignment policy"
}
resource "azuread_access_package" "test" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "TestAcc Access Package %[1]d for access assignment policy"
catalog_id = azuread_access_package_catalog.test_catalog.id
}
resource "azuread_access_package_assignment_policy" "test" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "TestAcc Access Package Assignnment Policy %[1]d"
duration_in_days = 90
access_package_id = azuread_access_package.test.id
Expand All @@ -256,7 +257,7 @@ resource "azuread_access_package_assignment_policy" "test" {
primary_approver {
object_id = azuread_group.test.object_id
subject_type = "groupMembers"
subject_type = "GroupMembers"
}
}
}
Expand Down Expand Up @@ -292,15 +293,16 @@ resource "azuread_group" "second_approver" {
}
resource "azuread_access_package_catalog" "test_catalog" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "TestAcc Catalog %[1]d for access assignment policy"
}
resource "azuread_access_package" "test" {
display_name = "testacc-asscess-assignment-%[1]d"
display_name = "testacc-access-assignment-%[1]d"
description = "Test Access Package %[1]d for assignment policy"
catalog_id = azuread_access_package_catalog.test_catalog.id
}
resource "azuread_access_package_assignment_policy" "test" {
display_name = "access-package-assignment-policy-%[1]d"
description = "Test Access Package Assignnment Policy %[1]d"
Expand All @@ -314,7 +316,7 @@ resource "azuread_access_package_assignment_policy" "test" {
requestor {
object_id = azuread_group.requestor.object_id
subject_type = "groupMembers"
subject_type = "GroupMembers"
}
}
Expand All @@ -330,12 +332,12 @@ resource "azuread_access_package_assignment_policy" "test" {
enable_alternative_approval_in_days = 8
primary_approver {
subject_type = "requestorManager"
subject_type = "RequestorManager"
}
alternative_approver {
object_id = azuread_group.second_approver.object_id
subject_type = "groupMembers"
subject_type = "GroupMembers"
}
}
Expand All @@ -344,12 +346,12 @@ resource "azuread_access_package_assignment_policy" "test" {
primary_approver {
object_id = azuread_group.second_approver.object_id
subject_type = "groupMembers"
subject_type = "GroupMembers"
}
primary_approver {
object_id = azuread_group.first_approver.object_id
subject_type = "groupMembers"
subject_type = "GroupMembers"
backup = true
}
}
Expand All @@ -359,13 +361,13 @@ resource "azuread_access_package_assignment_policy" "test" {
enabled = true
review_frequency = "annual"
review_type = "Reviewers"
duration_in_days = "10"
duration_in_days = 10
access_recommendation_enabled = true
access_review_timeout_behavior = "acceptAccessRecommendation"
reviewer {
object_id = azuread_group.first_approver.object_id
subject_type = "groupMembers"
subject_type = "GroupMembers"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ package identitygovernance

import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/common-types/beta"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/identitygovernance/beta/entitlementmanagementaccesspackagecatalog"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azuread/internal/tf/validation"
"github.com/manicminer/hamilton/msgraph"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/tf/validation"
)

func accessPackageCatalogDataSource() *pluginsdk.Resource {
Expand Down Expand Up @@ -70,57 +73,63 @@ func accessPackageCatalogDataRead(ctx context.Context, d *pluginsdk.ResourceData
objectId := d.Get("object_id").(string)
displayName := d.Get("display_name").(string)

var catalog *msgraph.AccessPackageCatalog
var err error
var catalog *beta.AccessPackageCatalog
if objectId != "" {
catalog, _, err = client.Get(ctx, objectId, odata.Query{})
id := beta.NewIdentityGovernanceEntitlementManagementAccessPackageCatalogID(objectId)
resp, err := client.GetEntitlementManagementAccessPackageCatalog(ctx, id, entitlementmanagementaccesspackagecatalog.DefaultGetEntitlementManagementAccessPackageCatalogOperationOptions())
if err != nil {
return tf.ErrorDiagF(err, "Error retrieving access package catalog with id %q", objectId)
return tf.ErrorDiagF(err, "Retrieving %s", id)
}

if resp.Model == nil {
return tf.ErrorDiagF(errors.New("model was nil"), "Retrieving %s", id)
}
catalog = resp.Model

} else if displayName != "" {
query := odata.Query{
Filter: fmt.Sprintf("displayName eq '%s'", displayName),
options := entitlementmanagementaccesspackagecatalog.ListEntitlementManagementAccessPackageCatalogsOperationOptions{
Filter: pointer.To(fmt.Sprintf("displayName eq '%s'", odata.EscapeSingleQuote(displayName))),
}

result, _, err := client.List(ctx, query)
resp, err := client.ListEntitlementManagementAccessPackageCatalogs(ctx, options)
if err != nil {
return tf.ErrorDiagF(err, "Error listing access package catalog with filter %s", query.Filter)
return tf.ErrorDiagF(err, "Listing access package catalogs with filter %s", *options.Filter)
}
if result == nil || len(*result) == 0 {
return tf.ErrorDiagF(fmt.Errorf("no access package catalog matched with filter %s", query.Filter), "Access package catalog not found!")

if resp.Model == nil || len(*resp.Model) == 0 {
return tf.ErrorDiagF(errors.New("no matching results"), "Listing access package catalogs with filter %s", *options.Filter)
}
if len(*result) > 1 {
return tf.ErrorDiagF(fmt.Errorf("multiple access package catalog matched with filter %s", query.Filter), "Multiple access package catalog found!")
if len(*resp.Model) > 1 {
return tf.ErrorDiagF(errors.New("multiple results matched"), "Listing access package catalogs with filter %s", *options.Filter)
}

for _, c := range *result {
name := c.DisplayName
if name == nil {
continue
}

if *name == displayName {
for _, c := range *resp.Model {
if strings.EqualFold(c.DisplayName.GetOrZero(), displayName) {
catalog = &c
break
}
}
}

if catalog == nil {
return tf.ErrorDiagF(fmt.Errorf("no access package catalog matched with specified parameters"), "Access access package catalog not found!")
return tf.ErrorDiagF(fmt.Errorf("no access package catalog matched with specified parameters"), "Access package catalog not found")
}
if catalog.Id == nil {
return tf.ErrorDiagF(fmt.Errorf("model has nil ID"), "Access package catalog not found")
}

published := false
if strings.EqualFold(catalog.State, msgraph.AccessPackageCatalogStatusPublished) {
if strings.EqualFold(catalog.CatalogStatus.GetOrZero(), CatalogStatusPublished) {
published = true
}

d.SetId(*catalog.ID)
id := beta.NewIdentityGovernanceEntitlementManagementAccessPackageCatalogID(*catalog.Id)
d.SetId(id.AccessPackageCatalogId)

tf.Set(d, "object_id", catalog.ID)
tf.Set(d, "display_name", catalog.DisplayName)
tf.Set(d, "description", catalog.Description)
tf.Set(d, "externally_visible", catalog.IsExternallyVisible)
tf.Set(d, "object_id", id.AccessPackageCatalogId)
tf.Set(d, "display_name", catalog.DisplayName.GetOrZero())
tf.Set(d, "description", catalog.Description.GetOrZero())
tf.Set(d, "externally_visible", catalog.IsExternallyVisible.GetOrZero())
tf.Set(d, "published", published)

return nil
Expand Down
Loading

0 comments on commit 9b88821

Please sign in to comment.