Skip to content

Commit

Permalink
Add project data source
Browse files Browse the repository at this point in the history
  • Loading branch information
HuyPhanNguyen committed Jul 28, 2024
1 parent 9250263 commit 6d05ddf
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 266 deletions.
2 changes: 0 additions & 2 deletions octopusdeploy/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func Provider() *schema.Provider {
"octopusdeploy_machine_policies": dataSourceMachinePolicies(),
"octopusdeploy_offline_package_drop_deployment_targets": dataSourceOfflinePackageDropDeploymentTargets(),
"octopusdeploy_polling_tentacle_deployment_targets": dataSourcePollingTentacleDeploymentTargets(),
"octopusdeploy_projects": dataSourceProjects(),
"octopusdeploy_script_modules": dataSourceScriptModules(),
"octopusdeploy_ssh_connection_deployment_targets": dataSourceSSHConnectionDeploymentTargets(),
"octopusdeploy_tag_sets": dataSourceTagSets(),
Expand Down Expand Up @@ -62,7 +61,6 @@ func Provider() *schema.Provider {
"octopusdeploy_offline_package_drop_deployment_target": resourceOfflinePackageDropDeploymentTarget(),
"octopusdeploy_polling_tentacle_deployment_target": resourcePollingTentacleDeploymentTarget(),
"octopusdeploy_polling_subscription_id": resourcePollingSubscriptionId(),
"octopusdeploy_project": resourceProject(),
"octopusdeploy_project_deployment_target_trigger": resourceProjectDeploymentTargetTrigger(),
"octopusdeploy_external_feed_create_release_trigger": resourceExternalFeedCreateReleaseTrigger(),
"octopusdeploy_project_scheduled_trigger": resourceProjectScheduledTrigger(),
Expand Down
156 changes: 0 additions & 156 deletions octopusdeploy/resource_project.go

This file was deleted.

99 changes: 99 additions & 0 deletions octopusdeploy_framework/data_source_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package octopusdeploy_framework

import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
"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"
)

var _ datasource.DataSource = &projectsDataSource{}

type projectsDataSource struct {
*Config
}

type projectsDataSourceModel struct {
ID types.String `tfsdk:"id"`
SpaceID types.String `tfsdk:"space_id"`
ClonedFromProjectID types.String `tfsdk:"cloned_from_project_id"`
IDs types.List `tfsdk:"ids"`
IsClone types.Bool `tfsdk:"is_clone"`
Name types.String `tfsdk:"name"`
PartialName types.String `tfsdk:"partial_name"`
Skip types.Int64 `tfsdk:"skip"`
Take types.Int64 `tfsdk:"take"`
Projects []projectResourceModel `tfsdk:"projects"`
}

func NewProjectsDataSource() datasource.DataSource {
return &projectsDataSource{}
}

func (p *projectsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = util.GetTypeName(schemas.ProjectResourceName)
}

func (p *projectsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Provides information about existing projects.",
Attributes: schemas.GetProjectDataSourceSchema(),
}
}

func (p *projectsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
p.Config = DataSourceConfiguration(req, resp)
}

func (p *projectsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data projectsDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

query := projects.ProjectsQuery{
ClonedFromProjectID: data.ClonedFromProjectID.ValueString(),
IsClone: data.IsClone.ValueBool(),
Name: data.Name.ValueString(),
PartialName: data.PartialName.ValueString(),
Skip: int(data.Skip.ValueInt64()),
Take: int(data.Take.ValueInt64()),
}

if !data.IDs.IsNull() {
var ids []string
resp.Diagnostics.Append(data.IDs.ElementsAs(ctx, &ids, false)...)
if resp.Diagnostics.HasError() {
return
}
query.IDs = ids
}

spaceID := data.SpaceID.ValueString()

existingProjects, err := projects.Get(p.Client, spaceID, query)
if err != nil {
resp.Diagnostics.AddError("Unable to query projects", err.Error())
return
}

data.Projects = make([]projectResourceModel, 0, len(existingProjects.Items))
for _, project := range existingProjects.Items {
flattenedProject, diags := flattenProject(ctx, project, &projectResourceModel{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
data.Projects = append(data.Projects, *flattenedProject)
}

data.ID = types.StringValue(fmt.Sprintf("Projects-%s", time.Now().UTC().String()))

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
3 changes: 2 additions & 1 deletion octopusdeploy_framework/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (p *octopusDeployFrameworkProvider) DataSources(ctx context.Context) []func
NewEnvironmentsDataSource,
NewGitCredentialsDataSource,
NewFeedsDataSource,
NewProjectsDataSource,
}
}

Expand All @@ -84,7 +85,7 @@ func (p *octopusDeployFrameworkProvider) Resources(ctx context.Context) []func()
NewGitHubRepositoryFeedResource,
NewAwsElasticContainerRegistryFeedResource,
NewNugetFeedResource,
//NewProjectResource,
NewProjectResource,
}
}

Expand Down
Loading

0 comments on commit 6d05ddf

Please sign in to comment.