|
| 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