/
resource_gatsby_text_heading.go
57 lines (50 loc) · 1.38 KB
/
resource_gatsby_text_heading.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package gatsby
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceGatsbyTextHeading() *schema.Resource {
return &schema.Resource{
Create: resourceGatsbyTextHeadingCreate,
Read: resourceGatsbyTextHeadingRead,
Update: resourceGatsbyTextHeadingUpdate,
Delete: schema.RemoveFromState,
Schema: map[string]*schema.Schema{
"text": {
Type: schema.TypeString,
Required: true,
},
"level": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(int)
if v < 1 {
errs = append(errs, fmt.Errorf("%q must be greater than or equal to 1, got: %d", key, v))
}
return
},
},
"contents": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGatsbyTextHeadingCreate(d *schema.ResourceData, m interface{}) error {
return resourceGatsbyTextHeadingRead(d, m)
}
func resourceGatsbyTextHeadingRead(d *schema.ResourceData, m interface{}) error {
text := d.Get("text").(string)
level := d.Get("level").(int)
formattedText := fmt.Sprintf("%s %s", strings.Repeat("#", level), text)
d.Set("contents", formattedText)
d.SetId(getID())
return nil
}
func resourceGatsbyTextHeadingUpdate(d *schema.ResourceData, m interface{}) error {
return resourceGatsbyTextHeadingRead(d, m)
}