-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9250263
commit 6d05ddf
Showing
5 changed files
with
401 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.