diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
commit | b09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch) | |
tree | d2caec2610d4ea887803ec9e9c3cd77136c448ba /pkg/icingadb/v1/environment.go | |
parent | Initial commit. (diff) | |
download | icingadb-upstream.tar.xz icingadb-upstream.zip |
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pkg/icingadb/v1/environment.go')
-rw-r--r-- | pkg/icingadb/v1/environment.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/pkg/icingadb/v1/environment.go b/pkg/icingadb/v1/environment.go new file mode 100644 index 0000000..fdddc5b --- /dev/null +++ b/pkg/icingadb/v1/environment.go @@ -0,0 +1,40 @@ +package v1 + +import ( + "context" + "github.com/icinga/icingadb/pkg/types" +) + +type Environment struct { + EntityWithoutChecksum `json:",inline"` + Name types.String `json:"name"` +} + +// NewContext returns a new Context that carries this Environment as value. +func (e *Environment) NewContext(parent context.Context) context.Context { + return context.WithValue(parent, environmentContextKey, e) +} + +// Meta returns the EnvironmentMeta for this Environment. +func (e *Environment) Meta() *EnvironmentMeta { + return &EnvironmentMeta{EnvironmentId: e.Id} +} + +// EnvironmentFromContext returns the Environment value stored in ctx, if any: +// +// e, ok := EnvironmentFromContext(ctx) +// if !ok { +// // Error handling. +// } +func EnvironmentFromContext(ctx context.Context) (*Environment, bool) { + if e, ok := ctx.Value(environmentContextKey).(*Environment); ok { + return e, true + } + + return nil, false +} + +// environmentContextKey is the key for Environment values in contexts. +// It's not exported, so callers use Environment.NewContext and EnvironmentFromContext +// instead of using that key directly. +var environmentContextKey contextKey |