/
data_source_integrations.go
178 lines (159 loc) · 5.59 KB
/
data_source_integrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package provider
import (
"context"
"fmt"
"github.com/apono-io/apono-sdk-go"
"github.com/apono-io/terraform-provider-apono/internal/models"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &integrationsDataSource{}
func NewIntegrationsDataSource() datasource.DataSource {
return &integrationsDataSource{}
}
// integrationsDataSource defines the data source implementation for Apono connector.
type integrationsDataSource struct {
provider *AponoProvider
}
// integrationsDataSourceModel describes the data source data model.
type integrationsDataSourceModel struct {
ID types.String `tfsdk:"id"`
Type types.String `tfsdk:"type"`
ConnectorID types.String `tfsdk:"connector_id"`
Integrations []models.IntegrationModel `tfsdk:"integrations"`
}
func (d *integrationsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_integrations"
}
func (d *integrationsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Use this data source to get list integrations.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"type": schema.StringAttribute{
MarkdownDescription: "Integration type to filter by",
Optional: true,
},
"connector_id": schema.StringAttribute{
MarkdownDescription: "Integration connector id to filter by",
Optional: true,
},
"integrations": schema.ListNestedAttribute{
Computed: true,
MarkdownDescription: "List of integrations",
NestedObject: schema.NestedAttributeObject{
Attributes: IntegrationDataSourceAttributes(),
},
},
},
}
}
func (d *integrationsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
d.provider, resp.Diagnostics = toProvider(req.ProviderData)
}
func (d *integrationsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var model integrationsDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read integrations")
response, _, err := d.provider.client.IntegrationsApi.ListIntegrationsV2(ctx).
Execute()
if err != nil {
if apiError, ok := err.(*apono.GenericOpenAPIError); ok {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to list integrations, error: %s, body: %s", apiError.Error(), string(apiError.Body())))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to list integrations: %s", err.Error()))
}
return
}
model.ID = types.StringValue(id.UniqueId())
for _, integration := range response.Data {
if !model.Type.IsNull() && integration.Type != model.Type.ValueString() {
continue
}
if !model.ConnectorID.IsNull() && *integration.ProvisionerId.Get() != model.ConnectorID.ValueString() {
continue
}
m, diagnostics := models.ConvertToIntegrationModel(ctx, &integration)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
}
model.Integrations = append(model.Integrations, *m)
}
// Save state into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}
func IntegrationDataSourceAttributes() map[string]schema.Attribute {
return map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Integration identifier",
},
"name": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Integration name",
},
"type": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Integration type",
},
"connector_id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Apono connector identifier",
},
"metadata": schema.MapAttribute{
MarkdownDescription: "Integration metadata",
Optional: true,
ElementType: types.StringType,
},
"aws_secret": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"region": schema.StringAttribute{
MarkdownDescription: "Aws secret region",
Required: true,
},
"secret_id": schema.StringAttribute{
MarkdownDescription: "Aws secret name or ARN",
Required: true,
},
},
},
"gcp_secret": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"project": schema.StringAttribute{
MarkdownDescription: "GCP secret project",
Required: true,
},
"secret_id": schema.StringAttribute{
MarkdownDescription: "GCP secret ID",
Required: true,
},
},
},
"kubernetes_secret": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"namespace": schema.StringAttribute{
MarkdownDescription: "Kubernetes secret namespace",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Kubernetes secret name",
Required: true,
},
},
},
}
}