@@ -24,10 +24,14 @@ import (
2424
2525var validMapFieldNameRe = regexp .MustCompile ("^[a-zA-Z0-9]+([a-zA-Z0-9_.-]*[a-zA-Z0-9]+)?$" )
2626
27+ // metricsDatasetKind is the dataset kind that does not support map fields.
28+ const metricsDatasetKind = "otel:metrics:v1"
29+
2730// Ensure provider defined types fully satisfy framework interfaces.
2831var (
2932 _ resource.Resource = & DatasetResource {}
3033 _ resource.ResourceWithImportState = & DatasetResource {}
34+ _ validator.List = unsupportedForKindValidator {}
3135)
3236
3337func NewDatasetResource () resource.Resource {
@@ -125,7 +129,7 @@ func (r *DatasetResource) Schema(_ context.Context, _ resource.SchemaRequest, re
125129 "map_fields" : schema.ListAttribute {
126130 Optional : true ,
127131 Computed : true ,
128- MarkdownDescription : "Map fields for the dataset" ,
132+ MarkdownDescription : "Map fields for the dataset. Not supported for datasets of kind 'otel:metrics:v1' " ,
129133 ElementType : types .StringType ,
130134 Validators : []validator.List {
131135 listvalidator .All (
@@ -136,13 +140,57 @@ func (r *DatasetResource) Schema(_ context.Context, _ resource.SchemaRequest, re
136140 ),
137141 ),
138142 listvalidator .UniqueValues (),
143+ unsupportedForKindValidator {kind : metricsDatasetKind },
139144 ),
140145 },
141146 },
142147 },
143148 }
144149}
145150
151+ // unsupportedForKindValidator rejects a list attribute that is set on a dataset of
152+ // an unsupported kind.
153+ type unsupportedForKindValidator struct {
154+ kind string
155+ }
156+
157+ func (v unsupportedForKindValidator ) Description (_ context.Context ) string {
158+ return fmt .Sprintf ("must not be set when kind is %q" , v .kind )
159+ }
160+
161+ func (v unsupportedForKindValidator ) MarkdownDescription (ctx context.Context ) string {
162+ return v .Description (ctx )
163+ }
164+
165+ func (v unsupportedForKindValidator ) ValidateList (ctx context.Context , req validator.ListRequest , resp * validator.ListResponse ) {
166+ var kind types.String
167+
168+ resp .Diagnostics .Append (req .Config .GetAttribute (ctx , path .Root ("kind" ), & kind )... )
169+ if resp .Diagnostics .HasError () {
170+ return
171+ }
172+
173+ if ! attributeConflictsWithKind (kind , req .ConfigValue , v .kind ) {
174+ return
175+ }
176+
177+ resp .Diagnostics .AddAttributeError (
178+ req .Path ,
179+ "Invalid Attribute Combination" ,
180+ fmt .Sprintf ("%s is not supported for datasets of kind %q, remove it from the configuration." , req .Path , v .kind ),
181+ )
182+ }
183+
184+ // attributeConflictsWithKind reports whether the attribute is configured on a dataset
185+ // of the kind that does not support it.
186+ func attributeConflictsWithKind (kind types.String , value types.List , unsupportedKind string ) bool {
187+ if kind .IsNull () || kind .IsUnknown () || kind .ValueString () != unsupportedKind {
188+ return false
189+ }
190+
191+ return ! value .IsNull () && ! value .IsUnknown ()
192+ }
193+
146194func (r * DatasetResource ) Configure (ctx context.Context , req resource.ConfigureRequest , resp * resource.ConfigureResponse ) {
147195 if req .ProviderData == nil {
148196 return
0 commit comments