-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbundle.go
More file actions
76 lines (66 loc) · 2.95 KB
/
Copy pathbundle.go
File metadata and controls
76 lines (66 loc) · 2.95 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package api
import (
"context"
"fmt"
"time"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
)
// Bundle represents a Massdriver bundle (IaC module) and its metadata.
type Bundle struct {
ID string `json:"id" mapstructure:"id"`
Name string `json:"name" mapstructure:"name"`
Version string `json:"version" mapstructure:"version"`
Description string `json:"description,omitempty" mapstructure:"description"`
Icon string `json:"icon,omitempty" mapstructure:"icon"`
SourceURL string `json:"sourceUrl,omitempty" mapstructure:"sourceUrl"`
Repo string `json:"repo,omitempty" mapstructure:"repo"`
CreatedAt time.Time `json:"createdAt,omitzero" mapstructure:"createdAt"`
UpdatedAt time.Time `json:"updatedAt,omitzero" mapstructure:"updatedAt"`
Dependencies []BundleSlot `json:"dependencies,omitempty" mapstructure:"dependencies"`
Resources []BundleSlot `json:"resources,omitempty" mapstructure:"resources"`
}
// BundleSlot describes one of a bundle's input dependencies or output
// resources. Dependencies are slots the user must wire up; resources are
// outputs the bundle produces on a successful deployment.
type BundleSlot struct {
Name string `json:"name" mapstructure:"name"`
Required bool `json:"required" mapstructure:"required"`
ResourceType *BundleResourceRef `json:"resourceType,omitempty" mapstructure:"resourceType"`
}
// BundleResourceRef points at a resource type the bundle declares. May be
// nil when the original resource type has been removed from the catalog.
type BundleResourceRef struct {
ID string `json:"id" mapstructure:"id"`
Name string `json:"name" mapstructure:"name"`
}
// GetBundle retrieves a bundle by its identifier (e.g., "aws-aurora-postgres@1.2.3" or "aws-aurora-postgres@latest").
func GetBundle(ctx context.Context, mdClient *client.Client, id string) (*Bundle, error) {
response, err := getBundle(ctx, mdClient.GQLv2, mdClient.Config.OrganizationID, id)
if err != nil {
return nil, fmt.Errorf("failed to get bundle %s: %w", id, err)
}
return toBundle(response.Bundle)
}
// ListBundles returns bundles, optionally filtered and sorted.
func ListBundles(ctx context.Context, mdClient *client.Client, filter *BundlesFilter, sort *BundlesSort) ([]Bundle, error) {
response, err := listBundles(ctx, mdClient.GQLv2, mdClient.Config.OrganizationID, filter, sort, nil)
if err != nil {
return nil, fmt.Errorf("failed to list bundles: %w", err)
}
bundles := make([]Bundle, 0, len(response.Bundles.Items))
for _, resp := range response.Bundles.Items {
b, bErr := toBundle(resp)
if bErr != nil {
return nil, fmt.Errorf("failed to convert bundle: %w", bErr)
}
bundles = append(bundles, *b)
}
return bundles, nil
}
func toBundle(v any) (*Bundle, error) {
b := Bundle{}
if err := decode(v, &b); err != nil {
return nil, fmt.Errorf("failed to decode bundle: %w", err)
}
return &b, nil
}