Skip to content

Commit

Permalink
Tidy schema helper and add tests (#759)
Browse files Browse the repository at this point in the history
Refactored all schema definitions to use the same interface
Added test to ensure type correctness in schemas
  • Loading branch information
benPearce1 authored Aug 29, 2024
1 parent 572b7e3 commit 03ebf36
Show file tree
Hide file tree
Showing 74 changed files with 1,187 additions and 1,092 deletions.
5 changes: 1 addition & 4 deletions docs/data-sources/space.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ Provides information about an existing space.
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of this resource, no more than 20 characters long

### Read-Only

- `description` (String) The description of this space.
- `id` (String) The unique ID for this resource.
- `is_default` (Boolean) Specifies if this space is the default space in Octopus.
- `is_task_queue_stopped` (Boolean) Specifies the status of the task queue for this space.
- `name` (String) The name of this resource, no more than 20 characters long
- `slug` (String) The unique slug of this space
- `space_managers_team_members` (Set of String) A list of user IDs designated to be managers of this space.
- `space_managers_teams` (Set of String) A list of team IDs designated to be managers of this space.
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data "octopusdeploy_variables" "example" {

### Optional

- `space_id` (String) A Space ID to filter by. Will revert what is specified on the provider if not set.
- `space_id` (String) The space ID associated with this variable.

### Read-Only

Expand Down
5 changes: 1 addition & 4 deletions docs/resources/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ resource "octopusdeploy_environment" "example" {
- `jira_extension_settings` (Block List) Provides extension settings for the Jira integration for this environment. (see [below for nested schema](#nestedblock--jira_extension_settings))
- `jira_service_management_extension_settings` (Block List) Provides extension settings for the Jira Service Management (JSM) integration for this environment. (see [below for nested schema](#nestedblock--jira_service_management_extension_settings))
- `servicenow_extension_settings` (Block List) Provides extension settings for the ServiceNow integration for this environment. (see [below for nested schema](#nestedblock--servicenow_extension_settings))
- `slug` (String) The unique slug of this environment
- `sort_order` (Number) The order number to sort an environment.
- `space_id` (String) The space ID associated with this environment.
- `use_guided_failure` (Boolean)

### Read-Only

- `slug` (String) The unique slug of this environment

<a id="nestedblock--jira_extension_settings"></a>
### Nested Schema for `jira_extension_settings`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (l *libraryVariableSetDataSource) Metadata(ctx context.Context, req datasou

func (l *libraryVariableSetDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
tflog.Debug(ctx, "library variable set Schema")
resp.Schema = schemas.GetLibraryVariableSetDataSourceSchema()
resp.Schema = schemas.LibraryVariableSetSchema{}.GetDatasourceSchema()
}

func (l *libraryVariableSetDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/data_source_script_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (l *scriptModulesDataSource) Metadata(ctx context.Context, req datasource.M

func (l *scriptModulesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
tflog.Debug(ctx, "script modules datasource Schema")
resp.Schema = schemas.GetDatasourceScriptModuleSchema()
resp.Schema = schemas.ScriptModuleSchema{}.GetDatasourceSchema()
}

func (l *scriptModulesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
24 changes: 1 addition & 23 deletions octopusdeploy_framework/datasource_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -39,28 +38,7 @@ func (*environmentDataSource) Metadata(_ context.Context, req datasource.Metadat
}

func (*environmentDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Provides information about existing environments.",
Attributes: map[string]schema.Attribute{
//request
"ids": util.GetQueryIDsDatasourceSchema(),
"space_id": schemas.GetSpaceIdDatasourceSchema(schemas.EnvironmentResourceDescription, false),
"name": util.GetQueryNameDatasourceSchema(),
"partial_name": util.GetQueryPartialNameDatasourceSchema(),
"skip": util.GetQuerySkipDatasourceSchema(),
"take": util.GetQueryTakeDatasourceSchema(),

//response
"id": schemas.GetIdDatasourceSchema(true),
"environments": schema.ListNestedAttribute{
Computed: true,
Optional: false,
NestedObject: schema.NestedAttributeObject{
Attributes: schemas.GetEnvironmentDatasourceSchema(),
},
},
},
}
resp.Schema = schemas.EnvironmentSchema{}.GetDatasourceSchema()
}

func (e *environmentDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
6 changes: 1 addition & 5 deletions octopusdeploy_framework/datasource_feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
datasourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"time"

Expand All @@ -29,10 +28,7 @@ func (e *feedsDataSource) Configure(_ context.Context, req datasource.ConfigureR
}

func (*feedsDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = datasourceSchema.Schema{
Description: "Provides information about existing feeds.",
Attributes: schemas.GetFeedsDataSourceSchema(),
}
resp.Schema = schemas.FeedsSchema{}.GetDatasourceSchema()
}

func (e *feedsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/datasource_git_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (g *gitCredentialsDataSource) Metadata(_ context.Context, req datasource.Me
}

func (g *gitCredentialsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schemas.GetGitCredentialDataSourceSchema()
resp.Schema = schemas.GitCredentialSchema{}.GetDatasourceSchema()
}

func (g *gitCredentialsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
6 changes: 4 additions & 2 deletions octopusdeploy_framework/datasource_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package octopusdeploy_framework
import (
"context"
"fmt"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"time"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/lifecycles"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand All @@ -28,6 +28,8 @@ type lifecyclesDataSourceModel struct {
Lifecycles types.List `tfsdk:"lifecycles"`
}

var _ datasource.DataSource = &lifecyclesDataSource{}

func NewLifecyclesDataSource() datasource.DataSource {
return &lifecyclesDataSource{}
}
Expand All @@ -39,7 +41,7 @@ func (l *lifecyclesDataSource) Metadata(ctx context.Context, req datasource.Meta

func (l *lifecyclesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
tflog.Debug(ctx, "lifecycles datasource Schema")
resp.Schema = schemas.GetDatasourceLifecycleSchema()
resp.Schema = schemas.LifecycleSchema{}.GetDatasourceSchema()
}

func (l *lifecyclesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/datasource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *projectsDataSource) Metadata(_ context.Context, req datasource.Metadata
}

func (p *projectsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schemas.GetProjectDataSourceSchema()
resp.Schema = schemas.ProjectSchema{}.GetDatasourceSchema()
}

func (p *projectsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
23 changes: 1 addition & 22 deletions octopusdeploy_framework/datasource_project_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"time"
Expand Down Expand Up @@ -45,27 +44,7 @@ func (p *projectGroupsDataSource) Metadata(_ context.Context, _ datasource.Metad
}

func (p *projectGroupsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
description := "project group"
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// request
"space_id": schemas.GetSpaceIdDatasourceSchema(description, false),
"ids": util.GetQueryIDsDatasourceSchema(),
"partial_name": util.GetQueryPartialNameDatasourceSchema(),
"skip": util.GetQuerySkipDatasourceSchema(),
"take": util.GetQueryTakeDatasourceSchema(),

// response
"id": schemas.GetIdDatasourceSchema(true),
"project_groups": schema.ListNestedAttribute{
Computed: true,
Description: "A list of project groups that match the filter(s).",
NestedObject: schema.NestedAttributeObject{
Attributes: schemas.GetProjectGroupDatasourceSchema(),
},
},
},
}
resp.Schema = schemas.ProjectGroupSchema{}.GetDatasourceSchema()
}

func (p *projectGroupsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
6 changes: 1 addition & 5 deletions octopusdeploy_framework/datasource_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand All @@ -27,10 +26,7 @@ func (*spaceDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest
}

func (*spaceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Provides information about an existing space.",
Attributes: schemas.GetSpaceDatasourceSchema(),
}
resp.Schema = schemas.SpaceSchema{}.GetDatasourceSchema()
}

func (b *spaceDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
23 changes: 2 additions & 21 deletions octopusdeploy_framework/datasource_spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"time"
)
Expand All @@ -33,25 +32,7 @@ func (*spacesDataSource) Metadata(_ context.Context, _ datasource.MetadataReques
}

func (*spacesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// request
"ids": schemas.GetQueryIDsDatasourceSchema(),
"partial_name": schemas.GetQueryPartialNameDatasourceSchema(),
"skip": schemas.GetQuerySkipDatasourceSchema(),
"take": schemas.GetQueryTakeDatasourceSchema(),

// response
"id": schemas.GetIdDatasourceSchema(true),
"spaces": schema.ListNestedAttribute{
Computed: true,
Optional: false,
NestedObject: schema.NestedAttributeObject{
Attributes: schemas.GetSpacesDatasourceSchema(),
},
},
},
}
resp.Schema = schemas.SpacesSchema{}.GetDatasourceSchema()
}

func (b *spacesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand All @@ -67,7 +48,7 @@ func (b *spacesDataSource) Read(ctx context.Context, req datasource.ReadRequest,
}

query := spaces.SpacesQuery{
IDs: schemas.GetIds(data.IDs),
IDs: util.GetIds(data.IDs),
PartialName: data.PartialName.ValueString(),
Skip: schemas.GetNumber(data.Skip),
Take: schemas.GetNumber(data.Take),
Expand Down
4 changes: 2 additions & 2 deletions octopusdeploy_framework/datasource_tag_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (t *tagSetsDataSource) Metadata(_ context.Context, req datasource.MetadataR
}

func (t *tagSetsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schemas.GetTagSetDataSourceSchema()
resp.Schema = schemas.TagSetSchema{}.GetDatasourceSchema()
}

func (t *tagSetsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand All @@ -42,7 +42,7 @@ func (t *tagSetsDataSource) Read(ctx context.Context, req datasource.ReadRequest
}

query := tagsets.TagSetsQuery{
IDs: schemas.GetIds(data.IDs),
IDs: util.GetIds(data.IDs),
PartialName: data.PartialName.ValueString(),
Skip: int(data.Skip.ValueInt64()),
Take: int(data.Take.ValueInt64()),
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/datasource_tenant_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t *tenantProjectsDataSource) Configure(_ context.Context, req datasource.C
}

func (*tenantProjectsDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schemas.GetTenantProjectsDataSourceSchema()
resp.Schema = schemas.TenantProjectVariableSchema{}.GetDatasourceSchema()
}

func (t *tenantProjectsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down
6 changes: 1 addition & 5 deletions octopusdeploy_framework/datasource_tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
datasourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"time"
)
Expand All @@ -28,10 +27,7 @@ func (e *tenantsDataSource) Configure(_ context.Context, req datasource.Configur
}

func (*tenantsDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = datasourceSchema.Schema{
Description: "Provides information about existing tenants.",
Attributes: schemas.GetTenantsDataSourceSchema(),
}
resp.Schema = schemas.TenantSchema{}.GetDatasourceSchema()
}

func (b *tenantsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/datasource_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (*variablesDataSource) Metadata(_ context.Context, req datasource.MetadataR
}

func (*variablesDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schemas.GetVariableDatasourceSchema()
resp.Schema = schemas.VariableSchema{}.GetDatasourceSchema()
}

func (e *variablesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down
6 changes: 1 addition & 5 deletions octopusdeploy_framework/resource_artifactory_generic_feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds"
Expand All @@ -33,10 +32,7 @@ func (r *artifactoryGenericFeedTypeResource) Metadata(ctx context.Context, req r
}

func (r *artifactoryGenericFeedTypeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: schemas.GetArtifactoryGenericFeedResourceSchema(),
Description: "This resource manages a Artifactory Generic feed in Octopus Deploy.",
}
resp.Schema = schemas.ArtifactoryGenericFeedSchema{}.GetResourceSchema()
}

func (r *artifactoryGenericFeedTypeResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand All @@ -32,10 +31,7 @@ func (r *awsElasticContainerRegistryFeedTypeResource) Metadata(ctx context.Conte
}

func (r *awsElasticContainerRegistryFeedTypeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: schemas.GetAwsElasticContainerRegistryFeedResourceSchema(),
Description: "This resource manages an AWS Elastic Container Registry in Octopus Deploy.",
}
resp.Schema = schemas.AwsElasticContainerRegistrySchema{}.GetResourceSchema()
}

func (r *awsElasticContainerRegistryFeedTypeResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand All @@ -32,10 +31,7 @@ func (r *dockerContainerRegistryFeedTypeResource) Metadata(ctx context.Context,
}

func (r *dockerContainerRegistryFeedTypeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: schemas.GetDockerContainerRegistryFeedResourceSchema(),
Description: "This resource manages a Docker Container Registry in Octopus Deploy.",
}
resp.Schema = schemas.DockerContainerRegistryFeedSchema{}.GetResourceSchema()
}

func (r *dockerContainerRegistryFeedTypeResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *environmentTypeResource) Metadata(ctx context.Context, req resource.Met
}

func (r *environmentTypeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schemas.GetEnvironmentResourceSchema()
resp.Schema = schemas.EnvironmentSchema{}.GetResourceSchema()
}

func (r *environmentTypeResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down
Loading

0 comments on commit 03ebf36

Please sign in to comment.