Skip to content

Commit

Permalink
refactor all functions to use new generic read function
Browse files Browse the repository at this point in the history
  • Loading branch information
rocktavious committed Oct 23, 2024
1 parent 394f8e8 commit a3e7a59
Show file tree
Hide file tree
Showing 74 changed files with 326 additions and 1,024 deletions.
5 changes: 1 addition & 4 deletions opslevel/datasource_opslevel_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ func (d *DomainDataSource) Schema(ctx context.Context, req datasource.SchemaRequ
}

func (d *DomainDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data domainDataSourceModelWithIdentifier

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
data := read[domainDataSourceModelWithIdentifier](ctx, &resp.Diagnostics, req.Config)

if resp.Diagnostics.HasError() {
return
Expand Down
9 changes: 0 additions & 9 deletions opslevel/datasource_opslevel_domains_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ func (d *DomainDataSourcesAll) Schema(ctx context.Context, req datasource.Schema
}

func (d *DomainDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel DomainDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)

if resp.Diagnostics.HasError() {
return
}

domains, err := d.client.ListDomains(nil)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read domain, got error: %s", err))
Expand Down
5 changes: 1 addition & 4 deletions opslevel/datasource_opslevel_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ func (d *FilterDataSource) Schema(ctx context.Context, req datasource.SchemaRequ
}

func (d *FilterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data filterDataSourceWithFilterModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
data := read[filterDataSourceWithFilterModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand Down
10 changes: 1 addition & 9 deletions opslevel/datasource_opslevel_filters_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,12 @@ func (d *FilterDataSourcesAll) Schema(ctx context.Context, req datasource.Schema
}

func (d *FilterDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel FilterDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}

filters, err := d.client.ListFilters(nil)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read filter, got error: %s", err))
return
}
stateModel = NewFilterDataSourcesAllModel(filters.Nodes)
stateModel := NewFilterDataSourcesAllModel(filters.Nodes)

// Save data into Terraform state
tflog.Trace(ctx, "listed all OpsLevel Filter data sources")
Expand Down
7 changes: 2 additions & 5 deletions opslevel/datasource_opslevel_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ func (i *IntegrationDataSource) Schema(ctx context.Context, req datasource.Schem
}

func (i *IntegrationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel integrationDataSourceWithFilterModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
planModel := read[integrationDataSourceWithFilterModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -87,7 +84,7 @@ func (i *IntegrationDataSource) Read(ctx context.Context, req datasource.ReadReq
return
}

stateModel = NewIntegrationDataSourceModel(ctx, *integration, planModel.Filter)
stateModel := NewIntegrationDataSourceModel(ctx, *integration, planModel.Filter)

// Save data into Terraform state
tflog.Trace(ctx, "read an OpsLevel Integration data source")
Expand Down
10 changes: 1 addition & 9 deletions opslevel/datasource_opslevel_integrations_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,14 @@ func (i *IntegrationDataSourcesAll) Schema(ctx context.Context, req datasource.S
}

func (i *IntegrationDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel integrationDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}

integrations, err := i.client.ListIntegrations(nil)
if err != nil || integrations == nil || integrations.Nodes == nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("unable to list integrations, got error: %s", err))
return
}

foundIntegrations := *integrations
stateModel = NewIntegrationDataSourcesAllModel(foundIntegrations.Nodes)
stateModel := NewIntegrationDataSourcesAllModel(foundIntegrations.Nodes)

// Save data into Terraform state
tflog.Trace(ctx, "listed all integrations data sources")
Expand Down
7 changes: 2 additions & 5 deletions opslevel/datasource_opslevel_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ func (lifecycleDataSource *LifecycleDataSource) Schema(ctx context.Context, req
}

func (lifecycleDataSource *LifecycleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel lifecycleDataSourceWithFilterModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
planModel := read[lifecycleDataSourceWithFilterModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -100,7 +97,7 @@ func (lifecycleDataSource *LifecycleDataSource) Read(ctx context.Context, req da
return
}

stateModel = NewLifecycleDataSourceModel(ctx, *lifecycle, planModel.Filter)
stateModel := NewLifecycleDataSourceModel(ctx, *lifecycle, planModel.Filter)

// Save data into Terraform state
tflog.Trace(ctx, "read an OpsLevel Lifecycle data source")
Expand Down
10 changes: 1 addition & 9 deletions opslevel/datasource_opslevel_lifecycles_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,13 @@ func (d *LifecycleDataSourcesAll) Schema(ctx context.Context, req datasource.Sch
}

func (d *LifecycleDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel lifecycleDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}

lifecycles, err := d.client.ListLifecycles()
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("unable to list lifecycles, got error: %s", err))
return
}

stateModel = NewLifecycleDataSourcesAllModel(lifecycles)
stateModel := NewLifecycleDataSourcesAllModel(lifecycles)

// Save data into Terraform state
tflog.Trace(ctx, "listed all lifecycle data sources")
Expand Down
8 changes: 2 additions & 6 deletions opslevel/datasource_opslevel_property_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ func (d *PropertyDefinitionDataSource) Schema(ctx context.Context, req datasourc
}

func (d *PropertyDefinitionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel propertyDefinitionDataSourceWithFilterModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)

planModel := read[propertyDefinitionDataSourceWithFilterModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -90,7 +86,7 @@ func (d *PropertyDefinitionDataSource) Read(ctx context.Context, req datasource.
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read property definition datasource, got error: %s", err))
return
}
stateModel = NewPropertyDefinitionDataSourceWithFilterModel(*propertydefinition, planModel.Identifier.ValueString())
stateModel := NewPropertyDefinitionDataSourceWithFilterModel(*propertydefinition, planModel.Identifier.ValueString())

// Save data into Terraform state
tflog.Trace(ctx, "read an OpsLevel PropertyDefinition data source")
Expand Down
11 changes: 1 addition & 10 deletions opslevel/datasource_opslevel_property_definitions_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,12 @@ func (d *PropertyDefinitionDataSourcesAll) Schema(ctx context.Context, req datas
}

func (d *PropertyDefinitionDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel propertyDefinitionDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)

if resp.Diagnostics.HasError() {
return
}

propertyDefinitions, err := d.client.ListPropertyDefinitions(nil)
if err != nil || propertyDefinitions == nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read property definition datasource, got error: %s", err))
return
}
stateModel = NewPropertyDefinitionDataSourcesAllModel(propertyDefinitions.Nodes)
stateModel := NewPropertyDefinitionDataSourcesAllModel(propertyDefinitions.Nodes)

// Save data into Terraform state
tflog.Trace(ctx, "read an OpsLevel PropertyDefinition data source")
Expand Down
7 changes: 2 additions & 5 deletions opslevel/datasource_opslevel_repositories_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ func (d *RepositoriesDataSourcesAll) Schema(ctx context.Context, req datasource.
}

func (d *RepositoriesDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel RepositoriesDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
planModel := read[RepositoriesDataSourcesAllModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -89,7 +86,7 @@ func (d *RepositoriesDataSourcesAll) Read(ctx context.Context, req datasource.Re
return
}

stateModel = NewRepositoriesDataSourcesAllModel(repos.Nodes)
stateModel := NewRepositoriesDataSourcesAllModel(repos.Nodes)
stateModel.Filter = planModel.Filter

// Save data into Terraform state
Expand Down
5 changes: 1 addition & 4 deletions opslevel/datasource_opslevel_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ func (d *RepositoryDataSource) Schema(ctx context.Context, req datasource.Schema
}

func (d *RepositoryDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data RepositoryDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
data := read[RepositoryDataSourceModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand Down
10 changes: 1 addition & 9 deletions opslevel/datasource_opslevel_rubric_categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,12 @@ func (d *CategoryDataSourcesAll) Schema(ctx context.Context, req datasource.Sche
}

func (d *CategoryDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel CategoryDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}

categories, err := d.client.ListCategories(nil)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to list rubric_categories datasource, got error: %s", err))
return
}
stateModel = NewCategoryDataSourcesAllModel(categories.Nodes)
stateModel := NewCategoryDataSourcesAllModel(categories.Nodes)

// Save data into Terraform state
tflog.Trace(ctx, "listed all rubric_categories data sources")
Expand Down
10 changes: 3 additions & 7 deletions opslevel/datasource_opslevel_rubric_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ func (d *CategoryDataSource) Schema(ctx context.Context, req datasource.SchemaRe
}

func (d *CategoryDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel categoryDataSourceWithFilterModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)

configModel := read[categoryDataSourceWithFilterModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -81,13 +77,13 @@ func (d *CategoryDataSource) Read(ctx context.Context, req datasource.ReadReques
return
}

category, err := filterRubricCategories(categories.Nodes, planModel.Filter)
category, err := filterRubricCategories(categories.Nodes, configModel.Filter)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to filter rubric_category datasource, got error: %s", err))
return
}

stateModel = NewCategoryDataSourceWithFilterModel(*category, planModel.Filter)
stateModel := NewCategoryDataSourceWithFilterModel(*category, configModel.Filter)

// Save data into Terraform state
tflog.Trace(ctx, "read an OpsLevel Rubric Category data source")
Expand Down
10 changes: 3 additions & 7 deletions opslevel/datasource_opslevel_rubric_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ func (d *LevelDataSource) Schema(ctx context.Context, req datasource.SchemaReque
}

func (d *LevelDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel levelDataSourceWithFilterModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)

configModel := read[levelDataSourceWithFilterModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -94,13 +90,13 @@ func (d *LevelDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}

level, err := filterRubricLevels(levels, planModel.Filter)
level, err := filterRubricLevels(levels, configModel.Filter)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to filter rubric_level datasource, got error: %s", err))
return
}

stateModel = NewLevelDataSourceWithFilterModel(ctx, *level, planModel.Filter)
stateModel := NewLevelDataSourceWithFilterModel(ctx, *level, configModel.Filter)

// Save data into Terraform state
tflog.Trace(ctx, "read an OpsLevel Rubric Level data source")
Expand Down
10 changes: 1 addition & 9 deletions opslevel/datasource_opslevel_rubric_levels_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,12 @@ func (d *LevelDataSourcesAll) Schema(ctx context.Context, req datasource.SchemaR
}

func (d *LevelDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel levelDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}

levels, err := d.client.ListLevels()
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to list rubric_levels datasource, got error: %s", err))
return
}
stateModel = NewLevelDataSourcesAllModel(levels)
stateModel := NewLevelDataSourcesAllModel(levels)

// Save data into Terraform state
tflog.Trace(ctx, "listed all rubric_levels data sources")
Expand Down
7 changes: 2 additions & 5 deletions opslevel/datasource_opslevel_scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ func (d *ScorecardDataSource) Schema(ctx context.Context, req datasource.SchemaR
}

func (d *ScorecardDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel scorecardDataSourceWithIdentifierModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
planModel := read[scorecardDataSourceWithIdentifierModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -156,7 +153,7 @@ func (d *ScorecardDataSource) Read(ctx context.Context, req datasource.ReadReque
if diags.HasError() {
return
}
stateModel = NewScorecardDataSourceWithIdentifierModel(*scorecard, planModel.Identifier.ValueString(), categoriesModel)
stateModel := NewScorecardDataSourceWithIdentifierModel(*scorecard, planModel.Identifier.ValueString(), categoriesModel)
resp.Diagnostics.Append(diags...)

// Save data into Terraform state
Expand Down
8 changes: 0 additions & 8 deletions opslevel/datasource_opslevel_scorecards_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ func (d *ScorecardDataSourcesAll) Schema(ctx context.Context, req datasource.Sch
}

func (d *ScorecardDataSourcesAll) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var planModel, stateModel scorecardDataSourcesAllModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}

scorecards, err := d.client.ListScorecards(nil)
if err != nil || scorecards == nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to list scorecards datasource, got error: %s", err))
Expand Down
6 changes: 2 additions & 4 deletions opslevel/datasource_opslevel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,10 @@ 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

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &planModel)...)
planModel := read[ServiceDataSourceModel](ctx, &resp.Diagnostics, req.Config)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -247,7 +245,7 @@ func (d *ServiceDataSource) Read(ctx context.Context, req datasource.ReadRequest
return
}

stateModel = NewServiceDataSourceModel(ctx, service, planModel.Alias.ValueString())
stateModel := NewServiceDataSourceModel(ctx, service, planModel.Alias.ValueString())

// NOTE: service's hydrate does not populate properties
properties, err := service.GetProperties(d.client, nil)
Expand Down
Loading

0 comments on commit a3e7a59

Please sign in to comment.