/
gen_obj_token_data_source.go
190 lines (169 loc) · 5.51 KB
/
gen_obj_token_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package awx
import (
"context"
"fmt"
"net/http"
p "path"
c "github.com/ilijamt/terraform-provider-awx/internal/client"
"github.com/ilijamt/terraform-provider-awx/internal/helpers"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = &tokensDataSource{}
_ datasource.DataSourceWithConfigure = &tokensDataSource{}
)
// NewTokensDataSource is a helper function to instantiate the Tokens data source.
func NewTokensDataSource() datasource.DataSource {
return &tokensDataSource{}
}
// tokensDataSource is the data source implementation.
type tokensDataSource struct {
client c.Client
endpoint string
}
// Configure adds the provider configured client to the data source.
func (o *tokensDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
o.client = req.ProviderData.(c.Client)
o.endpoint = "/api/v2/tokens/"
}
// Metadata returns the data source type name.
func (o *tokensDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_token"
}
// Schema defines the schema for the data source.
func (o *tokensDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Data only elements
"application": schema.Int64Attribute{
Description: "Application",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"description": schema.StringAttribute{
Description: "Optional description of this access token.",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"expires": schema.StringAttribute{
Description: "Expires",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"id": schema.Int64Attribute{
Description: "Database ID for this access token.",
Sensitive: false,
Optional: true,
Computed: true,
Validators: []validator.Int64{
int64validator.ExactlyOneOf(
path.MatchRoot("id"),
),
},
},
"refresh_token": schema.StringAttribute{
Description: "Refresh token",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"scope": schema.StringAttribute{
Description: "Allowed scopes, further restricts user's permissions. Must be a simple space-separated string with allowed scopes ['read', 'write'].",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"token": schema.StringAttribute{
Description: "Token",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"user": schema.Int64Attribute{
Description: "The user representing the token owner",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
// Write only elements
},
}
}
func (o *tokensDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{}
}
// Read refreshes the Terraform state with the latest data.
func (o *tokensDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state tokensTerraformModel
var err error
var endpoint string
var searchDefined bool
// Only one group should evaluate to True, terraform should prevent from being able to set
// the conflicting groups
// Evaluate group 'by_id' based on the schema definition
var groupByIdExists = func() bool {
var groupByIdExists = true
var paramsById = []any{o.endpoint}
var attrID types.Int64
req.Config.GetAttribute(ctx, path.Root("id"), &attrID)
groupByIdExists = groupByIdExists && (!attrID.IsNull() && !attrID.IsUnknown())
paramsById = append(paramsById, attrID.ValueInt64())
if groupByIdExists {
endpoint = p.Clean(fmt.Sprintf("%s/%d/", paramsById...))
}
return groupByIdExists
}()
searchDefined = searchDefined || groupByIdExists
if !searchDefined {
var detailMessage string
resp.Diagnostics.AddError(
"missing configuration for one of the predefined search groups",
detailMessage,
)
return
}
// Creates a new request for Tokens
var r *http.Request
if r, err = o.client.NewRequest(ctx, http.MethodGet, endpoint, nil); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for Tokens on %s for read", o.endpoint),
err.Error(),
)
return
}
// Try and actually fetch the data for Tokens
var data map[string]any
if data, err = o.client.Do(ctx, r); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to read resource for Tokens on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if data, d, err = helpers.ExtractDataIfSearchResult(data); err != nil {
resp.Diagnostics.Append(d...)
return
}
if d, err = state.updateFromApiData(data); err != nil {
resp.Diagnostics.Append(d...)
return
}
// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}