summaryrefslogtreecommitdiff
path: root/internal/resource/resource.go
blob: 2a01629d2e20fa1021423a87bb6b1fe1000f7ae3 (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
47
48
49
50
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
	applier   Applier
	dependsOn map[string]struct{}
}

func Register(type_, name string, apply Applier) Resource {
	r := Resource{
		Type:      type_,
		Name:      name,
		applier:   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)
}

func (r Resource) Apply() error {
	return r.applier.Apply()
}