-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from CruGlobal/add-project-view-resource
feat(project_view): Add project view resource and data source
- Loading branch information
Showing
22 changed files
with
789 additions
and
13 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 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,29 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "semaphoreui_project_view Data Source - semaphoreui" | ||
subcategory: "" | ||
description: |- | ||
The project view data source allows you to read a Views details. | ||
--- | ||
|
||
# semaphoreui_project_view (Data Source) | ||
|
||
The project view data source allows you to read a Views details. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `project_id` (Number) The project ID that the template belongs to. | ||
|
||
### Optional | ||
|
||
- `id` (Number) The view ID. Ensure that one and only one attribute from this collection is set : `id`, `title`. | ||
- `title` (String) Title of the view. Ensure that one and only one attribute from this collection is set : `id`, `title`. | ||
|
||
### Read-Only | ||
|
||
- `position` (Number) The position of the view in the project. |
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 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,56 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "semaphoreui_project_view Resource - semaphoreui" | ||
subcategory: "" | ||
description: |- | ||
The project view resource allows you to manage a Views in a project. | ||
--- | ||
|
||
# semaphoreui_project_view (Resource) | ||
|
||
The project view resource allows you to manage a Views in a project. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "semaphoreui_project" "project" { | ||
name = "Example Project" | ||
} | ||
resource "semaphoreui_project_view" "view" { | ||
project_id = semaphoreui_project.project.id | ||
title = "Section A" | ||
position = 0 | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `position` (Number) The position of the view in the project. Value must be at least 0. | ||
- `project_id` (Number) (ForceNew) The project ID that the template belongs to. | ||
- `title` (String) Title of the view. | ||
|
||
### Read-Only | ||
|
||
- `id` (Number) The view ID. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
# Import ID is specified by the string "project/{project_id}/view/{view_id}". | ||
# - {project_id} is the ID of the project in SemaphoreUI. | ||
# - {view_id} is the ID of the view in SemaphoreUI. | ||
terraform import semaphoreui_project_view.example project/1/view/2 | ||
``` | ||
Or using `import {}` block in the configuration file: | ||
```hcl | ||
import { | ||
to = semaphoreui_project_view.example | ||
id = "project/1/view/2" | ||
} | ||
``` |
11 changes: 11 additions & 0 deletions
11
examples/data-sources/semaphoreui_view_repository/data-source.tf
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,11 @@ | ||
# Lookup by View ID | ||
data "semaphoreui_project_view" "view" { | ||
project_id = 1 | ||
id = 3 | ||
} | ||
|
||
# Lookup by View Name | ||
data "semaphoreui_project_view" "prod" { | ||
project_id = 1 | ||
title = "Prod" | ||
} |
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,11 @@ | ||
# Import ID is specified by the string "project/{project_id}/view/{view_id}". | ||
# - {project_id} is the ID of the project in SemaphoreUI. | ||
# - {view_id} is the ID of the view in SemaphoreUI. | ||
terraform import semaphoreui_project_view.example project/1/view/2 | ||
``` | ||
Or using `import {}` block in the configuration file: | ||
```hcl | ||
import { | ||
to = semaphoreui_project_view.example | ||
id = "project/1/view/2" | ||
} |
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,9 @@ | ||
resource "semaphoreui_project" "project" { | ||
name = "Example Project" | ||
} | ||
|
||
resource "semaphoreui_project_view" "view" { | ||
project_id = semaphoreui_project.project.id | ||
title = "Section A" | ||
position = 0 | ||
} |
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 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 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 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 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,105 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
apiclient "terraform-provider-semaphoreui/semaphoreui/client" | ||
"terraform-provider-semaphoreui/semaphoreui/client/project" | ||
) | ||
|
||
// Ensure the implementation satisfies the expected interfaces. | ||
var ( | ||
_ datasource.DataSource = &projectViewDataSource{} | ||
) | ||
|
||
func NewProjectViewDataSource() datasource.DataSource { | ||
return &projectViewDataSource{} | ||
} | ||
|
||
type projectViewDataSource struct { | ||
client *apiclient.SemaphoreUI | ||
} | ||
|
||
func (d *projectViewDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*apiclient.SemaphoreUI) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
"Expected *client.SemaphoreUI, got %T. Please report this issue to the provider developers.", | ||
) | ||
return | ||
} | ||
d.client = client | ||
} | ||
|
||
// Metadata returns the data source type name. | ||
func (d *projectViewDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_project_view" | ||
} | ||
|
||
// Schema defines the schema for the data source. | ||
func (d *projectViewDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = ProjectViewSchema().GetDataSource(ctx) | ||
} | ||
|
||
func (d *projectViewDataSource) GetViewModelByTitle(projectID int64, title types.String) (*ProjectViewModel, error) { | ||
payload, err := d.client.Project.GetProjectProjectIDViews(&project.GetProjectProjectIDViewsParams{ | ||
ProjectID: projectID, | ||
}, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not read Views: %s", err.Error()) | ||
} | ||
|
||
for _, view := range payload.Payload { | ||
if view.Title == title.ValueString() { | ||
viewModel := convertViewResponseToProjectViewModel(view) | ||
return &viewModel, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("view with title %s not found", title.ValueString()) | ||
} | ||
|
||
func (d *projectViewDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var config ProjectViewModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
var model ProjectViewModel | ||
if !config.ID.IsNull() && !config.ID.IsUnknown() { | ||
response, err := d.client.Project.GetProjectProjectIDViewsViewID(&project.GetProjectProjectIDViewsViewIDParams{ | ||
ProjectID: config.ProjectID.ValueInt64(), | ||
ViewID: config.ID.ValueInt64(), | ||
}, nil) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Error Reading SemaphoreUI Project View", | ||
"Could not read project view, unexpected error: "+err.Error(), | ||
) | ||
return | ||
} | ||
model = convertViewResponseToProjectViewModel(response.Payload) | ||
} else if !config.Title.IsNull() && !config.Title.IsUnknown() { | ||
view, err := d.GetViewModelByTitle(config.ProjectID.ValueInt64(), config.Title) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Error Reading SemaphoreUI Project View", | ||
err.Error(), | ||
) | ||
return | ||
} | ||
model = *view | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} |
Oops, something went wrong.