Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 2 additions & 3 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/microsoft/terraform-provider-power-platform/internal/constants"
"github.com/microsoft/terraform-provider-power-platform/internal/customerrors"
"github.com/microsoft/terraform-provider-power-platform/internal/helpers"
"github.com/microsoft/terraform-provider-power-platform/internal/helpers/array"
)

// ProviderClient is a wrapper around the API client that provides additional helper methods.
Expand Down Expand Up @@ -154,7 +153,7 @@ func (client *Client) Execute(ctx context.Context, scopes []string, method, url
return resp, err
}

isAcceptable := len(acceptableStatusCodes) > 0 && array.Contains(acceptableStatusCodes, resp.HttpResponse.StatusCode)
isAcceptable := len(acceptableStatusCodes) > 0 && helpers.ArrayContains(acceptableStatusCodes, resp.HttpResponse.StatusCode)
if isAcceptable {
if responseObj != nil && len(resp.BodyAsBytes) > 0 {
err = resp.MarshallTo(responseObj)
Expand All @@ -166,7 +165,7 @@ func (client *Client) Execute(ctx context.Context, scopes []string, method, url
return resp, nil
}

isRetryable := array.Contains(retryableStatusCodes, resp.HttpResponse.StatusCode)
isRetryable := helpers.ArrayContains(retryableStatusCodes, resp.HttpResponse.StatusCode)
if !isRetryable {
return resp, customerrors.NewUnexpectedHttpStatusCodeError(acceptableStatusCodes, resp.HttpResponse.StatusCode, resp.HttpResponse.Status, resp.BodyAsBytes)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func (client *Client) doRequest(ctx context.Context, token *string, request *htt
return resp, err
}

resp := &Response{
HttpResponse: apiResponse,
if apiResponse == nil {
return nil, errors.New("unexpected nil response without error")
}

if apiResponse == nil {
return resp, errors.New("unexpected nil response without error")
resp := &Response{
HttpResponse: apiResponse,
}

defer apiResponse.Body.Close()
Expand Down
16 changes: 8 additions & 8 deletions internal/helpers/array/array.go → internal/helpers/array.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package array
package helpers

// DiffArrays returns the added and removed items between two string arrays.
// ArrayDiff returns the added and removed items between two string arrays.
// This can be useful for comparing plan vs state arrays.
func Diff(newArr, oldArr []string) (added []string, removed []string) {
func ArrayDiff(newArr, oldArr []string) (added []string, removed []string) {
addedElements := make([]string, 0)
removedElements := make([]string, 0)
oldMap := make(map[string]bool)
Expand All @@ -31,7 +31,7 @@ func Diff(newArr, oldArr []string) (added []string, removed []string) {
}

// ArrayContains returns true if the given array contains the given item.
func Contains[T comparable](arr []T, item T) bool {
func ArrayContains[T comparable](arr []T, item T) bool {
for _, v := range arr {
if v == item {
return true
Expand All @@ -40,8 +40,8 @@ func Contains[T comparable](arr []T, item T) bool {
return false
}

// Except returns a slice of elements that are in 'a' but not in 'b'.
func Except[T comparable](a, b []T) []T {
// ArrayExcept returns a slice of elements that are in 'a' but not in 'b'.
func ArrayExcept[T comparable](a, b []T) []T {
bSet := make(map[T]struct{}, len(b))
for _, value := range b {
bSet[value] = struct{}{}
Expand All @@ -57,8 +57,8 @@ func Except[T comparable](a, b []T) []T {
return diff
}

// Find returns the first element in the array that satisfies the predicate.
func Find[T comparable](arr []T, predicate func(T) bool) T {
// ArrayFind returns the first element in the array that satisfies the predicate.
func ArrayFind[T comparable](arr []T, predicate func(T) bool) T {
for _, v := range arr {
if predicate(v) {
return v
Expand Down
11 changes: 0 additions & 11 deletions internal/helpers/contains.go

This file was deleted.

3 changes: 1 addition & 2 deletions internal/services/authorization/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/microsoft/terraform-provider-power-platform/internal/constants"
"github.com/microsoft/terraform-provider-power-platform/internal/customerrors"
"github.com/microsoft/terraform-provider-power-platform/internal/helpers"
"github.com/microsoft/terraform-provider-power-platform/internal/helpers/array"
"github.com/microsoft/terraform-provider-power-platform/internal/services/environment"
)

Expand Down Expand Up @@ -187,7 +186,7 @@ func (client *client) RemoveEnvironmentUserSecurityRoles(ctx context.Context, en
}

for _, role := range securityRoles {
savedRoleData := array.Find(savedRoles, func(roleDto securityRoleDto) bool {
savedRoleData := helpers.ArrayFind(savedRoles, func(roleDto securityRoleDto) bool {
return roleDto.RoleId == role
})

Expand Down
5 changes: 2 additions & 3 deletions internal/services/authorization/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/microsoft/terraform-provider-power-platform/internal/api"
"github.com/microsoft/terraform-provider-power-platform/internal/customerrors"
"github.com/microsoft/terraform-provider-power-platform/internal/helpers"
"github.com/microsoft/terraform-provider-power-platform/internal/helpers/array"
)

var _ resource.Resource = &UserResource{}
Expand Down Expand Up @@ -345,7 +344,7 @@ func (r *UserResource) Update(ctx context.Context, req resource.UpdateRequest, r
}
tflog.Debug(ctx, fmt.Sprintf("Dataverse exists in environment: %t", hasEnvDataverse))

addedSecurityRoles, removedSecurityRoles := array.Diff(plan.SecurityRoles, state.SecurityRoles)
addedSecurityRoles, removedSecurityRoles := helpers.ArrayDiff(plan.SecurityRoles, state.SecurityRoles)
user := userDto{}
if hasEnvDataverse {
if len(addedSecurityRoles) > 0 {
Expand Down Expand Up @@ -494,7 +493,7 @@ func validateRequiredStringField(diagnostics *diag.Diagnostics, field types.Stri
}

func validateEnvironmentSecurityRoles(roles []string) error {
except := array.Except(roles, []string{ROLE_ENVIRONMENT_ADMIN, ROLE_ENVIRONMENT_MAKER})
except := helpers.ArrayExcept(roles, []string{ROLE_ENVIRONMENT_ADMIN, ROLE_ENVIRONMENT_MAKER})
if len(except) > 0 {
return fmt.Errorf("invalid security roles. only '%s' and '%s' are allowed", ROLE_ENVIRONMENT_ADMIN, ROLE_ENVIRONMENT_MAKER)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (r *ManagedEnvironmentResource) validateAndPrepareSolutionCheckerRules(ctx
if !plan.SolutionCheckerRuleOverrides.IsNull() {
overrides := helpers.SetToStringSlice(plan.SolutionCheckerRuleOverrides)
for _, override := range overrides {
if !helpers.Contains(validRules, override) {
if !helpers.ArrayContains(validRules, override) {
diagnostics.AddError(
"Invalid Solution Checker Rule Override",
fmt.Sprintf("The solution checker rule override '%s' is not valid. Valid rules are: %v", override, validRules),
Expand Down
Loading