Skip to content

Commit

Permalink
Add argocd_application data source
Browse files Browse the repository at this point in the history
  • Loading branch information
onematchfox authored Jul 7, 2023
1 parent 6ae499d commit cced478
Show file tree
Hide file tree
Showing 15 changed files with 2,703 additions and 3 deletions.
361 changes: 361 additions & 0 deletions docs/data-sources/application.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions examples/data-sources/argocd_application/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data "argocd_application" "foo" {
metadata = {
name = "foo"
namespace = "argocd"
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/argoproj/gitops-engine v0.7.3
github.com/argoproj/pkg v0.13.7-0.20221221191914-44694015343d
github.com/cristalhq/jwt/v3 v3.1.0
github.com/elliotchance/pie/v2 v2.6.0
github.com/golang/protobuf v1.5.3
github.com/hashicorp/terraform-plugin-docs v0.15.0
github.com/hashicorp/terraform-plugin-framework v1.3.1
Expand Down Expand Up @@ -206,7 +207,7 @@ require (
go.opentelemetry.io/otel v1.11.1 // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 // indirect
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFP
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elliotchance/pie/v2 v2.6.0 h1:lAQxtrxijynVjXuU0btqg9oh4Izq6IQVtypJw/45gZ4=
github.com/elliotchance/pie/v2 v2.6.0/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful/v3 v3.8.0 h1:eCZ8ulSerjdAiaNpF7GxXIE7ZCMo1moN1qX+S609eVw=
Expand Down Expand Up @@ -1159,8 +1161,9 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 h1:Or4Ra3AAlhUlNn8WmIzw2Yq2vUHSkrP6E2e/FIESpF8=
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2/go.mod h1:a3o/VtDNHN+dCVLEpzjjUHOzR+Ln3DHX056ZPzoZGGA=
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 h1:ba9YlqfDGTTQ5aZ2fwOoQ1hf32QySyQkR6ODGDzHlnE=
golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
134 changes: 134 additions & 0 deletions internal/provider/data_source_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package provider

import (
"context"
"fmt"
"strings"

"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/oboukili/terraform-provider-argocd/internal/diagnostics"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &applicationDataSource{}

func NewArgoCDApplicationDataSource() datasource.DataSource {
return &applicationDataSource{}
}

// applicationDataSource defines the data source implementation.
type applicationDataSource struct {
si *ServerInterface
}

func (d *applicationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_application"
}

func (d *applicationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Reads an existing ArgoCD application.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "ArgoCD application identifier",
Computed: true,
},
"metadata": objectMetaSchemaAttribute("applications.argoproj.io", true),
"spec": applicationSpecSchemaAttribute(true, true),
"status": applicationStatusSchemaAttribute(),
},
}
}

func (d *applicationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

si, ok := req.ProviderData.(*ServerInterface)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Provider Data",
fmt.Sprintf("Expected *ServerInterface, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.si = si
}

func (d *applicationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data applicationModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

// Initialize API clients
resp.Diagnostics.Append(d.si.InitClients(ctx)...)

// Check for errors before proceeding
if resp.Diagnostics.HasError() {
return
}

id := fmt.Sprintf("%s:%s", data.Metadata.Name.ValueString(), data.Metadata.Namespace.ValueString())
data.ID = types.StringValue(id)

// Read application
resp.Diagnostics.Append(readApplication(ctx, d.si, &data)...)

tflog.Trace(ctx, "read ArgoCD application")

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func readApplication(ctx context.Context, si *ServerInterface, data *applicationModel) (diags diag.Diagnostics) {
ids := strings.Split(data.ID.ValueString(), ":")
appName := ids[0]
namespace := ids[1]

apps, err := si.ApplicationClient.List(ctx, &application.ApplicationQuery{
Name: &appName,
AppNamespace: &namespace,
})
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
data.ID = types.StringUnknown()
return diags
}

diags.Append(diagnostics.ArgoCDAPIError("read", "application", appName, err)...)

return diags
}

l := len(apps.Items)

switch {
case l < 1:
data.ID = types.StringUnknown()
return diags
case l == 1:
break
case l > 1:
diags.AddError(fmt.Sprintf("found multiple applications matching name '%s' and namespace '%s'", appName, namespace), "")
return diags
}

app := apps.Items[0]

data.Metadata = newObjectMeta(app.ObjectMeta)
data.Spec = newApplicationSpec(app.Spec)
data.Status = newApplicationStatus(app.Status)

return diags
}
Loading

0 comments on commit cced478

Please sign in to comment.