1
1
package epic
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"math/rand"
7
+ "os"
8
+ "path/filepath"
6
9
"strconv"
10
+ "strings"
7
11
"time"
8
12
9
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
14
)
11
15
16
+ type TitleData struct {
17
+ Title string `json:"title"`
18
+ Names []string `json:"names"`
19
+ }
20
+
12
21
func resourceRandomName () * schema.Resource {
13
22
return & schema.Resource {
14
23
Description : "Generates a random character name based on the media type and title specified." ,
@@ -35,51 +44,49 @@ func resourceRandomName() *schema.Resource {
35
44
Description : "The randomly generated name." ,
36
45
},
37
46
},
47
+ CustomizeDiff : customValidateMediaTypeAndTitle ,
48
+ }
49
+ }
50
+
51
+ func loadNames (mediaType , title string ) ([]string , error ) {
52
+ sanitizedTitle := strings .ReplaceAll (title , " " , "_" )
53
+ fileName := fmt .Sprintf ("%s.json" , sanitizedTitle )
54
+ filePath := filepath .Join ("data" , mediaType , fileName )
55
+
56
+ data , err := os .ReadFile (filePath )
57
+ if err != nil {
58
+ return nil , fmt .Errorf ("failed to read %s: %v" , filePath , err )
59
+ }
60
+
61
+ var titleData TitleData
62
+ if err := json .Unmarshal (data , & titleData ); err != nil {
63
+ return nil , fmt .Errorf ("failed to parse names from %s: %v" , filePath , err )
38
64
}
65
+
66
+ return titleData .Names , nil
39
67
}
40
68
41
69
func resourceRandomNameCreate (d * schema.ResourceData , m interface {}) error {
42
70
mediaType := d .Get ("media_type" ).(string )
43
71
title := d .Get ("title" ).(string )
44
72
45
- rand .Seed (time .Now ().UnixNano ())
46
- var names []string
73
+ names , err := loadNames (mediaType , title )
74
+ if err != nil {
75
+ return fmt .Errorf ("error loading names for %s '%s': %s" , mediaType , title , err )
76
+ }
47
77
48
- switch mediaType {
49
- case "movie" :
50
- names = getNamesForMovie (title )
51
- case "tv_series" :
52
- names = getNamesForTVSeries (title )
53
- default :
54
- return fmt .Errorf ("unsupported media type: %s" , mediaType )
78
+ if len (names ) == 0 {
79
+ return fmt .Errorf ("no names found for %s '%s'" , mediaType , title )
55
80
}
56
81
57
- selectedName := names [rand .Intn (len (names ))]
82
+ // Setup a local random source
83
+ source := rand .NewSource (time .Now ().UnixNano ())
84
+ localRand := rand .New (source )
85
+ selectedName := names [localRand .Intn (len (names ))]
58
86
87
+ // Set the resource ID and the computed name
59
88
d .SetId (strconv .FormatInt (time .Now ().UnixNano (), 10 ))
60
89
d .Set ("name" , selectedName )
61
90
62
91
return nil
63
92
}
64
-
65
- func getNamesForMovie (title string ) []string {
66
- // Placeholder: return a slice of names based on the movie title
67
- // Example implementation
68
- switch title {
69
- case "lord of the rings" :
70
- return []string {"Aragorn" , "Gandalf" , "Bilbo" , "Frodo" , "Legolas" }
71
- default :
72
- return []string {"John Doe" }
73
- }
74
- }
75
-
76
- func getNamesForTVSeries (title string ) []string {
77
- // Placeholder: return a slice of names based on the TV series title
78
- // Example implementation
79
- switch title {
80
- case "game of thrones" :
81
- return []string {"Jon Snow" , "Tyrion Lannister" , "Daenerys Targaryen" }
82
- default :
83
- return []string {"Jane Doe" }
84
- }
85
- }
0 commit comments