|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/meshcloud/terraform-provider-meshstack/client" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | +) |
| 13 | + |
| 14 | +// Ensure the implementation satisfies the expected interfaces. |
| 15 | +var ( |
| 16 | + _ datasource.DataSource = &integrationsDataSource{} |
| 17 | +) |
| 18 | + |
| 19 | +// NewIntegrationsDataSource is a helper function to simplify the provider implementation. |
| 20 | +func NewIntegrationsDataSource() datasource.DataSource { |
| 21 | + return &integrationsDataSource{} |
| 22 | +} |
| 23 | + |
| 24 | +// integrationsDataSource is the data source implementation. |
| 25 | +type integrationsDataSource struct { |
| 26 | + client *client.MeshStackProviderClient |
| 27 | +} |
| 28 | + |
| 29 | +// Metadata returns the data source type name. |
| 30 | +func (d *integrationsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 31 | + resp.TypeName = req.ProviderTypeName + "_integrations" |
| 32 | +} |
| 33 | + |
| 34 | +// Schema defines the schema for the data source. |
| 35 | +func (d *integrationsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 36 | + workloadIdentityFederation := schema.SingleNestedAttribute{ |
| 37 | + Computed: true, |
| 38 | + Attributes: map[string]schema.Attribute{ |
| 39 | + "issuer": schema.StringAttribute{ |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "subject": schema.StringAttribute{ |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "gcp": schema.SingleNestedAttribute{ |
| 46 | + Computed: true, |
| 47 | + Attributes: map[string]schema.Attribute{ |
| 48 | + "audience": schema.StringAttribute{ |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + "aws": schema.SingleNestedAttribute{ |
| 54 | + Computed: true, |
| 55 | + Attributes: map[string]schema.Attribute{ |
| 56 | + "audience": schema.StringAttribute{ |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + "thumbprint": schema.StringAttribute{ |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "azure": schema.SingleNestedAttribute{ |
| 65 | + Computed: true, |
| 66 | + Attributes: map[string]schema.Attribute{ |
| 67 | + "audience": schema.StringAttribute{ |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + resp.Schema = schema.Schema{ |
| 76 | + MarkdownDescription: "List of integrations.", |
| 77 | + |
| 78 | + Attributes: map[string]schema.Attribute{ |
| 79 | + "workload_identity_federation": schema.SingleNestedAttribute{ |
| 80 | + MarkdownDescription: "Workload identity federation information for built in integrations.", |
| 81 | + Computed: true, |
| 82 | + Attributes: map[string]schema.Attribute{ |
| 83 | + "replicator": workloadIdentityFederation, |
| 84 | + "metering": workloadIdentityFederation, |
| 85 | + }, |
| 86 | + }, |
| 87 | + "integrations": schema.ListNestedAttribute{ |
| 88 | + MarkdownDescription: "List of integrations", |
| 89 | + Computed: true, |
| 90 | + NestedObject: schema.NestedAttributeObject{ |
| 91 | + Attributes: map[string]schema.Attribute{ |
| 92 | + "api_version": schema.StringAttribute{ |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "kind": schema.StringAttribute{ |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + "metadata": schema.SingleNestedAttribute{ |
| 99 | + Computed: true, |
| 100 | + Attributes: map[string]schema.Attribute{ |
| 101 | + "uuid": schema.StringAttribute{ |
| 102 | + Computed: true, |
| 103 | + }, |
| 104 | + "owned_by_workspace": schema.StringAttribute{ |
| 105 | + Computed: true, |
| 106 | + }, |
| 107 | + "created_on": schema.StringAttribute{ |
| 108 | + Computed: true, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + "spec": schema.SingleNestedAttribute{ |
| 113 | + Computed: true, |
| 114 | + Attributes: map[string]schema.Attribute{ |
| 115 | + "display_name": schema.StringAttribute{ |
| 116 | + Computed: true, |
| 117 | + }, |
| 118 | + "config": schema.SingleNestedAttribute{ |
| 119 | + Computed: true, |
| 120 | + Attributes: map[string]schema.Attribute{ |
| 121 | + "type": schema.StringAttribute{ |
| 122 | + Computed: true, |
| 123 | + }, |
| 124 | + "github": schema.SingleNestedAttribute{ |
| 125 | + Computed: true, |
| 126 | + Optional: true, |
| 127 | + Attributes: map[string]schema.Attribute{ |
| 128 | + "owner": schema.StringAttribute{ |
| 129 | + Computed: true, |
| 130 | + }, |
| 131 | + "base_url": schema.StringAttribute{ |
| 132 | + Computed: true, |
| 133 | + }, |
| 134 | + "app_id": schema.StringAttribute{ |
| 135 | + Computed: true, |
| 136 | + }, |
| 137 | + "app_private_key": schema.StringAttribute{ |
| 138 | + Computed: true, |
| 139 | + }, |
| 140 | + "runner_ref": schema.SingleNestedAttribute{ |
| 141 | + Computed: true, |
| 142 | + Attributes: map[string]schema.Attribute{ |
| 143 | + "uuid": schema.StringAttribute{ |
| 144 | + Computed: true, |
| 145 | + }, |
| 146 | + "kind": schema.StringAttribute{ |
| 147 | + Computed: true, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + "gitlab": schema.SingleNestedAttribute{ |
| 154 | + Computed: true, |
| 155 | + Optional: true, |
| 156 | + Attributes: map[string]schema.Attribute{ |
| 157 | + "base_url": schema.StringAttribute{ |
| 158 | + Computed: true, |
| 159 | + }, |
| 160 | + "runner_ref": schema.SingleNestedAttribute{ |
| 161 | + Computed: true, |
| 162 | + Attributes: map[string]schema.Attribute{ |
| 163 | + "uuid": schema.StringAttribute{ |
| 164 | + Computed: true, |
| 165 | + }, |
| 166 | + "kind": schema.StringAttribute{ |
| 167 | + Computed: true, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + "azuredevops": schema.SingleNestedAttribute{ |
| 174 | + Computed: true, |
| 175 | + Optional: true, |
| 176 | + Attributes: map[string]schema.Attribute{ |
| 177 | + "base_url": schema.StringAttribute{ |
| 178 | + Computed: true, |
| 179 | + }, |
| 180 | + "organization": schema.StringAttribute{ |
| 181 | + Computed: true, |
| 182 | + }, |
| 183 | + "personal_access_token": schema.StringAttribute{ |
| 184 | + Computed: true, |
| 185 | + }, |
| 186 | + "runner_ref": schema.SingleNestedAttribute{ |
| 187 | + Computed: true, |
| 188 | + Attributes: map[string]schema.Attribute{ |
| 189 | + "uuid": schema.StringAttribute{ |
| 190 | + Computed: true, |
| 191 | + }, |
| 192 | + "kind": schema.StringAttribute{ |
| 193 | + Computed: true, |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + "status": schema.SingleNestedAttribute{ |
| 204 | + Computed: true, |
| 205 | + Optional: true, |
| 206 | + Attributes: map[string]schema.Attribute{ |
| 207 | + "is_built_in": schema.BoolAttribute{ |
| 208 | + Computed: true, |
| 209 | + }, |
| 210 | + "workload_identity_federation": workloadIdentityFederation, |
| 211 | + }, |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + }, |
| 216 | + }, |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +// Configure adds the provider configured client to the data source. |
| 221 | +func (d *integrationsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 222 | + if req.ProviderData == nil { |
| 223 | + return |
| 224 | + } |
| 225 | + |
| 226 | + client, ok := req.ProviderData.(*client.MeshStackProviderClient) |
| 227 | + if !ok { |
| 228 | + resp.Diagnostics.AddError( |
| 229 | + "Unexpected Data Source Configure Type", |
| 230 | + fmt.Sprintf("Expected *client.MeshStackProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 231 | + ) |
| 232 | + return |
| 233 | + } |
| 234 | + d.client = client |
| 235 | +} |
| 236 | + |
| 237 | +// Read refreshes the Terraform state with the latest data. |
| 238 | +func (d *integrationsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 239 | + integrations, err := d.client.ReadIntegrations() |
| 240 | + if err != nil { |
| 241 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read integrations, got error: %s", err)) |
| 242 | + return |
| 243 | + } |
| 244 | + |
| 245 | + // Save data into Terraform state |
| 246 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("integrations"), &integrations)...) |
| 247 | + |
| 248 | + for _, integration := range *integrations { |
| 249 | + if integration.Status != nil && integration.Status.IsBuiltIn { |
| 250 | + if integration.Spec.Config.Type == "replicator" { |
| 251 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, |
| 252 | + path.Root("workload_identity_federation").AtName("replicator"), integration.Status.WorkloadIdentityFederation)...) |
| 253 | + } |
| 254 | + if integration.Spec.Config.Type == "metering" { |
| 255 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, |
| 256 | + path.Root("workload_identity_federation").AtName("metering"), integration.Status.WorkloadIdentityFederation)...) |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments