Skip to content

Commit

Permalink
update OptionalStringListValue and refactor code using it (#400)
Browse files Browse the repository at this point in the history
* update OptionalStringListValue and refactor code using it

* remove unneeded changie log
  • Loading branch information
davidbloss authored Jul 12, 2024
1 parent 83b7a11 commit ea6b6d9
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 174 deletions.
16 changes: 7 additions & 9 deletions opslevel/datasource_opslevel_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -85,19 +84,19 @@ 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),
Id: ComputedStringValue(string(domain.Id)),
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) {
Expand Down Expand Up @@ -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)...)
}
4 changes: 1 addition & 3 deletions opslevel/datasource_opslevel_domains_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions opslevel/datasource_opslevel_scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions opslevel/datasource_opslevel_scorecards_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
30 changes: 10 additions & 20 deletions opslevel/datasource_opslevel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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
}
Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions opslevel/datasource_opslevel_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -88,19 +87,19 @@ 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),
Domain: ComputedStringValue(string(system.Parent.Id)),
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) {
Expand Down Expand Up @@ -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)...)
}
4 changes: 1 addition & 3 deletions opslevel/datasource_opslevel_systems_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 7 additions & 9 deletions opslevel/datasource_opslevel_webhook_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions opslevel/datasource_opslevel_webhook_actions_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 6 additions & 10 deletions opslevel/resource_opslevel_check_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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)...)
Expand Down Expand Up @@ -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)...)
Expand Down
Loading

0 comments on commit ea6b6d9

Please sign in to comment.