76 lines
1.3 KiB
Go
76 lines
1.3 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package resource
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
_ "errors"
|
||
|
"fmt"
|
||
|
"gopkg.in/yaml.v3"
|
||
|
_ "io"
|
||
|
"net/url"
|
||
|
_ "os"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
ResourceTypes.Register("http", HTTPFactory)
|
||
|
ResourceTypes.Register("https", HTTPFactory)
|
||
|
}
|
||
|
|
||
|
func HTTPFactory(u *url.URL) Resource {
|
||
|
h := NewHTTP()
|
||
|
return h
|
||
|
}
|
||
|
|
||
|
// Manage the state of an HTTP endpoint
|
||
|
type HTTP struct {
|
||
|
loader YamlLoader
|
||
|
Endpoint string `yaml:"endpoint"`
|
||
|
|
||
|
Body string `yaml:"body,omitempty"`
|
||
|
State string `yaml:"state"`
|
||
|
}
|
||
|
|
||
|
func NewHTTP() *HTTP {
|
||
|
return &HTTP{loader: YamlLoadDecl}
|
||
|
}
|
||
|
|
||
|
func (h *HTTP) URI() string {
|
||
|
return h.Endpoint
|
||
|
}
|
||
|
|
||
|
func (h *HTTP) SetURI(uri string) error {
|
||
|
if _, e := url.Parse(uri); e != nil {
|
||
|
return fmt.Errorf("%w: %s is not a file", ErrInvalidResourceURI, uri)
|
||
|
}
|
||
|
h.Endpoint = uri
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (h *HTTP) Apply() error {
|
||
|
|
||
|
switch h.State {
|
||
|
case "absent":
|
||
|
case "present":
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (h *HTTP) LoadDecl(yamlFileResourceDeclaration string) error {
|
||
|
return h.loader(yamlFileResourceDeclaration, h)
|
||
|
}
|
||
|
|
||
|
func (h *HTTP) ResolveId(ctx context.Context) string {
|
||
|
return h.Endpoint
|
||
|
}
|
||
|
|
||
|
func (h *HTTP) Read(ctx context.Context) ([]byte, error) {
|
||
|
return yaml.Marshal(h)
|
||
|
}
|
||
|
|
||
|
func (h *HTTP) Type() string {
|
||
|
u, _ := url.Parse(h.Endpoint)
|
||
|
return u.Scheme
|
||
|
}
|