summaryrefslogtreecommitdiff
path: root/internal/resource/resource.go
blob: 4e72485bf06025e51f9b2d4bc5cf2f160aa3bab4 (plain)
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
package resource

import (
	"fmt"
	"log"
)

type Applier interface {
	Apply() error
}

type ApplierFunc func() error

func (f ApplierFunc) Apply() error {
	return f()
}

type Resource struct {
	Type      string
	Name      string
	Apply     Applier
	dependsOn map[string]struct{}
}

func Register(type_, name string, apply Applier) Resource {
	r := Resource{
		Type:      type_,
		Name:      name,
		Apply:     apply,
		dependsOn: make(map[string]struct{}),
	}

	if err := getRepository().register(r); err != nil {
		log.Fatalf("resource registration failed: %v", err)
	}

	return r
}

func (r Resource) String() string {
	return r.ID()
}

func (r Resource) ID() string {
	return fmt.Sprintf("%s[%s]", r.Type, r.Name)
}