Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'Raw Git' to modules #561

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/data-sources/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ data "spacelift_module" "k8s-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.
- `raw_git` (List of Object) One-way VCS integration using a raw Git repository link (see [below for nested schema](#nestedatt--raw_git))
- `repository` (String) Name of the repository, without the owner part
- `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 Expand Up @@ -97,3 +98,12 @@ Read-Only:
- `id` (String)
- `is_default` (Boolean)
- `namespace` (String)


<a id="nestedatt--raw_git"></a>
### Nested Schema for `raw_git`

Read-Only:

- `namespace` (String)
- `url` (String)
10 changes: 10 additions & 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`.
- `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
- `terraform_provider` (String) The module provider 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 gave the module a custom name, you can provide the provider name here.
Expand Down Expand Up @@ -145,6 +146,15 @@ Read-Only:

- `is_default` (Boolean) Indicates whether this is the default GitLab integration


<a id="nestedblock--raw_git"></a>
### Nested Schema for `raw_git`

Required:

- `namespace` (String) User-friendly namespace for the repository, this is for cosmetic purposes only
- `url` (String) HTTPS URL of the Git repository

## Import

Import is supported using the following syntax:
Expand Down
19 changes: 19 additions & 0 deletions spacelift/data_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ func dataModule() *schema.Resource {
Description: "Protect this module from accidental deletion. If set, attempts to delete this module will fail.",
Computed: true,
},
"raw_git": {
Type: schema.TypeList,
Description: "One-way VCS integration using a raw Git repository link",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Description: "User-friendly namespace for the repository, this is for cosmetic purposes only",
Computed: true,
},
"url": {
Type: schema.TypeString,
Description: "HTTPS URL of the Git repository",
Computed: true,
},
},
},
},
"repository": {
Type: schema.TypeString,
Description: "Name of the repository, without the owner part",
Expand Down
34 changes: 34 additions & 0 deletions spacelift/data_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,40 @@ func TestModuleData(t *testing.T) {
},
})
})

t.Run("with Raw Git", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

testSteps(t, []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "spacelift_module" "test" {
name = "test-module-%s"
administrative = false
branch = "main"
repository = "terraform-bacon-tasty"

raw_git {
namespace = "bacon"
url = "https://gist.github.com/d8d18c7c2841b578de22be34cb5943f5.git"
}
}
data "spacelift_module" "test" {
module_id = spacelift_module.test.id
}
`, randomID),
Check: Resource(
"data.spacelift_module.test",
Nested("raw_git",
CheckInList(
Attribute("namespace", Equals("bacon")),
Attribute("url", Equals("https://gist.github.com/d8d18c7c2841b578de22be34cb5943f5.git")),
),
),
),
},
})
})
}

func TestModuleDataSpace(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions spacelift/internal/structs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Module struct {
ProtectFromDeletion bool `graphql:"protectFromDeletion"`
Provider VCSProvider `graphql:"provider"`
Repository string `graphql:"repository"`
RepositoryURL *string `graphql:"repositoryURL"`
SharedAccounts []string `graphql:"sharedAccounts"`
Space string `graphql:"space"`
TerraformProvider string `graphql:"terraformProvider"`
Expand Down Expand Up @@ -84,6 +85,12 @@ func (m *Module) ExportVCSSettings(d *schema.ResourceData) error {
}
}
fieldName = "gitlab"
case VCSProviderRawGit:
vcsSettings = map[string]interface{}{
"namespace": m.Namespace,
"url": m.RepositoryURL,
}
fieldName = "raw_git"
}

