Skip to content

Commit

Permalink
TPP-3 (#4)
Browse files Browse the repository at this point in the history
* TPP-3 updated gitignore

* TPP-3 updated documentation

* TPP-3 updated examples for documentation

* TPP-3 added checksum algorithm
  • Loading branch information
jsporna authored May 31, 2022
1 parent f97bc0f commit d8fbf70
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ bin/
terraform-provider-pesel
examples/**/.terraform
examples/**/.terraform.lock.hcl
examples/**/terraform.tfstate*
examples/**/terraform.tfstate
29 changes: 20 additions & 9 deletions docs/data-sources/id.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,41 @@
page_title: "pesel_id Data Source - terraform-provider-pesel"
subcategory: ""
description: |-
The pesel_id data source implements the standard data source lifecycle but does not interact with any external APIs.
Get information about provided PESEL ID like date of birthday, gender. Additionally data source validates checksum of ID.
More information about PESEL ID on wikipedia https://en.wikipedia.org/wiki/PESEL.
---

# pesel_id (Data Source)

The `pesel_id` data source implements the standard data source lifecycle but does not interact with any external APIs.

Get information about provided PESEL ID like date of birthday, gender. Additionally data source validates checksum of ID.
More information about PESEL ID on [wikipedia](https://en.wikipedia.org/wiki/PESEL).

## Example Usage

```terraform
data "pesel_id" "somebody" {
id = "65432101239"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `id` (String) The ID of this resource.
- `id` (String) PESEL ID

### Read-Only

- `date` (String)
- `day` (Number)
- `female` (Boolean)
- `gender` (String)
- `male` (Boolean)
- `month` (Number)
- `year` (Number)
- `date` (String) Date of birthday of person described by PESEL ID
- `day` (Number) Day of birthday of person described by PESEL ID
- `female` (Boolean) Does PESEL ID belong to female person
- `gender` (String) Gender of person described by PESEL ID
- `male` (Boolean) Does PESEL ID belong to male person
- `month` (Number) Month of birthday of person described by PESEL ID
- `year` (Number) Year of birthday of person described by PESEL ID


11 changes: 4 additions & 7 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ terraform {
}
}
provider "pesel" {}
resource "pesel_id" "random" {
day = 29
}
data "pesel_id" "bulwa" {
id = "83272809010"
data "pesel_id" "somebody" {
id = "65432101239"
}
output "random" {
value = pesel_id.random
}
output "bulwa" {
value = data.pesel_id.bulwa
output "somebody" {
value = data.pesel_id.somebody
}
```
33 changes: 25 additions & 8 deletions docs/resources/id.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,40 @@ description: |-



## Example Usage

```terraform
resource "pesel_id" "random" {
}
resource "pesel_id" "year" {
year = 2022
}
resource "pesel_id" "month" {
month = 5
}
resource "pesel_id" "day" {
day = 11
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `day` (Number)
- `gender` (String)
- `month` (Number)
- `year` (Number)
- `day` (Number) Day of birthday of person described by generated PESEL ID
- `gender` (String) Gender of person described by generated PESEL ID
- `month` (Number) Month of birthday of person described by generated PESEL ID
- `year` (Number) Year of birthday of person described by generated PESEL ID

### Read-Only

- `date` (String)
- `female` (Boolean)
- `id` (String) The ID of this resource.
- `male` (Boolean)
- `date` (String) Date of birthday of person described by generated PESEL ID
- `female` (Boolean) Does PESEL ID belong to female person
- `id` (String) Generate PESEL ID
- `male` (Boolean) Does PESEL ID belong to male person


3 changes: 3 additions & 0 deletions examples/data-sources/pesel_id/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "pesel_id" "somebody" {
id = "65432101239"
}
9 changes: 4 additions & 5 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ terraform {
}
}

provider "pesel" {}

resource "pesel_id" "random" {}
resource "pesel_id" "random" {
}

data "pesel_id" "somebody" {
id = "65432101239"
Expand All @@ -18,6 +17,6 @@ output "random" {
value = pesel_id.random
}

output "bulwa" {
output "somebody" {
value = data.pesel_id.somebody
}
}
14 changes: 14 additions & 0 deletions examples/resources/pesel_id/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resource "pesel_id" "random" {
}

resource "pesel_id" "year" {
year = 2022
}

resource "pesel_id" "month" {
month = 5
}

resource "pesel_id" "day" {
day = 11
}
36 changes: 25 additions & 11 deletions internal/provider/data_pesel_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,52 @@ import (
func peselIdData() *schema.Resource {
return &schema.Resource{
ReadContext: peselIdDataRead,
Description: `The ` + "`pesel_id`" + ` data source implements the standard data source lifecycle but does not interact with any external APIs.
Get information about provided PESEL ID like date of birthday, gender. Additionally data source validates checksum of ID.
More information about PESEL ID on [wikipedia](https://en.wikipedia.org/wiki/PESEL).
`,
Schema: map[string]*schema.Schema{
"year": {
Description: "",
Description: "Year of birthday of person described by PESEL ID",
Type: schema.TypeInt,
Computed: true,
},
"month": {
Description: "",
Description: "Month of birthday of person described by PESEL ID",
Type: schema.TypeInt,
Computed: true,
},
"day": {
Description: "",
Description: "Day of birthday of person described by PESEL ID",
Type: schema.TypeInt,
Computed: true,
},
"gender": {
Description: "",
Description: "Gender of person described by PESEL ID",
Type: schema.TypeString,
Computed: true,
},
"date": {
Description: "",
Description: "Date of birthday of person described by PESEL ID",
Type: schema.TypeString,
Computed: true,
},
"male": {
Description: "",
Description: "Does PESEL ID belong to male person",
Type: schema.TypeBool,
Computed: true,
},
"female": {
Description: "",
Description: "Does PESEL ID belong to female person",
Type: schema.TypeBool,
Computed: true,
},
"id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "PESEL ID",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
Expand All @@ -63,10 +69,18 @@ func peselIdDataRead(_ context.Context, d *schema.ResourceData, _ interface{}) d
if c < '0' || c > '9' {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Pesel cointains not only digits",
Summary: "Provided PESEL ID contains not only digits",
})
}
}
calculatedChecksum := checksum(pesel)
peselChecksum := int(pesel[10]) - '0'
if calculatedChecksum != peselChecksum {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Provided PESEL ID has not valid checksum; pesel: %s, cs: %d, calc: %d", pesel, peselChecksum, calculatedChecksum),
})
}
year, _ := strconv.Atoi(pesel[0:2])
month, _ := strconv.Atoi(pesel[2:4])
offset := month - month%20
Expand Down
10 changes: 9 additions & 1 deletion internal/provider/pesel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var INT2GENDER = map[int]string{
}

var MONTHSOFYEAR = [13]int{31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
var WEIGHTS = [10]int{1, 3, 7, 9, 1, 3, 7, 9, 1, 3}

func isLeapYear(year int) bool {
leapFlag := false
Expand Down Expand Up @@ -81,5 +82,12 @@ func randomGenderValue(gender int) int {
func checksum(pesel string) int {
_checksum := 0

return _checksum
for idx, char := range pesel {
if idx > 9 {
break
}
_checksum += WEIGHTS[idx] * (int(char) - '0')
}

return (10 - (_checksum % 10)) % 10
}
42 changes: 19 additions & 23 deletions internal/provider/resource_pesel_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,61 @@ func peselIdResource() *schema.Resource {
Description: ``,
CreateContext: peselIdResourceWrite,
DeleteContext: peselIdResourceDelete,
ReadContext: schema.NoopContext,
ReadContext: peselIdResourceRead,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"year": {
Description: "",
Description: "Year of birthday of person described by generated PESEL ID",
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"month": {
Description: "",
Description: "Month of birthday of person described by generated PESEL ID",
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"day": {
Description: "",
Description: "Day of birthday of person described by generated PESEL ID",
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"gender": {
Description: "",
Description: "Gender of person described by generated PESEL ID",
Type: schema.TypeString,
Optional: true,
Default: "male",
ForceNew: true,
},
"date": {
Description: "",
Description: "Date of birthday of person described by generated PESEL ID",
Type: schema.TypeString,
Computed: true,
},
"male": {
Description: "",
Description: "Does PESEL ID belong to male person",
Type: schema.TypeBool,
Computed: true,
},
"female": {
Description: "",
Description: "Does PESEL ID belong to female person",
Type: schema.TypeBool,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Generate PESEL ID",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func peselIdResourceWrite(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
func peselIdResourceWrite(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

year := d.Get("year").(int)
Expand All @@ -82,6 +84,7 @@ func peselIdResourceWrite(_ context.Context, d *schema.ResourceData, _ interface
}

rand.Seed(time.Now().UnixNano())

var _gender int
if gender == "" {
_gender = rand.Intn(2)
Expand Down Expand Up @@ -152,15 +155,6 @@ func peselIdResourceWrite(_ context.Context, d *schema.ResourceData, _ interface
_checksum := checksum(_subPesel)
_pesel := fmt.Sprintf("%s%d", _subPesel, _checksum)

if err := d.Set("year", _year); err != nil {
return diag.FromErr(err)
}
if err := d.Set("month", _month); err != nil {
return diag.FromErr(err)
}
if err := d.Set("day", _day); err != nil {
return diag.FromErr(err)
}
if err := d.Set("date", fmt.Sprintf("%d-%02d-%02d", _year, _month, _day)); err != nil {
return diag.FromErr(err)
}
Expand All @@ -170,11 +164,13 @@ func peselIdResourceWrite(_ context.Context, d *schema.ResourceData, _ interface
if err := d.Set("female", _gender == 0); err != nil {
return diag.FromErr(err)
}
if err := d.Set("gender", INT2GENDER[_gender]); err != nil {
return diag.FromErr(err)
}

d.SetId(_pesel)

return diags
}

func peselIdResourceRead(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

Expand Down

0 comments on commit d8fbf70

Please sign in to comment.