summaryrefslogtreecommitdiff
path: root/internal/resource/multi.go
blob: 39162871f3c595f3715173b00b8cd6901ecfa17f (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
51
52
53
54
55
56
57
58
59
package resource

import (
	"errors"
	"strings"
)

// Dependency is implemented by anything that can be depended upon. It returns
// the flattened list of individual resource IDs, so that depending on a Multi
// expands into a dependency on each of its members individually.
type Dependency interface {
	Dependencies() []string
}

// Multi is a collection of resources that satisfies the api.Resource interface.
type Multi []Resource

func (m Multi) String() string {
	strs := make([]string, 0, len(m))

	for _, res := range m {
		strs = append(strs, res.String())
	}

	return strings.Join(strs, ", ")
}

func (m Multi) ID() string {
	ids := make([]string, 0, len(m))

	for _, res := range m {
		ids = append(ids, res.String())
	}

	return strings.Join(ids, "+")
}

// Dependencies flattens the Multi into the IDs of each of its members, so a
// dependency on a Multi becomes an individual dependency on every resource it
// contains.
func (m Multi) Dependencies() []string {
	ids := make([]string, 0, len(m))

	for _, res := range m {
		ids = append(ids, res.Dependencies()...)
	}

	return ids
}

func (m Multi) Apply() error {
	var errs []error

	for _, res := range m {
		errs = append(errs, res.Apply())
	}

	return errors.Join(errs...)
}