diff --git a/docs/resources/module.md b/docs/resources/module.md index fb689570..67097866 100644 --- a/docs/resources/module.md +++ b/docs/resources/module.md @@ -55,6 +55,7 @@ resource "spacelift_module" "example-module" { - `name` (String) The module name will by default be inferred from the repository name if it follows the terraform-provider-name naming convention. However, if the repository doesn't follow this convention, or you want to give it a custom name, you can provide it here. - `project_root` (String) Project root is the optional directory relative to the repository root containing the module source code. - `protect_from_deletion` (Boolean) Protect this module from accidental deletion. If set, attempts to delete this module will fail. Defaults to `false`. +- `public` (Boolean) Make this module publicly accessible. Can only be set at creation time. Defaults to `false`. - `raw_git` (Block List, Max: 1) One-way VCS integration using a raw Git repository link (see [below for nested schema](#nestedblock--raw_git)) - `shared_accounts` (Set of String) List of the accounts (subdomains) which should have access to the Module - `space_id` (String) ID (slug) of the space the module is in diff --git a/spacelift/internal/structs/module.go b/spacelift/internal/structs/module.go index 685128a0..7fb4d73d 100644 --- a/spacelift/internal/structs/module.go +++ b/spacelift/internal/structs/module.go @@ -19,6 +19,7 @@ type Module struct { ProjectRoot *string `graphql:"projectRoot"` ProtectFromDeletion bool `graphql:"protectFromDeletion"` Provider VCSProvider `graphql:"provider"` + Public bool `graphql:"public"` Repository string `graphql:"repository"` RepositoryURL *string `graphql:"repositoryURL"` SharedAccounts []string `graphql:"sharedAccounts"` diff --git a/spacelift/resource_module.go b/spacelift/resource_module.go index f452616d..c0f406e0 100644 --- a/spacelift/resource_module.go +++ b/spacelift/resource_module.go @@ -247,6 +247,13 @@ func resourceModule() *schema.Resource { Optional: true, Default: false, }, + "public": { + Type: schema.TypeBool, + Description: "Make this module publicly accessible. Can only be set at creation time. Defaults to `false`.", + Optional: true, + Default: false, + ForceNew: true, + }, "raw_git": { Type: schema.TypeList, Description: "One-way VCS integration using a raw Git repository link", @@ -325,6 +332,18 @@ func resourceModuleCreate(ctx context.Context, d *schema.ResourceData, meta inte d.SetId(mutation.CreateModule.ID) + if d.Get("public").(bool) { + var publicMutation struct { + MakeModulePublic *structs.Module `graphql:"modulePublish(id: $id)"` + } + + publicVariables := map[string]interface{}{"id": graphql.ID(mutation.CreateModule.ID)} + + if err := meta.(*internal.Client).Mutate(ctx, "ModulePublish", &publicMutation, publicVariables); err != nil { + return diag.Errorf("could not make module public: %v", internal.FromSpaceliftError(err)) + } + } + return resourceModuleRead(ctx, d, meta) } @@ -351,6 +370,7 @@ func resourceModuleRead(ctx context.Context, d *schema.ResourceData, meta interf d.Set("name", module.Name) d.Set("enable_local_preview", module.LocalPreviewEnabled) d.Set("protect_from_deletion", module.ProtectFromDeletion) + d.Set("public", module.Public) d.Set("repository", module.Repository) d.Set("terraform_provider", module.TerraformProvider) d.Set("space_id", module.Space) diff --git a/spacelift/resource_module_test.go b/spacelift/resource_module_test.go index 8aa7d518..5e68ad8b 100644 --- a/spacelift/resource_module_test.go +++ b/spacelift/resource_module_test.go @@ -47,6 +47,7 @@ func TestModuleResource(t *testing.T) { AttributeNotPresent("project_root"), Attribute("enable_local_preview", Equals("false")), Attribute("protect_from_deletion", Equals("true")), + Attribute("public", Equals("false")), Attribute("repository", Equals("terraform-bacon-tasty")), SetEquals("shared_accounts", "bar-subdomain", "foo-subdomain"), Attribute("terraform_provider", Equals("default")), @@ -64,6 +65,7 @@ func TestModuleResource(t *testing.T) { Attribute("description", Equals("new description")), Attribute("enable_local_preview", Equals("true")), Attribute("protect_from_deletion", Equals("false")), + Attribute("public", Equals("false")), ), }, }) @@ -104,6 +106,37 @@ func TestModuleResource(t *testing.T) { }) }) + t.Run("with public", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + config := func() string { + return fmt.Sprintf(` + resource "spacelift_module" "test" { + name = "public-test-%s" + administrative = false + branch = "main" + repository = "terraform-bacon-tasty" + public = true + + raw_git { + namespace = "bacon" + url = "https://gist.github.com/d8d18c7c2841b578de22be34cb5943f5.git" + } + } + `, randomID) + } + + testSteps(t, []resource.TestStep{ + { + Config: config(), + Check: Resource( + "spacelift_module.test", + Attribute("public", Equals("true")), + ), + }, + }) + }) + t.Run("project root and custom name", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)