From ea6b6d99d838e0056af350e384419331659de9e0 Mon Sep 17 00:00:00 2001 From: David Bloss Date: Fri, 12 Jul 2024 09:59:06 -0700 Subject: [PATCH] update OptionalStringListValue and refactor code using it (#400) * update OptionalStringListValue and refactor code using it * remove unneeded changie log --- opslevel/datasource_opslevel_domain.go | 16 ++++----- opslevel/datasource_opslevel_domains_all.go | 4 +-- opslevel/datasource_opslevel_scorecard.go | 9 +++-- .../datasource_opslevel_scorecards_all.go | 3 +- opslevel/datasource_opslevel_service.go | 30 ++++++----------- opslevel/datasource_opslevel_system.go | 16 ++++----- opslevel/datasource_opslevel_systems_all.go | 4 +-- .../datasource_opslevel_webhook_action.go | 16 ++++----- ...datasource_opslevel_webhook_actions_all.go | 4 +-- ...resource_opslevel_check_repository_file.go | 16 ++++----- ...resource_opslevel_check_repository_grep.go | 17 ++++------ opslevel/resource_opslevel_domain.go | 17 ++++------ opslevel/resource_opslevel_integration_aws.go | 25 ++++---------- opslevel/resource_opslevel_scorecard.go | 33 ++++--------------- opslevel/resource_opslevel_system.go | 19 ++++------- .../resource_opslevel_trigger_definition.go | 20 ++--------- opslevel/terraform_type_conversions.go | 14 +++++--- 17 files changed, 89 insertions(+), 174 deletions(-) diff --git a/opslevel/datasource_opslevel_domain.go b/opslevel/datasource_opslevel_domain.go index 0e277daa..95a0c773 100644 --- a/opslevel/datasource_opslevel_domain.go +++ b/opslevel/datasource_opslevel_domain.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/opslevel/opslevel-go/v2024" @@ -75,8 +74,8 @@ type domainDataSourceModelWithIdentifier struct { } // newDomainDataSourceModelWithIdentifier used for a single Domain -func newDomainDataSourceModelWithIdentifier(ctx context.Context, domain opslevel.Domain, identifier types.String) (domainDataSourceModelWithIdentifier, diag.Diagnostics) { - domainAliases, diags := OptionalStringListValue(ctx, domain.Aliases) +func newDomainDataSourceModelWithIdentifier(domain opslevel.Domain, identifier types.String) domainDataSourceModelWithIdentifier { + domainAliases := OptionalStringListValue(domain.Aliases) domainDataSourceModelWithIdentifier := domainDataSourceModelWithIdentifier{ Aliases: domainAliases, Description: ComputedStringValue(domain.Description), @@ -85,11 +84,11 @@ func newDomainDataSourceModelWithIdentifier(ctx context.Context, domain opslevel Name: ComputedStringValue(domain.Name), Owner: ComputedStringValue(string(domain.Owner.Id())), } - return domainDataSourceModelWithIdentifier, diags + return domainDataSourceModelWithIdentifier } -func newDomainDataSourceModel(ctx context.Context, domain opslevel.Domain) (domainDataSourceModel, diag.Diagnostics) { - domainAliases, diags := OptionalStringListValue(ctx, domain.Aliases) +func newDomainDataSourceModel(domain opslevel.Domain) domainDataSourceModel { + domainAliases := OptionalStringListValue(domain.Aliases) domainDataSourceModel := domainDataSourceModel{ Aliases: domainAliases, Description: ComputedStringValue(domain.Description), @@ -97,7 +96,7 @@ func newDomainDataSourceModel(ctx context.Context, domain opslevel.Domain) (doma Name: ComputedStringValue(domain.Name), Owner: ComputedStringValue(string(domain.Owner.Id())), } - return domainDataSourceModel, diags + return domainDataSourceModel } func (d *DomainDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -133,10 +132,9 @@ func (d *DomainDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read domain, got error: %s", err)) return } - domainDataModel, diags := newDomainDataSourceModelWithIdentifier(ctx, *domain, data.Identifier) + domainDataModel := newDomainDataSourceModelWithIdentifier(*domain, data.Identifier) // Save data into Terraform state tflog.Trace(ctx, "read an OpsLevel Domain data source") - resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(resp.State.Set(ctx, &domainDataModel)...) } diff --git a/opslevel/datasource_opslevel_domains_all.go b/opslevel/datasource_opslevel_domains_all.go index 789bc5b3..364366a8 100644 --- a/opslevel/datasource_opslevel_domains_all.go +++ b/opslevel/datasource_opslevel_domains_all.go @@ -32,9 +32,7 @@ func NewDomainDataSourcesAllModel(ctx context.Context, domains []opslevel.Domain var diags diag.Diagnostics domainModels := []domainDataSourceModel{} for _, domain := range domains { - domainModel, domainDiag := newDomainDataSourceModel(ctx, domain) - diags.Append(domainDiag...) - domainModels = append(domainModels, domainModel) + domainModels = append(domainModels, newDomainDataSourceModel(domain)) } return DomainDataSourcesAllModel{Domains: domainModels}, diags } diff --git a/opslevel/datasource_opslevel_scorecard.go b/opslevel/datasource_opslevel_scorecard.go index 874ddbc9..be2acc87 100644 --- a/opslevel/datasource_opslevel_scorecard.go +++ b/opslevel/datasource_opslevel_scorecard.go @@ -99,12 +99,11 @@ type scorecardDataSourceWithIdentifierModel struct { } func NewScorecardDataSourceWithIdentifierModel( - ctx context.Context, scorecard opslevel.Scorecard, identifier string, categoriesModel []categoryDataSourceModel, -) (scorecardDataSourceWithIdentifierModel, diag.Diagnostics) { - scorecardAliases, diags := OptionalStringListValue(ctx, scorecard.Aliases) +) scorecardDataSourceWithIdentifierModel { + scorecardAliases := OptionalStringListValue(scorecard.Aliases) return scorecardDataSourceWithIdentifierModel{ AffectsOverallServiceLevels: types.BoolValue(scorecard.AffectsOverallServiceLevels), Aliases: scorecardAliases, @@ -118,7 +117,7 @@ func NewScorecardDataSourceWithIdentifierModel( PassingChecks: types.Int64Value(int64(scorecard.PassingChecks)), ServiceCount: types.Int64Value(int64(scorecard.ServiceCount)), TotalChecks: types.Int64Value(int64(scorecard.ChecksCount)), - }, diags + } } func (d *ScorecardDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -157,7 +156,7 @@ func (d *ScorecardDataSource) Read(ctx context.Context, req datasource.ReadReque if diags.HasError() { return } - stateModel, diags = NewScorecardDataSourceWithIdentifierModel(ctx, *scorecard, planModel.Identifier.ValueString(), categoriesModel) + stateModel = NewScorecardDataSourceWithIdentifierModel(*scorecard, planModel.Identifier.ValueString(), categoriesModel) resp.Diagnostics.Append(diags...) // Save data into Terraform state diff --git a/opslevel/datasource_opslevel_scorecards_all.go b/opslevel/datasource_opslevel_scorecards_all.go index f6048782..a8f6089d 100644 --- a/opslevel/datasource_opslevel_scorecards_all.go +++ b/opslevel/datasource_opslevel_scorecards_all.go @@ -48,8 +48,7 @@ func NewScorecardDataSourcesAllModel(ctx context.Context, client *opslevel.Clien scorecardModels := []scorecardDataSourceModel{} for _, scorecard := range scorecards { - scorecardAliases, scorecardDiag := OptionalStringListValue(ctx, scorecard.Aliases) - diags.Append(scorecardDiag...) + scorecardAliases := OptionalStringListValue(scorecard.Aliases) categoriesModel, categoriesDiags := getCategoriesModelFromScorecard(client, &scorecard) diags.Append(categoriesDiags...) diff --git a/opslevel/datasource_opslevel_service.go b/opslevel/datasource_opslevel_service.go index 563717e4..72179180 100644 --- a/opslevel/datasource_opslevel_service.go +++ b/opslevel/datasource_opslevel_service.go @@ -56,11 +56,8 @@ type propertyDefinitionModel struct { Id types.String `tfsdk:"id"` } -func NewPropertyModel(ctx context.Context, opslevelProperty opslevel.Property) (propertyModel, diag.Diagnostics) { - aliases, diags := OptionalStringListValue(ctx, opslevelProperty.Definition.Aliases) - if diags != nil && diags.HasError() { - return propertyModel{}, diags - } +func NewPropertyModel(opslevelProperty opslevel.Property) propertyModel { + aliases := OptionalStringListValue(opslevelProperty.Definition.Aliases) propModel := propertyModel{ Definition: propertyDefinitionModel{ Id: ComputedStringValue(string(opslevelProperty.Definition.Id)), @@ -70,16 +67,14 @@ func NewPropertyModel(ctx context.Context, opslevelProperty opslevel.Property) ( if opslevelProperty.Value != nil { propModel.Value = ComputedStringValue(string(*opslevelProperty.Value)) } - return propModel, diags + return propModel } func NewPropertiesAllModel(ctx context.Context, opslevelProperties []opslevel.Property) ([]propertyModel, diag.Diagnostics) { var diags diag.Diagnostics propertiesModel := []propertyModel{} for _, property := range opslevelProperties { - propertyModel, propertyDiag := NewPropertyModel(ctx, property) - diags.Append(propertyDiag...) - propertiesModel = append(propertiesModel, propertyModel) + propertiesModel = append(propertiesModel, NewPropertyModel(property)) } return propertiesModel, diags } @@ -106,9 +101,7 @@ var opslevelPropertyAttrs = map[string]schema.Attribute{ }, } -func NewServiceDataSourceModel(ctx context.Context, service opslevel.Service, alias string) (ServiceDataSourceModel, diag.Diagnostics) { - var diags diag.Diagnostics - +func NewServiceDataSourceModel(ctx context.Context, service opslevel.Service, alias string) ServiceDataSourceModel { serviceDataSourceModel := ServiceDataSourceModel{ Alias: OptionalStringValue(alias), ApiDocumentPath: ComputedStringValue(service.ApiDocumentPath), @@ -124,8 +117,7 @@ func NewServiceDataSourceModel(ctx context.Context, service opslevel.Service, al TierAlias: ComputedStringValue(service.Tier.Alias), } - serviceAliases, svcDiags := OptionalStringListValue(ctx, service.Aliases) - diags = append(diags, svcDiags...) + serviceAliases := OptionalStringListValue(service.Aliases) serviceDataSourceModel.Aliases = serviceAliases if service.PreferredApiDocumentSource != nil { @@ -135,12 +127,10 @@ func NewServiceDataSourceModel(ctx context.Context, service opslevel.Service, al if service.Tags == nil { serviceDataSourceModel.Tags = types.ListNull(types.StringType) } else { - serviceTags, tagsDiags := types.ListValueFrom(ctx, types.StringType, flattenTagArray(service.Tags.Nodes)) - serviceDataSourceModel.Tags = serviceTags - diags = append(diags, tagsDiags...) + serviceDataSourceModel.Tags = OptionalStringListValue(flattenTagArray(service.Tags.Nodes)) } - return serviceDataSourceModel, diags + return serviceDataSourceModel } func (d *ServiceDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -233,6 +223,7 @@ func (d *ServiceDataSource) Schema(ctx context.Context, req datasource.SchemaReq } func (d *ServiceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var diags diag.Diagnostics var planModel, stateModel ServiceDataSourceModel var service opslevel.Service var err error @@ -256,8 +247,7 @@ func (d *ServiceDataSource) Read(ctx context.Context, req datasource.ReadRequest return } - stateModel, diags := NewServiceDataSourceModel(ctx, service, planModel.Alias.ValueString()) - resp.Diagnostics.Append(diags...) + stateModel = NewServiceDataSourceModel(ctx, service, planModel.Alias.ValueString()) // NOTE: service's hydrate does not populate properties properties, err := service.GetProperties(d.client, nil) diff --git a/opslevel/datasource_opslevel_system.go b/opslevel/datasource_opslevel_system.go index 2f250c0a..e92123df 100644 --- a/opslevel/datasource_opslevel_system.go +++ b/opslevel/datasource_opslevel_system.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/opslevel/opslevel-go/v2024" @@ -78,8 +77,8 @@ type systemDataSourceModelWithIdentifier struct { Owner types.String `tfsdk:"owner"` } -func newSystemDataSourceModelWithIdentifier(ctx context.Context, system opslevel.System, identifier types.String) (systemDataSourceModelWithIdentifier, diag.Diagnostics) { - aliases, diags := OptionalStringListValue(ctx, system.Aliases) +func newSystemDataSourceModelWithIdentifier(system opslevel.System, identifier types.String) systemDataSourceModelWithIdentifier { + aliases := OptionalStringListValue(system.Aliases) return systemDataSourceModelWithIdentifier{ Aliases: aliases, Description: ComputedStringValue(system.Description), @@ -88,11 +87,11 @@ func newSystemDataSourceModelWithIdentifier(ctx context.Context, system opslevel Identifier: identifier, Name: ComputedStringValue(system.Name), Owner: ComputedStringValue(string(system.Owner.Id())), - }, diags + } } -func newSystemDataSourceModel(ctx context.Context, system opslevel.System) (systemDataSourceModel, diag.Diagnostics) { - aliases, diags := OptionalStringListValue(ctx, system.Aliases) +func newSystemDataSourceModel(system opslevel.System) systemDataSourceModel { + aliases := OptionalStringListValue(system.Aliases) return systemDataSourceModel{ Aliases: aliases, Description: ComputedStringValue(system.Description), @@ -100,7 +99,7 @@ func newSystemDataSourceModel(ctx context.Context, system opslevel.System) (syst Id: ComputedStringValue(string(system.Id)), Name: ComputedStringValue(system.Name), Owner: ComputedStringValue(string(system.Owner.Id())), - }, diags + } } func (sys *SystemDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -135,10 +134,9 @@ func (sys *SystemDataSource) Read(ctx context.Context, req datasource.ReadReques resp.Diagnostics.AddError("Client Error", fmt.Sprintf("unable to read system, got error: %s", err)) return } - systemDataModel, diags := newSystemDataSourceModelWithIdentifier(ctx, *system, data.Identifier) + systemDataModel := newSystemDataSourceModelWithIdentifier(*system, data.Identifier) // Save data into Terraform state tflog.Trace(ctx, "read an OpsLevel System data source") - resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(resp.State.Set(ctx, &systemDataModel)...) } diff --git a/opslevel/datasource_opslevel_systems_all.go b/opslevel/datasource_opslevel_systems_all.go index 1521f212..9defe443 100644 --- a/opslevel/datasource_opslevel_systems_all.go +++ b/opslevel/datasource_opslevel_systems_all.go @@ -29,9 +29,7 @@ func NewSystemDataSourcesAllModel(ctx context.Context, systems []opslevel.System var diags diag.Diagnostics systemModels := make([]systemDataSourceModel, 0) for _, system := range systems { - systemModel, systemDiag := newSystemDataSourceModel(ctx, system) - diags.Append(systemDiag...) - systemModels = append(systemModels, systemModel) + systemModels = append(systemModels, newSystemDataSourceModel(system)) } return SystemDataSourcesAllModel{Systems: systemModels}, diags } diff --git a/opslevel/datasource_opslevel_webhook_action.go b/opslevel/datasource_opslevel_webhook_action.go index 5879ffab..169b5767 100644 --- a/opslevel/datasource_opslevel_webhook_action.go +++ b/opslevel/datasource_opslevel_webhook_action.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -65,8 +64,8 @@ func jsonToMapValue(json map[string]any) basetypes.MapValue { return types.MapValueMust(types.StringType, jsonAttrs) } -func newWebhookActionWithIdentifierDataSourceModel(ctx context.Context, webhookAction opslevel.CustomActionsExternalAction, identifier string) (webhookActionWithIdentifierDataSourceModel, diag.Diagnostics) { - aliases, diags := OptionalStringListValue(ctx, webhookAction.Aliases) +func newWebhookActionWithIdentifierDataSourceModel(webhookAction opslevel.CustomActionsExternalAction, identifier string) webhookActionWithIdentifierDataSourceModel { + aliases := OptionalStringListValue(webhookAction.Aliases) action := webhookActionWithIdentifierDataSourceModel{ Aliases: aliases, Description: types.StringValue(webhookAction.Description), @@ -79,11 +78,11 @@ func newWebhookActionWithIdentifierDataSourceModel(ctx context.Context, webhookA Url: types.StringValue(webhookAction.CustomActionsWebhookAction.WebhookURL), } - return action, diags + return action } -func newWebhookActionDataSourceModel(ctx context.Context, webhookAction opslevel.CustomActionsExternalAction) (webhookActionDataSourceModel, diag.Diagnostics) { - aliases, diags := OptionalStringListValue(ctx, webhookAction.Aliases) +func newWebhookActionDataSourceModel(webhookAction opslevel.CustomActionsExternalAction) webhookActionDataSourceModel { + aliases := OptionalStringListValue(webhookAction.Aliases) action := webhookActionDataSourceModel{ Aliases: aliases, Description: types.StringValue(webhookAction.Description), @@ -95,7 +94,7 @@ func newWebhookActionDataSourceModel(ctx context.Context, webhookAction opslevel Url: types.StringValue(webhookAction.CustomActionsWebhookAction.WebhookURL), } - return action, diags + return action } var webhookActionDatasourceSchemaAttrs = map[string]schema.Attribute{ @@ -173,8 +172,7 @@ func (d *WebhookActionDataSource) Read(ctx context.Context, req datasource.ReadR resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read webhookAction datasource, got error: %s", err)) return } - webhookActionDataModel, diags := newWebhookActionWithIdentifierDataSourceModel(ctx, *webhookAction, data.Identifier.ValueString()) - resp.Diagnostics.Append(diags...) + webhookActionDataModel := newWebhookActionWithIdentifierDataSourceModel(*webhookAction, data.Identifier.ValueString()) if resp.Diagnostics.HasError() { return } diff --git a/opslevel/datasource_opslevel_webhook_actions_all.go b/opslevel/datasource_opslevel_webhook_actions_all.go index 33bc4413..1461478b 100644 --- a/opslevel/datasource_opslevel_webhook_actions_all.go +++ b/opslevel/datasource_opslevel_webhook_actions_all.go @@ -30,9 +30,7 @@ func newWebhookActionDataSourcesAllModel(ctx context.Context, webhookActions []o var diags diag.Diagnostics webhookActionModels := make([]webhookActionDataSourceModel, 0) for _, webhookAction := range webhookActions { - webhookActionModel, tmpDiags := newWebhookActionDataSourceModel(ctx, webhookAction) - diags.Append(tmpDiags...) - webhookActionModels = append(webhookActionModels, webhookActionModel) + webhookActionModels = append(webhookActionModels, newWebhookActionDataSourceModel(webhookAction)) } return webhookActionDataSourcesAllModel{WebhookActions: webhookActionModels}, diags } diff --git a/opslevel/resource_opslevel_check_repository_file.go b/opslevel/resource_opslevel_check_repository_file.go index 96a4a68d..87023aab 100644 --- a/opslevel/resource_opslevel_check_repository_file.go +++ b/opslevel/resource_opslevel_check_repository_file.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-framework/attr" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -48,7 +47,7 @@ type CheckRepositoryFileResourceModel struct { UseAbsoluteRoot types.Bool `tfsdk:"use_absolute_root"` } -func NewCheckRepositoryFileResourceModel(ctx context.Context, check opslevel.Check, planModel CheckRepositoryFileResourceModel) (CheckRepositoryFileResourceModel, diag.Diagnostics) { +func NewCheckRepositoryFileResourceModel(ctx context.Context, check opslevel.Check, planModel CheckRepositoryFileResourceModel) CheckRepositoryFileResourceModel { var stateModel CheckRepositoryFileResourceModel stateModel.Category = RequiredStringValue(string(check.Category.Id)) @@ -72,8 +71,7 @@ func NewCheckRepositoryFileResourceModel(ctx context.Context, check opslevel.Che stateModel.Owner = OptionalStringValue(string(check.Owner.Team.Id)) stateModel.DirectorySearch = RequiredBoolValue(check.RepositoryFileCheckFragment.DirectorySearch) - data, diags := types.ListValueFrom(ctx, types.StringType, check.RepositoryFileCheckFragment.Filepaths) - stateModel.Filepaths = data + stateModel.Filepaths = OptionalStringListValue(check.RepositoryFileCheckFragment.Filepaths) if check.RepositoryFileCheckFragment.FileContentsPredicate == nil { stateModel.FileContentsPredicate = types.ObjectNull(predicateType) @@ -87,7 +85,7 @@ func NewCheckRepositoryFileResourceModel(ctx context.Context, check opslevel.Che } stateModel.UseAbsoluteRoot = RequiredBoolValue(check.RepositoryFileCheckFragment.UseAbsoluteRoot) - return stateModel, diags + return stateModel } func (r *CheckRepositoryFileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -185,7 +183,7 @@ func (r *CheckRepositoryFileResource) Create(ctx context.Context, req resource.C return } - stateModel, diags := NewCheckRepositoryFileResourceModel(ctx, *data, planModel) + stateModel := NewCheckRepositoryFileResourceModel(ctx, *data, planModel) resp.Diagnostics.Append(diags...) tflog.Trace(ctx, "created a check repository file resource") @@ -207,9 +205,8 @@ func (r *CheckRepositoryFileResource) Read(ctx context.Context, req resource.Rea resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check repository file, got error: %s", err)) return } - stateModel, diags := NewCheckRepositoryFileResourceModel(ctx, *data, planModel) + stateModel := NewCheckRepositoryFileResourceModel(ctx, *data, planModel) stateModel.EnableOn = planModel.EnableOn - resp.Diagnostics.Append(diags...) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) @@ -267,8 +264,7 @@ func (r *CheckRepositoryFileResource) Update(ctx context.Context, req resource.U return } - stateModel, diags := NewCheckRepositoryFileResourceModel(ctx, *data, planModel) - resp.Diagnostics.Append(diags...) + stateModel := NewCheckRepositoryFileResourceModel(ctx, *data, planModel) tflog.Trace(ctx, "updated a check repository file resource") resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) diff --git a/opslevel/resource_opslevel_check_repository_grep.go b/opslevel/resource_opslevel_check_repository_grep.go index 9a74f5f2..add4a318 100644 --- a/opslevel/resource_opslevel_check_repository_grep.go +++ b/opslevel/resource_opslevel_check_repository_grep.go @@ -6,7 +6,6 @@ import ( "slices" "github.com/hashicorp/terraform-plugin-framework/attr" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -48,7 +47,7 @@ type CheckRepositoryGrepResourceModel struct { FileContentsPredicate types.Object `tfsdk:"file_contents_predicate"` } -func NewCheckRepositoryGrepResourceModel(ctx context.Context, check opslevel.Check, planModel CheckRepositoryGrepResourceModel) (CheckRepositoryGrepResourceModel, diag.Diagnostics) { +func NewCheckRepositoryGrepResourceModel(ctx context.Context, check opslevel.Check, planModel CheckRepositoryGrepResourceModel) CheckRepositoryGrepResourceModel { var stateModel CheckRepositoryGrepResourceModel stateModel.Category = RequiredStringValue(string(check.Category.Id)) @@ -72,8 +71,7 @@ func NewCheckRepositoryGrepResourceModel(ctx context.Context, check opslevel.Che stateModel.Owner = OptionalStringValue(string(check.Owner.Team.Id)) stateModel.DirectorySearch = RequiredBoolValue(check.RepositoryGrepCheckFragment.DirectorySearch) - data, diags := types.ListValueFrom(ctx, types.StringType, check.RepositoryGrepCheckFragment.Filepaths) - stateModel.Filepaths = data + stateModel.Filepaths = OptionalStringListValue(check.RepositoryGrepCheckFragment.Filepaths) predicate := check.RepositoryGrepCheckFragment.FileContentsPredicate predicateAttrValues := map[string]attr.Value{ @@ -82,7 +80,7 @@ func NewCheckRepositoryGrepResourceModel(ctx context.Context, check opslevel.Che } stateModel.FileContentsPredicate = types.ObjectValueMust(predicateType, predicateAttrValues) - return stateModel, diags + return stateModel } func (r *CheckRepositoryGrepResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -171,8 +169,7 @@ func (r *CheckRepositoryGrepResource) Create(ctx context.Context, req resource.C return } - stateModel, diags := NewCheckRepositoryGrepResourceModel(ctx, *data, planModel) - resp.Diagnostics.Append(diags...) + stateModel := NewCheckRepositoryGrepResourceModel(ctx, *data, planModel) tflog.Trace(ctx, "created a check repository grep resource") resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) @@ -193,8 +190,7 @@ func (r *CheckRepositoryGrepResource) Read(ctx context.Context, req resource.Rea resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read check repository grep, got error: %s", err)) return } - stateModel, diags := NewCheckRepositoryGrepResourceModel(ctx, *data, planModel) - resp.Diagnostics.Append(diags...) + stateModel := NewCheckRepositoryGrepResourceModel(ctx, *data, planModel) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) @@ -241,8 +237,7 @@ func (r *CheckRepositoryGrepResource) Update(ctx context.Context, req resource.U return } - stateModel, diags := NewCheckRepositoryGrepResourceModel(ctx, *data, planModel) - resp.Diagnostics.Append(diags...) + stateModel := NewCheckRepositoryGrepResourceModel(ctx, *data, planModel) tflog.Trace(ctx, "updated a check repository grep resource") resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) diff --git a/opslevel/resource_opslevel_domain.go b/opslevel/resource_opslevel_domain.go index 960f0d6f..53d13d16 100644 --- a/opslevel/resource_opslevel_domain.go +++ b/opslevel/resource_opslevel_domain.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -39,10 +38,9 @@ type DomainResourceModel struct { Owner types.String `tfsdk:"owner"` } -func NewDomainResourceModel(ctx context.Context, domain opslevel.Domain, givenModel DomainResourceModel) (DomainResourceModel, diag.Diagnostics) { - domainAliases, diags := types.ListValueFrom(ctx, types.StringType, domain.Aliases) +func NewDomainResourceModel(ctx context.Context, domain opslevel.Domain, givenModel DomainResourceModel) DomainResourceModel { domainResourceModel := DomainResourceModel{ - Aliases: domainAliases, + Aliases: OptionalStringListValue(domain.Aliases), Description: StringValueFromResourceAndModelField(domain.Description, givenModel.Description), Id: ComputedStringValue(string(domain.Id)), Name: RequiredStringValue(domain.Name), @@ -50,7 +48,7 @@ func NewDomainResourceModel(ctx context.Context, domain opslevel.Domain, givenMo Owner: StringValueFromResourceAndModelField(string(domain.Owner.Id()), givenModel.Owner), } - return domainResourceModel, diags + return domainResourceModel } func (r *DomainResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -116,8 +114,7 @@ func (r *DomainResource) Create(ctx context.Context, req resource.CreateRequest, resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to create domain, got error: %s", err)) return } - createdDomainResourceModel, diags := NewDomainResourceModel(ctx, *resource, planModel) - resp.Diagnostics.Append(diags...) + createdDomainResourceModel := NewDomainResourceModel(ctx, *resource, planModel) tflog.Trace(ctx, "created a domain resource") resp.Diagnostics.Append(resp.State.Set(ctx, &createdDomainResourceModel)...) @@ -138,8 +135,7 @@ func (r *DomainResource) Read(ctx context.Context, req resource.ReadRequest, res resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read domain, got error: %s", err)) return } - readDomainResourceModel, diags := NewDomainResourceModel(ctx, *resource, stateModel) - resp.Diagnostics.Append(diags...) + readDomainResourceModel := NewDomainResourceModel(ctx, *resource, stateModel) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &readDomainResourceModel)...) @@ -165,8 +161,7 @@ func (r *DomainResource) Update(ctx context.Context, req resource.UpdateRequest, resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to update domain, got error: %s", err)) return } - updatedDomainResourceModel, diags := NewDomainResourceModel(ctx, *resource, planModel) - resp.Diagnostics.Append(diags...) + updatedDomainResourceModel := NewDomainResourceModel(ctx, *resource, planModel) tflog.Trace(ctx, "updated a domain resource") resp.Diagnostics.Append(resp.State.Set(ctx, &updatedDomainResourceModel)...) diff --git a/opslevel/resource_opslevel_integration_aws.go b/opslevel/resource_opslevel_integration_aws.go index 4295eba4..2a378000 100644 --- a/opslevel/resource_opslevel_integration_aws.go +++ b/opslevel/resource_opslevel_integration_aws.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -40,7 +39,7 @@ type IntegrationAwsResourceModel struct { OwnershipTagKeys types.List `tfsdk:"ownership_tag_keys"` } -func NewIntegrationAwsResourceModel(ctx context.Context, awsIntegration opslevel.Integration) (IntegrationAwsResourceModel, diag.Diagnostics) { +func NewIntegrationAwsResourceModel(awsIntegration opslevel.Integration) IntegrationAwsResourceModel { integrationAwsResourceModel := IntegrationAwsResourceModel{ ExternalID: RequiredStringValue(awsIntegration.ExternalID), IamRole: RequiredStringValue(awsIntegration.IAMRole), @@ -48,10 +47,10 @@ func NewIntegrationAwsResourceModel(ctx context.Context, awsIntegration opslevel Name: OptionalStringValue(awsIntegration.Name), OwnershipTagOverrides: types.BoolValue(awsIntegration.OwnershipTagOverride), } - ownershipTagKeys, diags := OptionalStringListValue(ctx, awsIntegration.OwnershipTagKeys) + ownershipTagKeys := OptionalStringListValue(awsIntegration.OwnershipTagKeys) integrationAwsResourceModel.OwnershipTagKeys = ownershipTagKeys - return integrationAwsResourceModel, diags + return integrationAwsResourceModel } func (r *IntegrationAwsResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -127,11 +126,7 @@ func (r *IntegrationAwsResource) Create(ctx context.Context, req resource.Create return } - stateModel, diags := NewIntegrationAwsResourceModel(ctx, *awsIntegration) - if diags != nil && diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } + stateModel := NewIntegrationAwsResourceModel(*awsIntegration) tflog.Trace(ctx, "created an AWS integration resource") resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) @@ -152,11 +147,7 @@ func (r *IntegrationAwsResource) Read(ctx context.Context, req resource.ReadRequ return } - verifiedStateModel, diags := NewIntegrationAwsResourceModel(ctx, *awsIntegration) - if diags != nil && diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } + verifiedStateModel := NewIntegrationAwsResourceModel(*awsIntegration) // Save updated data into Terraform state tflog.Trace(ctx, "read an AWS integration resource") @@ -191,11 +182,7 @@ func (r *IntegrationAwsResource) Update(ctx context.Context, req resource.Update return } - stateModel, diags := NewIntegrationAwsResourceModel(ctx, *awsIntegration) - if diags != nil && diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } + stateModel := NewIntegrationAwsResourceModel(*awsIntegration) tflog.Trace(ctx, "updated an AWS integration resource") resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) diff --git a/opslevel/resource_opslevel_scorecard.go b/opslevel/resource_opslevel_scorecard.go index b0460262..33461fce 100644 --- a/opslevel/resource_opslevel_scorecard.go +++ b/opslevel/resource_opslevel_scorecard.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -44,8 +43,7 @@ type ScorecardResourceModel struct { TotalChecks types.Int64 `tfsdk:"total_checks"` } -func NewScorecardResourceModel(ctx context.Context, scorecard opslevel.Scorecard, categoryIds []string, givenModel ScorecardResourceModel) (ScorecardResourceModel, diag.Diagnostics) { - var diags diag.Diagnostics +func NewScorecardResourceModel(ctx context.Context, scorecard opslevel.Scorecard, categoryIds []string, givenModel ScorecardResourceModel) ScorecardResourceModel { scorecardDataSourceModel := ScorecardResourceModel{ AffectsOverallServiceLevels: types.BoolValue(scorecard.AffectsOverallServiceLevels), Description: StringValueFromResourceAndModelField(scorecard.Description, givenModel.Description), @@ -58,15 +56,10 @@ func NewScorecardResourceModel(ctx context.Context, scorecard opslevel.Scorecard TotalChecks: types.Int64Value(int64(scorecard.ChecksCount)), } - scorecardCategoryIds, idsDiags := types.ListValueFrom(ctx, types.StringType, categoryIds) - diags.Append(idsDiags...) - scorecardDataSourceModel.CategoryIds = scorecardCategoryIds + scorecardDataSourceModel.CategoryIds = OptionalStringListValue(categoryIds) + scorecardDataSourceModel.Aliases = OptionalStringListValue(scorecard.Aliases) - scorecardAliases, aliasesDiags := types.ListValueFrom(ctx, types.StringType, scorecard.Aliases) - diags.Append(aliasesDiags...) - scorecardDataSourceModel.Aliases = scorecardAliases - - return scorecardDataSourceModel, diags + return scorecardDataSourceModel } func (r *ScorecardResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -159,11 +152,7 @@ func (r *ScorecardResource) Create(ctx context.Context, req resource.CreateReque if err != nil { resp.Diagnostics.AddWarning("opslevel client error", fmt.Sprintf("Unable to retrieve category ids from scorecard, got error: %s", err)) } - createdScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *scorecard, categoryIds, planModel) - if diags != nil && diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } + createdScorecardResourceModel := NewScorecardResourceModel(ctx, *scorecard, categoryIds, planModel) tflog.Trace(ctx, "created a scorecard resource") resp.Diagnostics.Append(resp.State.Set(ctx, &createdScorecardResourceModel)...) @@ -202,11 +191,7 @@ func (r *ScorecardResource) Read(ctx context.Context, req resource.ReadRequest, if err != nil { resp.Diagnostics.AddWarning("opslevel client error", fmt.Sprintf("Unable to retrieve category ids from scorecard, got error: %s", err)) } - readScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *readScorecard, categoryIds, stateModel) - if diags != nil && diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } + readScorecardResourceModel := NewScorecardResourceModel(ctx, *readScorecard, categoryIds, stateModel) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &readScorecardResourceModel)...) @@ -236,11 +221,7 @@ func (r *ScorecardResource) Update(ctx context.Context, req resource.UpdateReque if err != nil { resp.Diagnostics.AddWarning("opslevel client error", fmt.Sprintf("Unable to retrieve category ids from scorecard, got error: %s", err)) } - updatedScorecardResourceModel, diags := NewScorecardResourceModel(ctx, *scorecard, categoryIds, planModel) - if diags != nil && diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } + updatedScorecardResourceModel := NewScorecardResourceModel(ctx, *scorecard, categoryIds, planModel) tflog.Trace(ctx, "updated a scorecard resource") resp.Diagnostics.Append(resp.State.Set(ctx, &updatedScorecardResourceModel)...) diff --git a/opslevel/resource_opslevel_system.go b/opslevel/resource_opslevel_system.go index 94d1adea..b721920e 100644 --- a/opslevel/resource_opslevel_system.go +++ b/opslevel/resource_opslevel_system.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -40,8 +39,8 @@ type SystemResourceModel struct { Owner types.String `tfsdk:"owner"` } -func NewSystemResourceModel(ctx context.Context, system opslevel.System, givenModel SystemResourceModel) (SystemResourceModel, diag.Diagnostics) { - aliases, diags := OptionalStringListValue(ctx, system.Aliases) +func NewSystemResourceModel(system opslevel.System, givenModel SystemResourceModel) SystemResourceModel { + aliases := OptionalStringListValue(system.Aliases) systemDataSourceModel := SystemResourceModel{ Aliases: aliases, Description: StringValueFromResourceAndModelField(system.Description, givenModel.Description), @@ -51,7 +50,7 @@ func NewSystemResourceModel(ctx context.Context, system opslevel.System, givenMo Note: StringValueFromResourceAndModelField(system.Note, givenModel.Note), Owner: OptionalStringValue(string(system.Owner.Id())), } - return systemDataSourceModel, diags + return systemDataSourceModel } func (r *SystemResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { @@ -126,8 +125,7 @@ func (r *SystemResource) Create(ctx context.Context, req resource.CreateRequest, resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to create system, got error: %s", err)) return } - stateModel, diags := NewSystemResourceModel(ctx, *system, planModel) - resp.Diagnostics.Append(diags...) + stateModel := NewSystemResourceModel(*system, planModel) tflog.Trace(ctx, "created a system resource") resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) @@ -148,11 +146,7 @@ func (r *SystemResource) Read(ctx context.Context, req resource.ReadRequest, res resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to read system, got error: %s", err)) return } - verifiedStateModel, diags := NewSystemResourceModel(ctx, *readSystem, stateModel) - if diags != nil && diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } + verifiedStateModel := NewSystemResourceModel(*readSystem, stateModel) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &verifiedStateModel)...) @@ -184,8 +178,7 @@ func (r *SystemResource) Update(ctx context.Context, req resource.UpdateRequest, resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to update system, got error: %s", err)) return } - stateModel, diags := NewSystemResourceModel(ctx, *system, planModel) - resp.Diagnostics.Append(diags...) + stateModel := NewSystemResourceModel(*system, planModel) tflog.Trace(ctx, "updated a system resource") resp.Diagnostics.Append(resp.State.Set(ctx, &stateModel)...) diff --git a/opslevel/resource_opslevel_trigger_definition.go b/opslevel/resource_opslevel_trigger_definition.go index 5f8f7d5c..534dddf3 100644 --- a/opslevel/resource_opslevel_trigger_definition.go +++ b/opslevel/resource_opslevel_trigger_definition.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -197,11 +196,7 @@ func (r *TriggerDefinitionResource) Create(ctx context.Context, req resource.Cre resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to get teams for 'extended_team_access', got error: %s", err)) return } - extendedTeamsAccess, diags := OptionalStringListValue(ctx, flattenTeamsArray(extendedTeams)) - if diags.HasError() { - resp.Diagnostics.AddError("opslevel client error", "failed to convert 'extendedTeams' to 'basetypes.ListValue'") - return - } + extendedTeamsAccess := OptionalStringListValue(flattenTeamsArray(extendedTeams)) stateModel := NewTriggerDefinitionResourceModel(*triggerDefinition, extendedTeamsAccess) tflog.Trace(ctx, "created a trigger definition resource") @@ -210,7 +205,6 @@ func (r *TriggerDefinitionResource) Create(ctx context.Context, req resource.Cre func (r *TriggerDefinitionResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var planModel TriggerDefinitionResourceModel - var diags diag.Diagnostics // Read Terraform prior state data into the model resp.Diagnostics.Append(req.State.Get(ctx, &planModel)...) @@ -229,11 +223,7 @@ func (r *TriggerDefinitionResource) Read(ctx context.Context, req resource.ReadR resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to get teams for 'extended_team_access', got error: %s", err)) return } - extendedTeamsAccess, diags := OptionalStringListValue(ctx, flattenTeamsArray(extendedTeams)) - if diags.HasError() { - resp.Diagnostics.AddError("opslevel client error", "failed to convert 'extendedTeams' to 'basetypes.ListValue'") - return - } + extendedTeamsAccess := OptionalStringListValue(flattenTeamsArray(extendedTeams)) stateModel := NewTriggerDefinitionResourceModel(*triggerDefinition, extendedTeamsAccess) // Save updated data into Terraform state @@ -283,11 +273,7 @@ func (r *TriggerDefinitionResource) Update(ctx context.Context, req resource.Upd resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to get teams for 'extended_team_access', got error: %s", err)) return } - extendedTeamsAccess, diags := OptionalStringListValue(ctx, flattenTeamsArray(extendedTeams)) - if diags.HasError() { - resp.Diagnostics.AddError("opslevel client error", "failed to convert 'extendedTeams' to 'basetypes.ListValue'") - return - } + extendedTeamsAccess := OptionalStringListValue(flattenTeamsArray(extendedTeams)) stateModel := NewTriggerDefinitionResourceModel(*updatedTriggerDefinition, extendedTeamsAccess) tflog.Trace(ctx, "updated a trigger definition resource") diff --git a/opslevel/terraform_type_conversions.go b/opslevel/terraform_type_conversions.go index 2d24859c..e609824e 100644 --- a/opslevel/terraform_type_conversions.go +++ b/opslevel/terraform_type_conversions.go @@ -65,11 +65,17 @@ func RequiredIntValue(value int) basetypes.Int64Value { } // Returns value wrapped in a types.StringValue, or types.ListNull if blank -func OptionalStringListValue(ctx context.Context, value []string) (basetypes.ListValue, diag.Diagnostics) { - if len(value) == 0 { - return types.ListNull(types.StringType), diag.Diagnostics{} +func OptionalStringListValue(values []string) basetypes.ListValue { + if len(values) == 0 { + return types.ListNull(types.StringType) } - return types.ListValueFrom(ctx, types.StringType, value) + + elems := make([]attr.Value, len(values)) + for i, v := range values { + elems[i] = types.StringValue(v) + } + + return types.ListValueMust(types.StringType, elems) } // unquotes unwanted quotes from strings in maps, returns original value in most cases