Skip to content

Commit

Permalink
Merge branch 'resource-persistent-state-mgmt' into 'master'
Browse files Browse the repository at this point in the history
state mgmt in resources

See merge request cidaas-management/terraform!95
  • Loading branch information
Tujit Bora committed Jul 31, 2024
2 parents 3a6b870 + 393bd86 commit 9a7c6be
Show file tree
Hide file tree
Showing 19 changed files with 1,876 additions and 2,394 deletions.
File renamed without changes.
394 changes: 21 additions & 373 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Logo](https://raw.githubusercontent.com/Cidaas/terraform-provider-cidaas/master/logo.jpg)

## About cidaas:
## About Cidaas:
[cidaas](https://www.cidaas.com)
is a fast and secure Cloud Identity & Access Management solution that standardises what’s important and simplifies what’s complex.

Expand Down
267 changes: 136 additions & 131 deletions helpers/cidaas/app.go

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions helpers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func InterfaceArray2StringArray(interfaceArray []interface{}) []string {
result := make([]string, 0)
//nolint:forcetypeassert
for _, txt := range interfaceArray {
if txt != nil {
result = append(result, txt.(string))
}
}
return result
}

func responseToStringConvert(resp *http.Response) string {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -78,3 +68,15 @@ func TimeValueOrNull(value *time.Time) types.String {
}
return types.StringNull()
}

func MapValueOrNull(value *map[string]string) (types.Map, diag.Diagnostics) {
if value == nil || len(*value) < 1 {
return types.MapNull(types.StringType), nil
}
mapAttributes := map[string]attr.Value{}
for key, value := range *value {
mapAttributes[key] = types.StringValue(value)
}
cf, diag := types.MapValue(types.StringType, mapAttributes)
return cf, diag
}
92 changes: 92 additions & 0 deletions internal/resources/app_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package resources

import (
"fmt"

"github.com/Cidaas/terraform-provider-cidaas/helpers/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

func StringValueOrNullWithPlanValue(value *string, configValue, planValue *basetypes.StringValue, attrName string, operation int) diag.Diagnostics {
var diags diag.Diagnostics
if value != nil && *value != "" {
if !planValue.IsNull() && !planValue.IsUnknown() && *value != planValue.ValueString() && operation != READ && operation != IMPORT {
msg := fmt.Sprintf(
"The attribute '%s' can not be updated in Cidaas according to the provided configuration."+
"This could be due to the Cidaas API no longer supporting the attribute or because the attribute value might be computed based on other attributes or dependencies."+
"The state is updated with the value '%s' computed by Cidaas to prevent unnecessary errors in the provider. To prevent this warning, please update your configuration accordingly.",
attrName, *value)
summaryMsg := fmt.Sprintf("Failed to Set %s", attrName)
diags.AddWarning(summaryMsg, msg)
}
*planValue = util.StringValueOrNull(value)
} else {
if !planValue.IsNull() && !planValue.IsUnknown() && operation != READ && operation != IMPORT {
if operation == UPDATE && configValue.IsNull() && planValue.Equal(types.StringValue("")) {
return diags
}
msg := fmt.Sprintf(
"The attribute '%s' can not be updated in Cidaas according to the provided configuration."+
"This could be due to the Cidaas API no longer supporting the attribute or because the attribute value might be computed based on other attributes or dependencies."+
"The state is updated with the planned value '%s' to prevent unnecessary errors in the provider. To prevent this warning, please update your configuration accordingly.",
attrName, planValue.ValueString())

summaryMsg := fmt.Sprintf("Failed to Set %s", attrName)
diags.AddWarning(summaryMsg, msg)
} else if planValue.IsUnknown() {
*planValue = types.StringValue("")
}
}
return diags
}

func SetValueOrNullWithPlanValue(values []string, planValue *basetypes.SetValue, attrName string, operation int) diag.Diagnostics {
var diags diag.Diagnostics
if len(values) > 0 {
var temp []attr.Value
for _, v := range values {
temp = append(temp, types.StringValue(v))
}
*planValue = types.SetValueMust(types.StringType, temp)
return diags
} else if !planValue.IsNull() && len(planValue.Elements()) > 0 && operation != READ && operation != IMPORT {
msg := fmt.Sprintf(
"The attribute '%s' can not be updated in Cidaas according to the provided configuration."+
"This could be due to the Cidaas API no longer supporting the attribute or because the attribute value might be computed based on other attributes or dependencies."+
"The state is updated with the planned value '%t' to prevent unnecessary errors in the provider. To prevent this warning, please update your configuration accordingly.",
attrName, planValue.Elements())
summaryMsg := fmt.Sprintf("Failed to Set %s", attrName)
diags.AddWarning(summaryMsg, msg)
}

computedSetAttributes := []string{
"redirect_uris", "allowed_scopes", "allowed_logout_urls",
"login_providers", "allowed_web_origins", "allowed_origins", "allowed_mfa", "allowed_roles", "default_roles", "default_scopes", "pending_scopes",
}
if planValue.IsUnknown() && util.StringInSlice(attrName, computedSetAttributes) {
*planValue = types.SetValueMust(types.StringType, []attr.Value{})
}
return diags
}

func BoolValueOrNullWithPlanValue(value *bool, planValue *basetypes.BoolValue, attrName string, operation int, defautlValue bool) diag.Diagnostics {
var diags diag.Diagnostics
if value != nil {
if !planValue.IsNull() && !planValue.IsUnknown() && *value != planValue.ValueBool() && operation != READ && operation != IMPORT {
msg := fmt.Sprintf(
"The attribute '%s' can not be updated in Cidaas according to the provided configuration."+
"This could be due to the Cidaas API no longer supporting the attribute or because the attribute value might be computed based on other attributes or dependencies."+
"The state is updated with the value '%t' computed by Cidaas to prevent unnecessary errors in the provider. To prevent this warning, please update your configuration accordingly.",
attrName, planValue.ValueBool())
summaryMsg := fmt.Sprintf("Failed to Set %s", attrName)
diags.AddWarning(summaryMsg, msg)
}
*planValue = types.BoolValue(*value)
} else if !(!planValue.IsNull() && !planValue.IsUnknown()) {
*planValue = types.BoolValue(defautlValue)
}
return diags
}
Loading

0 comments on commit 9a7c6be

Please sign in to comment.