From df6cdf687379de942cf43f0e9657312e87f88c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Thu, 7 Sep 2023 06:04:33 +0200 Subject: [PATCH] feat: add woodpecker_repository_cron data source (#10) --- docs/data-sources/repository_cron.md | 40 +++++++ .../woodpecker_repository_cron/data-source.tf | 8 ++ internal/data_source_repository.go | 2 +- internal/data_source_repository_cron.go | 101 ++++++++++++++++++ internal/data_source_repository_cron_test.go | 51 +++++++++ internal/data_source_repository_secret.go | 2 +- internal/data_source_secret.go | 2 +- internal/data_source_user.go | 2 +- internal/provider.go | 1 + 9 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 docs/data-sources/repository_cron.md create mode 100644 examples/data-sources/woodpecker_repository_cron/data-source.tf create mode 100644 internal/data_source_repository_cron.go create mode 100644 internal/data_source_repository_cron_test.go diff --git a/docs/data-sources/repository_cron.md b/docs/data-sources/repository_cron.md new file mode 100644 index 0000000..f332f94 --- /dev/null +++ b/docs/data-sources/repository_cron.md @@ -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 + +### 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) diff --git a/examples/data-sources/woodpecker_repository_cron/data-source.tf b/examples/data-sources/woodpecker_repository_cron/data-source.tf new file mode 100644 index 0000000..28bba57 --- /dev/null +++ b/examples/data-sources/woodpecker_repository_cron/data-source.tf @@ -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 +} diff --git a/internal/data_source_repository.go b/internal/data_source_repository.go index a2e7802..6a3859d 100644 --- a/internal/data_source_repository.go +++ b/internal/data_source_repository.go @@ -137,7 +137,7 @@ func (d *repositoryDataSource) Read(ctx context.Context, req datasource.ReadRequ repo, err := d.client.RepoLookup(data.FullName.ValueString()) if err != nil { - resp.Diagnostics.AddError("Couldn't read repository data", err.Error()) + resp.Diagnostics.AddError("Couldn't get repository data", err.Error()) return } diff --git a/internal/data_source_repository_cron.go b/internal/data_source_repository_cron.go new file mode 100644 index 0000000..bc1bd97 --- /dev/null +++ b/internal/data_source_repository_cron.go @@ -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)...) +} diff --git a/internal/data_source_repository_cron_test.go b/internal/data_source_repository_cron_test.go new file mode 100644 index 0000000..864d6b4 --- /dev/null +++ b/internal/data_source_repository_cron_test.go @@ -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"), + ), + }, + }, + }) +} diff --git a/internal/data_source_repository_secret.go b/internal/data_source_repository_secret.go index 3b91184..4cf64af 100644 --- a/internal/data_source_repository_secret.go +++ b/internal/data_source_repository_secret.go @@ -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()) if err != nil { - resp.Diagnostics.AddError("Couldn't read secret data", err.Error()) + resp.Diagnostics.AddError("Couldn't get secret data", err.Error()) return } diff --git a/internal/data_source_secret.go b/internal/data_source_secret.go index 2774217..dd5172c 100644 --- a/internal/data_source_secret.go +++ b/internal/data_source_secret.go @@ -83,7 +83,7 @@ func (d *secretDataSource) Read(ctx context.Context, req datasource.ReadRequest, secret, err := d.client.GlobalSecret(data.Name.ValueString()) if err != nil { - resp.Diagnostics.AddError("Couldn't read secret data", err.Error()) + resp.Diagnostics.AddError("Couldn't get secret data", err.Error()) return } diff --git a/internal/data_source_user.go b/internal/data_source_user.go index a8abe4c..2421707 100644 --- a/internal/data_source_user.go +++ b/internal/data_source_user.go @@ -90,7 +90,7 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r user, err = d.client.Self() } if err != nil { - resp.Diagnostics.AddError("Couldn't read user data", err.Error()) + resp.Diagnostics.AddError("Couldn't get user data", err.Error()) return } diff --git a/internal/provider.go b/internal/provider.go index adaa699..78beafc 100644 --- a/internal/provider.go +++ b/internal/provider.go @@ -60,6 +60,7 @@ func (p *woodpeckerProvider) DataSources(_ context.Context) []func() datasource. newSecretDataSource, newRepositoryDataSource, newRepositorySecretDataSource, + newRepositoryCronDataSource, } }