Skip to content

Commit f43cd95

Browse files
committed
set unique id for data source city
1 parent 766278f commit f43cd95

File tree

11 files changed

+185
-23
lines changed

11 files changed

+185
-23
lines changed

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
on: [
2+
push,
3+
pull_request
4+
]
5+
name: Test
6+
jobs:
7+
test:
8+
strategy:
9+
matrix:
10+
go-version: [
11+
1.15.x
12+
]
13+
os: [
14+
ubuntu-latest,
15+
]
16+
runs-on: ${{ matrix.os }}
17+
steps:
18+
- name: Install Go
19+
uses: actions/setup-go@v2
20+
with:
21+
go-version: ${{ matrix.go-version }}
22+
- name: Checkout code
23+
uses: actions/checkout@v2
24+
- name: Test
25+
env:
26+
TF_ACC: 1
27+
run: go test ./...

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.terraform/
22
*tfstate*
3-
terraform-provider-mcbroken
3+
terraform-provider-mcbroken
4+
*.log

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PROVIDER_NAME=mcbroken
33
PROVIDER_FULL_PATH=$(REGISTRY)/$(NAMESPACE)/$(PROVIDER_NAME)
44
PROVIDER_FULL_NAME=terraform-provider-$(PROVIDER_NAME)
55
PROJECT=$(NAMESPACE)/$(PROVIDER_FULL_NAME)
6-
VERSION=0.1.2
6+
VERSION=0.1.3
77

88
build-mac: PLUGIN_DIR = ~/.terraform.d/plugins/local/provider/$(PROVIDER_NAME)/$(VERSION)/darwin_amd64
99
build-mac:
@@ -15,6 +15,10 @@ build-linux:
1515
test -d $(PLUGIN_DIR) || mkdir -p $(PLUGIN_DIR)
1616
go build -o $(PLUGIN_DIR)/$(PROVIDER_FULL_NAME)
1717