if fieldName != "" {
Expand Down
2 changes: 2 additions & 0 deletions spacelift/internal/structs/module_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ModuleCreateInput struct {
Namespace *graphql.String `json:"namespace"`
Provider *graphql.String `json:"provider"`
Repository graphql.String `json:"repository"`
RepositoryURL *graphql.String `json:"repositoryURL"`
TerraformProvider *graphql.String `json:"terraformProvider"`
Space *graphql.String `json:"space"`
VCSIntegrationID *graphql.ID `json:"vcsIntegrationId"`
Expand Down Expand Up @@ -42,6 +43,7 @@ type ModuleUpdateV2Input struct {
ProtectFromDeletion graphql.Boolean `json:"protectFromDeletion"`
Provider *graphql.String `json:"provider"`
Repository graphql.String `json:"repository"`
RepositoryURL *graphql.String `json:"repositoryURL"`
SharedAccounts *[]graphql.String `json:"sharedAccounts"`
Space *graphql.String `json:"space"`
VCSIntegrationID *graphql.ID `json:"vcsIntegrationId"`
Expand Down
35 changes: 32 additions & 3 deletions spacelift/resource_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,29 @@ func resourceModule() *schema.Resource {
Optional: true,
Default: false,
},
"raw_git": {
Type: schema.TypeList,
Description: "One-way VCS integration using a raw Git repository link",
Optional: true,
ConflictsWith: conflictingVCSProviders("raw_git"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "User-friendly namespace for the repository, this is for cosmetic purposes only",
ValidateDiagFunc: validations.DisallowEmptyString,
},
"url": {
Type: schema.TypeString,
Required: true,
Description: "HTTPS URL of the Git repository",
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
},
},
"repository": {
Type: schema.TypeString,
Description: "Name of the repository, without the owner part",
Expand Down Expand Up @@ -404,7 +427,7 @@ func resourceModuleDelete(ctx context.Context, d *schema.ResourceData, meta inte
return nil
}

func getSourceData(d *schema.ResourceData) (provider *graphql.String, namespace *graphql.String, vcsIntegrationID *graphql.ID) {
func getSourceData(d *schema.ResourceData) (provider *graphql.String, namespace *graphql.String, repositoryURL *graphql.String, vcsIntegrationID *graphql.ID) {
provider = graphql.NewString("GITHUB")

if azureDevOps, ok := d.Get("azure_devops").([]interface{}); ok && len(azureDevOps) > 0 {
Expand Down Expand Up @@ -460,6 +483,12 @@ func getSourceData(d *schema.ResourceData) (provider *graphql.String, namespace
provider = graphql.NewString(graphql.String(structs.VCSProviderGitlab))
}

if rawGit, ok := d.Get("raw_git").([]interface{}); ok && len(rawGit) > 0 {
repositoryURL = toOptionalString(rawGit[0].(map[string]interface{})["url"])
namespace = toOptionalString(rawGit[0].(map[string]interface{})["namespace"])
provider = graphql.NewString(graphql.String(structs.VCSProviderRawGit))
}

return
}

Expand All @@ -468,7 +497,7 @@ func moduleCreateInput(d *schema.ResourceData) structs.ModuleCreateInput {
UpdateInput: moduleUpdateInput(d),
Repository: toString(d.Get("repository")),
}
ret.Provider, ret.Namespace, ret.VCSIntegrationID = getSourceData(d)
ret.Provider, ret.Namespace, ret.RepositoryURL, ret.VCSIntegrationID = getSourceData(d)

name, ok := d.GetOk("name")
if ok {
Expand Down Expand Up @@ -547,7 +576,7 @@ func moduleUpdateV2Input(d *schema.ResourceData) structs.ModuleUpdateV2Input {
ProtectFromDeletion: graphql.Boolean(d.Get("protect_from_deletion").(bool)),
Repository: toString(d.Get("repository")),
}
ret.Provider, ret.Namespace, ret.VCSIntegrationID = getSourceData(d)
ret.Provider, ret.Namespace, ret.RepositoryURL, ret.VCSIntegrationID = getSourceData(d)

description, ok := d.GetOk("description")
if ok {
Expand Down
35 changes: 35 additions & 0 deletions spacelift/resource_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ func TestModuleResource(t *testing.T) {
})
})

t.Run("with Raw Git", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := func() string {
return fmt.Sprintf(`
resource "spacelift_module" "test" {
name = "raw-git-%s"
administrative = false
branch = "main"
repository = "terraform-bacon-tasty"

raw_git {
namespace = "bacon"
url = "https://gist.github.com/d8d18c7c2841b578de22be34cb5943f5.git"
}
}
`, randomID)
}

testSteps(t, []resource.TestStep{
{
Config: config(),
Check: Resource(
"spacelift_module.test",
Nested("raw_git",
CheckInList(
Attribute("namespace", Equals("bacon")),
Attribute("url", Equals("https://gist.github.com/d8d18c7c2841b578de22be34cb5943f5.git")),
),
),
),
},
})
})

t.Run("project root and custom name", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

Expand Down
Loading