mirror of
https://github.com/Kichiyaki/terraform-provider-woodpecker.git
synced 2024-07-01 08:38:06 +00:00
feat: add woodpecker_repository_cron data source (#10)
This commit is contained in:
parent
1f1d37b82c
commit
df6cdf6873
40
docs/data-sources/repository_cron.md
Normal file
40
docs/data-sources/repository_cron.md
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
---
|
||||||
|
# generated by https://github.com/hashicorp/terraform-plugin-docs
|
||||||
|
page_title: "woodpecker_repository_cron Data Source - terraform-provider-woodpecker"
|
||||||
|
subcategory: ""
|
||||||
|
description: |-
|
||||||
|
Use this data source to retrieve information about a cron job in a specific repository.
|
||||||
|
---
|
||||||
|
|
||||||
|
# woodpecker_repository_cron (Data Source)
|
||||||
|
|
||||||
|
Use this data source to retrieve information about a cron job in a specific repository.
|
||||||
|
|
||||||
|
## Example Usage
|
||||||
|
|
||||||
|
```terraform
|
||||||
|
resource "woodpecker_repository" "test_repo" {
|
||||||
|
full_name = "Kichiyaki/test-repo"
|
||||||
|
}
|
||||||
|
|
||||||
|
data "woodpecker_repository_secret" "test_secret" {
|
||||||
|
repository_id = woodpecker_repository.test_repo.id
|
||||||
|
id = 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<!-- schema generated by tfplugindocs -->
|
||||||
|
## Schema
|
||||||
|
|
||||||
|
### Required
|
||||||
|
|
||||||
|
- `id` (Number) the id of the cron job
|
||||||
|
- `repository_id` (Number) the ID of the repository
|
||||||
|
|
||||||
|
### Read-Only
|
||||||
|
|
||||||
|
- `branch` (String) the name of the branch (uses default branch if empty)
|
||||||
|
- `created_at` (Number) date the cron job was created
|
||||||
|
- `creator_id` (Number) id of user who created the cron job
|
||||||
|
- `name` (String) the name of the cron job
|
||||||
|
- `schedule` (String) [cron expression](https://pkg.go.dev/github.com/robfig/cron#hdr-CRON_Expression_Format)
|
|
@ -0,0 +1,8 @@
|
||||||
|
resource "woodpecker_repository" "test_repo" {
|
||||||
|
full_name = "Kichiyaki/test-repo"
|
||||||
|
}
|
||||||
|
|
||||||
|
data "woodpecker_repository_secret" "test_secret" {
|
||||||
|
repository_id = woodpecker_repository.test_repo.id
|
||||||
|
id = 1
|
||||||
|
}
|
|
@ -137,7 +137,7 @@ func (d *repositoryDataSource) Read(ctx context.Context, req datasource.ReadRequ
|
||||||
|
|
||||||
repo, err := d.client.RepoLookup(data.FullName.ValueString())
|
repo, err := d.client.RepoLookup(data.FullName.ValueString())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Diagnostics.AddError("Couldn't read repository data", err.Error())
|
resp.Diagnostics.AddError("Couldn't get repository data", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
101
internal/data_source_repository_cron.go
Normal file
101
internal/data_source_repository_cron.go
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||||
|
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
|
||||||
|
)
|
||||||
|
|
||||||
|
type repositoryCronDataSource struct {
|
||||||
|
client woodpecker.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ datasource.DataSource = (*repositoryCronDataSource)(nil)
|
||||||
|
var _ datasource.DataSourceWithConfigure = (*repositoryCronDataSource)(nil)
|
||||||
|
|
||||||
|
func newRepositoryCronDataSource() datasource.DataSource {
|
||||||
|
return &repositoryCronDataSource{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *repositoryCronDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||||
|
resp.TypeName = req.ProviderTypeName + "_repository_cron"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *repositoryCronDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||||
|
resp.Schema = schema.Schema{
|
||||||
|
MarkdownDescription: "Use this data source to retrieve information about a cron job in a specific repository.",
|
||||||
|
Attributes: map[string]schema.Attribute{
|
||||||
|
"id": schema.Int64Attribute{
|
||||||
|
Required: true,
|
||||||
|
Description: "the id of the cron job",
|
||||||
|
},
|
||||||
|
"repository_id": schema.Int64Attribute{
|
||||||
|
Required: true,
|
||||||
|
Description: "the ID of the repository",
|
||||||
|
},
|
||||||
|
"name": schema.StringAttribute{
|
||||||
|
Computed: true,
|
||||||
|
Description: "the name of the cron job",
|
||||||
|
},
|
||||||
|
"schedule": schema.StringAttribute{
|
||||||
|
Computed: true,
|
||||||
|
MarkdownDescription: "[cron expression](https://pkg.go.dev/github.com/robfig/cron#hdr-CRON_Expression_Format)",
|
||||||
|
},
|
||||||
|
"branch": schema.StringAttribute{
|
||||||
|
Computed: true,
|
||||||
|
Description: "the name of the branch (uses default branch if empty)",
|
||||||
|
},
|
||||||
|
"creator_id": schema.Int64Attribute{
|
||||||
|
Computed: true,
|
||||||
|
Description: "id of user who created the cron job",
|
||||||
|
},
|
||||||
|
"created_at": schema.Int64Attribute{
|
||||||
|
Computed: true,
|
||||||
|
Description: "date the cron job was created",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *repositoryCronDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||||
|
// Prevent panic if the provider has not been configured.
|
||||||
|
if req.ProviderData == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
client, ok := req.ProviderData.(woodpecker.Client)
|
||||||
|
if !ok {
|
||||||
|
resp.Diagnostics.AddError(
|
||||||
|
"Unexpected Data Source Configure Type",
|
||||||
|
fmt.Sprintf("Expected woodpecker.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
d.client = client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *repositoryCronDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||||
|
var data repositoryCronModel
|
||||||
|
|
||||||
|
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cron, err := d.client.CronGet(data.RepositoryID.ValueInt64(), data.ID.ValueInt64())
|
||||||
|
if err != nil {
|
||||||
|
resp.Diagnostics.AddError("Couldn't get cron job data", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.Diagnostics.Append(data.setValues(ctx, cron)...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||||
|
}
|
51
internal/data_source_repository_cron_test.go
Normal file
51
internal/data_source_repository_cron_test.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package internal_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRepositoryCronDataSource(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
repo := createRepo(t)
|
||||||
|
name := uuid.NewString()
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
{
|
||||||
|
Config: fmt.Sprintf(`
|
||||||
|
resource "woodpecker_repository" "test_repo" {
|
||||||
|
full_name = "%s"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "woodpecker_repository_cron" "test_cron" {
|
||||||
|
repository_id = woodpecker_repository.test_repo.id
|
||||||
|
name = "%s"
|
||||||
|
schedule = "@daily"
|
||||||
|
}
|
||||||
|
|
||||||
|
data "woodpecker_repository_cron" "test_cron" {
|
||||||
|
repository_id = woodpecker_repository_cron.test_cron.repository_id
|
||||||
|
id = woodpecker_repository_cron.test_cron.id
|
||||||
|
}
|
||||||
|
`, repo.FullName, name),
|
||||||
|
Check: resource.ComposeAggregateTestCheckFunc(
|
||||||
|
resource.TestCheckResourceAttrSet("data.woodpecker_repository_cron.test_cron", "id"),
|
||||||
|
resource.TestCheckResourceAttr("data.woodpecker_repository_cron.test_cron", "repository_id", strconv.FormatInt(repo.ID, 10)),
|
||||||
|
resource.TestCheckResourceAttr("data.woodpecker_repository_cron.test_cron", "name", name),
|
||||||
|
resource.TestCheckResourceAttr("data.woodpecker_repository_cron.test_cron", "schedule", "@daily"),
|
||||||
|
resource.TestCheckResourceAttr("data.woodpecker_repository_cron.test_cron", "branch", ""),
|
||||||
|
resource.TestCheckResourceAttrSet("data.woodpecker_repository_cron.test_cron", "created_at"),
|
||||||
|
resource.TestCheckResourceAttrSet("data.woodpecker_repository_cron.test_cron", "creator_id"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
|
@ -87,7 +87,7 @@ func (d *repositorySecretDataSource) Read(ctx context.Context, req datasource.Re
|
||||||
|
|
||||||
secret, err := d.client.Secret(data.RepositoryID.ValueInt64(), data.Name.ValueString())
|
secret, err := d.client.Secret(data.RepositoryID.ValueInt64(), data.Name.ValueString())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Diagnostics.AddError("Couldn't read secret data", err.Error())
|
resp.Diagnostics.AddError("Couldn't get secret data", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ func (d *secretDataSource) Read(ctx context.Context, req datasource.ReadRequest,
|
||||||
|
|
||||||
secret, err := d.client.GlobalSecret(data.Name.ValueString())
|
secret, err := d.client.GlobalSecret(data.Name.ValueString())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Diagnostics.AddError("Couldn't read secret data", err.Error())
|
resp.Diagnostics.AddError("Couldn't get secret data", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -90,7 +90,7 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
|
||||||
user, err = d.client.Self()
|
user, err = d.client.Self()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Diagnostics.AddError("Couldn't read user data", err.Error())
|
resp.Diagnostics.AddError("Couldn't get user data", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,7 @@ func (p *woodpeckerProvider) DataSources(_ context.Context) []func() datasource.
|
||||||
newSecretDataSource,
|
newSecretDataSource,
|
||||||
newRepositoryDataSource,
|
newRepositoryDataSource,
|
||||||
newRepositorySecretDataSource,
|
newRepositorySecretDataSource,
|
||||||
|
newRepositoryCronDataSource,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user