mirror of
https://github.com/Kichiyaki/terraform-provider-woodpecker.git
synced 2024-06-01 18:28:03 +00:00
feat: add woodpecker_repository_secret data source (#8)
This commit is contained in:
parent
4659663f09
commit
bca36e9571
39
docs/data-sources/repository_secret.md
Normal file
39
docs/data-sources/repository_secret.md
Normal 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)
|
|
@ -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"
|
||||
}
|
100
internal/data_source_repository_secret.go
Normal file
100
internal/data_source_repository_secret.go
Normal 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)...)
|
||||
}
|
49
internal/data_source_repository_secret_test.go
Normal file
49
internal/data_source_repository_secret_test.go
Normal 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"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -57,6 +57,7 @@ func (p *woodpeckerProvider) DataSources(_ context.Context) []func() datasource.
|
|||
newUserDataSource,
|
||||
newSecretDataSource,
|
||||
newRepositoryDataSource,
|
||||
newRepositorySecretDataSource,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user