Skip to content

Commit

Permalink
feat: add support for validate option when provisioning/maintaining a…
Browse files Browse the repository at this point in the history
…rgocd applications (#411)

Signed-off-by: Jiacheng Xu <xjcmaxwellcjx@gmail.com>
  • Loading branch information
jiachengxu authored Aug 8, 2024
1 parent 8b48c7c commit dfa0f72
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
11 changes: 11 additions & 0 deletions argocd/resource_argocd_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func resourceArgoCDApplication() *schema.Resource {
Optional: true,
Default: true,
},
"validate": {
Type: schema.TypeBool,
Description: "Whether to validate the application spec before creating or updating the application.",
Optional: true,
Default: true,
},
"status": applicationStatusSchema(),
},
SchemaVersion: 4,
Expand Down Expand Up @@ -132,6 +138,7 @@ func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData
return featureNotSupported(features.ManagedNamespaceMetadata)
}

validate := d.Get("validate").(bool)
app, err := si.ApplicationClient.Create(ctx, &applicationClient.ApplicationCreateRequest{
Application: &application.Application{
ObjectMeta: objectMeta,
Expand All @@ -141,7 +148,9 @@ func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData
APIVersion: "argoproj.io/v1alpha1",
},
},
Validate: &validate,
})

if err != nil {
return argoCDAPIError("create", "application", objectMeta.Name, err)
} else if app == nil {
Expand Down Expand Up @@ -296,6 +305,7 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
}
}

validate := d.Get("validate").(bool)
if _, err = si.ApplicationClient.Update(ctx, &applicationClient.ApplicationUpdateRequest{
Application: &application.Application{
ObjectMeta: objectMeta,
Expand All @@ -305,6 +315,7 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
APIVersion: "argoproj.io/v1alpha1",
},
},
Validate: &validate,
}); err != nil {
return argoCDAPIError("update", "application", objectMeta.Name, err)
}
Expand Down
82 changes: 62 additions & 20 deletions argocd/resource_argocd_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestAccArgoCDApplication(t *testing.T) {
ResourceName: "argocd_application." + name,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
{
// Update
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestAccArgoCDApplication(t *testing.T) {
ResourceName: "argocd_application." + name,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "validate"},
},
},
})
Expand Down Expand Up @@ -149,7 +149,7 @@ ingress:
ResourceName: "argocd_application.helm",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand All @@ -176,7 +176,10 @@ func TestAccArgoCDApplication_Kustomize(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccArgoCDApplicationKustomize(
acctest.RandomWithPrefix("test-acc")),
acctest.RandomWithPrefix("test-acc"),
"examples/helloWorld",
true,
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"argocd_application.kustomize",
Expand All @@ -198,7 +201,7 @@ func TestAccArgoCDApplication_Kustomize(t *testing.T) {
ResourceName: "argocd_application.kustomize",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -233,7 +236,7 @@ func TestAccArgoCDApplication_IgnoreDifferences(t *testing.T) {
ResourceName: "argocd_application.ignore_differences",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "status", "validate"},
},
{
Config: testAccArgoCDApplicationIgnoreDiffJQPathExpressions(
Expand All @@ -259,7 +262,7 @@ func TestAccArgoCDApplication_IgnoreDifferences(t *testing.T) {
ResourceName: "argocd_application.ignore_differences_jqpe",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -293,7 +296,7 @@ func TestAccArgoCDApplication_RevisionHistoryLimit(t *testing.T) {
ResourceName: "argocd_application.revision_history_limit",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -322,7 +325,7 @@ func TestAccArgoCDApplication_OptionalDestinationNamespace(t *testing.T) {
ResourceName: "argocd_application.no_namespace",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -407,7 +410,7 @@ func TestAccArgoCDApplication_DirectoryJsonnet(t *testing.T) {
ResourceName: "argocd_application.directory",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -513,7 +516,7 @@ func TestAccArgoCDApplication_EmptyDirectory(t *testing.T) {
ResourceName: "argocd_application.directory",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -545,7 +548,7 @@ func TestAccArgoCDApplication_DirectoryIncludeExclude(t *testing.T) {
ResourceName: "argocd_application.directory",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -605,7 +608,7 @@ func TestAccArgoCDApplication_SyncPolicy(t *testing.T) {
ResourceName: "argocd_application.sync_policy",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -957,7 +960,7 @@ func TestAccArgoCDApplication_CustomNamespace(t *testing.T) {
ResourceName: "argocd_application.custom_namespace",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -991,7 +994,7 @@ func TestAccArgoCDApplication_MultipleSources(t *testing.T) {
ResourceName: "argocd_application.multiple_sources",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand Down Expand Up @@ -1029,7 +1032,7 @@ func TestAccArgoCDApplication_HelmValuesFromExternalGitRepo(t *testing.T) {
ResourceName: "argocd_application.helm_values_external",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "status", "validate"},
},
},
})
Expand All @@ -1052,7 +1055,7 @@ func TestAccArgoCDApplication_ManagedNamespaceMetadata(t *testing.T) {
ResourceName: "argocd_application.namespace_metadata",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version"},
ImportStateVerifyIgnore: []string{"wait", "cascade", "metadata.0.generation", "metadata.0.resource_version", "validate"},
},
},
})
Expand Down Expand Up @@ -1085,6 +1088,42 @@ func TestAccArgoCDApplication_Wait(t *testing.T) {
})
}

func TestAccArgoCDApplication_Validate(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccArgoCDApplicationKustomize(
acctest.RandomWithPrefix("test-acc"),
"path-does-not-exist",
true,
),
ExpectError: regexp.MustCompile("app path does not exist"),
},
{
Config: testAccArgoCDApplicationKustomize(
acctest.RandomWithPrefix("test-acc"),
"path-does-not-exist",
false,
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"argocd_application.kustomize",
"validate",
"false",
),
resource.TestCheckResourceAttr(
"argocd_application.kustomize",
"spec.0.source.0.path",
"path-does-not-exist",
),
),
},
},
})
}

