2024-07-01 07:16:55 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
2024-09-19 08:03:23 +00:00
|
|
|
package fan
|
2024-07-01 07:16:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
_ "encoding/json"
|
2024-07-01 21:54:18 +00:00
|
|
|
"fmt"
|
2024-07-01 07:16:55 +00:00
|
|
|
_ "gopkg.in/yaml.v3"
|
|
|
|
"net/url"
|
|
|
|
_ "path/filepath"
|
|
|
|
"decl/internal/resource"
|
2024-09-19 08:03:23 +00:00
|
|
|
"decl/internal/folio"
|
|
|
|
"decl/internal/data"
|
2024-07-01 07:16:55 +00:00
|
|
|
_ "os"
|
|
|
|
_ "io"
|
2024-07-01 21:54:18 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2024-07-01 07:16:55 +00:00
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"log/slog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Container struct {
|
|
|
|
apiClient resource.ContainerClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContainer(containerClientApi resource.ContainerClient) *Container {
|
|
|
|
var apiClient resource.ContainerClient = containerClientApi
|
|
|
|
if apiClient == nil {
|
|
|
|
var err error
|
|
|
|
apiClient, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Container{
|
|
|
|
apiClient: apiClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2024-09-19 08:03:23 +00:00
|
|
|
folio.DocumentRegistry.ConverterTypes.Register([]string{"container"}, func(u *url.URL) data.Converter {
|
2024-07-01 07:16:55 +00:00
|
|
|
c := NewContainer(nil)
|
|
|
|
return c
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-19 08:03:23 +00:00
|
|
|
func (c *Container) Type() data.TypeName { return "container" }
|
2024-07-01 07:16:55 +00:00
|
|
|
|
2024-09-19 08:03:23 +00:00
|
|
|
func (c *Container) Extract(sourceResource data.Resource, filter data.ElementSelector) (document data.Document, err error) {
|
2024-07-01 21:54:18 +00:00
|
|
|
var extractErr error
|
2024-07-01 07:16:55 +00:00
|
|
|
ctx := context.Background()
|
2024-09-19 08:03:23 +00:00
|
|
|
slog.Info("container source Extract()", "container", c)
|
2024-07-01 21:54:18 +00:00
|
|
|
containers, err := c.apiClient.ContainerList(ctx, container.ListOptions{All: true})
|
2024-07-01 07:16:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-19 08:03:23 +00:00
|
|
|
document = folio.DocumentRegistry.NewDocument(folio.URI(sourceResource.URI()))
|
2024-07-01 07:16:55 +00:00
|
|
|
for _, container := range containers {
|
|
|
|
runningContainer := resource.NewContainer(nil)
|
2024-07-01 21:54:18 +00:00
|
|
|
if inspectErr := runningContainer.Inspect(ctx, container.ID); inspectErr != nil {
|
|
|
|
extractErr = fmt.Errorf("%w: %w", extractErr, inspectErr)
|
|
|
|
}
|
2024-09-19 08:03:23 +00:00
|
|
|
document.(*folio.Document).AddResourceDeclaration("container", runningContainer)
|
2024-07-01 07:16:55 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 08:03:23 +00:00
|
|
|
return document, extractErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Emit(document data.Document, filter data.ElementSelector) (resource data.Resource, err error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Close() error {
|
|
|
|
return nil
|
2024-07-01 07:16:55 +00:00
|
|
|
}
|