generated from hashicorp/terraform-provider-scaffolding-framework
/
administered_identities_me_data_source.go
165 lines (143 loc) · 5.9 KB
/
administered_identities_me_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
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
package provider
import (
"context"
"fmt"
"github.com/core-infra-svcs/terraform-provider-meraki/tools"
"github.com/hashicorp/terraform-plugin-framework/types"
"time"
"github.com/core-infra-svcs/terraform-provider-meraki/internal/provider/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-log/tflog"
openApiClient "github.com/meraki/dashboard-api-go/client"
)
// Ensure provider defined types fully satisfy framework interfaces
var _ datasource.DataSource = &AdministeredIdentitiesMeDataSource{}
func NewAdministeredIdentitiesMeDataSource() datasource.DataSource {
return &AdministeredIdentitiesMeDataSource{}
}
// AdministeredIdentitiesMeDataSource defines the data source implementation.
type AdministeredIdentitiesMeDataSource struct {
client *openApiClient.APIClient
}
// AdministeredIdentitiesMeDataSourceModel describes the data source data model.
type AdministeredIdentitiesMeDataSourceModel struct {
Id types.String `tfsdk:"id"`
AuthenticationApiKeyCreated jsontypes.Bool `tfsdk:"authentication_api_key_created"`
AuthenticationMode jsontypes.String `tfsdk:"authentication_mode"`
AuthenticationSaml jsontypes.Bool `tfsdk:"authentication_saml_enabled"`
AuthenticationTwofactor jsontypes.Bool `tfsdk:"authentication_two_factor_enabled"`
Email jsontypes.String `tfsdk:"email"`
LastUsedDashboardAt jsontypes.String `tfsdk:"last_used_dashboard_at"`
Name jsontypes.String `tfsdk:"name"`
}
func (d *AdministeredIdentitiesMeDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_administered_identities_me"
}
func (d *AdministeredIdentitiesMeDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Returns the identity of the current user",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Username",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"email": schema.StringAttribute{
MarkdownDescription: "User email",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"last_used_dashboard_at": schema.StringAttribute{
MarkdownDescription: "Last seen active on Dashboard UI",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"authentication_mode": schema.StringAttribute{
MarkdownDescription: "Authentication mode",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"authentication_api_key_created": schema.BoolAttribute{
MarkdownDescription: "If API key is created for this user",
Optional: true,
Computed: true,
CustomType: jsontypes.BoolType,
},
"authentication_two_factor_enabled": schema.BoolAttribute{
MarkdownDescription: "If twoFactor authentication is enabled for this user",
Optional: true,
Computed: true,
CustomType: jsontypes.BoolType,
},
"authentication_saml_enabled": schema.BoolAttribute{
MarkdownDescription: "If SAML authentication is enabled for this user",
Optional: true,
Computed: true,
CustomType: jsontypes.BoolType,
},
},
}
}
func (d *AdministeredIdentitiesMeDataSource) 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.(*openApiClient.APIClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *AdministeredIdentitiesMeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data AdministeredIdentitiesMeDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
inlineResp, httpResp, err := d.client.AdministeredApi.GetAdministeredIdentitiesMe(context.Background()).Execute()
if err != nil {
resp.Diagnostics.AddError(
"HTTP Client Failure",
tools.HttpDiagnostics(httpResp),
)
return
}
// Check for API success inlineResp code
if httpResp.StatusCode != 200 {
resp.Diagnostics.AddError(
"Unexpected HTTP Response Status Code",
fmt.Sprintf("%v", httpResp.StatusCode),
)
return
}
// Check for errors after diagnostics collected
if resp.Diagnostics.HasError() {
return
}
data.Id = types.StringValue("example-id")
data.Name = jsontypes.StringValue(inlineResp.GetName())
data.Email = jsontypes.StringValue(inlineResp.GetEmail())
data.LastUsedDashboardAt = jsontypes.StringValue(inlineResp.GetLastUsedDashboardAt().Format(time.RFC3339))
data.AuthenticationMode = jsontypes.StringValue(inlineResp.Authentication.GetMode())
data.AuthenticationApiKeyCreated = jsontypes.BoolValue(inlineResp.Authentication.Api.Key.GetCreated())
data.AuthenticationTwofactor = jsontypes.BoolValue(inlineResp.Authentication.TwoFactor.GetEnabled())
data.AuthenticationSaml = jsontypes.BoolValue(inlineResp.Authentication.Saml.GetEnabled())
// Write logs using the tflog package
tflog.Trace(ctx, "read a data source")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}