func testAccArgoCDApplicationSimple(name, targetRevision string, wait bool) string {
return fmt.Sprintf(`
resource "argocd_application" "%[1]s" {
Expand Down Expand Up @@ -1219,7 +1258,7 @@ resource "argocd_application" "helm_file_parameters" {
}`, name)
}

func testAccArgoCDApplicationKustomize(name string) string {
func testAccArgoCDApplicationKustomize(name string, path string, validate bool) string {
return fmt.Sprintf(`
resource "argocd_application" "kustomize" {
metadata {
Expand All @@ -1233,7 +1272,7 @@ resource "argocd_application" "kustomize" {
spec {
source {
repo_url = "https://github.com/kubernetes-sigs/kustomize"
path = "examples/helloWorld"
path = "%s"
target_revision = "release-kustomize-v3.7"
kustomize {
name_prefix = "foo-"
Expand All @@ -1257,8 +1296,11 @@ resource "argocd_application" "kustomize" {
namespace = "default"
}
}
validate = %t
}
`, name)
`, name, path, validate)
}

func testAccArgoCDApplicationDirectoryNoPath(name string) string {
Expand Down
1 change: 1 addition & 0 deletions docs/resources/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ resource "argocd_application" "multiple_sources" {

- `cascade` (Boolean) Whether to applying cascading deletion when application is removed.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
- `validate` (Boolean) Whether to validate the application spec before creating or updating the application.
- `wait` (Boolean) Upon application creation or update, wait for application health/sync status to be healthy/Synced, upon application deletion, wait for application to be removed, when set to true. Wait timeouts are controlled by Terraform Create, Update and Delete resource timeouts (all default to 5 minutes). **Note**: if ArgoCD decides not to sync an application (e.g. because the project to which the application belongs has a `sync_window` applied) then you will experience an expected timeout event if `wait = true`.

### Read-Only
Expand Down

0 comments on commit dfa0f72

Please sign in to comment.