feat: add woodpecker_repository_secret data source (#8)

This commit is contained in:
Dawid Wysokiński 2023-09-03 06:47:05 +02:00 committed by GitHub
parent 4659663f09
commit bca36e9571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 221 additions and 0 deletions

View File

@ -0,0 +1,39 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "woodpecker_repository_secret Data Source - terraform-provider-woodpecker"
subcategory: ""
description: |-
Use this data source to retrieve information about a secret in a specific repository.
---
# woodpecker_repository_secret (Data Source)
Use this data source to retrieve information about a secret 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
name = "secret_name"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String) the name of the secret
- `repository_id` (Number) the ID of the repository
### Read-Only
- `events` (Set of String) events for which the secret is available (push, tag, pull_request, deployment, cron, manual)
- `id` (Number) the secret's id
- `images` (Set of String) list of Docker images for which this secret is available
- `plugins_only` (Boolean) whether secret is only available for [plugins](https://woodpecker-ci.org/docs/usage/plugins/plugins)

View File

@ -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
name = "secret_name"
}

View File

@ -0,0 +1,100 @@
package internal
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
)
type repositorySecretDataSource struct {
client woodpecker.Client
}
var _ datasource.DataSource = (*repositorySecretDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*repositorySecretDataSource)(nil)
func newRepositorySecretDataSource() datasource.DataSource {
return &repositorySecretDataSource{}
}
func (d *repositorySecretDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_repository_secret"
}
func (d *repositorySecretDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Use this data source to retrieve information about a secret in a specific repository.",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Computed: true,
Description: "the secret's id",
},
"repository_id": schema.Int64Attribute{
Required: true,
Description: "the ID of the repository",
},
"name": schema.StringAttribute{
Required: true,
Description: "the name of the secret",
},
"events": schema.SetAttribute{
ElementType: types.StringType,
Computed: true,
Description: "events for which the secret is available (push, tag, pull_request, deployment, cron, manual)",
},
"plugins_only": schema.BoolAttribute{
Computed: true,
MarkdownDescription: "whether secret is only available for [plugins](https://woodpecker-ci.org/docs/usage/plugins/plugins)",
},
"images": schema.SetAttribute{
ElementType: types.StringType,
Computed: true,
Description: "list of Docker images for which this secret is available",
},
},
}
}
func (d *repositorySecretDataSource) 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 *repositorySecretDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data repositorySecretDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
secret, err := d.client.Secret(data.RepositoryID.ValueInt64(), data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError("Couldn't read secret data", err.Error())
return
}
resp.Diagnostics.Append(data.setValues(ctx, secret)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

View File

@ -0,0 +1,49 @@
package internal_test
import (
"fmt"
"testing"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
func TestRepositorySecretDataSource(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_secret" "test_secret" {
repository_id = woodpecker_repository.test_repo.id
name = "%s"
value = "test123"
events = ["push"]
}
data "woodpecker_repository_secret" "test_secret" {
repository_id = woodpecker_repository_secret.test_secret.repository_id
name = woodpecker_repository_secret.test_secret.name
}
`, repo.FullName, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.woodpecker_repository_secret.test_secret", "id"),
resource.TestCheckResourceAttrSet("data.woodpecker_repository_secret.test_secret", "repository_id"),
resource.TestCheckResourceAttr("data.woodpecker_repository_secret.test_secret", "name", name),
resource.TestCheckTypeSetElemAttr("data.woodpecker_repository_secret.test_secret", "events.*", "push"),
resource.TestCheckResourceAttr("data.woodpecker_repository_secret.test_secret", "plugins_only", "false"),
),
},
},
})
}

View File

@ -193,3 +193,27 @@ func (m *repositorySecretResourceModel) toWoodpeckerModel(ctx context.Context) (
return secret, diags
}
type repositorySecretDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
RepositoryID types.Int64 `tfsdk:"repository_id"`
Name types.String `tfsdk:"name"`
Images types.Set `tfsdk:"images"`
PluginsOnly types.Bool `tfsdk:"plugins_only"`
Events types.Set `tfsdk:"events"`
}
func (m *repositorySecretDataSourceModel) setValues(ctx context.Context, secret *woodpecker.Secret) diag.Diagnostics {
var diagsRes diag.Diagnostics
var diags diag.Diagnostics
m.ID = types.Int64Value(secret.ID)
m.Name = types.StringValue(secret.Name)
m.Images, diags = types.SetValueFrom(ctx, types.StringType, secret.Images)
diagsRes.Append(diags...)
m.PluginsOnly = types.BoolValue(secret.PluginsOnly)
m.Events, diags = types.SetValueFrom(ctx, types.StringType, secret.Events)
diagsRes.Append(diags...)
return diagsRes
}

View File

@ -57,6 +57,7 @@ func (p *woodpeckerProvider) DataSources(_ context.Context) []func() datasource.
newUserDataSource,
newSecretDataSource,
newRepositoryDataSource,
newRepositorySecretDataSource,
}
}