18+
test: export TF_ACC=true
19+
test:
20+
go test -v ./...
21+
1822
lint:
1923
@if ! command -v golangci-lint 1>/dev/null; then\
2024
echo "Need to install golangci-lint";\

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
![GitHub release (latest by date)](https://img.shields.io/github/v/release/circa10a/terraform-provider-mcbroken?style=plastic)
66
[![Buy Me A Coffee](https://img.shields.io/badge/BuyMeACoffee-Donate-ff813f.svg?logo=CoffeeScript&style=plastic)](https://www.buymeacoffee.com/caleblemoine)
77

8-
Base the count of your infrastucture resources on the national average of broken mcdonald's ice machines or by a city of your choosing. Powered by [Mcbroken](https://mcbroken.com/).
8+
Base the count of your infrastucture resources on the current number of broken mcdonald's ice machines nationally or by a city of your choosing. Powered by [Mcbroken](https://mcbroken.com/).
99

1010
- [terraform-provider-mcbroken](#terraform-provider-mcbroken)
1111
* [Usage](#usage)
1212
* [Development](#development)
1313
+ [Linting](#linting)
14+
+ [Testing](#testing)
1415
+ [Mac](#mac)
1516
+ [Linux](#linux)
1617
+ [Windows](#windows)
@@ -138,6 +139,12 @@ output "user_specified_city_not_found" {
138139
make lint
139140
```
140141

142+
### Testing
143+
144+
```bash
145+
make test
146+
```
147+
141148
### Mac
142149

143150
```bash

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mcbroken Provider
22

3-
Base the count of your infrastucture resources on the national average of broken mcdonald's ice machines or by a city of your choosing. Powered by [Mcbroken](https://mcbroken.com/).
3+
Base the count of your infrastucture resources on the current number of broken mcdonald's ice machines nationally or by a city of your choosing. Powered by [Mcbroken](https://mcbroken.com/).
44

55
## Example Usage
66

mcbroken/data_source_cities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func dataSourceCitiesRead(ctx context.Context, d *schema.ResourceData, m interfa
8181
return diag.FromErr(err)
8282
}
8383

84-
// Always run to give unique id
84+
// Change ID every run to force update
8585
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
8686

8787
return diags
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mcbroken
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccMcbrokenCities(t *testing.T) {
11+
brokenNumberRegex, _ := regexp.Compile(`\d{1,3}.\d{1,2}`)
12+
cityRegex, _ := regexp.Compile(`\w`)
13+
resource.ParallelTest(t, resource.TestCase{
14+
PreCheck: func() { /* no precheck needed testAccPreCheck(t) */ },
15+
ProviderFactories: testAccProviderFactories,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCheckCities(),
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttrSet(
21+
"data.mcbroken_cities.all", "broken"),
22+
resource.TestMatchResourceAttr(
23+
"data.mcbroken_cities.all", "broken", brokenNumberRegex),
24+
resource.TestCheckResourceAttrSet(
25+
"data.mcbroken_cities.all", "cities.0.city"),
26+
resource.TestCheckResourceAttrSet(
27+
"data.mcbroken_cities.all", "cities.0.broken"),
28+
resource.TestMatchResourceAttr(
29+
"data.mcbroken_cities.all", "cities.0.city", cityRegex),
30+
resource.TestMatchResourceAttr(
31+
"data.mcbroken_cities.all", "cities.0.broken", brokenNumberRegex),
32+
resource.TestCheckResourceAttrSet(
33+
"data.mcbroken_cities.all", "cities.1.city"),
34+
resource.TestCheckResourceAttrSet(
35+
"data.mcbroken_cities.all", "cities.1.broken"),
36+
resource.TestMatchResourceAttr(
37+
"data.mcbroken_cities.all", "cities.1.city", cityRegex),
38+
resource.TestMatchResourceAttr(
39+
"data.mcbroken_cities.all", "cities.1.broken", brokenNumberRegex),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func testAccCheckCities() string {
47+
return `data "mcbroken_cities" "all" {}`
48+
}

mcbroken/data_source_city.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package mcbroken
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"net/http"
8+
"strconv"
79
"strings"
810
"time"
911

@@ -68,7 +70,8 @@ func dataSourceCityRead(ctx context.Context, d *schema.ResourceData, m interface
6870
}
6971
}
7072

71-
d.SetId(userChosenCity)
73+
// Change ID every run to force update
74+
d.SetId(fmt.Sprintf("%v-%v", userChosenCity, strconv.FormatInt(time.Now().Unix(), 10)))
7275

7376
return diags
7477
}

mcbroken/data_source_city_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package mcbroken
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
func TestAccMcbrokenCity(t *testing.T) {
13+
brokenNumberRegex, _ := regexp.Compile(`\d{1,3}.\d{1,2}`)
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { /* no precheck needed testAccPreCheck(t) */ },
16+
ProviderFactories: testAccProviderFactories,
17+
Steps: []resource.TestStep{
18+
// valid city
19+
{
20+
Config: testAccCheckCity("Dallas"),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttr(
23+
"data.mcbroken_city.dallas", "city", "dallas"),
24+
resource.TestCheckResourceAttrSet(
25+
"data.mcbroken_city.dallas", "broken"),
26+
resource.TestMatchResourceAttr(
27+
"data.mcbroken_city.dallas", "broken", brokenNumberRegex),
28+
),
29+
},
30+
// invalid city
31+
{
32+
Config: testAccCheckCity("not_found"),
33+
Check: resource.ComposeTestCheckFunc(
34+
resource.TestCheckResourceAttr(
35+
"data.mcbroken_city.not_found", "city", "not_found"),
36+
resource.TestCheckResourceAttr(
37+
"data.mcbroken_city.not_found", "broken", "-1"),
38+
),
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccCheckCity(city string) string {
45+
return fmt.Sprintf(`
46+
data "mcbroken_city" "%[1]v" {
47+
city = "%[1]v"
48+
}
49+
`, strings.ToLower(city))
50+
}

mcbroken/provider.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package mcbroken
22

33
import (
44
"context"
5-
"net/url"
65

76
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
99
)
1010

1111
// Provider -
1212
func Provider() *schema.Provider {
1313
return &schema.Provider{
1414
Schema: map[string]*schema.Schema{
1515
"url": {
16-
Type: schema.TypeString,
17-
Optional: true,
18-
Default: "https://mcbroken.com/stats.json",
16+
Type: schema.TypeString,
17+
Optional: true,
18+
Default: "https://mcbroken.com/stats.json",
19+
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
1920
},
2021
},
2122
DataSourcesMap: map[string]*schema.Resource{
@@ -29,18 +30,6 @@ func Provider() *schema.Provider {
2930
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
3031
var diags diag.Diagnostics
3132
providerConfig := make(map[string]interface{})
32-
mcbrokenEndpoint := d.Get("url").(string)
33-
34-
_, err := url.Parse(mcbrokenEndpoint)
35-
if err != nil {
36-
diags = append(diags, diag.Diagnostic{
37-
Severity: diag.Error,
38-
Summary: "Unable to parse url",
39-
Detail: "Mcbroken endpoint provided is not a valid url",
40-
})
41-
return nil, diags
42-
}
43-
44-
providerConfig["url"] = mcbrokenEndpoint
33+
providerConfig["url"] = d.Get("url").(string)
4534
return providerConfig, diags
4635
}

0 commit comments

Comments
 (0)