Skip to content

Commit 631be7f

Browse files
committed
feat: integrations data source
1 parent 56c786e commit 631be7f

File tree

3 files changed

+270
-10
lines changed

3 files changed

+270
-10
lines changed

client/integrations.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ type MeshIntegrationStatus struct {
3636

3737
// Integration Config wrapper with type discrimination
3838
type MeshIntegrationConfig struct {
39-
Type string `json:"type" tfsdk:"type"`
40-
Github *MeshGithubIntegrationProperties `json:"github,omitempty" tfsdk:"github"`
41-
Gitlab *MeshGitlabIntegrationProperties `json:"gitlab,omitempty" tfsdk:"gitlab"`
42-
AzureDevops *MeshAzureDevopsIntegrationProperties `json:"azuredevops,omitempty" tfsdk:"azuredevops"`
39+
Type string `json:"type" tfsdk:"type"`
40+
Github *MeshIntegrationGithubConfig `json:"github,omitempty" tfsdk:"github"`
41+
Gitlab *MeshIntegrationGitlabConfig `json:"gitlab,omitempty" tfsdk:"gitlab"`
42+
AzureDevops *MeshIntegrationAzureDevopsConfig `json:"azuredevops,omitempty" tfsdk:"azuredevops"`
4343
}
4444

4545
// GitHub Integration
46-
type MeshGithubIntegrationProperties struct {
46+
type MeshIntegrationGithubConfig struct {
4747
Owner string `json:"owner" tfsdk:"owner"`
4848
BaseUrl string `json:"baseUrl" tfsdk:"base_url"`
4949
AppId string `json:"appId" tfsdk:"app_id"`
@@ -52,13 +52,13 @@ type MeshGithubIntegrationProperties struct {
5252
}
5353

5454
// GitLab Integration
55-
type MeshGitlabIntegrationProperties struct {
55+
type MeshIntegrationGitlabConfig struct {
5656
BaseUrl string `json:"baseUrl" tfsdk:"base_url"`
5757
RunnerRef BuildingBlockRunnerRef `json:"runnerRef" tfsdk:"runner_ref"`
5858
}
5959

6060
// Azure DevOps Integration
61-
type MeshAzureDevopsIntegrationProperties struct {
61+
type MeshIntegrationAzureDevopsConfig struct {
6262
BaseUrl string `json:"baseUrl" tfsdk:"base_url"`
6363
Organization string `json:"organization" tfsdk:"organization"`
6464
PersonalAccessToken string `json:"personalAccessToken" tfsdk:"personal_access_token"`
@@ -68,7 +68,7 @@ type MeshAzureDevopsIntegrationProperties struct {
6868
// Building Block Runner Reference
6969
type BuildingBlockRunnerRef struct {
7070
Uuid string `json:"uuid" tfsdk:"uuid"`
71-
Kind string `json:"kind,omitempty" tfsdk:"kind"`
71+
Kind string `json:"kind" tfsdk:"kind"`
7272
}
7373

7474
// Workload Identity Federation
@@ -130,13 +130,12 @@ func (c *MeshStackProviderClient) ReadIntegration(workspace string, uuid string)
130130
return &integration, nil
131131
}
132132

133-
func (c *MeshStackProviderClient) ReadIntegrations(workspaceIdentifier string) (*[]MeshIntegration, error) {
133+
func (c *MeshStackProviderClient) ReadIntegrations() (*[]MeshIntegration, error) {
134134
var allIntegrations []MeshIntegration
135135

136136
pageNumber := 0
137137
targetUrl := c.endpoints.Integrations
138138
query := targetUrl.Query()
139-
query.Set("workspaceIdentifier", workspaceIdentifier)
140139

141140
for {
142141
query.Set("page", fmt.Sprintf("%d", pageNumber))
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
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+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func (p *MeshStackProvider) DataSources(ctx context.Context) []func() datasource
145145
NewLandingZoneDataSource,
146146
NewPlatformDataSource,
147147
NewPaymentMethodDataSource,
148+
NewIntegrationsDataSource,
148149
}
149150
}
150151

0 commit comments

Comments
 (0)