Skip to content

Commit 333c83e

Browse files
committed
add resource provider config options
1 parent d7c3e5a commit 333c83e

File tree

1 file changed

+62
-27
lines changed

1 file changed

+62
-27
lines changed

cmd/ctrlc/root/apply/apply.go

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020

2121
// Config represents the structure of the YAML file
2222
type Config struct {
23-
Systems map[string]System `yaml:"systems"`
23+
Systems map[string]System `yaml:"systems"`
24+
Providers ResourceProvider `yaml:"resourceProvider"`
2425
}
2526

2627
type System struct {
@@ -40,6 +41,20 @@ type Deployment struct {
4041
JobAgent *JobAgent `yaml:"jobAgent,omitempty"`
4142
}
4243

44+
type ResourceProvider struct {
45+
Name string `yaml:"name"`
46+
Resources map[string]Resource `yaml:"resources"`
47+
}
48+
49+
type Resource struct {
50+
Name string `yaml:"name"`
51+
Version string `yaml:"version"`
52+
Kind string `yaml:"kind"`
53+
Config map[string]any `yaml:"config"`
54+
Metadata map[string]string `yaml:"metadata"`
55+
Variables map[string]any `yaml:"variables"`
56+
}
57+
4358
// NewApplyCmd creates a new apply command
4459
func NewApplyCmd() *cobra.Command {
4560
var filePath string
@@ -75,13 +90,43 @@ func runApply(filePath string) error {
7590
}
7691

7792
// Process systems and collect errors
78-
errors := processAllSystems(ctx, client, workspaceID, config.Systems)
93+
processAllSystems(ctx, client, workspaceID, config.Systems)
94+
processResourceProvider(ctx, client, workspaceID.String(), config.Providers)
95+
96+
return nil
97+
}
7998

80-
if len(errors) > 0 {
81-
return fmt.Errorf("encountered %d errors during apply", len(errors))
99+
func processResourceProvider(ctx context.Context, client *api.ClientWithResponses, workspaceID string, provider ResourceProvider) {
100+
if provider.Name == "" {
101+
log.Info("Resource provider not provided, skipping")
102+
return
82103
}
83104

84-
return nil
105+
rp, err := api.NewResourceProvider(client, workspaceID, provider.Name)
106+
if err != nil {
107+
log.Error("Failed to create resource provider", "name", provider.Name, "error", err)
108+
return
109+
}
110+
111+
resources := make([]api.AgentResource, 0)
112+
for id, resource := range provider.Resources {
113+
resources = append(resources, api.AgentResource{
114+
Identifier: id,
115+
Name: resource.Name,
116+
Version: resource.Version,
117+
Kind: resource.Kind,
118+
Config: resource.Config,
119+
Metadata: resource.Metadata,
120+
})
121+
}
122+
123+
upsertResp, err := rp.UpsertResource(ctx, resources)
124+
if err != nil {
125+
log.Error("Failed to upsert resources", "name", provider.Name, "error", err)
126+
return
127+
}
128+
129+
log.Info("Response from upserting resources", "status", upsertResp.Status)
85130
}
86131

87132
func createAPIClient() (*api.ClientWithResponses, uuid.UUID, error) {
@@ -107,32 +152,22 @@ func processAllSystems(
107152
client *api.ClientWithResponses,
108153
workspaceID uuid.UUID,
109154
systems map[string]System,
110-
) []error {
111-
systemErrors := make(chan error, len(systems))
155+
) {
112156
var systemWg sync.WaitGroup
113157

114158
for slug, system := range systems {
115159
systemWg.Add(1)
116160
go processSystem(
117-
ctx,
118-
client,
161+
ctx,
162+
client,
119163
workspaceID,
120-
slug,
121-
system,
164+
slug,
165+
system,
122166
&systemWg,
123167
)
124168
}
125169

126170
systemWg.Wait()
127-
close(systemErrors)
128-
129-
// Collect all errors
130-
var errList []error
131-
for err := range systemErrors {
132-
errList = append(errList, err)
133-
}
134-
135-
return errList
136171
}
137172

138173
func processSystem(
@@ -144,7 +179,7 @@ func processSystem(
144179
systemWg *sync.WaitGroup,
145180
) {
146181
defer systemWg.Done()
147-
182+
148183
log.Info("Upserting system", "name", system.Name)
149184
systemID, err := upsertSystem(ctx, client, workspaceID, slug, system)
150185
if err != nil {
@@ -173,11 +208,11 @@ func processSystemDeployments(
173208
deploymentWg.Add(1)
174209
log.Info("Creating deployment", "system", system.Name, "name", deployment.Name)
175210
go processDeployment(
176-
ctx,
177-
client,
178-
systemID,
179-
deploymentSlug,
180-
deployment,
211+
ctx,
212+
client,
213+
systemID,
214+
deploymentSlug,
215+
deployment,
181216
&deploymentWg,
182217
)
183218
}
@@ -195,7 +230,7 @@ func processDeployment(
195230
defer deploymentWg.Done()
196231

197232
body := createDeploymentRequestBody(systemID, deploymentSlug, deployment)
198-
233+
199234
if deployment.JobAgent != nil {
200235
jobAgentUUID, err := uuid.Parse(deployment.JobAgent.Id)
201236
if err != nil {

0 commit comments

Comments
 (0)