/
subuser_data_source.go
123 lines (103 loc) · 3.64 KB
/
subuser_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
122
123
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/kenzo0107/sendgrid"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &subuserDataSource{}
_ datasource.DataSourceWithConfigure = &subuserDataSource{}
)
func newSubuserDataSource() datasource.DataSource {
return &subuserDataSource{}
}
type subuserDataSource struct {
client *sendgrid.Client
}
type subuserDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
Username types.String `tfsdk:"username"`
Email types.String `tfsdk:"email"`
}
func (d *subuserDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_subuser"
}
func (d *subuserDataSource) 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.(*sendgrid.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *sendgrid.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *subuserDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `
Provides a subuser resource.
Subusers help you segment your email sending and API activity. You assign permissions and credit limits when you create the subuser. We recommend creating subusers for each of the different types of emails you send - one subuser for transactional emails, and another for marketing emails. Breaking your sending up this way allows you to get separate statistics for each type of email you send.
For more detailed information, please see the [SendGrid documentation](https://docs.sendgrid.com/ui/account-and-settings/api-keys).
`,
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "The user ID of the subuser.",
Computed: true,
},
"username": schema.StringAttribute{
MarkdownDescription: "The username of the subuser.",
Required: true,
},
"email": schema.StringAttribute{
MarkdownDescription: "The email of the subuser.",
Computed: true,
},
},
}
}
func (d *subuserDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var s subuserDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &s)...)
if resp.Diagnostics.HasError() {
return
}
username := s.Username.ValueString()
subusers, err := d.client.GetSubusers(ctx, &sendgrid.InputGetSubusers{
Username: username,
Limit: 1,
})
if err != nil {
resp.Diagnostics.AddError(
"Reading subuser",
fmt.Sprintf("Unable to get subuser by username: %s, err: %s", username, err.Error()),
)
return
}
if len(subusers) == 0 {
resp.Diagnostics.AddError(
"Reading subuser",
fmt.Sprintf("Unable to read subuser (username: %s)", username),
)
return
}
subuser := subusers[0]
s = subuserDataSourceModel{
ID: types.Int64Value(subuser.ID),
Username: types.StringValue(subuser.Username),
Email: types.StringValue(subuser.Email),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &s)...)
if resp.Diagnostics.HasError() {
return
}
}