diff --git a/docs/resources/file.md b/docs/resources/file.md index 680608c..84b3960 100644 --- a/docs/resources/file.md +++ b/docs/resources/file.md @@ -44,11 +44,11 @@ resource "ctfd_file" "http_file" { ### Required -- `challenge_id` (String) Challenge of the file. - `name` (String) Name of the file as displayed to end-users. ### Optional +- `challenge_id` (String) Challenge of the file. - `content` (String, Sensitive) Raw content of the file, perfectly fit the use-cases of a .txt document or anything with a simple binary content. You could provide it from the file-system using `file("${path.module}/...")`. - `contentb64` (String, Sensitive) Base 64 content of the file, perfectly fit the use-cases of complex binaries. You could provide it from the file-system using `filebase64("${path.module}/...")`. - `location` (String) Location where the file is stored on the CTFd instance, for download purposes. diff --git a/go.mod b/go.mod index 0c79b23..6367fbc 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ctfer-io/terraform-provider-ctfd go 1.22 require ( - github.com/ctfer-io/go-ctfd v0.8.0 + github.com/ctfer-io/go-ctfd v0.8.1 github.com/hashicorp/terraform-plugin-docs v0.19.2 github.com/hashicorp/terraform-plugin-framework v1.8.0 github.com/hashicorp/terraform-plugin-go v0.23.0 diff --git a/go.sum b/go.sum index 442804b..5fdabb7 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,8 @@ github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZ github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= -github.com/ctfer-io/go-ctfd v0.8.0 h1:as4TWW0OFI/4oZnJi8ad1pEeVqcZWVw2HiQl0rMDWNk= -github.com/ctfer-io/go-ctfd v0.8.0/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM= +github.com/ctfer-io/go-ctfd v0.8.1 h1:/cXBR6rJ6S4Q2w7HS5otQvyQ4GK9pzDTsrqCxOl69oc= +github.com/ctfer-io/go-ctfd v0.8.1/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM= github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/provider/file_resource.go b/provider/file_resource.go index afd01b5..1ded325 100644 --- a/provider/file_resource.go +++ b/provider/file_resource.go @@ -60,7 +60,7 @@ func (r *fileResource) Schema(ctx context.Context, req resource.SchemaRequest, r }, "challenge_id": schema.StringAttribute{ MarkdownDescription: "Challenge of the file.", - Required: true, + Optional: true, }, "name": schema.StringAttribute{ MarkdownDescription: "Name of the file as displayed to end-users.", @@ -138,8 +138,7 @@ func (r *fileResource) Create(ctx context.Context, req resource.CreateRequest, r } // Create file - res, err := r.client.PostFiles(&api.PostFilesParams{ - Challenge: utils.Ptr(utils.Atoi(data.ChallengeID.ValueString())), + params := &api.PostFilesParams{ Files: []*api.InputFile{ { Name: data.Name.ValueString(), @@ -147,7 +146,11 @@ func (r *fileResource) Create(ctx context.Context, req resource.CreateRequest, r }, }, Location: data.Location.ValueStringPointer(), - }, api.WithContext(ctx)) + } + if !data.ChallengeID.IsNull() { + params.Challenge = utils.Ptr(utils.Atoi(data.ChallengeID.ValueString())) + } + res, err := r.client.PostFiles(params, api.WithContext(ctx)) if err != nil { resp.Diagnostics.AddError( "Client Error", @@ -291,9 +294,5 @@ func lookForChallengeId(ctx context.Context, client *api.Client, fileID int, dia } } } - diags.AddError( - "Provider Error", - fmt.Sprintf("Unable to find challenge of file %d", fileID), - ) return types.StringNull() } diff --git a/provider/file_resource_test.go b/provider/file_resource_test.go index 4da8e4a..44e2632 100644 --- a/provider/file_resource_test.go +++ b/provider/file_resource_test.go @@ -25,6 +25,11 @@ resource "ctfd_file" "pouet" { name = "pouet.txt" content = "Pouet is a clown cat" } + +resource "ctfd_file" "pouet_2" { + name = "pouet-2.txt" + content = "Pouet is a clown cat, but has not challenge" +} `, }, // ImportState testing @@ -48,6 +53,11 @@ resource "ctfd_file" "pouet" { name = "pouet.txt" content = "Pouet the 2nd is the clowniest cat ever" } + +resource "ctfd_file" "pouet_2" { + name = "pouet-2.txt" + content = "Pouet is a clown cat, but has not challenge" +} `, }, // Delete testing automatically occurs in TestCase