generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_data_source.go
121 lines (104 loc) · 3.66 KB
/
project_data_source.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
package provider
import (
"context"
"fmt"
"github.com/elestio/elestio-go-api-client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ datasource.DataSource = &ProjectDataSource{}
type (
ProjectDataSource struct {
client *elestio.Client
}
ProjectDataSourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
TechnicalEmails types.String `tfsdk:"technical_emails"` // deprecated
TechnicalEmail types.String `tfsdk:"technical_email"`
NetworkCIDR types.String `tfsdk:"network_cidr"`
CreationDate types.String `tfsdk:"creation_date"`
}
)
func NewProjectDataSource() datasource.DataSource {
return &ProjectDataSource{}
}
func (d *ProjectDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project"
}
func (d *ProjectDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Project data source",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Project identifier",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Project name",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "Project description",
Computed: true,
},
"technical_emails": schema.StringAttribute{
MarkdownDescription: "Project technical emails",
DeprecationMessage: "Use `technical_email` instead",
Computed: true,
},
"technical_email": schema.StringAttribute{
MarkdownDescription: "Email address which will receive technical notifications",
Computed: true,
},
"network_cidr": schema.StringAttribute{
MarkdownDescription: "Project network CIDR",
Computed: true,
},
"creation_date": schema.StringAttribute{
MarkdownDescription: "Project creation date",
Computed: true,
},
},
}
}
func (d *ProjectDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*elestio.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *elestio.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state ProjectDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
project, err := d.client.Project.Get(state.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Elestio Project",
err.Error(),
)
return
}
state.Id = types.StringValue(project.ID.String())
state.Name = types.StringValue(project.Name)
state.Description = types.StringValue(project.Description)
state.TechnicalEmails = types.StringValue(project.TechnicalEmail)
state.TechnicalEmail = types.StringValue(project.TechnicalEmail)
state.NetworkCIDR = types.StringValue(project.NetworkCIDR)
state.CreationDate = types.StringValue(project.CreationDate)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}