feat: add secret data source (#4)

This commit is contained in:
Dawid Wysokiński 2023-08-28 08:38:02 +02:00 committed by GitHub
parent 6e53397be0
commit 860baf9957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 204 additions and 7 deletions

View File

@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "woodpecker_secret Data Source - terraform-provider-woodpecker"
subcategory: ""
description: |-
Use this data source to retrieve information about a global secret.
---
# woodpecker_secret (Data Source)
Use this data source to retrieve information about a global secret.
## Example Usage
```terraform
data "woodpecker_secret" "test_secret" {
name = "test"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String) the name of the secret
### 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,3 @@
data "woodpecker_secret" "test_secret" {
name = "test"
}

View File

@ -0,0 +1,96 @@
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 secretDataSource struct {
client woodpecker.Client
}
var _ datasource.DataSource = (*secretDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*secretDataSource)(nil)
func newSecretDataSource() datasource.DataSource {
return &secretDataSource{}
}
func (d *secretDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_secret"
}
func (d *secretDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Use this data source to retrieve information about a global secret.",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Computed: true,
Description: "the secret's id",
},
"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 *secretDataSource) 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 *secretDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data secretDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
secret, err := d.client.GlobalSecret(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,41 @@
package internal_test
import (
"fmt"
"testing"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
func TestDataSourceSecret(t *testing.T) {
t.Parallel()
name := uuid.NewString()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "woodpecker_secret" "test_secret" {
name = "%s"
value = "test123"
events = ["push"]
}
data "woodpecker_secret" "test_secret" {
name = woodpecker_secret.test_secret.name
}
`, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.woodpecker_secret.test_secret", "id"),
resource.TestCheckResourceAttr("data.woodpecker_secret.test_secret", "name", name),
resource.TestCheckTypeSetElemAttr("data.woodpecker_secret.test_secret", "events.*", "push"),
resource.TestCheckResourceAttr("data.woodpecker_secret.test_secret", "plugins_only", "false"),
),
},
},
})
}

View File

@ -38,7 +38,7 @@ func (m *userModel) toWoodpeckerModel(_ context.Context) (*woodpecker.User, diag
}, nil
}
type secretModel struct {
type secretResourceModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
@ -47,7 +47,7 @@ type secretModel struct {
Events types.Set `tfsdk:"events"`
}
func (m *secretModel) setValues(ctx context.Context, secret *woodpecker.Secret) diag.Diagnostics {
func (m *secretResourceModel) setValues(ctx context.Context, secret *woodpecker.Secret) diag.Diagnostics {
var diagsRes diag.Diagnostics
var diags diag.Diagnostics
@ -62,7 +62,7 @@ func (m *secretModel) setValues(ctx context.Context, secret *woodpecker.Secret)
return diagsRes
}
func (m *secretModel) toWoodpeckerModel(ctx context.Context) (*woodpecker.Secret, diag.Diagnostics) {
func (m *secretResourceModel) toWoodpeckerModel(ctx context.Context) (*woodpecker.Secret, diag.Diagnostics) {
var diags diag.Diagnostics
secret := &woodpecker.Secret{
@ -76,3 +76,26 @@ func (m *secretModel) toWoodpeckerModel(ctx context.Context) (*woodpecker.Secret
return secret, diags
}
type secretDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Images types.Set `tfsdk:"images"`
PluginsOnly types.Bool `tfsdk:"plugins_only"`
Events types.Set `tfsdk:"events"`
}
func (m *secretDataSourceModel) 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

@ -55,6 +55,7 @@ func (p *woodpeckerProvider) Schema(_ context.Context, _ provider.SchemaRequest,
func (p *woodpeckerProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
newUserDataSource,
newSecretDataSource,
}
}

View File

@ -98,7 +98,7 @@ func (r *secretResource) Configure(_ context.Context, req resource.ConfigureRequ
}
func (r *secretResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data secretModel
var data secretResourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
@ -126,7 +126,7 @@ func (r *secretResource) Create(ctx context.Context, req resource.CreateRequest,
}
func (r *secretResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data secretModel
var data secretResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
@ -147,7 +147,7 @@ func (r *secretResource) Read(ctx context.Context, req resource.ReadRequest, res
}
func (r *secretResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data secretModel
var data secretResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
@ -175,7 +175,7 @@ func (r *secretResource) Update(ctx context.Context, req resource.UpdateRequest,
}
func (r *secretResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data secretModel
var data secretResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {