Skip to content

Commit cced478

Browse files
authored
Add argocd_application data source
1 parent 6ae499d commit cced478

File tree

15 files changed

+2703
-3
lines changed

15 files changed

+2703
-3
lines changed

docs/data-sources/application.md

Lines changed: 361 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
data "argocd_application" "foo" {
2+
metadata = {
3+
name = "foo"
4+
namespace = "argocd"
5+
}
6+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/argoproj/gitops-engine v0.7.3
99
github.com/argoproj/pkg v0.13.7-0.20221221191914-44694015343d
1010
github.com/cristalhq/jwt/v3 v3.1.0
11+
github.com/elliotchance/pie/v2 v2.6.0
1112
github.com/golang/protobuf v1.5.3
1213
github.com/hashicorp/terraform-plugin-docs v0.15.0
1314
github.com/hashicorp/terraform-plugin-framework v1.3.1
@@ -206,7 +207,7 @@ require (
206207
go.opentelemetry.io/otel v1.11.1 // indirect
207208
go.opentelemetry.io/otel/trace v1.11.1 // indirect
208209
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
209-
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 // indirect
210+
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 // indirect
210211
golang.org/x/mod v0.10.0 // indirect
211212
golang.org/x/net v0.10.0 // indirect
212213
golang.org/x/oauth2 v0.4.0 // indirect

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFP
246246
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
247247
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
248248
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
249+
github.com/elliotchance/pie/v2 v2.6.0 h1:lAQxtrxijynVjXuU0btqg9oh4Izq6IQVtypJw/45gZ4=
250+
github.com/elliotchance/pie/v2 v2.6.0/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og=
249251
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
250252
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
251253
github.com/emicklei/go-restful/v3 v3.8.0 h1:eCZ8ulSerjdAiaNpF7GxXIE7ZCMo1moN1qX+S609eVw=
@@ -1159,8 +1161,9 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
11591161
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
11601162
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
11611163
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
1162-
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 h1:Or4Ra3AAlhUlNn8WmIzw2Yq2vUHSkrP6E2e/FIESpF8=
11631164
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2/go.mod h1:a3o/VtDNHN+dCVLEpzjjUHOzR+Ln3DHX056ZPzoZGGA=
1165+
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 h1:ba9YlqfDGTTQ5aZ2fwOoQ1hf32QySyQkR6ODGDzHlnE=
1166+
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
11641167
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
11651168
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
11661169
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
"github.com/hashicorp/terraform-plugin-log/tflog"
14+
"github.com/oboukili/terraform-provider-argocd/internal/diagnostics"
15+
)
16+
17+
// Ensure provider defined types fully satisfy framework interfaces.
18+
var _ datasource.DataSource = &applicationDataSource{}
19+
20+
func NewArgoCDApplicationDataSource() datasource.DataSource {
21+
return &applicationDataSource{}
22+
}
23+
24+
// applicationDataSource defines the data source implementation.
25+
type applicationDataSource struct {
26+
si *ServerInterface
27+
}
28+
29+
func (d *applicationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
30+
resp.TypeName = req.ProviderTypeName + "_application"
31+
}
32+
33+
func (d *applicationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
34+
resp.Schema = schema.Schema{
35+
MarkdownDescription: "Reads an existing ArgoCD application.",
36+
37+
Attributes: map[string]schema.Attribute{
38+
"id": schema.StringAttribute{
39+
MarkdownDescription: "ArgoCD application identifier",
40+
Computed: true,
41+
},
42+
"metadata": objectMetaSchemaAttribute("applications.argoproj.io", true),
43+
"spec": applicationSpecSchemaAttribute(true, true),
44+
"status": applicationStatusSchemaAttribute(),
45+
},
46+
}
47+
}
48+
49+
func (d *applicationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
50+
// Prevent panic if the provider has not been configured.
51+
if req.ProviderData == nil {
52+
return
53+
}
54+
55+
si, ok := req.ProviderData.(*ServerInterface)
56+
if !ok {
57+
resp.Diagnostics.AddError(
58+
"Unexpected Provider Data",
59+
fmt.Sprintf("Expected *ServerInterface, got: %T. Please report this issue to the provider developers.", req.ProviderData),
60+
)
61+
62+
return
63+
}
64+
65+
d.si = si
66+
}
67+
68+
func (d *applicationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
69+
var data applicationModel
70+
71+
// Read Terraform configuration data into the model
72+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
73+
74+
// Initialize API clients
75+
resp.Diagnostics.Append(d.si.InitClients(ctx)...)
76+
77+
// Check for errors before proceeding
78+
if resp.Diagnostics.HasError() {
79+
return
80+
}
81+
82+
id := fmt.Sprintf("%s:%s", data.Metadata.Name.ValueString(), data.Metadata.Namespace.ValueString())
83+
data.ID = types.StringValue(id)
84+
85+
// Read application
86+
resp.Diagnostics.Append(readApplication(ctx, d.si, &data)...)
87+
88+
tflog.Trace(ctx, "read ArgoCD application")
89+
90+
// Save data into Terraform state
91+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
92+
}
93+
94+
func readApplication(ctx context.Context, si *ServerInterface, data *applicationModel) (diags diag.Diagnostics) {
95+
ids := strings.Split(data.ID.ValueString(), ":")
96+
appName := ids[0]
97+
namespace := ids[1]
98+
99+
apps, err := si.ApplicationClient.List(ctx, &application.ApplicationQuery{
100+
Name: &appName,
101+
AppNamespace: &namespace,
102+
})
103+
if err != nil {
104+
if strings.Contains(err.Error(), "NotFound") {
105+
data.ID = types.StringUnknown()
106+
return diags
107+
}
108+
109+
diags.Append(diagnostics.ArgoCDAPIError("read", "application", appName, err)...)
110+
111+
return diags
112+
}
113+
114+
l := len(apps.Items)
115+
116+
switch {
117+
case l < 1:
118+
data.ID = types.StringUnknown()
119+
return diags
120+
case l == 1:
121+
break
122+
case l > 1:
123+
diags.AddError(fmt.Sprintf("found multiple applications matching name '%s' and namespace '%s'", appName, namespace), "")
124+
return diags
125+
}
126+
127+
app := apps.Items[0]
128+
129+
data.Metadata = newObjectMeta(app.ObjectMeta)
130+
data.Spec = newApplicationSpec(app.Spec)
131+
data.Status = newApplicationStatus(app.Status)
132+
133+
return diags
134+
}

0 commit comments

Comments
 (0)