Skip to content

Commit 6510292

Browse files
committed
data structure and validation created
1 parent 405c10c commit 6510292

17 files changed

+487
-81
lines changed

.github/workflows/validate_data.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Validate validate_data.go validCombinations matches the structure of /data
3+
# Validate .json files under /data/* are formatted as expected
4+
# replace spaces with _ and validate file contents are valid json syntax

assets/cleanup.sh

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Define the paths for the directory and file
44
terraformDir=".terraform"
55
terraformLockFile=".terraform.lock.hcl"
6+
terraformState="terraform.tfstate"
7+
backupstate="terraform.tfstate.backup"
68

79
# Check and remove the .terraform directory if it exists
810
if [ -d "$terraformDir" ]; then
@@ -19,3 +21,19 @@ if [ -f "$terraformLockFile" ]; then
1921
else
2022
echo ".terraform.lock.hcl file does not exist."
2123
fi
24+
25+
# Check and remove the terraform.tfstate file if it exists
26+
if [ -f "$terraformState" ]; then
27+
rm -f "$terraformState"
28+
echo "Removed .terraform.tfstate file."
29+
else
30+
echo ".terraform.tfstate file does not exist."
31+
fi
32+
33+
# Check and remove the terrraform.tfstate.backup file if it exists
34+
if [ -f "$backupstate" ]; then
35+
rm -f "$backupstate"
36+
echo "Removed terrraform.tfstate.backup file."
37+
else
38+
echo "terrraform.tfstate.backup file does not exist."
39+
fi

assets/learning.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Docs and Stuff I Learned
2+
3+
## Autogeneration of Docs
4+
5+
Go here https://github.com/hashicorp/terraform-plugin-docs and follow the README.md
6+
7+
`go generate ./...`

data/movie/lord_of_the_rings.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"title": "Lord of the Rings",
3+
"names": [
4+
"Aragorn",
5+
"Gandalf",
6+
"Frodo Baggins",
7+
"Samwise Gamgee",
8+
"Legolas",
9+
"Gimli",
10+
"Boromir",
11+
"Meriadoc Brandybuck",
12+
"Peregrin Took",
13+
"Gollum",
14+
"Saruman",
15+
"Galadriel",
16+
"Éowyn"
17+
]
18+
}
19+
20+

data/movie/star_wars.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"title": "Star Wars",
3+
"names": [
4+
"Luke Skywalker",
5+
"Leia Organa",
6+
"Han Solo",
7+
"Darth Vader",
8+
"Obi-Wan Kenobi",
9+
"Yoda",
10+
"R2-D2",
11+
"C-3PO",
12+
"Chewbacca",
13+
"Lando Calrissian",
14+
"Anakin Skywalker",
15+
"Mace Windu",
16+
"Padmé Amidala"
17+
]
18+
}
19+

data/tv_series/breaking_bad.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"title": "Breaking Bad",
3+
"names": [
4+
"Walter White",
5+
"Jesse Pinkman",
6+
"Skyler White",
7+
"Hank Schrader",
8+
"Saul Goodman",
9+
"Gustavo Fring",
10+
"Mike Ehrmantraut",
11+
"Marie Schrader",
12+
"Lydia Rodarte-Quayle",
13+
"Todd Alquist"
14+
]
15+
}
16+

data/tv_series/game_of_thrones.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"title": "Game of Thrones",
3+
"names": [
4+
"Jon Snow",
5+
"Tyrion Lannister",
6+
"Daenerys Targaryen",
7+
"Arya Stark",
8+
"Sansa Stark",
9+
"Bran Stark",
10+
"Cersei Lannister",
11+
"Jaime Lannister",
12+
"Jorah Mormont",
13+
"Theon Greyjoy",
14+
"Samwell Tarly",
15+
"Lord Varys",
16+
"Petyr Baelish"
17+
]
18+
}
19+

docs/index.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "epic Provider"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# epic Provider
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema

docs/resources/random_name.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "epic_random_name Resource - epic"
4+
subcategory: ""
5+
description: |-
6+
Generates a random character name based on the media type and title specified.
7+
---
8+
9+
# epic_random_name (Resource)
10+
11+
Generates a random character name based on the media type and title specified.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `media_type` (String) The type of media, e.g., 'movie' or 'tv_series'.
21+
- `title` (String) The title of the media to base the name generation on.
22+
23+
### Read-Only
24+
25+
- `id` (String) The ID of this resource.
26+
- `name` (String) The randomly generated name.

epic/provider_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package epic
22

33
import (
44
"testing"
5-
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
75
)
86

97
func TestProvider(t *testing.T) {
10-
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
8+
if err := Provider().InternalValidate(); err != nil {
119
t.Fatalf("err: %s", err)
1210
}
1311
}

epic/resouce_random_name.go

+39-32
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package epic
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"math/rand"
7+
"os"
8+
"path/filepath"
69
"strconv"
10+
"strings"
711
"time"
812

913
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1014
)
1115

16+
type TitleData struct {
17+
Title string `json:"title"`
18+
Names []string `json:"names"`
19+
}
20+
1221
func resourceRandomName() *schema.Resource {
1322
return &schema.Resource{
1423
Description: "Generates a random character name based on the media type and title specified.",
@@ -35,51 +44,49 @@ func resourceRandomName() *schema.Resource {
3544
Description: "The randomly generated name.",
3645
},
3746
},
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)
3864
}
65+
66+
return titleData.Names, nil
3967
}
4068

4169
func resourceRandomNameCreate(d *schema.ResourceData, m interface{}) error {
4270
mediaType := d.Get("media_type").(string)
4371
title := d.Get("title").(string)
4472

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+
}
4777

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)
5580
}
5681

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))]
5886

87+
// Set the resource ID and the computed name
5988
d.SetId(strconv.FormatInt(time.Now().UnixNano(), 10))
6089
d.Set("name", selectedName)
6190

6291
return nil
6392
}
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-
}

epic/resource_random_name_test.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package epic
22

33
import (
4-
"fmt"
54
"testing"
65

76
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
97
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
108
)
119

10+
// Acceptance tests. Set TF_ACC=1 env variable to enable.
1211
func TestAccEpicRandomName_basic(t *testing.T) {
1312
resource.Test(t, resource.TestCase{
1413
Providers: map[string]*schema.Provider{
@@ -18,7 +17,6 @@ func TestAccEpicRandomName_basic(t *testing.T) {
1817
{
1918
Config: testAccCheckEpicRandomNameConfig_basic(),
2019
Check: resource.ComposeTestCheckFunc(
21-
testAccCheckEpicRandomNameExists("epic_random_name.test"),
2220
resource.TestCheckResourceAttr("epic_random_name.test", "media_type", "movie"),
2321
resource.TestCheckResourceAttr("epic_random_name.test", "title", "lord of the rings"),
2422
),
@@ -37,18 +35,3 @@ resource "epic_random_name" "test" {
3735
}
3836
`
3937
}
40-
41-
func testAccCheckEpicRandomNameExists(n string) resource.TestCheckFunc {
42-
return func(s *terraform.State) error {
43-
rs, ok := s.RootModule().Resources[n]
44-
if !ok {
45-
return fmt.Errorf("Not found: %s", n)
46-
}
47-
48-
if rs.Primary.ID == "" {
49-
return fmt.Errorf("No resource ID is set")
50-
}
51-
52-
return nil
53-
}
54-
}

0 commit comments

Comments
 (0)