Skip to content

Commit

Permalink
Merge pull request #579 from spacelift-io/create-public-modules
Browse files Browse the repository at this point in the history
feat: support creating public modules
  • Loading branch information
Apollorion authored Oct 30, 2024
2 parents 154331b + 9109560 commit ab02d41
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spacelift/internal/structs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
20 changes: 20 additions & 0 deletions spacelift/resource_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions spacelift/resource_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand All @@ -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")),
),
},
})
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ab02d41

Please sign in to comment.