Skip to content

Commit dff55f5

Browse files
authored
feat: add 'name' field to application source object (#651)
* feat: add feature gate for applicationSourceName * feat: add attribute checker for test Signed-off-by: Jodhi Lesmana <[email protected]>
1 parent f3b0543 commit dff55f5

File tree

12 files changed

+132
-55
lines changed

12 files changed

+132
-55
lines changed

argocd/resource_argocd_application.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ func resourceArgoCDApplication() *schema.Resource {
8383
}
8484

8585
func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
86-
objectMeta, spec, err := expandApplication(d)
87-
if err != nil {
88-
return errorToDiagnostics("failed to expand application", err)
89-
}
90-
9186
si := meta.(*provider.ServerInterface)
9287
if diags := si.InitClients(ctx); diags != nil {
9388
return pluginSDKDiags(diags)
9489
}
9590

91+
objectMeta, spec, err := expandApplication(d, si.IsFeatureSupported(features.ApplicationSourceName))
92+
if err != nil {
93+
return errorToDiagnostics("failed to expand application", err)
94+
}
95+
9696
apps, err := si.ApplicationClient.List(ctx, &applicationClient.ApplicationQuery{
9797
Name: &objectMeta.Name,
9898
AppNamespace: &objectMeta.Namespace,
@@ -259,7 +259,7 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
259259
AppNamespace: &ids[1],
260260
}
261261

262-
objectMeta, spec, err := expandApplication(d)
262+
objectMeta, spec, err := expandApplication(d, si.IsFeatureSupported(features.ApplicationSourceName))
263263
if err != nil {
264264
return errorToDiagnostics(fmt.Sprintf("failed to expand application %s", *appQuery.Name), err)
265265
}

argocd/resource_argocd_application_set.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ func resourceArgoCDApplicationSetCreate(ctx context.Context, d *schema.ResourceD
4949
return featureNotSupported(features.ApplicationSet)
5050
}
5151

52-
objectMeta, spec, err := expandApplicationSet(d, si.IsFeatureSupported(features.MultipleApplicationSources), si.IsFeatureSupported(features.ApplicationSetIgnoreApplicationDifferences), si.IsFeatureSupported(features.ApplicationSetTemplatePatch))
52+
objectMeta, spec, err := expandApplicationSet(
53+
d,
54+
si.IsFeatureSupported(features.MultipleApplicationSources),
55+
si.IsFeatureSupported(features.ApplicationSetIgnoreApplicationDifferences),
56+
si.IsFeatureSupported(features.ApplicationSetTemplatePatch),
57+
si.IsFeatureSupported(features.ApplicationSourceName),
58+
)
5359
if err != nil {
5460
return errorToDiagnostics("failed to expand application set", err)
5561
}
@@ -141,7 +147,13 @@ func resourceArgoCDApplicationSetUpdate(ctx context.Context, d *schema.ResourceD
141147
return nil
142148
}
143149

144-
objectMeta, spec, err := expandApplicationSet(d, si.IsFeatureSupported(features.MultipleApplicationSources), si.IsFeatureSupported(features.ApplicationSetIgnoreApplicationDifferences), si.IsFeatureSupported(features.ApplicationSetTemplatePatch))
150+
objectMeta, spec, err := expandApplicationSet(
151+
d,
152+
si.IsFeatureSupported(features.MultipleApplicationSources),
153+
si.IsFeatureSupported(features.ApplicationSetIgnoreApplicationDifferences),
154+
si.IsFeatureSupported(features.ApplicationSetTemplatePatch),
155+
si.IsFeatureSupported(features.ApplicationSourceName),
156+
)
145157
if err != nil {
146158
return errorToDiagnostics(fmt.Sprintf("failed to expand application set %s", d.Id()), err)
147159
}

argocd/resource_argocd_application_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,11 @@ func TestAccArgoCDApplication_CustomNamespace(t *testing.T) {
10621062

10631063
func TestAccArgoCDApplication_MultipleSources(t *testing.T) {
10641064
resource.ParallelTest(t, resource.TestCase{
1065-
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, features.MultipleApplicationSources) },
1065+
PreCheck: func() {
1066+
testAccPreCheck(t)
1067+
testAccPreCheckFeatureSupported(t, features.MultipleApplicationSources)
1068+
testAccPreCheckFeatureSupported(t, features.ApplicationSourceName)
1069+
},
10661070
ProviderFactories: testAccProviders,
10671071
Steps: []resource.TestStep{
10681072
{
@@ -1077,11 +1081,21 @@ func TestAccArgoCDApplication_MultipleSources(t *testing.T) {
10771081
"spec.0.source.0.chart",
10781082
"opensearch",
10791083
),
1084+
resource.TestCheckResourceAttr(
1085+
"argocd_application.multiple_sources",
1086+
"spec.0.source.0.name",
1087+
"",
1088+
),
10801089
resource.TestCheckResourceAttr(
10811090
"argocd_application.multiple_sources",
10821091
"spec.0.source.1.path",
10831092
"test/e2e/testdata/guestbook",
10841093
),
1094+
resource.TestCheckResourceAttr(
1095+
"argocd_application.multiple_sources",
1096+
"spec.0.source.1.name",
1097+
"guestbook",
1098+
),
10851099
),
10861100
},
10871101
{
@@ -1096,7 +1110,11 @@ func TestAccArgoCDApplication_MultipleSources(t *testing.T) {
10961110

10971111
func TestAccArgoCDApplication_HelmValuesFromExternalGitRepo(t *testing.T) {
10981112
resource.ParallelTest(t, resource.TestCase{
1099-
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, features.MultipleApplicationSources) },
1113+
PreCheck: func() {
1114+
testAccPreCheck(t)
1115+
testAccPreCheckFeatureSupported(t, features.MultipleApplicationSources)
1116+
testAccPreCheckFeatureSupported(t, features.ApplicationSourceName)
1117+
},
11001118
ProviderFactories: testAccProviders,
11011119
Steps: []resource.TestStep{
11021120
{
@@ -1111,6 +1129,11 @@ func TestAccArgoCDApplication_HelmValuesFromExternalGitRepo(t *testing.T) {
11111129
"spec.0.source.0.chart",
11121130
"wordpress",
11131131
),
1132+
resource.TestCheckResourceAttr(
1133+
"argocd_application.helm_values_external",
1134+
"spec.0.source.0.name",
1135+
"wordpress-helm",
1136+
),
11141137
resource.TestCheckResourceAttrSet(
11151138
"argocd_application.helm_values_external",
11161139
"spec.0.source.0.helm.0.value_files.#",
@@ -1120,6 +1143,11 @@ func TestAccArgoCDApplication_HelmValuesFromExternalGitRepo(t *testing.T) {
11201143
"spec.0.source.1.ref",
11211144
"values",
11221145
),
1146+
resource.TestCheckResourceAttr(
1147+
"argocd_application.helm_values_external",
1148+
"spec.0.source.1.name",
1149+
"wordpress-values",
1150+
),
11231151
),
11241152
},
11251153
{
@@ -2378,6 +2406,7 @@ resource "argocd_application" "multiple_sources" {
23782406
repo_url = "https://github.com/argoproj/argo-cd.git"
23792407
path = "test/e2e/testdata/guestbook"
23802408
target_revision = "HEAD"
2409+
name = "guestbook"
23812410
}
23822411
23832412
destination {
@@ -2437,6 +2466,7 @@ resource "argocd_application" "helm_values_external" {
24372466
project = "default"
24382467
24392468
source {
2469+
name = "wordpress-helm"
24402470
repo_url = "https://charts.helm.sh/stable"
24412471
chart = "wordpress"
24422472
target_revision = "9.0.3"
@@ -2446,9 +2476,10 @@ resource "argocd_application" "helm_values_external" {
24462476
}
24472477
24482478
source {
2479+
name = "wordpress-values"
2480+
ref = "values"
24492481
repo_url = "https://github.com/argoproj/argocd-example-apps.git"
24502482
target_revision = "HEAD"
2451-
ref = "values"
24522483
}
24532484
24542485
destination {

argocd/schema_application.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,11 @@ func applicationSpecSchemaV4(allOptional, isAppSet bool) *schema.Schema {
12991299
Description: "Reference to another `source` within defined sources. See associated documentation on [Helm value files from external Git repository](https://argo-cd.readthedocs.io/en/stable/user-guide/multiple_sources/#helm-value-files-from-external-git-repository) regarding combining `ref` with `path` and/or `chart`.",
13001300
Optional: true,
13011301
},
1302+
"name": {
1303+
Type: schema.TypeString,
1304+
Description: "Name is used to refer to a source and is displayed in the UI. It is supported in multi-source Applications since version 2.14",
1305+
Optional: true,
1306+
},
13021307
"chart": {
13031308
Type: schema.TypeString,
13041309
Description: "Helm chart name. Must be specified for applications sourced from a Helm repo.",

argocd/structure_application.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ import (
44
"encoding/json"
55
"fmt"
66

7+
"github.com/argoproj-labs/terraform-provider-argocd/internal/features"
78
application "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
)
1112

1213
// Expand
1314

14-
func expandApplication(d *schema.ResourceData) (metadata meta.ObjectMeta, spec application.ApplicationSpec, err error) {
15+
func expandApplication(d *schema.ResourceData, featureApplicationSourceNameSupported bool) (metadata meta.ObjectMeta, spec application.ApplicationSpec, err error) {
1516
metadata = expandMetadata(d)
16-
spec, err = expandApplicationSpec(d.Get("spec.0").(map[string]interface{}))
17+
spec, err = expandApplicationSpec(d.Get("spec.0").(map[string]interface{}), featureApplicationSourceNameSupported)
1718

1819
return
1920
}
2021

21-
func expandApplicationSpec(s map[string]interface{}) (spec application.ApplicationSpec, err error) {
22+
func expandApplicationSpec(s map[string]interface{}, featureApplicationSourceNameSupported bool) (spec application.ApplicationSpec, err error) {
2223
if v, ok := s["project"]; ok {
2324
spec.Project = v.(string)
2425
}
@@ -51,14 +52,17 @@ func expandApplicationSpec(s map[string]interface{}) (spec application.Applicati
5152
}
5253

5354
if v, ok := s["source"].([]interface{}); ok && len(v) > 0 {
54-
spec.Sources = expandApplicationSource(v)
55+
spec.Sources, err = expandApplicationSource(v, featureApplicationSourceNameSupported)
56+
if err != nil {
57+
return spec, err
58+
}
5559
}
5660

5761
return spec, nil
5862
}
5963

60-
func expandApplicationSource(_ass []interface{}) []application.ApplicationSource {
61-
ass := make([]application.ApplicationSource, len(_ass))
64+
func expandApplicationSource(_ass []interface{}, featureApplicationSourceNameSupported bool) (ass []application.ApplicationSource, err error) {
65+
ass = make([]application.ApplicationSource, len(_ass))
6266

6367
for i, v := range _ass {
6468
as := v.(map[string]interface{})
@@ -76,6 +80,17 @@ func expandApplicationSource(_ass []interface{}) []application.ApplicationSource
7680
s.Ref = v.(string)
7781
}
7882

83+
if v, ok := as["name"]; ok && v.(string) != "" {
84+
if !featureApplicationSourceNameSupported {
85+
f := features.ConstraintsMap[features.ApplicationSourceName]
86+
err = fmt.Errorf("%s is only supported from ArgoCD %s onwards", f.Name, f.MinVersion.String())
87+
88+
return ass, err
89+
}
90+
91+
s.Name = v.(string)
92+
}
93+
7994
if v, ok := as["target_revision"]; ok {
8095
s.TargetRevision = v.(string)
8196
}
@@ -103,7 +118,7 @@ func expandApplicationSource(_ass []interface{}) []application.ApplicationSource
103118
ass[i] = s
104119
}
105120

106-
return ass
121+
return ass, err
107122
}
108123

109124
func expandApplicationSourcePlugin(in []interface{}) *application.ApplicationSourcePlugin {
@@ -742,6 +757,7 @@ func flattenApplicationSource(source []application.ApplicationSource) (result []
742757
"directory": flattenApplicationSourceDirectory([]*application.ApplicationSourceDirectory{s.Directory}),
743758
"helm": flattenApplicationSourceHelm([]*application.ApplicationSourceHelm{s.Helm}),
744759
"kustomize": flattenApplicationSourceKustomize([]*application.ApplicationSourceKustomize{s.Kustomize}),
760+
"name": s.Name,
745761
"path": s.Path,
746762
"plugin": flattenApplicationSourcePlugin([]*application.ApplicationSourcePlugin{s.Plugin}),
747763
"ref": s.Ref,

0 commit comments

Comments
 (0)