@@ -11,6 +11,10 @@ import (
11
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
12
)
13
13
14
+ // validate_data.go ensures that the map of media_types to titles is cached during the plan.
15
+ // I'm not actually sure if this carries over to the apply?
16
+ // It also validates that the media_types in the /data directory contain a title provided as an input.
17
+
14
18
var mediaTypeCache map [string ]map [string ][]string
15
19
var cacheLoaded bool
16
20
@@ -23,11 +27,13 @@ type MediaTypeData struct {
23
27
// LoadMediaTypes scans the specified dataDir directory, reads each subdirectory as a media type,
24
28
// and loads each JSON file in those subdirectories as titles of that media type.
25
29
func LoadMediaTypes (dataDir string ) (map [string ]map [string ][]string , error ) {
30
+
26
31
// This map will hold the media type as key and a map of titles with their names as value.
27
32
mediaTypes := make (map [string ]map [string ][]string )
28
33
29
34
// Read the main data directory.
30
- entries , err := os .ReadDir (dataDir )
35
+ dataDirPath := filepath .FromSlash (dataDir )
36
+ entries , err := os .ReadDir (dataDirPath )
31
37
if err != nil {
32
38
return nil , fmt .Errorf ("failed to read data directory: %v" , err )
33
39
}
@@ -36,7 +42,7 @@ func LoadMediaTypes(dataDir string) (map[string]map[string][]string, error) {
36
42
for _ , entry := range entries {
37
43
if entry .IsDir () {
38
44
mediaType := entry .Name ()
39
- mediaPath := filepath .Join (dataDir , mediaType )
45
+ mediaPath := filepath .Join (dataDirPath , mediaType )
40
46
mediaTypeMap := make (map [string ][]string )
41
47
42
48
// Read files within the media type directory.
@@ -59,6 +65,7 @@ func LoadMediaTypes(dataDir string) (map[string]map[string][]string, error) {
59
65
}
60
66
61
67
// Use the title from the JSON or the filename without extension as a fallback.
68
+ // I actually don't know if this will cause unexpected outcomes for users? We shall see.
62
69
title := mediaData .Title
63
70
if title == "" {
64
71
title = strings .TrimSuffix (file .Name (), ".json" )
@@ -79,6 +86,7 @@ func LoadMediaTypes(dataDir string) (map[string]map[string][]string, error) {
79
86
return mediaTypes , nil
80
87
}
81
88
89
+ // Function to load media_type to title mapping from cache if the cache is not considered loaded already.
82
90
func LoadAndCacheMediaTypes (dataDir string ) error {
83
91
if ! cacheLoaded {
84
92
var err error
@@ -91,35 +99,39 @@ func LoadAndCacheMediaTypes(dataDir string) error {
91
99
return nil
92
100
}
93
101
102
+ // Function called by customizeDiff: during resource creation
94
103
func customValidateMediaTypeAndTitle (ctx context.Context , diff * schema.ResourceDiff , v interface {}) error {
95
104
mediaType := diff .Get ("media_type" ).(string )
96
105
title := diff .Get ("title" ).(string )
97
106
98
- if ! isValidMediaTypeAndTitle (mediaType , title ) {
99
- return fmt .Errorf ("'%s' is not a valid title for media type '%s'" , title , mediaType )
107
+ isValid , errMsg := isValidMediaTypeAndTitle (mediaType , title )
108
+ if ! isValid {
109
+ return fmt .Errorf (errMsg )
100
110
}
101
111
102
112
return nil
103
113
}
104
114
105
- func isValidMediaTypeAndTitle (mediaType , title string ) bool {
115
+ // Function to validate that the user provided media_type and title are valid or not
116
+ func isValidMediaTypeAndTitle (mediaType , title string ) (bool , string ) {
106
117
if ! cacheLoaded {
107
118
if err := LoadAndCacheMediaTypes ("data" ); err != nil {
108
- fmt .Printf ("Error loading media types: %v\n " , err )
109
- return false
119
+ return false , fmt .Sprintf ("error loading media types: %v" , err )
110
120
}
111
121
}
112
122
123
+ // Check if the media_type is valid
113
124
titlesMap , ok := mediaTypeCache [mediaType ]
114
125
if ! ok {
115
- return false
126
+ return false , fmt . Sprintf ( "'%s' is not a recognized media type" , mediaType )
116
127
}
117
128
129
+ // Check if the title is valid for the given media_type
118
130
for t := range titlesMap {
119
131
if strings .EqualFold (t , title ) {
120
- return true
132
+ return true , ""
121
133
}
122
134
}
123
135
124
- return false
136
+ return false , fmt . Sprintf ( "'%s' is not a valid title for media type '%s'" , title , mediaType )
125
137
}
0 commit comments